/*
  * Drop Event Handlers
  */
 private TreeNode getNodeForEvent(DropTargetDragEvent dtde) {
   Point p = dtde.getLocation();
   DropTargetContext dtc = dtde.getDropTargetContext();
   JTree tree = (JTree) dtc.getComponent();
   TreePath path = tree.getClosestPathForLocation(p.x, p.y);
   return (TreeNode) path.getLastPathComponent();
 }
  public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) parentpath.getLastPathComponent();

    try {
      // Transferable tr = dtde.getTransferable(); // !!! this should work, but doesn't. after one
      // click on the draggable node, this call here will not return the TransferableTreeNode that
      // is created in dragGestureRecognized()
      Transferable tr =
          ObjectInspector
              .transferNode; // therefore, we use this hack, which just stores the current
      // transferable in a static variable

      DataFlavor[] flavors = tr.getTransferDataFlavors();
      for (int i = 0; i < flavors.length; i++) {
        if (tr.isDataFlavorSupported(flavors[i])) {
          dtde.acceptDrop(dtde.getDropAction());
          TreePath p = (TreePath) tr.getTransferData(flavors[i]);
          DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent();
          DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

          Object sourceObject = node.getUserObject();

          FieldWrapperObjectTree targetField = ((FieldWrapperObjectTree) parent.getUserObject());

          if (sourceObject != null) {
            if (sourceObject instanceof FieldWrapperObjectTree) {
              FieldWrapperObjectTree sourceField = (FieldWrapperObjectTree) sourceObject;

              if (sourceField.getTheField().getType().equals(targetField.getTheField().getType())) {
                targetField
                    .getTheField()
                    .set(
                        targetField.getOnObject(),
                        sourceField.getTheField().get(sourceField.getOnObject()));
              }
            } else if (sourceObject.getClass().equals(targetField.getTheField().getType())) {
              targetField.getTheField().set(targetField.getOnObject(), sourceObject);
            }

            tree.invalidate();
            tree.repaint();
          }

          dtde.dropComplete(true);
          return;
        }
      }
      dtde.rejectDrop();
    } catch (Exception e) {
      e.printStackTrace();
      // dtde.rejectDrop();
    }
  }
