1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.actions;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.gef.ui.actions.ActionRegistry;
18 import org.eclipse.gef.ui.actions.SelectionAction;
19 import org.eclipse.ui.IWorkbenchPart;
20
21 import sk.uniba.euromath.document.DocumentModifyHelper;
22 import sk.uniba.euromath.document.XMLAccess;
23 import sk.uniba.euromath.editor.selections.DOMSelectionChangedEvent;
24 import sk.uniba.euromath.editor.selections.IDOMSelectionChangedListener;
25 import sk.uniba.euromath.editor.selections.IDOMSelectionProvider;
26 import sk.uniba.euromath.editor.xmlEditor.actions.EmptyAction;
27
28 /***
29 * Generic abstract action contributor for ations of type IMultiViewAction
30 * interface. Can be used anywhere.
31 *
32 * @author Tomáš Studva 16.10.2005
33 */
34 public abstract class AbstractActionContributor {
35
36 /***
37 * XMLAccess instance.
38 */
39 private XMLAccess xmlAccess;
40
41 /***
42 * MultiViewActionFactory for creating action and sets of actions.
43 */
44 private MultiViewActionFactory actionFactory;
45
46 /***
47 * List of actions, to be explicitly updated when selection changes(in
48 * provided ISelectionProvider).
49 */
50 private List<SelectionAction> selectionActions;
51
52 /***
53 * Constructor.
54 *
55 * @param selectionProvider
56 * selection provider
57 */
58 public AbstractActionContributor(IDOMSelectionProvider selectionProvider) {
59 super();
60 setActionFactory(new MultiViewActionFactory());
61 configureFactory();
62
63 selectionProvider
64 .addSelectionChangedListener(new IDOMSelectionChangedListener() {
65 public void selectionChanged(DOMSelectionChangedEvent event) {
66 selectionChangedHandle(event);
67 }
68
69 });
70 setSelectionActions(new ArrayList<SelectionAction>());
71
72 }
73
74 /***
75 * Handler for selection changes. Updates all selection actions.
76 *
77 * @param event
78 */
79 protected void selectionChangedHandle(@SuppressWarnings("unused")
80 DOMSelectionChangedEvent event) {
81 for (SelectionAction action : getSelectionActions())
82 action.update();
83 }
84
85 /***
86 * Configures factory. Registers action in factory by their ids to their
87 * classes.
88 */
89 abstract protected void configureFactory();
90
91 /***
92 * Checks for action with id key in registry. If exist - is registered, than
93 * is returned. Otherwise is created by action factory. SelectionActions are
94 * added to selectionAction list for selection change update.
95 *
96 * @param key
97 * to identify action, must be IAction.getId()
98 * @return desired action
99 */
100 protected IMultiViewAction checkOrCreateAction(String key) {
101 IMultiViewAction action = (IMultiViewAction) getActionRegistry()
102 .getAction(key);
103 if (action != null)
104 return action;
105
106
107 if (key == EmptyAction.id)
108 return new EmptyAction(getWorkbenchPart());
109 action = getActionFactory().createAction(key, getWorkbenchPart());
110 getActionRegistry().registerAction(action);
111 if (action instanceof SelectionAction)
112 getSelectionActions().add((SelectionAction) action);
113
114 action.update();
115 return action;
116 }
117
118 /***
119 * Checks for action with id key in registry. If is registered, than is
120 * added to result list. Otherwise is created by action factory and added to
121 * result list.
122 *
123 * @param keys
124 * to identify action, must be IAction.getId()
125 * @return desired actions
126 */
127 protected List<IMultiViewAction> checkOrCreateActions(String[] keys) {
128 List<IMultiViewAction> result = new ArrayList<IMultiViewAction>();
129 for (String key : keys) {
130 result.add(checkOrCreateAction(key));
131 }
132 return result;
133 }
134
135 /***
136 * Getter for document modifier.
137 *
138 * @return document modifier.
139 */
140 protected DocumentModifyHelper getDocumentModifyHelper() {
141 return getXMLAccess().getDocumentModifyHelper();
142 }
143
144 /***
145 * @param xmlAccess
146 * The xmlAccess to set.
147 */
148 public void setXMLAccess(XMLAccess xmlAccess) {
149 this.xmlAccess = xmlAccess;
150 }
151
152 /***
153 * Returns XMLAccess instance.
154 *
155 * @return XMLAccess instance
156 */
157 public XMLAccess getXMLAccess() {
158 return this.xmlAccess;
159 }
160
161 /***
162 * @return Returns the xmlActionFactory.
163 */
164 public MultiViewActionFactory getActionFactory() {
165 return this.actionFactory;
166 }
167
168 /***
169 * Setter for action factory.
170 *
171 * @param actionFactory
172 * to set.
173 */
174 public void setActionFactory(MultiViewActionFactory actionFactory) {
175 this.actionFactory = actionFactory;
176 }
177
178 /***
179 * @return Returns the selectionActions.
180 */
181 public List<SelectionAction> getSelectionActions() {
182 return this.selectionActions;
183 }
184
185 /***
186 * @param selectionActions
187 * The updateActions to set.
188 */
189 public void setSelectionActions(List<SelectionAction> selectionActions) {
190 this.selectionActions = selectionActions;
191 }
192
193 /***
194 * Returns workbench part for which actions are created.
195 *
196 * @return workbech part
197 */
198 abstract public IWorkbenchPart getWorkbenchPart();
199
200 /***
201 * Returns action registry in which created actions are registered.
202 *
203 * @return action registry
204 */
205 abstract public ActionRegistry getActionRegistry();
206
207 }