示例#1
0
 /**
  * Adds an undone NullOperation (i.e. has no effects on executing, undoing or redoing) to the
  * Eclipse IOperationHistory to be sure that redoing is possible. This is necessary for the UI
  * Redo possibility being activated.
  */
 protected void simulateUndo() {
   IUndoableOperation auxOp = new NullOperation();
   try {
     auxOp.addContext(context);
     eclipseHistory.execute(auxOp, null, null);
     eclipseHistory.undo(context, null, null);
     if (!eclipseHistory.canRedo(context)) log.error("Simulating Undo failed");
   } catch (ExecutionException e) {
     log.error("Simulating Undo failed");
   }
 }
  /** @see IActionDelegate#run(IAction) */
  public void run(IAction action) {
    try {
      final Element selectedElement = getSelectedElement();

      IUndoableOperation operation =
          new AbstractEMFOperation(editingDomain, "Create Nested Subclass") {
            @Override
            protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) {
              Property property = null;

              if (selectedElement instanceof Association) {
                Association association = (Association) selectedElement;
                property = UMLUtil.getNavigableEnd(association);
              } else if (selectedElement instanceof Property) {
                property = (Property) selectedElement;
              }

              if (property == null || !(property.getType() instanceof Class)) {
                return Status.CANCEL_STATUS;
              }

              Class baseClass = (Class) property.getType();

              // prompt for new class name
              String className = null;
              InputDialog inputDialog =
                  new InputDialog(
                      activePart.getSite().getShell(),
                      "Nested Class",
                      "Enter class name",
                      baseClass.getName(),
                      null);
              if (inputDialog.open() == Window.OK) {
                className = inputDialog.getValue();
              }
              if (className == null || className.length() == 0) {
                return Status.CANCEL_STATUS;
              }

              Class newClass = UMLFactory.eINSTANCE.createClass();
              property.getClass_().getNestedClassifiers().add(newClass);
              newClass.setName(className);
              property.setType(newClass);

              // create new generalization
              newClass.createGeneralization(baseClass);

              // prompt for selection of redefined properties
              // TODO customize dialog title and prompt
              SubclassHandler subclassHandler =
                  new SubclassHandler(activePart.getSite().getShell(), newClass);
              subclassHandler.openSubclassDialog();

              // TODO this does not select in CommonNavigator. maybe need a refresh first?
              if (activePart instanceof ISetSelectionTarget) {
                ((ISetSelectionTarget) activePart).selectReveal(new StructuredSelection(newClass));
              }

              return Status.OK_STATUS;
            }
          };

      try {
        IWorkspaceCommandStack commandStack =
            (IWorkspaceCommandStack) editingDomain.getCommandStack();
        operation.addContext(commandStack.getDefaultUndoContext());
        commandStack.getOperationHistory().execute(operation, new NullProgressMonitor(), null);

      } catch (ExecutionException ee) {
        Logger.logException(ee);
      }

    } catch (Exception e) {
      throw new RuntimeException(e.getCause());
    }
  }