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