View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.actions;
13  
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  import org.eclipse.ui.IWorkbenchPart;
20  
21  import sk.uniba.euromath.editor.actions.lang.Messages;
22  
23  /***
24   * @author Tomáš Studva 28.11.2005
25   */
26  public class MultiViewActionFactory {
27  
28      /***
29       * Map holding registered action classes.
30       */
31      protected final Map<String, Class<? extends IMultiViewAction>> actionClasses = new HashMap<String, Class<? extends IMultiViewAction>>();
32  
33      /***
34       * Contructor.
35       */
36      public MultiViewActionFactory() {
37          super();
38      }
39  
40      /***
41       * Returns CLass registered in this factory.
42       * @param key registration key
43       * @return Class of action registered under key key
44       */
45      protected Class<? extends IMultiViewAction> getActionClass(String key) {
46          return getActionClasses().get(key);
47      }
48  
49      /***
50       * Creates instance of action.
51       * 
52       * @param clz
53       *            of action
54       * @return new instance IMultiViewAction of class clz
55       */
56      public IMultiViewAction createAction(Class<? extends IMultiViewAction> clz) {
57          IMultiViewAction result = null;
58          if (clz != null) {
59              try {
60                  result = clz.newInstance();
61              } catch (InstantiationException ie) {
62                  throw new Error(ie);
63              } catch (IllegalAccessException iae) {
64                  throw new Error(iae);
65              }
66          }
67          return result;
68      }
69  
70      /***
71       * Creates instance of action. Sets workbench for it.
72       * 
73       * @param key
74       *            of action to create, by which is registered in factory.
75       * @param part
76       *            workbench part to be set to created action
77       * @return new instance of class registered under key key.
78       */
79      public IMultiViewAction createAction(String key, IWorkbenchPart part) {
80          IMultiViewAction result = null;
81          Class<? extends IMultiViewAction> clz = getActionClass(key);
82          if (clz != null) {
83              try {
84                  result = clz.newInstance();
85              } catch (InstantiationException ie) {
86                  throw new Error(ie);
87              } catch (IllegalAccessException iae) {
88                  throw new Error(iae);
89              }
90          }
91          result.setWorkbenchPart(part);
92          return result;
93      }
94  
95      /***
96       * Creates list of instances of actions. Action classes are specified by
97       * ids. Sets workbench for them.
98       * 
99       * @param ids
100      *            of actions to create, by which are registered in factory.
101      * @param part
102      *            workbench part to be set to created action
103      * 
104      * @return List of newly created actions specified by ids
105      */
106     protected List<IMultiViewAction> createActions(String[] ids,
107             IWorkbenchPart part) {
108 
109         List<IMultiViewAction> result = new ArrayList<IMultiViewAction>();
110         if (ids != null) {
111             for (int i = 0; i < ids.length; i++) {
112                 Class<? extends IMultiViewAction> clz = getActionClass(ids[i]);
113                 IMultiViewAction act = createAction(clz);
114                 if (act != null) {
115                     act.setWorkbenchPart(part);
116                     result.add(act);
117                 }
118             }
119         }
120         return result;
121     }
122 
123     /***
124      * Registers action id to class for creation. If for id is already another
125      * action class registered, throws IllegalArgumentException.
126      * 
127      * @param id
128      *            id of action
129      * @param cls
130      *            class of action, must extends IMultiViewAction class
131      */
132     public void registerAction(String id, Class<? extends IMultiViewAction> cls) {
133         if (getActionClasses().containsKey(id)
134                 && !getActionClasses().get(id).equals(cls))
135             throw new IllegalArgumentException(Messages
136                     .getString("MultiViewActionFactory.0")); //$NON-NLS-1$
137         getActionClasses().put(id, cls);
138     }
139 
140     /***
141      * @return Returns the actionClasses.
142      */
143     public Map<String, Class<? extends IMultiViewAction>> getActionClasses() {
144         return this.actionClasses;
145     }
146 }