Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javax/ide/command/Controller.java


1   package javax.ide.command;
2   
3   import javax.ide.menu.IDEAction;
4   import javax.ide.util.MetaClass;
5   
6   /**
7    * Controllers encapulate extension defined {@link UpdateHandler}s and 
8    * {@link InvokeHandler}s. Generally, {@link javax.ide.view.View}s use 
9    * controllers to check whether a context sensitive command can be 
10   * executed given the current context, and to do the actual execution of 
11   * that command.
12   */
13  public final class Controller 
14  {
15    private MetaClass _updateClass;
16    private MetaClass _invokeClass;
17    
18    private final boolean _isContextSensitive;
19    
20    private UpdateHandler _updateHandler;
21    private InvokeHandler _invokeHandler;
22    
23    private static UpdateHandler _nullUpdateHandler;
24    private static InvokeHandler _nullInvokeHandler;
25    
26    /**
27     * Constructs a controller object, deferring class loading and instantiation
28     * of its invoke and update handler objects until needed. Generally, this is
29     * only used by IDE implementations when loading controllers from the manifest
30     * file.
31     * 
32     * @param invokeHandlerClass a metaclass of an <tt>InvokeHandler</tt>.
33     * @param updateHandlerClass a metaclass of an <tt>UpdateHandler</tt>. May
34     *    be null.
35     */
36    public Controller( MetaClass invokeHandlerClass, 
37      MetaClass updateHandlerClass )
38    {
39      if ( invokeHandlerClass == null )
40      {
41        throw new NullPointerException( "invokeHandlerClass is null" );
42      }
43      
44      _isContextSensitive = updateHandlerClass == null;
45    
46      _updateClass = updateHandlerClass;
47      _invokeClass = invokeHandlerClass;
48    }
49    
50    /**
51     * Constructs a controller object.
52     * 
53     * @param invokeHandler the invocation handler. Must not be null.
54     * @param updateHandler the update handler. May be null for actions which
55     * are always enabled.
56     */
57    public Controller( InvokeHandler invokeHandler, UpdateHandler updateHandler )
58    {
59      if ( _invokeHandler == null )
60      {
61        throw new NullPointerException( "invokeHandler is null" );
62      }
63      _isContextSensitive = _updateHandler == null;    
64      _invokeHandler = invokeHandler;
65      _updateHandler = updateHandler;
66    }
67    
68    /**
69     * Get the invoke handler for this controller.
70     * 
71     * @return the invoke handler for this controller. Will never return null. 
72     */
73    public InvokeHandler getInvokeHandler()
74    {
75      if ( _invokeHandler == null )
76      {
77        try
78        {
79          _invokeHandler = (InvokeHandler) _invokeClass.newInstance();
80        }
81        catch ( Exception e )
82        {
83          e.printStackTrace();
84          // TODO report error.
85          _nullInvokeHandler = new NullInvokeHandler();
86          _invokeHandler = _nullInvokeHandler;
87        }
88      }
89      return _invokeHandler;
90    }
91    
92    /**
93     * Get the update handler for this controller.
94     * 
95     * @return the update handler for this controller. If the action is always
96     *    enabled, returns a handler that always sets the action enabled. Will
97     *    never return null.
98     */
99    public UpdateHandler getUpdateHandler()
100   {
101     if ( _updateHandler == null && _updateClass != null )
102     {
103       try
104       {
105         _updateHandler = (UpdateHandler) _updateClass.newInstance();
106       }
107       catch ( Exception e )
108       {
109         e.printStackTrace();
110         // TODO error handling.
111       }
112     }
113     
114     if ( _updateHandler == null )
115     {
116       // Either _updateClass == null or an exception occurred creating the
117       // update class. 
118       if ( _nullUpdateHandler == null )
119       {
120         _nullUpdateHandler = new NullUpdateHandler();
121       }
122       _updateHandler = _nullUpdateHandler;
123     }
124     
125     return _updateHandler;
126     
127   }
128 
129   /**
130    * Get whether this controller is context sensitive. A context sensitive
131    * controller has an associated <tt>UpdateHandler</tt> that adjusts the 
132    * action based on the context.
133    * 
134    * @return <tt>true</tt> if the <tt>UpdateHandler</tt> associated with this
135    *    <tt>Controller</tt> may change the state of the action based on the
136    *    context.
137    */
138   public boolean isContextSensitive()
139   {
140     return _isContextSensitive;
141   }
142 
143   private static final class NullInvokeHandler implements InvokeHandler
144   {
145     public boolean invoke( IDEAction action, Context context )
146     {
147       return true;
148     }
149   }
150   
151   private static final class NullUpdateHandler implements UpdateHandler
152   {
153     public boolean update( IDEAction action, Context context )
154     {
155       action.setEnabled( true );
156       return true;
157     }
158   }
159 }