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.lang.reflect.InvocationTargetException;
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.eclipse.ui.IWorkbenchPart;
21  
22  import sk.uniba.euromath.editor.actions.lang.Messages;
23  import sk.uniba.euromath.editor.xmlEditor.actions.XMLAccessModifyAction;
24  
25  /***
26   * @author Tomáš Studva 28.11.2005
27   */
28  public class MultiViewActionFactory {
29  
30          /***
31           * Map holding registered action classes.
32           */
33          protected final Map<String, Class<? extends XMLAccessModifyAction>> actionClasses = new HashMap<String, Class<? extends XMLAccessModifyAction>>();
34  
35          /***
36           * Contructor.
37           */
38          public MultiViewActionFactory() {
39                  super();
40          }
41  
42          /***
43           * Returns CLass registered in this factory.
44           * 
45           * @param key
46           *                registration key
47           * @return Class of action registered under key key
48           */
49          protected Class<? extends XMLAccessModifyAction> getActionClass(
50                          String key) {
51                  return this.actionClasses.get(key);
52          }
53  
54          /***
55           * Creates instance of action.
56           * 
57           * @param clz
58           *                of action
59           * @return new instance IMultiViewAction of class clz
60           */
61          public XMLAccessModifyAction createAction(
62                          Class<? extends XMLAccessModifyAction> clz,
63                          IWorkbenchPart part) {
64                  try {
65                          return clz.getConstructor(
66                                          new Class[] { IWorkbenchPart.class })
67                                          .newInstance(part);
68                  } catch (InstantiationException e) {
69                          throw new IllegalArgumentException();
70                  } catch (IllegalAccessException e) {
71                          throw new IllegalArgumentException();
72                  } catch (InvocationTargetException e) {
73                          throw new IllegalArgumentException();
74                  } catch (NoSuchMethodException e) {
75                          throw new IllegalArgumentException();
76                  }
77  
78          }
79  
80          /***
81           * Creates instance of action. Sets workbench for it.
82           * 
83           * @param key
84           *                of action to create, by which is registered in
85           *                factory.
86           * @param part
87           *                workbench part to be set to created action
88           * @return new instance of class registered under key key.
89           */
90          public XMLAccessModifyAction createAction(String key,
91                          IWorkbenchPart part) {
92                  Class<? extends XMLAccessModifyAction> clz = getActionClass(key);
93                  return createAction(clz, part);
94          }
95  
96          /***
97           * Creates list of instances of actions. Action classes are specified by
98           * ids. Sets workbench for them.
99           * 
100          * @param ids
101          *                of actions to create, by which are registered in
102          *                factory.
103          * @param part
104          *                workbench part to be set to created action
105          * 
106          * @return List of newly created actions specified by ids
107          */
108         protected List<XMLAccessModifyAction> createActions(String[] ids,
109                         IWorkbenchPart part) {
110 
111                 List<XMLAccessModifyAction> result = new ArrayList<XMLAccessModifyAction>();
112                 if (ids != null) {
113                         for (int i = 0; i < ids.length; i++) {
114                                 Class<? extends XMLAccessModifyAction> clz = getActionClass(ids[i]);
115                                 XMLAccessModifyAction act = createAction(clz,
116                                                 part);
117                                 if (act != null) {
118                                         act.setWorkbenchPart(part);
119                                         result.add(act);
120                                 }
121                         }
122                 }
123                 return result;
124         }
125 
126         /***
127          * Registers action id to class for creation. If for id is already
128          * another action class registered, throws IllegalArgumentException.
129          * 
130          * @param id
131          *                id of action
132          * @param cls
133          *                class of action, must extends IMultiViewAction class
134          */
135         public void registerAction(String id,
136                         Class<? extends XMLAccessModifyAction> cls) {
137                 if (this.actionClasses.containsKey(id)
138                                 && !this.actionClasses.get(id).equals(cls))
139                         throw new IllegalArgumentException(
140                                         Messages
141                                                         .getString("For this id is already registered another action.")); //$NON-NLS-1$
142                 this.actionClasses.put(id, cls);
143         }
144 }