/** The mouse was dragged - either draw a marquee or move some classes. */
  public void mouseDragged(MouseEvent evt) {
    if (isButtonOne(evt)) {
      if (marquee.isActive()) {
        Rectangle oldRect = marquee.getRectangle();
        marquee.move(evt.getX(), evt.getY());
        Rectangle newRect = (Rectangle) marquee.getRectangle().clone();
        if (oldRect != null) {
          newRect.add(oldRect);
        }
        newRect.width++;
        newRect.height++;
        graphEditor.repaint(newRect);
      } else if (rubberBand != null) {
        rubberBand.setEnd(evt.getX(), evt.getY());
        graphEditor.repaint();
      } else {
        if (!selection.isEmpty()) {
          int deltaX = snapToGrid(evt.getX() - dragStartX);
          int deltaY = snapToGrid(evt.getY() - dragStartY);

          if (resizing) {
            selection.resize(deltaX, deltaY);
          } else if (moving) {
            selection.move(deltaX, deltaY);
          }
        }
        graphEditor.repaint();
      }
    }
  }
 /** A key was released. Check whether a key-based move or resize operation has ended. */
 public void keyReleased(KeyEvent evt) {
   if (moving && (!evt.isShiftDown())) { // key-based moving stopped
     selection.moveStopped();
     moving = false;
   } else if (resizing && (!evt.isControlDown())) { // key-based moving stopped
     selection.moveStopped();
     resizing = false;
   }
   graphEditor.repaint();
 }
  /** The mouse was released. */
  public void mouseReleased(MouseEvent evt) {
    if (isDrawingDependency()) {
      SelectableGraphElement selectedElement = graph.findGraphElement(evt.getX(), evt.getY());
      notifyPackage(selectedElement);
      graphEditor.repaint();
    }
    rubberBand = null;

    SelectionSet newSelection = marquee.stop(); // may or may not have had a marquee...
    if (newSelection != null) {
      selection.addAll(newSelection);
      graphEditor.repaint();
    }

    if (moving || resizing) {
      endMove();
      graphEditor.revalidate();
      graphEditor.repaint();
    }
  }
  /** A key was pressed in the graph editor. */
  public void keyPressed(KeyEvent evt) {
    boolean handled = true; // assume for a start that we are handling the
    // key here

    if (isArrowKey(evt)) {
      if (evt.isControlDown()) { // resizing
        if (!resizing) startKeyboardResize();
        setKeyDelta(evt);
        selection.resize(keyDeltaX, keyDeltaY);
      } else if (evt.isShiftDown()) { // moving targets
        if (!moving) startKeyboardMove();
        setKeyDelta(evt);
        selection.move(keyDeltaX, keyDeltaY);
      } else { // navigate the diagram
        navigate(evt);
      }
    } else if (isPlusOrMinusKey(evt)) {
      resizeWithFixedRatio(evt);
    }

    // dependency selection
    else if (evt.getKeyCode() == KeyEvent.VK_PAGE_UP || evt.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
      selectDependency(evt);
    }

    // post context menu
    else if (evt.getKeyCode() == KeyEvent.VK_SPACE || evt.getKeyCode() == KeyEvent.VK_ENTER) {
      postMenu();
    }

    // 'A' (with any or no modifiers) selects all
    else if (evt.getKeyCode() == KeyEvent.VK_A) {
      selectAll();
    }

    // Escape removes selections
    else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
      if (moving || resizing) {
        endMove();
      }
      clearSelection();
    } else {
      handled = false;
    }

    if (handled) evt.consume();

    graphEditor.repaint();
  }
  /** A mouse-pressed event. Analyse what we should do with it. */
  public void mousePressed(MouseEvent evt) {
    graphEditor.requestFocus();
    int clickX = evt.getX();
    int clickY = evt.getY();

    SelectableGraphElement clickedElement = graph.findGraphElement(clickX, clickY);
    notifyPackage(clickedElement);

    if (clickedElement == null) { // nothing hit
      if (!isMultiselectionKeyDown(evt)) {
        selection.clear();
      }
      if (isButtonOne(evt)) marquee.start(clickX, clickY);
    } else if (isButtonOne(evt)) { // clicked on something
      if (isMultiselectionKeyDown(evt)) {
        // a class was clicked, while multiselectionKey was down.
        if (clickedElement.isSelected()) {
          selection.remove(clickedElement);
        } else {
          selection.add(clickedElement);
        }
      } else {
        // a class was clicked without multiselection
        if (!clickedElement.isSelected()) {
          selection.selectOnly(clickedElement);
        }
      }

      if (isDrawingDependency()) {
        if (clickedElement instanceof Target)
          rubberBand = new RubberBand(clickX, clickY, clickX, clickY);
      } else {
        dragStartX = clickX;
        dragStartY = clickY;

        if (clickedElement.isHandle(clickX, clickY)) {
          resizing = true;
        } else {
          moving = true;
        }
      }
    }
  }
 // move into editor?
 private ContainerProxy findComponentsAndParent(List<ComponentProxy> components) {
   Node[] nodes = em.getSelectedNodes();
   ContainerProxy parent = null;
   for (Node node : nodes) {
     ComponentProxy cmp = node.getLookup().lookup(ComponentProxy.class);
     if (cmp != null) {
       ContainerProxy p = cmp.getParent();
       if (p == null || cmp == editor.getContainer()) {
         parent = null;
         break;
       }
       if (parent == null) {
         parent = p;
       } else if (parent != p) {
         parent = null;
         break;
       }
       if (components != null) {
         components.add(cmp);
       }
     }
   }
   return parent;
 }
 @Override
 public void actionPerformed(ActionEvent e) {
   LOG.finest("Copy action invoked");
   assert EventQueue.isDispatchThread();
   List<ComponentProxy> cmps = new ArrayList<>();
   ContainerProxy container = findComponentsAndParent(cmps);
   if (container == null) {
     LOG.finest("No container found");
     return;
   }
   Point offset = Utils.findOffset(cmps);
   LOG.log(Level.FINEST, "Found offset : {0}", offset);
   Set<String> childIDs =
       cmps.stream().map(cmp -> cmp.getAddress().getID()).collect(Collectors.toSet());
   Task copyTask =
       editor
           .getActionSupport()
           .createCopyTask(
               container,
               childIDs,
               () -> Utils.offsetComponents(cmps, offset, false),
               () -> Utils.offsetComponents(cmps, offset, true));
   copyTask.execute();
 }
