@Override
  public MPartDescriptor getPartDescriptor(String id) {
    MApplication application = appContext.get(MApplication.class);

    // If the id contains a ':' use the part before it as the descriptor id
    int colonIndex = id == null ? -1 : id.indexOf(':');
    String descId = colonIndex == -1 ? id : id.substring(0, colonIndex);

    for (MPartDescriptor descriptor : application.getDescriptors()) {
      if (descriptor.getElementId().equals(descId)) {
        return descriptor;
      }
    }
    return null;
  }
Esempio n. 2
0
  public static void createParts(
      MApplication application, EModelService service, EPartService partService) {
    // Sometimes, when switching windows at startup, the active context
    // is null or doesn't have a window, and the part instantiation fails.
    // Ensure that a child context with a window is activated:
    IEclipseContext activeChild = application.getContext().getActiveChild();
    if (activeChild == null || activeChild.get(MTrimmedWindow.class) == null) {
      boolean activated = false;
      if (application.getContext() instanceof EclipseContext) {
        for (IEclipseContext child : ((EclipseContext) application.getContext()).getChildren()) {
          MTrimmedWindow window = child.get(MTrimmedWindow.class);
          if (window != null) {
            child.activate();
            activated = true;
            break;
          }
        }
      }
      if (!activated) {
        logger.error("Could not activate window for part instantiation"); // $NON-NLS-1$
        return;
      }
    }

    List<MPart> ontops = new ArrayList<MPart>();
    for (MPartDescriptor descriptor : application.getDescriptors()) {
      if (!(descriptor.getPersistedState().containsKey(VISIBLE_ID)
          && Boolean.toString(true)
              .equalsIgnoreCase(descriptor.getPersistedState().get(VISIBLE_ID)))) {
        continue;
      }
      List<MPart> existingParts =
          service.findElements(application, descriptor.getElementId(), MPart.class, null);
      if (!existingParts.isEmpty()) {
        // part is already instantiated
        continue;
      }
      MPart part = partService.createPart(descriptor.getElementId());
      if (part == null) {
        continue;
      }
      addPartToAppropriateContainer(part, descriptor, application, service);
      partService.activate(part);
      if (descriptor.getPersistedState().containsKey(ONTOP_ID)
          && Boolean.toString(true)
              .equalsIgnoreCase(descriptor.getPersistedState().get(ONTOP_ID))) {
        ontops.add(part);
      }
    }

    // reactivate ontop parts to ensure they are on-top
    for (MPart ontop : ontops) {
      partService.activate(ontop);
    }
  }
