1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.rcp.configuration.extension; |
17 | |
|
18 | |
import java.util.List; |
19 | |
|
20 | |
import org.seasar.framework.util.StringUtil; |
21 | |
import org.seasar.uruma.component.UIComponentContainer; |
22 | |
import org.seasar.uruma.component.UIElement; |
23 | |
import org.seasar.uruma.component.jface.MenuComponent; |
24 | |
import org.seasar.uruma.component.jface.MenuItemComponent; |
25 | |
import org.seasar.uruma.component.jface.SeparatorComponent; |
26 | |
import org.seasar.uruma.component.rcp.ViewPartComponent; |
27 | |
import org.seasar.uruma.component.rcp.WorkbenchComponent; |
28 | |
import org.seasar.uruma.core.UrumaConstants; |
29 | |
import org.seasar.uruma.rcp.binding.CommandDesc; |
30 | |
import org.seasar.uruma.rcp.binding.CommandRegistry; |
31 | |
import org.seasar.uruma.rcp.configuration.ConfigurationElement; |
32 | |
import org.seasar.uruma.rcp.configuration.Extension; |
33 | |
import org.seasar.uruma.rcp.configuration.ExtensionBuilder; |
34 | |
import org.seasar.uruma.rcp.configuration.ExtensionFactory; |
35 | |
import org.seasar.uruma.rcp.configuration.ExtensionPoints; |
36 | |
import org.seasar.uruma.rcp.configuration.elements.CategoryElement; |
37 | |
import org.seasar.uruma.rcp.configuration.elements.CommandElement; |
38 | |
import org.seasar.uruma.rcp.configuration.elements.ContextElement; |
39 | |
import org.seasar.uruma.rcp.configuration.elements.KeyElement; |
40 | |
import org.seasar.uruma.rcp.configuration.elements.MenuCommandElement; |
41 | |
import org.seasar.uruma.rcp.configuration.elements.MenuContributionElement; |
42 | |
import org.seasar.uruma.rcp.configuration.elements.MenuElement; |
43 | |
import org.seasar.uruma.rcp.configuration.elements.SchemeElement; |
44 | |
import org.seasar.uruma.rcp.configuration.elements.SeparatorElement; |
45 | |
import org.seasar.uruma.rcp.configuration.elements.ToolbarElement; |
46 | |
import org.seasar.uruma.rcp.util.UrumaServiceUtil; |
47 | |
import org.seasar.uruma.util.MnemonicUtil; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public class MenusBuilder extends AbstractExtensionBuilder implements |
56 | |
UrumaConstants { |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public static final String CONTEXT_SUFFIX = ".context"; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public static final String DEFAULT_COMMAND_ID_SUFFIX = ".command."; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public static final String DEFAULT_MENUS_ID_SUFFIX = ".menus."; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | public static String DEFAULT_CATEGORY_ID_SUFFIX = ".command.category"; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | public static String DEFAULT_CONTEXT_PARENT_ID = "org.eclipse.ui.contexts.window"; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
protected static final String DEFAULT_MENU_URI = "menu:org.eclipse.ui.main.menu?after=additions"; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected static final String DEFAULT_TOOLBAR_URI = "toolbar:org.eclipse.ui.main.toolbar?after=additions"; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected static final String DEFAULT_POPUP_URI = "popup:org.eclipse.ui.popup.any?after=additions"; |
97 | |
|
98 | |
protected int actionCount; |
99 | |
|
100 | |
protected Extension contexts; |
101 | |
|
102 | |
protected Extension commands; |
103 | |
|
104 | |
protected Extension handlers; |
105 | |
|
106 | |
protected Extension bindings; |
107 | |
|
108 | |
protected Extension menus; |
109 | |
|
110 | |
protected Extension definitions; |
111 | |
|
112 | |
protected CommandRegistry commandRegistry; |
113 | |
|
114 | |
protected String localContextId; |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public Extension[] buildExtension() { |
120 | 0 | this.commandRegistry = service.getCommandRegistry(); |
121 | |
|
122 | 0 | createExtensions(); |
123 | |
|
124 | 0 | setupScheme(); |
125 | 0 | setupContexts(); |
126 | |
|
127 | 0 | MenuContributionElement menuContribution = setupMenuContribution(); |
128 | 0 | MenuContributionElement popupContribution = setupPopupContribution(); |
129 | 0 | MenuContributionElement toolbarContribution = setupToolbarContribution(); |
130 | |
|
131 | 0 | CategoryElement category = setupCategory(); |
132 | |
|
133 | 0 | WorkbenchComponent workbenchComponent = service.getWorkbenchComponent(); |
134 | 0 | for (MenuComponent menu : workbenchComponent.getMenus()) { |
135 | 0 | traverseMenu(category, menu, null, menuContribution); |
136 | 0 | traverseToolbar(category, menu, null, toolbarContribution); |
137 | |
} |
138 | |
|
139 | 0 | for (ViewPartComponent view : service.getViewPartComponent()) { |
140 | |
|
141 | 0 | MenuContributionElement viewMenuContribution = setupViewMenuContribution(view); |
142 | 0 | MenuContributionElement viewToolbarContribution = setupViewToolbarContribution(view); |
143 | 0 | if (!StringUtil.isEmpty(view.menu)) { |
144 | 0 | for (MenuComponent menu : view.getMenus()) { |
145 | 0 | if (view.menu.equals(menu.getId())) { |
146 | 0 | traverseMenu(category, menu, null, viewMenuContribution); |
147 | 0 | traverseToolbar(category, menu, null, |
148 | |
viewToolbarContribution); |
149 | |
} |
150 | |
} |
151 | |
} |
152 | |
|
153 | 0 | for (MenuComponent menu : view.getMenus()) { |
154 | |
|
155 | |
|
156 | |
} |
157 | 0 | } |
158 | 0 | return new Extension[] { contexts, commands, handlers, bindings, menus, |
159 | |
definitions }; |
160 | |
|
161 | |
} |
162 | |
|
163 | |
private void visitForMenuSearch(final CategoryElement category, |
164 | |
final UIElement element, |
165 | |
final MenuContributionElement popupContribution) { |
166 | 0 | if (element instanceof UIComponentContainer) { |
167 | 0 | UIComponentContainer uiCC = (UIComponentContainer) element; |
168 | |
|
169 | 0 | if (element instanceof MenuComponent) { |
170 | 0 | traverseMenu(category, (MenuComponent) element, null, |
171 | |
popupContribution); |
172 | |
} else { |
173 | 0 | for (UIElement child : uiCC.getChildren()) { |
174 | 0 | visitForMenuSearch(category, child, popupContribution); |
175 | |
} |
176 | |
} |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
protected void createExtensions() { |
181 | 0 | contexts = ExtensionFactory.createExtension(ExtensionPoints.CONTEXTS); |
182 | 0 | commands = ExtensionFactory.createExtension(ExtensionPoints.COMMANDS); |
183 | 0 | handlers = ExtensionFactory.createExtension(ExtensionPoints.HANDLERS); |
184 | 0 | bindings = ExtensionFactory.createExtension(ExtensionPoints.BINDINGS); |
185 | 0 | menus = ExtensionFactory.createExtension(ExtensionPoints.MENUS); |
186 | 0 | definitions = ExtensionFactory |
187 | |
.createExtension(ExtensionPoints.DEFINITIONS); |
188 | 0 | } |
189 | |
|
190 | |
protected void setupScheme() { |
191 | 0 | SchemeElement scheme = new SchemeElement(URUMA_APP_SCHEME_ID, |
192 | |
URUMA_APP_SCHEME_NAME); |
193 | 0 | bindings.addElement(scheme); |
194 | 0 | } |
195 | |
|
196 | |
protected CategoryElement setupCategory() { |
197 | 0 | String pluginId = service.getPluginId(); |
198 | 0 | CategoryElement category = new CategoryElement(pluginId |
199 | |
+ DEFAULT_CATEGORY_ID_SUFFIX, pluginId); |
200 | 0 | commands.addElement(category); |
201 | 0 | return category; |
202 | |
} |
203 | |
|
204 | |
protected MenuContributionElement setupMenuContribution() { |
205 | 0 | MenuContributionElement menuContribution = new MenuContributionElement(); |
206 | 0 | menuContribution.locationURI = DEFAULT_MENU_URI; |
207 | 0 | this.menus.addElement(menuContribution); |
208 | 0 | return menuContribution; |
209 | |
} |
210 | |
|
211 | |
protected MenuContributionElement setupPopupContribution() { |
212 | 0 | MenuContributionElement menuContribution = new MenuContributionElement(); |
213 | 0 | menuContribution.locationURI = DEFAULT_POPUP_URI; |
214 | 0 | this.menus.addElement(menuContribution); |
215 | 0 | return menuContribution; |
216 | |
} |
217 | |
|
218 | |
protected MenuContributionElement setupToolbarContribution() { |
219 | 0 | MenuContributionElement menuContribution = new MenuContributionElement(); |
220 | 0 | menuContribution.locationURI = DEFAULT_TOOLBAR_URI; |
221 | 0 | this.menus.addElement(menuContribution); |
222 | 0 | return menuContribution; |
223 | |
} |
224 | |
|
225 | |
protected MenuContributionElement setupViewToolbarContribution( |
226 | |
final ViewPartComponent view) { |
227 | 0 | MenuContributionElement menuContribution = new MenuContributionElement(); |
228 | 0 | menuContribution.locationURI = "toolbar:" |
229 | |
+ service.createRcpId(view.getId()) + "?after=additions"; |
230 | 0 | this.menus.addElement(menuContribution); |
231 | 0 | return menuContribution; |
232 | |
} |
233 | |
|
234 | |
protected MenuContributionElement setupViewMenuContribution( |
235 | |
final ViewPartComponent view) { |
236 | 0 | MenuContributionElement menuContribution = new MenuContributionElement(); |
237 | 0 | menuContribution.locationURI = "menu:" |
238 | |
+ service.createRcpId(view.getId()) + "?after=additions"; |
239 | 0 | this.menus.addElement(menuContribution); |
240 | 0 | return menuContribution; |
241 | |
} |
242 | |
|
243 | |
protected void traverseMenu(final CategoryElement category, |
244 | |
final MenuComponent menuComponent, |
245 | |
final MenuElement parentMenuElement, |
246 | |
final MenuContributionElement menuContribution) { |
247 | 0 | List<UIElement> children = menuComponent.getChildren(); |
248 | 0 | for (UIElement child : children) { |
249 | 0 | if (child instanceof MenuComponent) { |
250 | |
MenuElement menuElement; |
251 | 0 | if (DEFAULT_MENU_URI.equals(menuContribution.locationURI) |
252 | |
|| DEFAULT_POPUP_URI |
253 | |
.equals(menuContribution.locationURI)) { |
254 | 0 | menuElement = setupMenu((MenuComponent) child, |
255 | |
parentMenuElement, menuContribution); |
256 | |
} else { |
257 | 0 | menuElement = parentMenuElement; |
258 | |
} |
259 | 0 | traverseMenu(category, (MenuComponent) child, menuElement, |
260 | |
menuContribution); |
261 | 0 | } else if (child instanceof SeparatorComponent) { |
262 | 0 | SeparatorComponent separator = (SeparatorComponent) child; |
263 | 0 | setupSeparator(separator, parentMenuElement, menuContribution); |
264 | |
|
265 | 0 | } else if (child instanceof SeparatorComponent) { |
266 | 0 | SeparatorComponent separator = (SeparatorComponent) child; |
267 | 0 | setupSeparator(separator, parentMenuElement, menuContribution); |
268 | |
|
269 | 0 | } else if (child instanceof MenuItemComponent) { |
270 | 0 | MenuItemComponent menuItem = (MenuItemComponent) child; |
271 | 0 | String commandId = createCommandId(menuItem); |
272 | |
|
273 | 0 | setupCommand(category, commandId, menuItem); |
274 | 0 | setupKey(commandId, menuItem); |
275 | 0 | setupMenuCommand(commandId, menuItem, parentMenuElement, |
276 | |
menuContribution); |
277 | 0 | } |
278 | |
} |
279 | 0 | } |
280 | |
|
281 | |
protected void traverseToolbar(final CategoryElement category, |
282 | |
final MenuComponent menuComponent, |
283 | |
final ToolbarElement parentToolbarElement, |
284 | |
final MenuContributionElement menuContribution) { |
285 | 0 | List<UIElement> children = menuComponent.getChildren(); |
286 | 0 | for (UIElement child : children) { |
287 | 0 | if (child instanceof MenuComponent) { |
288 | |
ToolbarElement toolbarElement; |
289 | 0 | if (DEFAULT_TOOLBAR_URI.equals(menuContribution.locationURI)) { |
290 | |
|
291 | 0 | toolbarElement = setupToolbar((MenuComponent) child, |
292 | |
parentToolbarElement, menuContribution); |
293 | |
} else { |
294 | |
|
295 | 0 | toolbarElement = parentToolbarElement; |
296 | |
} |
297 | 0 | traverseToolbar(category, (MenuComponent) child, |
298 | |
toolbarElement, menuContribution); |
299 | |
|
300 | 0 | } else if (child instanceof SeparatorComponent) { |
301 | |
|
302 | |
|
303 | 0 | } else if (child instanceof MenuItemComponent) { |
304 | 0 | MenuItemComponent menuItem = (MenuItemComponent) child; |
305 | 0 | String commandId = createCommandId(menuItem); |
306 | |
|
307 | 0 | setupCommand(category, commandId, menuItem); |
308 | 0 | setupKey(commandId, menuItem); |
309 | 0 | setupToolbarCommand(commandId, menuItem, parentToolbarElement, |
310 | |
menuContribution); |
311 | 0 | } |
312 | |
} |
313 | 0 | } |
314 | |
|
315 | |
protected MenuElement setupMenu(final MenuComponent component, |
316 | |
final MenuElement parentElement, |
317 | |
final MenuContributionElement menuContribution) { |
318 | 0 | if (!(component.getParent() instanceof WorkbenchComponent)) { |
319 | 0 | String text = component.text; |
320 | 0 | if (text == null) { |
321 | 0 | text = NULL_STRING; |
322 | |
} |
323 | 0 | MenuElement menu = new MenuElement(MnemonicUtil |
324 | |
.chopMnemonicAndAccelerator(text)); |
325 | 0 | if (!StringUtil.isEmpty(component.getId())) { |
326 | 0 | menu.id = service.createRcpId(component.getId()); |
327 | |
} |
328 | |
|
329 | 0 | if (!StringUtil.isEmpty(text)) { |
330 | 0 | menu.mnemonic = MnemonicUtil.getMnemonic(text); |
331 | |
} |
332 | |
|
333 | 0 | if (!StringUtil.isEmpty(component.image)) { |
334 | 0 | menu.icon = getRcpImagePath(component.image); |
335 | |
} |
336 | |
|
337 | 0 | if (parentElement == null) { |
338 | |
|
339 | 0 | menuContribution.addElement(menu); |
340 | |
} else { |
341 | |
|
342 | 0 | parentElement.addElement(menu); |
343 | |
} |
344 | |
|
345 | 0 | return menu; |
346 | |
} else { |
347 | 0 | return null; |
348 | |
} |
349 | |
} |
350 | |
|
351 | |
protected ToolbarElement setupToolbar(final MenuComponent component, |
352 | |
final ToolbarElement parentElement, |
353 | |
final MenuContributionElement menuContribution) { |
354 | 0 | if (!(component.getParent() instanceof WorkbenchComponent)) { |
355 | 0 | String id = service.getPluginId() + ".toolbar." + component.text; |
356 | 0 | ToolbarElement toolbar = new ToolbarElement(id); |
357 | |
|
358 | 0 | if (parentElement == null) { |
359 | |
|
360 | 0 | menuContribution.addElement(toolbar); |
361 | |
} else { |
362 | |
|
363 | 0 | parentElement.addElement(toolbar); |
364 | |
} |
365 | 0 | return toolbar; |
366 | |
} else { |
367 | 0 | return null; |
368 | |
} |
369 | |
} |
370 | |
|
371 | |
protected String createCommandId(final MenuItemComponent menuItem) { |
372 | 0 | String id = menuItem.getId(); |
373 | 0 | if (StringUtil.isEmpty(id)) { |
374 | 0 | id = AUTO_ACTION_ID_PREFIX + actionCount++; |
375 | 0 | menuItem.setId(id); |
376 | |
} |
377 | 0 | id = service.getPluginId() + DEFAULT_COMMAND_ID_SUFFIX + id; |
378 | 0 | return id; |
379 | |
} |
380 | |
|
381 | |
protected void setupCommand(final CategoryElement category, |
382 | |
final String commandId, final MenuItemComponent component) { |
383 | 0 | if (component instanceof SeparatorComponent) { |
384 | 0 | return; |
385 | |
} |
386 | |
|
387 | 0 | for (ConfigurationElement ce : commands.getElements()) { |
388 | 0 | if (ce instanceof CommandElement) { |
389 | 0 | CommandElement existCommandElement = (CommandElement) ce; |
390 | 0 | if (existCommandElement.id.equals(commandId)) { |
391 | 0 | return; |
392 | |
} |
393 | 0 | } |
394 | |
} |
395 | |
|
396 | 0 | String name = MnemonicUtil.chopMnemonicAndAccelerator(component.text); |
397 | |
|
398 | 0 | CommandElement command = new CommandElement(commandId, name); |
399 | 0 | command.categoryId = category.id; |
400 | 0 | commands.addElement(command); |
401 | |
|
402 | 0 | CommandDesc desc = new CommandDesc(commandId, component.getId()); |
403 | 0 | commandRegistry.registerCommandDesc(desc); |
404 | 0 | } |
405 | |
|
406 | |
protected void setupContexts() { |
407 | 0 | this.localContextId = UrumaServiceUtil.getService().getPluginId() |
408 | |
+ CONTEXT_SUFFIX; |
409 | 0 | ContextElement context = new ContextElement(localContextId, |
410 | |
"Uruma allication local context", DEFAULT_CONTEXT_PARENT_ID); |
411 | 0 | contexts.addElement(context); |
412 | 0 | } |
413 | |
|
414 | |
protected void setupKey(final String commandId, |
415 | |
final MenuItemComponent menuItem) { |
416 | 0 | if (!StringUtil.isEmpty(menuItem.accelerator)) { |
417 | 0 | for (ConfigurationElement ce : bindings.getElements()) { |
418 | 0 | if (ce instanceof KeyElement) { |
419 | 0 | KeyElement existKeyElement = (KeyElement) ce; |
420 | 0 | if (existKeyElement.commandId.equals(commandId)) { |
421 | 0 | return; |
422 | |
} |
423 | 0 | } |
424 | |
} |
425 | |
|
426 | 0 | String sequence = menuItem.accelerator; |
427 | 0 | KeyElement key = new KeyElement(sequence, URUMA_APP_SCHEME_ID, |
428 | |
DEFAULT_CONTEXT_PARENT_ID); |
429 | 0 | key.commandId = commandId; |
430 | |
|
431 | |
|
432 | 0 | bindings.addElement(key); |
433 | |
} |
434 | 0 | } |
435 | |
|
436 | |
protected void setupMenuCommand(final String commandId, |
437 | |
final MenuItemComponent menuItem, |
438 | |
final MenuElement parentMenuElement, |
439 | |
final MenuContributionElement menuContribution) { |
440 | 0 | MenuCommandElement command = new MenuCommandElement(commandId); |
441 | 0 | if (!StringUtil.isEmpty(menuItem.text)) { |
442 | 0 | command.mnemonic = MnemonicUtil.getMnemonic(menuItem.text); |
443 | |
} |
444 | |
|
445 | 0 | command.icon = getRcpImagePath(menuItem.image); |
446 | 0 | command.disabledIcon = getRcpImagePath(menuItem.disabledImage); |
447 | 0 | command.hoverIcon = getRcpImagePath(menuItem.hoverImage); |
448 | 0 | command.style = parseStyle(menuItem.getStyle()); |
449 | |
|
450 | 0 | if (parentMenuElement == null) { |
451 | 0 | menuContribution.addElement(command); |
452 | |
} else { |
453 | 0 | parentMenuElement.addElement(command); |
454 | |
} |
455 | |
|
456 | 0 | } |
457 | |
|
458 | |
protected void setupToolbarCommand(final String commandId, |
459 | |
final MenuItemComponent menuItem, |
460 | |
final ToolbarElement parentToolbarElement, |
461 | |
final MenuContributionElement menuContribution) { |
462 | 0 | MenuCommandElement command = new MenuCommandElement(commandId); |
463 | 0 | if (!StringUtil.isEmpty(menuItem.text)) { |
464 | 0 | command.mnemonic = MnemonicUtil.getMnemonic(menuItem.text); |
465 | |
} |
466 | |
|
467 | 0 | command.icon = getRcpImagePath(menuItem.image); |
468 | 0 | command.disabledIcon = getRcpImagePath(menuItem.disabledImage); |
469 | 0 | command.hoverIcon = getRcpImagePath(menuItem.hoverImage); |
470 | 0 | command.style = parseStyle(menuItem.getStyle()); |
471 | |
|
472 | 0 | if (parentToolbarElement == null) { |
473 | 0 | menuContribution.addElement(command); |
474 | |
} else { |
475 | 0 | parentToolbarElement.addElement(command); |
476 | |
} |
477 | 0 | } |
478 | |
|
479 | |
protected void setupSeparator(final SeparatorComponent separatorComponent, |
480 | |
final MenuElement parentMenuElement, |
481 | |
final MenuContributionElement menuContribution) { |
482 | 0 | SeparatorElement element = new SeparatorElement("none", true); |
483 | 0 | if (parentMenuElement == null) { |
484 | 0 | menuContribution.addElement(element); |
485 | |
} else { |
486 | 0 | parentMenuElement.addElement(element); |
487 | |
} |
488 | 0 | } |
489 | |
|
490 | |
protected String parseStyle(final String style) { |
491 | 0 | if (StringUtil.isEmpty(style)) { |
492 | 0 | return MenuCommandElement.STYLE_PUSH; |
493 | |
} else { |
494 | 0 | String result = style.toLowerCase(); |
495 | 0 | if ("check".equals(result)) { |
496 | 0 | return MenuCommandElement.STYLE_TOGGLE; |
497 | |
} |
498 | 0 | return result; |
499 | |
} |
500 | |
} |
501 | |
|
502 | |
} |