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.ui;
17  
18  import java.util.List;
19  
20  import org.eclipse.ui.IFolderLayout;
21  import org.eclipse.ui.IPageLayout;
22  import org.eclipse.ui.IPerspectiveFactory;
23  import org.seasar.framework.util.StringUtil;
24  import org.seasar.uruma.component.Template;
25  import org.seasar.uruma.component.UIElement;
26  import org.seasar.uruma.component.UIElementContainer;
27  import org.seasar.uruma.component.rcp.PartComponent;
28  import org.seasar.uruma.component.rcp.PartFolderComponent;
29  import org.seasar.uruma.component.rcp.PerspectiveComponent;
30  import org.seasar.uruma.component.rcp.ViewPartComponent;
31  import org.seasar.uruma.core.TemplateManager;
32  import org.seasar.uruma.core.UrumaMessageCodes;
33  import org.seasar.uruma.exception.NotFoundException;
34  import org.seasar.uruma.rcp.UrumaService;
35  import org.seasar.uruma.rcp.util.UrumaServiceUtil;
36  import org.seasar.uruma.rcp.util.ViewPartUtil;
37  
38  /**
39   * <code>workbench.xml</code> に記述された <code>perspective</code>
40   * 要素からパースペクティブを生成するクラスです。<br />
41   * 
42   * @author y-komori
43   */
44  public class GenericPerspectiveFactory implements IPerspectiveFactory,
45          UrumaMessageCodes {
46  
47      private static final String PART_LEFT = "LEFT";
48  
49      private static final String PART_RIGHT = "RIGHT";
50  
51      private static final String PART_TOP = "TOP";
52  
53      private static final String PART_BOTTOM = "BOTTOM";
54  
55      protected UrumaService service;
56  
57      protected TemplateManager templateManager;
58  
59      protected int folderNum = 1;
60  
61      /**
62       * {@link GenericPerspectiveFactory} を構築します。<br />
63       */
64      public GenericPerspectiveFactory() {
65          this.service = UrumaServiceUtil.getService();
66          this.templateManager = (TemplateManager) service.getContainer()
67                  .getComponent(TemplateManager.class);
68      }
69  
70      /*
71       * @see org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui.IPageLayout)
72       */
73      public void createInitialLayout(final IPageLayout layout) {
74          UrumaService service = UrumaServiceUtil.getService();
75  
76          layout.setEditorAreaVisible(false);
77  
78          String perspectiveId = layout.getDescriptor().getId();
79  
80          List<PerspectiveComponent> perspectives = service
81                  .getWorkbenchComponent().getPerspectives();
82  
83          PerspectiveComponent perspective = findPerspective(perspectives,
84                  perspectiveId);
85  
86          if (perspective == null) {
87              return;
88          }
89  
90          setupChildren(perspective, layout, perspectiveId);
91      }
92  
93      protected void setupChildren(final UIElementContainer container,
94              final Object layout, final String perspectiveId) {
95          List<UIElement> children = container.getChildren();
96          for (UIElement child : children) {
97              if (child instanceof PartComponent) {
98                  PartComponent part = (PartComponent) child;
99                  if (!setupLayout(layout, part, container)) {
100                     throw new NotFoundException(PART_IN_PERSPECTIVE_NOT_FOUND,
101                             service.getLocalId(perspectiveId), (part).ref);
102                 }
103             } else if (child instanceof PartFolderComponent) {
104                 PartFolderComponent component = (PartFolderComponent) child;
105                 IFolderLayout folder = createFolder((IPageLayout) layout,
106                         component);
107                 setupChildren(component, folder, perspectiveId);
108             }
109         }
110 
111     }
112 
113     protected IFolderLayout createFolder(final IPageLayout layout,
114             final PartFolderComponent folder) {
115         int pos = getPosition(folder.position);
116         float ratio = getRatio(folder.ratio);
117         String id = getFolderId(folder);
118         return layout.createFolder(id, pos, ratio, layout.getEditorArea());
119     }
120 
121     protected boolean setupLayout(final Object layout,
122             final PartComponent part, final UIElementContainer parent) {
123         int pos = getPosition(part.position);
124         float ratio = getRatio(part.ratio);
125         String refViewId = UrumaServiceUtil.getService().createRcpId(part.ref);
126 
127         if (findViewPart(refViewId)) {
128             addView(layout, refViewId, pos, ratio);
129             return true;
130         } else {
131             if (ViewPartUtil.findViewDescriptor(part.ref) != null) {
132                 addView(layout, part.ref, pos, ratio);
133                 return true;
134             } else {
135                 return false;
136             }
137         }
138     }
139 
140     protected void addView(final Object parent, final String viewId,
141             final int pos, final float ratio) {
142         if (parent instanceof IPageLayout) {
143             IPageLayout layout = (IPageLayout) parent;
144             layout.addStandaloneView(viewId, true, pos, ratio, layout
145                     .getEditorArea());
146         } else if (parent instanceof IFolderLayout) {
147             IFolderLayout layout = (IFolderLayout) parent;
148             layout.addView(viewId);
149         }
150     }
151 
152     protected int getPosition(final String str) {
153         int pos = IPageLayout.LEFT;
154         if (PART_LEFT.equals(str)) {
155             pos = IPageLayout.LEFT;
156         } else if (PART_RIGHT.equals(str)) {
157             pos = IPageLayout.RIGHT;
158         } else if (PART_TOP.equals(str)) {
159             pos = IPageLayout.TOP;
160         } else if (PART_BOTTOM.equals(str)) {
161             pos = IPageLayout.BOTTOM;
162         }
163         return pos;
164     }
165 
166     protected float getRatio(final String str) {
167         if (!StringUtil.isEmpty(str)) {
168             return Integer.parseInt(str) / (float) 100;
169         } else {
170             return 0.95f;
171         }
172     }
173 
174     protected String getFolderId(final PartFolderComponent folder) {
175         if (!StringUtil.isEmpty(folder.id)) {
176             return folder.id;
177         } else {
178             return "folder" + folderNum++;
179         }
180     }
181 
182     protected PerspectiveComponent findPerspective(
183             final List<PerspectiveComponent> perspectives,
184             final String perspectiveId) {
185         String localId = service.getLocalId(perspectiveId);
186         for (PerspectiveComponent perspective : perspectives) {
187             if (localId.equals(perspective.id)) {
188                 return perspective;
189             }
190         }
191         return null;
192     }
193 
194     protected boolean findViewPart(final String viewId) {
195         List<Template> templates = templateManager
196                 .getTemplates(ViewPartComponent.class);
197         String localViewId = ViewPartUtil.getPrimaryId(service
198                 .getLocalId(viewId));
199 
200         for (Template template : templates) {
201             ViewPartComponent viewPart = (ViewPartComponent) template
202                     .getRootComponent();
203             if (localViewId.equals(viewPart.getId())) {
204                 return true;
205             }
206         }
207 
208         return false;
209     }
210 }