Esempio n. 3
0
 public static void addPartToAppropriateContainer(
     MPart part, MPartDescriptor descriptor, MApplication application, EModelService service) {
   // first try and find the container specified by the id in the "parent" persisted state
   MElementContainer<MUIElement> container = null;
   String parentId = descriptor.getPersistedState().get(PARENT_ID);
   if (parentId != null) {
     @SuppressWarnings("rawtypes")
     List<MElementContainer> containers =
         service.findElements(application, parentId, MElementContainer.class, null);
     if (!containers.isEmpty()) {
       @SuppressWarnings("unchecked")
       MElementContainer<MUIElement> uncheckedContainer = containers.get(0);
       container = uncheckedContainer;
     }
   }
   // next try and find a sibling, and use the sibling's container
   if (container == null) {
     List<MPart> siblings =
         service.findElements(application, descriptor.getElementId(), MPart.class, null);
     for (MPart sibling : siblings) {
       if (sibling == part) {
         continue;
       }
       container = sibling.getParent();
       if (sibling.isToBeRendered()) {
         // prefer visible siblings
         break;
       }
     }
   }
   if (container != null) {
     String position = descriptor.getPersistedState().get(POSITION_ID);
     int index = getPositionIndex(position, container);
     if (index < 0 || index > container.getChildren().size()) {
       container.getChildren().add(part);
     } else {
       container.getChildren().add(index, part);
     }
   }
 }
  /**
   * Adds a part to the current container if it isn't already in the container. The part may still
   * be added to the container if the part supports having multiple copies of itself in a given
   * container.
   *
   * @param providedPart the part to add
   * @param localPart a part that shares attributes with <code>providedPart</code>, for example, it
   *     may have been backed by the same part descriptor, this part may already be in the current
   *     container
   * @return a part that has been added to the current container, note that this may not necessarily
   *     be <code>providedPart</code>
   * @see MPartDescriptor#isAllowMultiple()
   */
  private MPart addPart(MPart providedPart, MPart localPart) {
    MPartDescriptor descriptor = modelService.getPartDescriptor(providedPart.getElementId());
    if (descriptor == null) {
      // there is no part descriptor backing the provided part, just add it to the container
      // if it's not already there
      if (!isInContainer(providedPart)) {
        adjustPlaceholder(providedPart);
        addToLastContainer(null, providedPart);
      }
    } else {
      if (providedPart != localPart && !descriptor.isAllowMultiple()) {
        // multiple copies of this part are not allowed, just return the local one
        return localPart;
      }

      // already in the container, return as is
      if (isInContainer(providedPart)) {
        return providedPart;
      }

      // corrects this part's placeholder if necessary
      adjustPlaceholder(providedPart);

      String category = descriptor.getCategory();
      if (category == null) {
        // no category, just add it to the end
        addToLastContainer(null, providedPart);
      } else {
        if ("org.eclipse.e4.primaryDataStack".equals(category)) { // $NON-NLS-1$
          MElementContainer<? extends MUIElement> container = getContainer();
          MUIElement area = modelService.find("org.eclipse.ui.editorss", container); // $NON-NLS-1$

          MPartStack activeStack = null;
          if (area instanceof MPlaceholder && ((MPlaceholder) area).getRef() instanceof MArea) {
            // Find the currently 'active' stack in the area
            MArea a = (MArea) ((MPlaceholder) area).getRef();
            MUIElement curActive = a.getSelectedElement();
            while (curActive instanceof MElementContainer<?>) {
              if (curActive instanceof MPartStack) {
                activeStack = (MPartStack) curActive;
                break;
              }
              MElementContainer<?> curContainer = (MElementContainer<?>) curActive;
              curActive = curContainer.getSelectedElement();
            }
          }

          if (activeStack != null) {
            activeStack.getChildren().add(providedPart);
          } else {
            // Find the first visible stack in the area
            List<MPartStack> sharedStacks =
                modelService.findElements(area, null, MPartStack.class, null);
            if (sharedStacks.size() > 0) {
              for (MPartStack stack : sharedStacks) {
                if (stack.isToBeRendered()) {
                  stack.getChildren().add(providedPart);
                  break;
                }
              }
            } else {
              addToLastContainer(null, providedPart);
            }
          }
        } else {
          @SuppressWarnings("rawtypes")
          List<MElementContainer> containers =
              modelService.findElements(
                  getContainer(),
                  null,
                  MElementContainer.class,
                  Collections.singletonList(category),
                  EModelService.PRESENTATION);
          if (containers.isEmpty()) {
            // couldn't find any containers with the specified tag, just add it to the
            // end
            addToLastContainer(category, providedPart);
          } else {
            // add the part to the container
            MElementContainer<MPartSashContainerElement> container = containers.get(0);
            MPlaceholder placeholder = providedPart.getCurSharedRef();
            if (placeholder == null) {
              container.getChildren().add(providedPart);
            } else {
              container.getChildren().add(placeholder);
            }
          }
        }
      }
    }
    return providedPart;
  }
 private MPart createPart(MPartDescriptor descriptor) {
   if (descriptor == null) {
     return null;
   }
   MPart part = modelService.createModelElement(MPart.class);
   part.setElementId(descriptor.getElementId());
   part.getMenus().addAll(EcoreUtil.copyAll(descriptor.getMenus()));
   if (descriptor.getToolbar() != null) {
     part.setToolbar((MToolBar) EcoreUtil.copy((EObject) descriptor.getToolbar()));
   }
   part.setContributorURI(descriptor.getContributorURI());
   part.setCloseable(descriptor.isCloseable());
   part.setContributionURI(descriptor.getContributionURI());
   part.setLabel(descriptor.getLabel());
   part.setIconURI(descriptor.getIconURI());
   part.setTooltip(descriptor.getTooltip());
   part.getHandlers().addAll(EcoreUtil.copyAll(descriptor.getHandlers()));
   part.getTags().addAll(descriptor.getTags());
   part.getPersistedState().putAll(descriptor.getPersistedState());
   part.getBindingContexts().addAll(descriptor.getBindingContexts());
   return part;
 }
Esempio n. 6
0
 protected void doOkPressed(MPartDescriptor[] selection) {
   for (MPartDescriptor d : selection) {
     PartUtils.openPart(d.getElementId(), true);
   }
 }