コード例 #1
0
  @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);
    }
  }