Example #8
0
  public EditorToolBar(final GraphEditor editor, int orientation) {
    super(orientation);
    this.editor = editor;
    setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(3, 3, 3, 3), getBorder()));
    setFloatable(false);

    actionSave = editor.bind(null, new ActionSave(), "/img/save.gif");
    JButton buttonSave = addButton(actionSave);
    addSeparator();

    actionUndo = editor.bind(null, new ActionUndo(true), "/img/undo.gif");
    addButton(actionUndo);
    actionRedo = editor.bind(null, new ActionUndo(false), "/img/redo.gif");
    addButton(actionRedo);
    addSeparator();

    actionSort = editor.bind(null, new ActionSort(), "/img/pan.gif");
    addButton(actionSort);
    addSeparator();

    actionPrev = editor.bind(null, new ActionPre(), "/img/pre.gif");
    addButton(actionPrev);
    actionNext = editor.bind(null, new ActionNext(), "/img/next.gif");
    addButton(actionNext);
    textFieldGraphNumber.setPreferredSize(new Dimension(200, buttonSave.getPreferredSize().height));
    textFieldGraphNumber.setMaximumSize(new Dimension(200, buttonSave.getPreferredSize().height));
    add(textFieldGraphNumber);
    textFieldGraphNumber.addFocusListener(
        new FocusListener() {

          @Override
          public void focusLost(FocusEvent e) {
            String temp = textFieldGraphNumber.getText();
            if (!Util.isNum(temp)) {
              JOptionPane.showMessageDialog(
                  editor,
                  Languages.getInstance().getString("Message.EnterValidSentenceNumber.Body"),
                  Languages.getInstance().getString("Message.Title.Warning"),
                  JOptionPane.WARNING_MESSAGE);
              textFieldGraphNumber.setText("");
            }
          }

          @Override
          public void focusGained(FocusEvent e) {
            textFieldGraphNumber.setText("");
          }
        });
    actionGo = editor.bind(null, new ActionGo(), "/img/go.png");
    addButton(actionGo);
    addSeparator();

    int nowCount = editor.getTabbedPane().getCurrentPane().count;
    add(description);
    addSeparator();

    JButton zoomInButton = addButton(editor.bind("+", mxGraphActions.getZoomInAction()));
    zoomInButton.setPreferredSize(new Dimension(40, buttonSave.getPreferredSize().height));
    JButton zoomOutButton = addButton(editor.bind("-", mxGraphActions.getZoomOutAction()));
    zoomOutButton.setPreferredSize(new Dimension(40, buttonSave.getPreferredSize().height));
    JButton revertZoomButton = addButton(editor.bind("=", mxGraphActions.getZoomActualAction()));
    revertZoomButton.setPreferredSize(new Dimension(40, buttonSave.getPreferredSize().height));

    add(Box.createHorizontalGlue());

    actionRebuild = editor.bind(null, new ActionRebuild());
    addButton(actionRebuild);

    comboBoxViewMode.addItem(""); // ShowOnlySentence
    comboBoxViewMode.addItem(""); // DisplayWords
    comboBoxViewMode.addItem(""); // DisplayWordsAndPOS
    comboBoxViewMode.addItem(""); // DisplayWordsAndConstraints
    comboBoxViewMode.setSelectedIndex(0);
    comboBoxViewMode.addItemListener(
        new ItemListener() {

          @Override
          public void itemStateChanged(ItemEvent e) {
            editor.setViewMode(comboBoxViewMode.getSelectedIndex());
            editor.updateBottomTextArea();
          }
        });
    comboBoxViewMode.setPreferredSize(new Dimension(200, buttonSave.getPreferredSize().height));
    comboBoxViewMode.setMaximumSize(new Dimension(200, buttonSave.getPreferredSize().height));
    add(comboBoxViewMode);
    addSeparator();
    actionRefresh = editor.bind(null, new ActionRefresh(), "/img/refresh.jpg");
    addButton(actionRefresh);

    Languages.getInstance()
        .addItemListener(
            new ItemListener() {
              public void itemStateChanged(ItemEvent evt) {
                Locale locale = (Locale) evt.getItem();
                setLocale(locale);
              }
            });
  }
 /**
  * Create the controller for a given graph editor.
  *
  * @param graphEditor
  * @param graph
  */
 public SelectionController(GraphEditor graphEditor) {
   this.graphEditor = graphEditor;
   this.graph = graphEditor.getGraph();
   marquee = new Marquee(graph);
   selection = new SelectionSet();
 }
 /**
  * Post the context menu on the diagram at the specified location
  *
  * @param x
  * @param y
  */
 private void postMenu(int x, int y) {
   graphEditor.popupMenu(x, y);
 }