@SuppressWarnings("rawtypes")
  public void drop(DropTargetDropEvent dtde) {
    try {
      Transferable tr = dtde.getTransferable();
      if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        List files = (List) tr.getTransferData(DataFlavor.javaFileListFlavor);

        this.txtRawData.setText(((File) files.get(0)).getAbsolutePath());

        dtde.getDropTargetContext().dropComplete(true);
      } else {
        System.err.println("DataFlavor.javaFileListFlavor is not supported, rejected");
        dtde.rejectDrop();
      }
    } catch (IOException ex) {
      System.err.println("IOException");
      ex.printStackTrace();
      dtde.rejectDrop();
    } catch (UnsupportedFlavorException ex) {
      System.err.println("UnsupportedFlavorException");
      ex.printStackTrace();
      dtde.rejectDrop();
    }
  }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  @Override
  public void drop(DropTargetDropEvent dtde) {
    for (DataFlavor dataFlover : dtde.getCurrentDataFlavors()) {
      if (dataFlover.isFlavorJavaFileListType()) {
        try {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

          for (File file :
              (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor)) {
            if (file.isFile() && file.canRead()) {
              fileText.setText(file.getCanonicalPath());
              break;
            }
          }

          dtde.getDropTargetContext().dropComplete(true);
        } catch (UnsupportedFlavorException e1) {
          //
        } catch (IOException e2) {
          //
        }
      }
    }
  }
  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();
    }
  }
Exemplo n.º 4
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();
    }
  }
  public void drop(DropTargetDropEvent evt) {
    try {
      Transferable t = evt.getTransferable();

      if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

        String s = (String) t.getTransferData(DataFlavor.stringFlavor);
        evt.getDropTargetContext().dropComplete(true);
        process(s);
      } else {
        evt.rejectDrop();
      }
    } catch (IOException e) {
      evt.rejectDrop();
    } catch (UnsupportedFlavorException e) {
      evt.rejectDrop();
    }
  }
  @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);
    }
  }
Exemplo n.º 7
0
	/**
	 * 当拖动系统中的文件放置drop到该List中时,调用此方法。
	 */
	public synchronized void drop(DropTargetDropEvent dropTargetDropEvent) {
		System.out
				.println("method: drop(DropTargetDropEvent dropTargetDropEvent)");
		try {
			// 获取传入的Transfer对象
			Transferable tr = dropTargetDropEvent.getTransferable();
			// 如果Transfer对象支持文件放置到java平台,则进行下一步处理
			if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
				// 使用“拷贝、移动”方式接收放置操作。
				dropTargetDropEvent
						.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
				// 从Transfer中取数据,得到的是一个文件列表。即可以一次拖放多个文件
				java.util.List fileList = (java.util.List) tr
						.getTransferData(DataFlavor.javaFileListFlavor);
				Iterator iterator = fileList.iterator();
				while (iterator.hasNext()) {
					// 将拖放进来的文件的信息添加到List的数据模型中
					File file = (File) iterator.next();
					Hashtable hashtable = new Hashtable();
					hashtable.put("name", file.getName());
					hashtable.put("url", file.toURL().toString());
					hashtable.put("path", file.getAbsolutePath());
					((DefaultListModel) getModel()).addElement(hashtable);
				}
				// 放置操作成功结束
				dropTargetDropEvent.getDropTargetContext().dropComplete(true);
			} else {
				// Transferable对象不支持文件放置到java中,拒绝。
				System.err.println("Rejected");
				dropTargetDropEvent.rejectDrop();
			}
		} catch (IOException io) {
			io.printStackTrace();
			dropTargetDropEvent.rejectDrop();
		} catch (UnsupportedFlavorException ufe) {
			ufe.printStackTrace();
			dropTargetDropEvent.rejectDrop();
		}
	}
