View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // AbstractWidget.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.core.behavioral.chainofresponsibility;
22  
23  import org.perfectjpattern.core.api.behavioral.chainofresponsibility.*;
24  
25  /** 
26   * Abstract base definition of a AbstractWidget. Adapts or maps the inner
27   * Handler implementation into abstract methods that can be overridden by 
28   * subclasses.
29   *
30   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
31   * @version $Revision: 1.0 $Date: Apr 19, 2008 12:25:59 PM $
32   */
33  public abstract 
34  class AbstractWidget
35  implements IHelpAware, IPrintAware
36  {
37      //------------------------------------------------------------------------
38      // public
39      //------------------------------------------------------------------------
40      /** 
41       * {@inheritDoc}
42       */
43      public IParameterlessHandler
44      getHelpHandler()
45      {
46          return theHelpHandler;
47      }
48  
49      //------------------------------------------------------------------------
50      /** 
51       * {@inheritDoc}
52       */
53      public IParameterlessHandler 
54      getPrintHandler()
55      {
56          return thePrintHandler;
57      }
58      
59      //------------------------------------------------------------------------
60      // protected
61      //------------------------------------------------------------------------
62      protected abstract
63      boolean canHandleHelp();
64  
65      //------------------------------------------------------------------------
66      protected abstract
67      void handleHelp();
68  
69      //------------------------------------------------------------------------
70      protected abstract
71      boolean canHandlePrint();
72  
73      //------------------------------------------------------------------------
74      protected abstract
75      void handlePrint();
76  
77      //------------------------------------------------------------------------
78      // inner classes
79      //------------------------------------------------------------------------
80      /**
81       * Inner implementation that handles Help requests
82       */
83      private IParameterlessHandler theHelpHandler = 
84          new AbstractParameterlessHandler()
85      {
86          //--------------------------------------------------------------------
87          @Override
88          public boolean 
89          canHandle(NullRequest aRequest)
90          throws IllegalArgumentException
91          {
92              return canHandleHelp();
93          }
94  
95          //--------------------------------------------------------------------
96          public void handle()
97          {
98              handleHelp();
99          }        
100     };
101     
102     //------------------------------------------------------------------------
103     /**
104      * Inner implementation that handles Print requests
105      */
106     private IParameterlessHandler thePrintHandler = 
107         new AbstractParameterlessHandler()
108     {
109         //--------------------------------------------------------------------
110         @Override
111         public boolean 
112         canHandle(NullRequest aRequest)
113         throws IllegalArgumentException
114         {
115             return canHandlePrint();
116         }
117 
118         //--------------------------------------------------------------------
119         public void 
120         handle()
121         {
122             handlePrint();
123         }                
124     };
125 }