public Object execute(ExecutionEvent event) throws ExecutionException {
    Element selectedElement = getSelection();
    ServicesRegistry registry = null;
    try {
      registry = ServiceUtilsForHandlers.getInstance().getServiceRegistry(event);
    } catch (ServiceException e1) {
      e1.printStackTrace();
    }
    ModelSet modelSet;
    try {
      modelSet = registry.getService(ModelSet.class);
    } catch (ServiceException e) {
      throw new ExecutionException("Can't get ModelSet", e);
    }
    FeatureArchitectureWizard bWizard = new FeatureArchitectureWizard(false);
    WizardDialog wizardDialog = new WizardDialog(new Shell(), bWizard);
    if (wizardDialog.open() == Window.OK) {
      TransactionalEditingDomain dom = modelSet.getTransactionalEditingDomain();
      if (selectedElement instanceof Package) {
        CompleteFeaturesArchitectureSnapshotCommand comd =
            new CompleteFeaturesArchitectureSnapshotCommand(
                dom, (Package) selectedElement, bWizard.getSelectedBundle());
        dom.getCommandStack().execute(comd);
      }
    }

    return null;
  }
예제 #2
0
  public Object execute(ExecutionEvent event) {
    ISelection selection;
    try {
      selection =
          ServiceUtilsForHandlers.getInstance()
              .getNestedActiveIEditorPart(event)
              .getSite()
              .getSelectionProvider()
              .getSelection();
      if (selection.isEmpty()) {
        return null;
      }
    } catch (ServiceException ex) {
      Activator.log.error(ex);
      return null;
    }

    if (!(selection instanceof IStructuredSelection)) {
      return null;
    }

    IStructuredSelection sSelection = (IStructuredSelection) selection;
    Object element = sSelection.getFirstElement();

    View view = NotationHelper.findView(element);
    if (view == null) {
      Activator.log.warn(
          "Cannot create a Style from the selected element ; the element is not a View");
      return null;
    }

    Shell parentShell = ((Event) event.getTrigger()).widget.getDisplay().getActiveShell();

    if (view.getElement() == null || view instanceof Diagram) {
      MessageDialog.open(
          MessageDialog.WARNING,
          parentShell,
          "Style error",
          "The selected element's style cannot be exported",
          SWT.NONE);
      return null;
    }

    Map<Declaration, Boolean> declarations = handleStyles(view);
    Map<Attribute, Boolean> conditions = handleSemantic(view);

    String selectorName = view.getElement().eClass().getName();

    AbstractStyleDialog dialog =
        createStyleDialog(parentShell, declarations, conditions, selectorName, view);

    if (dialog.open() != Window.OK) {
      return null;
    }

    Ruleset ruleset = getRuleset(dialog);
    SimpleSelector selector = CssFactory.eINSTANCE.createSimpleSelector();

    if (dialog.useSelectorName()) {
      selector.setElementName(selectorName);
    } else {
      selector.setElementName("*"); // $NON-NLS-1$
    }

    if (dialog.getDiagramRestriction()) {
      String diagramType = getDiagramType(view.getDiagram());
      CompositeSelector compositeSelector = CssFactory.eINSTANCE.createCompositeSelector();
      compositeSelector.setRight(selector);

      SimpleSelector diagramSelector = CssFactory.eINSTANCE.createSimpleSelector();
      diagramSelector.setElementName(diagramType);
      compositeSelector.setLeft(diagramSelector);

      ruleset.getSelectors().add(compositeSelector);
    } else {
      ruleset.getSelectors().add(selector);
    }

    if (dialog.getCSSClass() != null) {
      String cssClass = dialog.getCSSClass();
      org.eclipse.papyrus.infra.gmfdiag.css.Class classCondition =
          CssFactory.eINSTANCE.createClass();
      classCondition.setClass(cssClass);
      selector.getCondition().add(classCondition);
    }

    for (SelectorCondition condition : conditions.keySet()) {
      if (conditions.get(condition)) {
        selector.getCondition().add(condition);
      }
    }

    for (Declaration declaration : declarations.keySet()) {
      if (declarations.get(declaration)) {
        ruleset.getProperties().add(declaration);
      }
    }

    Stylesheet xtextStylesheet = getStyleSheet(dialog, view);

    if (xtextStylesheet == null) {
      return null;
    }

    Resource resource = xtextStylesheet.eResource();

    if (!xtextStylesheet.getContents().contains(ruleset)) {
      xtextStylesheet.getContents().add(ruleset);
    }

    try {
      resource.save(new HashMap<Object, Object>());
      BaseCSSEngine.INSTANCE.reset();
      DiagramHelper.setNeedsRefresh();
      DiagramHelper.refreshDiagrams();
    } catch (IOException ex) {
      Activator.log.error(ex);
      MessageDialog.open(
          MessageDialog.ERROR,
          parentShell,
          "Style error",
          "An unexpected error occured while trying to save the Stylesheet",
          SWT.NONE);
    } catch (Exception ex) {
      Activator.log.error(ex);
      MessageDialog.open(
          MessageDialog.ERROR,
          parentShell,
          "Style error",
          "An unexpected error occured while trying to save the Stylesheet",
          SWT.NONE);
    }

    return null;
  }