Exemplo n.º 8
0
  public void drop(DropTargetDropEvent dtde) {
    Transferable t = dtde.getTransferable();
    StringBuffer dropData = null;
    DataFlavor dtf;

    JConfig.log().logVerboseDebug("Dropping!");

    if (t.getTransferDataFlavors().length == 0) {
      Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
      Transferable t2 = sysClip.getContents(null);
      DataFlavor[] dfa2;
      int j;

      JConfig.log().logDebug("Dropped 0 data flavors, trying clipboard.");
      dfa2 = null;

      if (t2 != null) {
        JConfig.log().logVerboseDebug("t2 is not null: " + t2);
        dfa2 = t2.getTransferDataFlavors();
        JConfig.log().logVerboseDebug("Back from getTransferDataFlavors()!");
      } else {
        JConfig.log().logVerboseDebug("t2 is null!");
      }

      if (JConfig.queryConfiguration("debug.uber", "false").equals("true")) {
        if (dfa2 != null) {
          if (dfa2.length == 0) {
            JConfig.log().logVerboseDebug("Length is still zero!");
          }
          for (j = 0; j < dfa2.length; j++) {
            JConfig.log()
                .logVerboseDebug("Flavah " + j + " == " + dfa2[j].getHumanPresentableName());
            JConfig.log().logVerboseDebug("Flavah/mime " + j + " == " + dfa2[j].getMimeType());
          }
        } else {
          JConfig.log().logVerboseDebug("Flavahs supported: none!\n");
        }
      }
    }

    if (JConfig.queryConfiguration("debug.uber", "false").equals("true") && JConfig.debugging)
      dumpFlavorsOld(t);

    dtf = testAllFlavors(t);
    if (dtf != null) {
      JConfig.log().logVerboseDebug("Accepting!");
      acceptDrop(dtde);

      dropData = getTransferData(t);
      dtde.dropComplete(true);
      dtde.getDropTargetContext().dropComplete(true);
      if (dropData != null) {
        if (handler != null) {
          handler.receiveDropString(dropData);
        }
      }
    } else {
      JConfig.log().logVerboseDebug("Rejecting!");
      dtde.rejectDrop();
      handler.receiveDropString(dropData);
    }
  }
Exemplo n.º 9
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();
    }
  }
