private boolean equipUnitIfPossible(UnitLabel unitLabel, AbstractGoods goods) { Unit unit = unitLabel.getUnit(); if (unit.hasAbility(Ability.CAN_BE_EQUIPPED)) { for (EquipmentType equipment : freeColClient.getGame().getSpecification().getEquipmentTypeList()) { if (unit.canBeEquippedWith(equipment) && equipment.getGoodsRequired().size() == 1) { AbstractGoods requiredGoods = equipment.getGoodsRequired().get(0); if (requiredGoods.getType().equals(goods.getType()) && requiredGoods.getAmount() <= goods.getAmount()) { int amount = Math.min( goods.getAmount() / requiredGoods.getAmount(), equipment.getMaximumCount()); freeColClient.getInGameController().equipUnit(unit, equipment, amount); unitLabel.updateIcon(); return true; } } } } return false; }
private void restoreSelection(UnitLabel oldSelectedUnit) { if (oldSelectedUnit != null && oldSelectedUnit.getParent() instanceof InPortPanel) { ((PortPanel) parentPanel).setSelectedUnitLabel(oldSelectedUnit); } }
/** * 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; }