View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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.UIElement;
22  import org.seasar.uruma.component.jface.MenuComponent;
23  import org.seasar.uruma.component.jface.MenuItemComponent;
24  import org.seasar.uruma.component.rcp.WorkbenchComponent;
25  import org.seasar.uruma.core.UrumaConstants;
26  import org.seasar.uruma.rcp.UrumaService;
27  import org.seasar.uruma.rcp.configuration.Extension;
28  import org.seasar.uruma.rcp.configuration.ExtensionBuilder;
29  import org.seasar.uruma.rcp.configuration.ExtensionFactory;
30  import org.seasar.uruma.rcp.configuration.ExtensionPoints;
31  import org.seasar.uruma.rcp.configuration.elements.ActionElement;
32  import org.seasar.uruma.rcp.configuration.elements.ActionSetElement;
33  import org.seasar.uruma.rcp.configuration.elements.ActionSetsMenuElement;
34  import org.seasar.uruma.rcp.util.UrumaServiceUtil;
35  
36  /**
37   * <code>actionSets</code> 拡張ポイントを生成するための {@link ExtensionBuilder} です。<br />
38   * 
39   * @author y-komori
40   */
41  public class ActionSetsBuilder implements ExtensionBuilder, UrumaConstants {
42      static final String START_MARKER = "startMarker";
43  
44      static final String ADDITIONS = "additions";
45  
46      static final String URUMA_MENU = "urumaMenu";
47  
48      protected UrumaService service;
49  
50      protected int menuCount;
51  
52      protected int actionCount;
53  
54      /**
55       * {@link ActionSetsBuilder} を構築します。<br />
56       */
57      public ActionSetsBuilder() {
58          this.service = UrumaServiceUtil.getService();
59      }
60  
61      /*
62       * @see org.seasar.uruma.rcp.configuration.ExtensionBuilder#buildExtension()
63       */
64      public Extension[] buildExtension() {
65          this.menuCount = 0;
66          this.actionCount = 0;
67  
68          return new Extension[] { setupActionSets() };
69      }
70  
71      protected Extension setupActionSets() {
72          Extension extension = ExtensionFactory
73                  .createExtension(ExtensionPoints.ACTIONSETS);
74  
75          ActionSetElement actionSet = createActionSet();
76  
77          WorkbenchComponent workbenchComponent = service.getWorkbenchComponent();
78          for (MenuComponent menu : workbenchComponent.getMenus()) {
79              List<UIElement> children = menu.getChildren();
80              for (UIElement child : children) {
81                  if (child instanceof MenuComponent) {
82                      setupMenu(actionSet, (MenuComponent) child, null);
83                  }
84              }
85          }
86  
87          extension.addElement(actionSet);
88  
89          return extension;
90      }
91  
92      protected ActionSetElement createActionSet() {
93          ActionSetElement actionSet = new ActionSetElement();
94          actionSet.id = service.getPluginId() + ".actionSet";
95          actionSet.label = service.getPluginId() + "'s actionSet.";
96          return actionSet;
97      }
98  
99      protected void setupMenu(final ActionSetElement actionSet,
100             final MenuComponent menuComponent, final String parentPath) {
101         ActionSetsMenuElement menu = new ActionSetsMenuElement(menuComponent);
102 
103         // Menu の id が設定されていない場合は自動設定する
104         String id = menuComponent.getId();
105         if (StringUtil.isEmpty(id)) {
106             id = AUTO_MENU_ID_PREFIX + menuCount++;
107         }
108         menu.id = service.createRcpId(id);
109 
110         String path = ((parentPath != null) ? parentPath + SLASH : NULL_STRING)
111                 + menu.id;
112         menu.path = path + SLASH + URUMA_MENU;
113         actionSet.addElement(menu);
114 
115         List<UIElement> children = menuComponent.getChildren();
116         for (UIElement child : children) {
117             if (child instanceof MenuComponent) {
118                 setupMenu(actionSet, (MenuComponent) child, path);
119             } else if (child instanceof MenuItemComponent) {
120                 setupMenuItem(actionSet, (MenuItemComponent) child, path);
121             }
122         }
123     }
124 
125     protected void setupMenuItem(final ActionSetElement actionSet,
126             final MenuItemComponent menuItem, final String parentPath) {
127         ActionElement action = new ActionElement(menuItem);
128 
129         // MenuItem の id が設定されていない場合は自動設定する
130         String id = menuItem.getId();
131         if (StringUtil.isEmpty(id)) {
132             id = AUTO_ACTION_ID_PREFIX + actionCount++;
133         }
134         action.id = service.createRcpId(id);
135 
136         action.menubarPath = ((parentPath != null) ? (parentPath + SLASH)
137                 : NULL_STRING)
138                 + ADDITIONS;
139 
140         actionSet.addElement(action);
141     }
142 }