/** Add any stops selected in the destination selector. */ private void addSelectedStops() { int startIndex = -1; int endIndex = -1; int sel = this.destinationSelector.getSelectedIndex(); if (sel == 0) { // All colonies + Europe startIndex = 1; endIndex = this.destinationSelector.getItemCount(); } else { // just one place startIndex = sel; endIndex = startIndex + 1; } List<GoodsType> cargo = new ArrayList<>(); for (Component comp : cargoPanel.getComponents()) { CargoLabel label = (CargoLabel) comp; cargo.add(label.getType()); } int maxIndex = this.stopList.getMaxSelectionIndex(); for (int i = startIndex; i < endIndex; i++) { String id = this.destinationSelector.getItemAt(i); FreeColGameObject fcgo = getGame().getFreeColGameObject(id); if (fcgo instanceof Location) { TradeRouteStop stop = new TradeRouteStop(getGame(), (Location) fcgo); stop.setCargo(cargo); if (maxIndex < 0) { this.stopListModel.addElement(stop); } else { maxIndex++; this.stopListModel.add(maxIndex, stop); } } } }
/** * Imports the data represented by the given Transferable into the given component. Returns 'true' * on success, 'false' otherwise. * * @param comp The component to import the data to. * @param t The Transferable that holds the data. * @return 'true' on success, 'false' otherwise. */ public boolean importData(JComponent comp, Transferable t) { try { JLabel data; /* This variable is used to temporarily keep the old selected unit, while moving cargo from one carrier to another: */ UnitLabel oldSelectedUnit = null; // Check flavor. if (t.isDataFlavorSupported(DefaultTransferHandler.flavor)) { data = (JLabel) t.getTransferData(DefaultTransferHandler.flavor); } else { logger.warning("Data flavor is not supported!"); return false; } // Do not allow a transferable to be dropped upon itself: if (comp == data) { return false; } // Make sure we don't drop onto other Labels. if (comp instanceof UnitLabel) { UnitLabel unitLabel = (UnitLabel) comp; /** * If the unit/cargo is dropped on a carrier in port (EuropePanel.InPortPanel), then the * ship is selected and the unit is added to its cargo. If the unit is not a carrier, but * can be equipped, and the goods can be converted to equipment, equip the unit. * * <p>If not, assume that the user wished to drop the unit/cargo on the panel below. */ if (unitLabel.getUnit().isCarrier() && unitLabel.getParent() instanceof InPortPanel && parentPanel instanceof PortPanel) { PortPanel portPanel = (PortPanel) parentPanel; if (data instanceof Draggable && ((Draggable) data).isOnCarrier()) { oldSelectedUnit = portPanel.getSelectedUnitLabel(); } portPanel.setSelectedUnitLabel(unitLabel); comp = portPanel.getCargoPanel(); } else if (unitLabel.canUnitBeEquipedWith(data)) { // don't do anything before partial amount has been checked } else { comp = getDropTarget(comp); } } else if (comp instanceof AbstractGoodsLabel) { comp = getDropTarget(comp); } // t is already in comp: if (data.getParent() == comp) { return false; } if (data instanceof UnitLabel) { // Check if the unit can be dragged to comp. Unit unit = ((UnitLabel) data).getUnit(); if (comp instanceof DropTarget) { DropTarget target = (DropTarget) comp; if (!target.accepts(unit)) { return false; } if ((comp instanceof ColonyPanel.OutsideColonyPanel || comp instanceof ColonyPanel.ColonyCargoPanel) && !gui.tryLeaveColony(unit)) { return false; } target.add(data, true); // Update unit selection // new unit selection has already been taken care // of if this unit was moved to ToAmericaPanel restoreSelection(oldSelectedUnit); comp.revalidate(); return true; } else { return false; } } else if (data instanceof GoodsLabel) { // Check if the goods can be dragged to comp. GoodsLabel label = (GoodsLabel) data; Goods goods = label.getGoods(); // Import the data. if (label.isPartialChosen()) { int defaultAmount = -1; if (goods.getLocation() instanceof GoodsLocation) { GoodsLocation loc = (GoodsLocation) goods.getLocation(); if (goods.getAmount() > loc.getGoodsCapacity()) { // If over capacity, favour the amount that would // correct the problem. defaultAmount = Math.min(goods.getAmount() - loc.getGoodsCapacity(), GoodsContainer.CARGO_SIZE); } } int amount = getAmount(goods.getType(), goods.getAmount(), defaultAmount, false); if (amount <= 0) return false; goods.setAmount(amount); } else if (goods.getAmount() > GoodsContainer.CARGO_SIZE) { goods.setAmount(GoodsContainer.CARGO_SIZE); } if (comp instanceof UnitLabel) { return equipUnitIfPossible((UnitLabel) comp, goods); } else if (comp instanceof JLabel) { logger.warning("Oops, I thought we didn't have to write this part."); return true; } else if (comp instanceof DropTarget) { DropTarget target = (DropTarget) comp; if (target.accepts(goods)) { target.add(data, true); } else { return false; } restoreSelection(oldSelectedUnit); comp.revalidate(); return true; } } else if (data instanceof MarketLabel) { // Check if the unit can be dragged to comp. MarketLabel label = ((MarketLabel) data); // Import the data. if (label.isPartialChosen()) { int amount = getAmount(label.getType(), label.getAmount(), -1, true); if (amount <= 0) { return false; } label.setAmount(amount); } if (comp instanceof UnitLabel) { return equipUnitIfPossible((UnitLabel) comp, label.getGoods()); } else if (comp instanceof JLabel) { logger.warning("Oops, I thought we didn't have to write this part."); return true; } else if (comp instanceof JPanel) { if (comp instanceof CargoPanel) { ((CargoPanel) comp).add(data, true); } else { logger.warning("The receiving component is of an invalid type."); return false; } comp.revalidate(); return true; } } logger.warning("The dragged component is of an invalid type."); } catch (Exception e) { // TODO: Suggest a reconnect. logger.log(Level.WARNING, "Import data fail", e); } return false; }