Ejemplo n.º 3
0
  @Override
  public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) parentpath.getLastPathComponent();
    if (!parent.isLeaf()) {
      dtde.rejectDrop();
      return;
    }

    try {
      Transferable tr = dtde.getTransferable();
      DataFlavor[] flavors = tr.getTransferDataFlavors();
      for (int i = 0; i < flavors.length; i++) {
        if (tr.isDataFlavorSupported(flavors[i])) {
          dtde.acceptDrop(dtde.getDropAction());
          EMedium eMedium = (EMedium) tr.getTransferData(flavors[i]);
          TreeNodeUserData userData = (TreeNodeUserData) parent.getUserObject();
          String shelfName = userData.getCaption();
          if (isLibrarySelected(dtde)
              && shelfName.equals(AppProperties.INSTANCE.RENTALS_SHELF_NAME)) {
            if (!uiDelegate.addRentalEMedium(eMedium))
              JOptionPane.showMessageDialog(
                  frame, "Cannot rent e-medium!", "Add document error", JOptionPane.ERROR_MESSAGE);
          } else if (!isLibrarySelected(dtde)
              && !shelfName.equals(AppProperties.INSTANCE.RENTALS_SHELF_NAME)) {
            if (!uiDelegate.addEMediumShelf(shelfName, eMedium))
              JOptionPane.showMessageDialog(
                  frame, "Duplicated e-medium!", "Add document error", JOptionPane.ERROR_MESSAGE);
          } else
            JOptionPane.showMessageDialog(
                frame,
                "Need to rent the e-medium first!",
                "Add document error",
                JOptionPane.ERROR_MESSAGE);
          dtde.dropComplete(true);
          return;
        }
      }
      // dtde.rejectDrop();
    } catch (OperationNotSupportedException e) {
      JOptionPane.showMessageDialog(
          frame,
          "Cannot add a document to this shelf",
          "Add document error",
          JOptionPane.ERROR_MESSAGE);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Override
  public void drop(DropTargetDropEvent dtde) {
    if (dtde.isDataFlavorSupported(CardFlavor.cardFlavor) && this.canDrop) {
      Transferable trans = dtde.getTransferable();
      try {
        Object data = trans.getTransferData(CardFlavor.cardFlavor);
        CardWindow card = (CardWindow) data;
        DropTargetContext context = dtde.getDropTargetContext();
        CardCollection comp = (CardCollection) context.getComponent();
        Container parent = comp.getParent();
        GameController.getController(false)
            .makeMove(
                CardCollection.getMoveFromIndex(
                    comp.getCardMouseLocation(), new OrderedCard(card.card, card.cardLocation)));
        if (parent != null) parent.remove(card);
        comp.setCard(card, comp.getCardMouseLocation());
        dtde.acceptDrop(DnDConstants.ACTION_MOVE);
        comp.validate();
        comp.repaint();

        dtde.dropComplete(true);
      } catch (Exception e) {
        dtde.rejectDrop();
        dtde.dropComplete(false);
        JOptionPane.showMessageDialog(
            MainWindow.getMainWindow(),
            "Invalid move.",
            "Invalid Move Error",
            JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
      }
    } else {
      dtde.rejectDrop();
      dtde.dropComplete(false);
      JOptionPane.showMessageDialog(
          MainWindow.getMainWindow(),
          "Invalid move.",
          "Invalid Move Error",
          JOptionPane.ERROR_MESSAGE);
    }
  }
Ejemplo n.º 5
0
  @Override
  public void dragEnter(DropTargetDragEvent dtde) {
    if (!dtde.isDataFlavorSupported(TransferableUDO.UDO_FLAVOR)
        && !dtde.isDataFlavorSupported(TransferableUDO.UDO_CAT_FLAVOR)) {
      dtde.rejectDrag();
      return;
    }

    Point p = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath path = tree.getClosestPathForLocation(p.x, p.y);

    if (path.getLastPathComponent() instanceof UDOCategory) {
      dtde.acceptDrag(dtde.getDropAction());
    } else if (dtde.isDataFlavorSupported(TransferableUDO.UDO_FLAVOR)) {
      dtde.acceptDrag(dtde.getDropAction());
    } else {
      dtde.rejectDrag();
    }
  }
Ejemplo n.º 6
0
  /**
   * Construct a <code>DropTargetEvent</code> with a specified <code>DropTargetContext</code>.
   *
   * <p>
   *
   * @param dtc the <code>DropTargetContext</code>
   */
  public DropTargetEvent(DropTargetContext dtc) {
    super(dtc.getDropTarget());

    context = dtc;
  }
Ejemplo n.º 7
0
  @Override
  public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    Object node = parentpath.getLastPathComponent();

    if (dtde.isDataFlavorSupported(TransferableUDO.UDO_CAT_FLAVOR)) {
      if (!(node instanceof UDOCategory)) {
        dtde.rejectDrop();
        return;
      }

      if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
        dtde.acceptDrop(dtde.getDropAction());

        Transferable tr = dtde.getTransferable();
        try {
          Object transferNode = tr.getTransferData(TransferableUDO.UDO_CAT_FLAVOR);

          UDOCategory udoCategory = (UDOCategory) transferNode;
          UDOCategory parentNode = (UDOCategory) node;

          UDOLibrary udoLibrary = (UDOLibrary) tree.getModel();

          udoLibrary.addCategory(parentNode, udoCategory);

          dtde.dropComplete(true);
        } catch (UnsupportedFlavorException | IOException e) {
          dtde.dropComplete(false);
        }
      } else {
        dtde.rejectDrop();
      }

    } else if (dtde.isDataFlavorSupported(TransferableUDO.UDO_FLAVOR)) {
      dtde.acceptDrop(dtde.getDropAction());

      try {
        Transferable tr = dtde.getTransferable();

        Object transferNode = tr.getTransferData(TransferableUDO.UDO_FLAVOR);

        UserDefinedOpcode udo = (UserDefinedOpcode) transferNode;
        UDOLibrary udoLibrary = (UDOLibrary) tree.getModel();

        // iLibrary.removeInstrument(instrument);
        if (node instanceof UDOCategory) {
          UDOCategory parentNode = (UDOCategory) node;

          udoLibrary.addUDO(parentNode, udo);
        } else if (node instanceof UserDefinedOpcode) {
          UDOCategory parentNode =
              (UDOCategory) parentpath.getPathComponent(parentpath.getPathCount() - 2);

          int index = ListUtil.indexOfByRef(parentNode.getUserDefinedOpcodes(), node);

          int closestRow = tree.getClosestRowForLocation(pt.x, pt.y);

          Rectangle bounds = tree.getRowBounds(closestRow);

          if (pt.y > bounds.y + bounds.height) {
            udoLibrary.addUDO(parentNode, udo);
          } else {
            udoLibrary.addUDO(parentNode, index, udo);
          }
        }

        dtde.dropComplete(true);
      } catch (UnsupportedFlavorException | IOException e) {
        dtde.dropComplete(false);
      }
    } else {
      dtde.rejectDrop();
    }
  }