|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  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 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  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 |    0 |        protected int folderNum = 1;  | 
  |  60 |     | 
     | 
  |  61 |     | 
         | 
  |  62 |     | 
     | 
  |  63 |     | 
     | 
  |  64 |    0 |        public GenericPerspectiveFactory() { | 
  |  65 |    0 |            this.service = UrumaServiceUtil.getService();  | 
  |  66 |    0 |            this.templateManager = (TemplateManager) service.getContainer()  | 
  |  67 |     | 
                   .getComponent(TemplateManager.class);  | 
  |  68 |    0 |        }  | 
  |  69 |     | 
     | 
  |  70 |     | 
         | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
       public void createInitialLayout(final IPageLayout layout) { | 
  |  74 |    0 |            UrumaService service = UrumaServiceUtil.getService();  | 
  |  75 |     | 
     | 
  |  76 |    0 |            layout.setEditorAreaVisible(false);  | 
  |  77 |     | 
     | 
  |  78 |    0 |            String perspectiveId = layout.getDescriptor().getId();  | 
  |  79 |     | 
     | 
  |  80 |    0 |            List<PerspectiveComponent> perspectives = service  | 
  |  81 |     | 
                   .getWorkbenchComponent().getPerspectives();  | 
  |  82 |     | 
     | 
  |  83 |    0 |            PerspectiveComponent perspective = findPerspective(perspectives,  | 
  |  84 |     | 
                   perspectiveId);  | 
  |  85 |     | 
     | 
  |  86 |    0 |            if (perspective == null) { | 
  |  87 |    0 |                return;  | 
  |  88 |     | 
           }  | 
  |  89 |     | 
     | 
  |  90 |    0 |            setupChildren(perspective, layout, perspectiveId);  | 
  |  91 |    0 |        }  | 
  |  92 |     | 
     | 
  |  93 |     | 
       protected void setupChildren(final UIElementContainer container,  | 
  |  94 |     | 
               final Object layout, final String perspectiveId) { | 
  |  95 |    0 |            List<UIElement> children = container.getChildren();  | 
  |  96 |    0 |            for (UIElement child : children) { | 
  |  97 |    0 |                if (child instanceof PartComponent) { | 
  |  98 |    0 |                    PartComponent part = (PartComponent) child;  | 
  |  99 |    0 |                    if (!setupLayout(layout, part, container)) { | 
  |  100 |    0 |                        throw new NotFoundException(PART_IN_PERSPECTIVE_NOT_FOUND,  | 
  |  101 |     | 
                               service.getLocalId(perspectiveId), (part).ref);  | 
  |  102 |     | 
                   }  | 
  |  103 |    0 |                } else if (child instanceof PartFolderComponent) { | 
  |  104 |    0 |                    PartFolderComponent component = (PartFolderComponent) child;  | 
  |  105 |    0 |                    IFolderLayout folder = createFolder((IPageLayout) layout,  | 
  |  106 |     | 
                           component);  | 
  |  107 |    0 |                    setupChildren(component, folder, perspectiveId);  | 
  |  108 |    0 |                }  | 
  |  109 |     | 
           }  | 
  |  110 |     | 
     | 
  |  111 |    0 |        }  | 
  |  112 |     | 
     | 
  |  113 |     | 
       protected IFolderLayout createFolder(final IPageLayout layout,  | 
  |  114 |     | 
               final PartFolderComponent folder) { | 
  |  115 |    0 |            int pos = getPosition(folder.position);  | 
  |  116 |    0 |            float ratio = getRatio(folder.ratio);  | 
  |  117 |    0 |            String id = getFolderId(folder);  | 
  |  118 |    0 |            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 |    0 |            int pos = getPosition(part.position);  | 
  |  124 |    0 |            float ratio = getRatio(part.ratio);  | 
  |  125 |    0 |            String refViewId = UrumaServiceUtil.getService().createRcpId(part.ref);  | 
  |  126 |     | 
     | 
  |  127 |    0 |            if (findViewPart(refViewId)) { | 
  |  128 |    0 |                addView(layout, refViewId, pos, ratio);  | 
  |  129 |    0 |                return true;  | 
  |  130 |     | 
           } else { | 
  |  131 |    0 |                if (ViewPartUtil.findViewDescriptor(part.ref) != null) { | 
  |  132 |    0 |                    addView(layout, part.ref, pos, ratio);  | 
  |  133 |    0 |                    return true;  | 
  |  134 |     | 
               } else { | 
  |  135 |    0 |                    return false;  | 
  |  136 |     | 
               }  | 
  |  137 |     | 
           }  | 
  |  138 |     | 
       }  | 
  |  139 |     | 
     | 
  |  140 |     | 
       protected void addView(final Object parent, final String viewId,  | 
  |  141 |     | 
               final int pos, final float ratio) { | 
  |  142 |    0 |            if (parent instanceof IPageLayout) { | 
  |  143 |    0 |                IPageLayout layout = (IPageLayout) parent;  | 
  |  144 |    0 |                layout.addStandaloneView(viewId, true, pos, ratio, layout  | 
  |  145 |     | 
                       .getEditorArea());  | 
  |  146 |    0 |            } else if (parent instanceof IFolderLayout) { | 
  |  147 |    0 |                IFolderLayout layout = (IFolderLayout) parent;  | 
  |  148 |    0 |                layout.addView(viewId);  | 
  |  149 |     | 
           }  | 
  |  150 |    0 |        }  | 
  |  151 |     | 
     | 
  |  152 |     | 
       protected int getPosition(final String str) { | 
  |  153 |    0 |            int pos = IPageLayout.LEFT;  | 
  |  154 |    0 |            if (PART_LEFT.equals(str)) { | 
  |  155 |    0 |                pos = IPageLayout.LEFT;  | 
  |  156 |    0 |            } else if (PART_RIGHT.equals(str)) { | 
  |  157 |    0 |                pos = IPageLayout.RIGHT;  | 
  |  158 |    0 |            } else if (PART_TOP.equals(str)) { | 
  |  159 |    0 |                pos = IPageLayout.TOP;  | 
  |  160 |    0 |            } else if (PART_BOTTOM.equals(str)) { | 
  |  161 |    0 |                pos = IPageLayout.BOTTOM;  | 
  |  162 |     | 
           }  | 
  |  163 |    0 |            return pos;  | 
  |  164 |     | 
       }  | 
  |  165 |     | 
     | 
  |  166 |     | 
       protected float getRatio(final String str) { | 
  |  167 |    0 |            if (!StringUtil.isEmpty(str)) { | 
  |  168 |    0 |                return Integer.parseInt(str) / (float) 100;  | 
  |  169 |     | 
           } else { | 
  |  170 |    0 |                return 0.95f;  | 
  |  171 |     | 
           }  | 
  |  172 |     | 
       }  | 
  |  173 |     | 
     | 
  |  174 |     | 
       protected String getFolderId(final PartFolderComponent folder) { | 
  |  175 |    0 |            if (!StringUtil.isEmpty(folder.id)) { | 
  |  176 |    0 |                return folder.id;  | 
  |  177 |     | 
           } else { | 
  |  178 |    0 |                return "folder" + folderNum++;  | 
  |  179 |     | 
           }  | 
  |  180 |     | 
       }  | 
  |  181 |     | 
     | 
  |  182 |     | 
       protected PerspectiveComponent findPerspective(  | 
  |  183 |     | 
               final List<PerspectiveComponent> perspectives,  | 
  |  184 |     | 
               final String perspectiveId) { | 
  |  185 |    0 |            String localId = service.getLocalId(perspectiveId);  | 
  |  186 |    0 |            for (PerspectiveComponent perspective : perspectives) { | 
  |  187 |    0 |                if (localId.equals(perspective.id)) { | 
  |  188 |    0 |                    return perspective;  | 
  |  189 |     | 
               }  | 
  |  190 |     | 
           }  | 
  |  191 |    0 |            return null;  | 
  |  192 |     | 
       }  | 
  |  193 |     | 
     | 
  |  194 |     | 
       protected boolean findViewPart(final String viewId) { | 
  |  195 |    0 |            List<Template> templates = templateManager  | 
  |  196 |     | 
                   .getTemplates(ViewPartComponent.class);  | 
  |  197 |    0 |            String localViewId = ViewPartUtil.getPrimaryId(service  | 
  |  198 |     | 
                   .getLocalId(viewId));  | 
  |  199 |     | 
     | 
  |  200 |    0 |            for (Template template : templates) { | 
  |  201 |    0 |                ViewPartComponent viewPart = (ViewPartComponent) template  | 
  |  202 |     | 
                       .getRootComponent();  | 
  |  203 |    0 |                if (localViewId.equals(viewPart.getId())) { | 
  |  204 |    0 |                    return true;  | 
  |  205 |     | 
               }  | 
  |  206 |    0 |            }  | 
  |  207 |     | 
     | 
  |  208 |    0 |            return false;  | 
  |  209 |     | 
       }  | 
  |  210 |     | 
   }  |