Exemplo n.º 10
0
  @SuppressWarnings("unchecked")
  public void drop(DropTargetDropEvent dtde) {
    Logger.debug("Drop detected...");
    Transferable t = dtde.getTransferable();
    boolean isAccepted = false;
    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      while (true) {
        Object o = null;
        try {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          o = t.getTransferData(DataFlavor.javaFileListFlavor);
          isAccepted = true;
        } catch (Exception e) {
          Logger.error("transfer failed");
        }
        // if o is still null we had an exception
        if (o instanceof List) {
          List<File> fileList = (List<File>) o;
          final int length = fileList.size();
          if (length == 1) {
            String fileName = fileList.get(0).getAbsolutePath().trim();
            if (fileName.endsWith(".bmp")) break; // try another flavor -- Mozilla bug
            dtde.getDropTargetContext().dropComplete(true);
            loadFile(fileName);
            return;
          }
          dtde.getDropTargetContext().dropComplete(true);
          loadFiles(fileList);
          return;
        }
        break;
      }
    }

    Logger.debug("browsing supported flavours to find something useful...");
    DataFlavor[] df = t.getTransferDataFlavors();

    if (df == null || df.length == 0) return;
    for (int i = 0; i < df.length; ++i) {
      DataFlavor flavor = df[i];
      Object o = null;
      if (true) {
        Logger.info("df " + i + " flavor " + flavor);
        Logger.info("  class: " + flavor.getRepresentationClass().getName());
        Logger.info("  mime : " + flavor.getMimeType());
      }

      if (flavor.getMimeType().startsWith("text/uri-list")
          && flavor.getRepresentationClass().getName().equals("java.lang.String")) {

        /*
         * This is one of the (many) flavors that KDE provides: df 2 flavour
         * java.awt.datatransfer.DataFlavor[mimetype=text/uri-list;
         * representationclass=java.lang.String] java.lang.String String: file
         * :/home/egonw/data/Projects/SourceForge/Jmol/Jmol-HEAD/samples/
         * cml/methanol2.cml
         *
         * A later KDE version gave me the following. Note the mime!! hence the
         * startsWith above
         *
         * df 3 flavor java.awt.datatransfer.DataFlavor[mimetype=text/uri-list
         * ;representationclass=java.lang.String] class: java.lang.String mime :
         * text/uri-list; class=java.lang.String; charset=Unicode
         */

        try {
          o = null;
          if (!isAccepted) dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          isAccepted = true;
          o = t.getTransferData(flavor);
        } catch (Exception e) {
          Logger.error(null, e);
        }

        if (o instanceof String) {
          if (Logger.debugging) {
            Logger.debug("  String: " + o.toString());
          }
          loadFile(o.toString());
          dtde.getDropTargetContext().dropComplete(true);
          return;
        }
      } else if (flavor
          .getMimeType()
          .equals("application/x-java-serialized-object; class=java.lang.String")) {

        /*
         * This is one of the flavors that jEdit provides:
         *
         * df 0 flavor java.awt.datatransfer.DataFlavor[mimetype=application/
         * x-java-serialized-object;representationclass=java.lang.String] class:
         * java.lang.String mime : application/x-java-serialized-object;
         * class=java.lang.String String: <molecule title="benzene.mol"
         * xmlns="http://www.xml-cml.org/schema/cml2/core"
         *
         * But KDE also provides:
         *
         * df 24 flavor java.awt.datatransfer.DataFlavor[mimetype=application
         * /x-java-serialized-object;representationclass=java.lang.String]
         * class: java.lang.String mime : application/x-java-serialized-object;
         * class=java.lang.String String: file:/home/egonw/Desktop/1PN8.pdb
         */

        try {
          o = null;
          if (!isAccepted) dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          isAccepted = true;
          o = t.getTransferData(df[i]);
        } catch (Exception e) {
          Logger.error(null, e);
        }

        if (o instanceof String) {
          String content = (String) o;
          if (Logger.debugging) {
            Logger.debug("  String: " + content);
          }
          if (content.startsWith("file:/")) {
            loadFile(content);
          } else {
            PropertyChangeEvent pce =
                new PropertyChangeEvent(this, FD_PROPERTY_INLINE, fd_oldFileName, content);
            fd_propSupport.firePropertyChange(pce);
          }
          dtde.getDropTargetContext().dropComplete(true);
          return;
        }
      }
    }
    if (!isAccepted) dtde.rejectDrop();
  }
  /**
   * The drag operation has terminated with a drop on this DropTarget. Be nice to somehow
   * incorporate FigureTransferCommand here.
   */
  public void drop(java.awt.dnd.DropTargetDropEvent dtde) {
    System.out.println("DropTargetDropEvent-drop");

    if (dtde.isDataFlavorSupported(DNDFiguresTransferable.DNDFiguresFlavor) == true) {
      log("DNDFiguresFlavor");
      if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
        log("copy or move");
        if (dtde.isLocalTransfer() == false) {
          System.err.println("Intra-JVM Transfers not implemented for figures yet.");
          dtde.rejectDrop();
          return;
        }
        dtde.acceptDrop(dtde.getDropAction());
        try {
            /* protection from a malicious dropped object */
          setTargetUndoActivity(createTargetUndoActivity(view()));
          DNDFigures ff =
              (DNDFigures)
                  DNDHelper.processReceivedData(
                      DNDFiguresTransferable.DNDFiguresFlavor, dtde.getTransferable());
          getTargetUndoActivity().setAffectedFigures(ff.getFigures());
          Point theO = ff.getOrigin();
          view().clearSelection();
          Point newP = dtde.getLocation();
          /**
           * origin is where the figure thinks it is now newP is where the mouse is now. we move the
           * figure to where the mouse is with this equation
           */
          int dx = newP.x - theO.x; /* distance the mouse has moved */
          int dy = newP.y - theO.y; /* distance the mouse has moved */
          log("mouse at " + newP);
          FigureEnumeration fe =
              view().insertFigures(getTargetUndoActivity().getAffectedFigures(), dx, dy, false);
          getTargetUndoActivity().setAffectedFigures(fe);

          if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
            view().addToSelectionAll(getTargetUndoActivity().getAffectedFigures());
          }

          view().checkDamage();
          editor().getUndoManager().pushUndo(getTargetUndoActivity());
          editor().getUndoManager().clearRedos();
          // update menus
          editor().figureSelectionChanged(view());
          dtde.dropComplete(true);
        } catch (NullPointerException npe) {
          npe.printStackTrace();
          dtde.dropComplete(false);
        }
      } else {
        dtde.rejectDrop();
      }
    } else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      log("String flavor dropped.");
      dtde.acceptDrop(dtde.getDropAction());
      Object o = DNDHelper.processReceivedData(DataFlavor.stringFlavor, dtde.getTransferable());
      if (o != null) {
        log("Received string flavored data.");
        dtde.getDropTargetContext().dropComplete(true);
      } else {
        dtde.getDropTargetContext().dropComplete(false);
      }
    } else if (dtde.isDataFlavorSupported(DNDHelper.ASCIIFlavor) == true) {
      log("ASCII Flavor dropped.");
      dtde.acceptDrop(DnDConstants.ACTION_COPY);
      Object o = DNDHelper.processReceivedData(DNDHelper.ASCIIFlavor, dtde.getTransferable());
      if (o != null) {
        log("Received ASCII Flavored data.");
        dtde.getDropTargetContext().dropComplete(true);
        // System.out.println(o);
      } else {
        dtde.getDropTargetContext().dropComplete(false);
      }
    } else if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      log("Java File List Flavor dropped.");
      dtde.acceptDrop(DnDConstants.ACTION_COPY);
      File[] fList =
          (File[])
              DNDHelper.processReceivedData(DataFlavor.javaFileListFlavor, dtde.getTransferable());
      if (fList != null) {
        log("Got list of files.");
        for (int x = 0; x < fList.length; x++) {
          System.out.println(fList[x].getAbsolutePath());
        }
        dtde.getDropTargetContext().dropComplete(true);
      } else {
        dtde.getDropTargetContext().dropComplete(false);
      }
    }
    fLastX = 0;
    fLastY = 0;
  }