Esempio n. 1
0
  CardCollection removeCards(
      CardCollection oldCards, Iterable<Card> cardsToRemove, TrackableProperty key) {
    if (cardsToRemove == null || oldCards == null) {
      return oldCards;
    }

    TrackableCollection<CardView> views = get(key);
    boolean needFlagAsChanged = false;
    for (Card c : cardsToRemove) {
      if (oldCards.remove(c)) {
        if (views == null) {
          set(key, null);
        } else if (views.remove(c.getView())) {
          if (views.isEmpty()) {
            views = null;
            set(key, null); // avoid keeping around an empty collection
            needFlagAsChanged = false; // doesn't need to be flagged a second time
          } else {
            needFlagAsChanged = true;
          }
        }
        if (oldCards.isEmpty()) {
          oldCards = null; // avoid keeping around an empty collection
          break;
        }
      }
    }
    if (needFlagAsChanged) {
      flagAsChanged(key);
    }
    return oldCards;
  }
Esempio n. 2
0
  CardCollection addCards(
      CardCollection oldCards, Iterable<Card> cardsToAdd, TrackableProperty key) {
    if (cardsToAdd == null) {
      return oldCards;
    }

    TrackableCollection<CardView> views = get(key);
    if (oldCards == null) {
      oldCards = new CardCollection();
    }
    boolean needFlagAsChanged = false;
    for (Card c : cardsToAdd) {
      if (c != null && oldCards.add(c)) {
        if (views == null) {
          views = new TrackableCollection<CardView>();
          views.add(c.getView());
          set(key, views);
        } else if (views.add(c.getView())) {
          needFlagAsChanged = true;
        }
      }
    }
    if (needFlagAsChanged) {
      flagAsChanged(key);
    }
    return oldCards;
  }
Esempio n. 3
0
 CardCollection setCards(CardCollection oldCards, CardCollection newCards, TrackableProperty key) {
   if (newCards == null || newCards.isEmpty()) { // avoid storing empty collections
     set(key, null);
     return null;
   }
   set(key, CardView.getCollection(newCards)); // TODO prevent overwriting list if not necessary
   return newCards;
 }
Esempio n. 4
0
  CardCollection removeCard(CardCollection oldCards, Card cardToRemove, TrackableProperty key) {
    if (cardToRemove == null || oldCards == null) {
      return oldCards;
    }

    if (oldCards.remove(cardToRemove)) {
      TrackableCollection<CardView> views = get(key);
      if (views == null) {
        set(key, null);
      } else if (views.remove(cardToRemove.getView())) {
        if (views.isEmpty()) {
          set(key, null); // avoid keeping around an empty collection
        } else {
          flagAsChanged(key);
        }
      }
      if (oldCards.isEmpty()) {
        oldCards = null; // avoid keeping around an empty collection
      }
    }
    return oldCards;
  }
  @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);
    }
  }
Esempio n. 6
0
  CardCollection addCard(CardCollection oldCards, Card cardToAdd, TrackableProperty key) {
    if (cardToAdd == null) {
      return oldCards;
    }

    if (oldCards == null) {
      oldCards = new CardCollection();
    }
    if (oldCards.add(cardToAdd)) {
      TrackableCollection<CardView> views = get(key);
      if (views == null) {
        views = new TrackableCollection<CardView>();
        views.add(cardToAdd.getView());
        ;
        set(key, views);
      } else if (views.add(cardToAdd.getView())) {
        flagAsChanged(key);
      }
    }
    return oldCards;
  }