From d2d0baa68a6492ddc07bfba4625bab1b7d083def Mon Sep 17 00:00:00 2001 From: Stian Grenborgen Date: Thu, 28 Dec 2023 12:20:29 +0100 Subject: [PATCH] Some code cleaning of amount types when dragging. --- .../client/gui/label/AbstractGoodsLabel.java | 73 +++++++------------ .../freecol/client/gui/label/GoodsLabel.java | 4 +- .../gui/panel/DefaultTransferHandler.java | 6 +- .../client/gui/panel/DragListener.java | 12 ++- 4 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/net/sf/freecol/client/gui/label/AbstractGoodsLabel.java b/src/net/sf/freecol/client/gui/label/AbstractGoodsLabel.java index 781689e66e..e3b068f6b0 100644 --- a/src/net/sf/freecol/client/gui/label/AbstractGoodsLabel.java +++ b/src/net/sf/freecol/client/gui/label/AbstractGoodsLabel.java @@ -34,20 +34,23 @@ * This label represents AbstractGoods. */ public class AbstractGoodsLabel extends FreeColLabel { + + public enum AmountType { + DEFAULT, + PARTIAL, + FULL, + + /** + * Special flag for SHIFT+ALT drag functionality on + * {@code DefaultTransferHandler}. + */ + SUPER_FULL + } private final ImageLibrary lib; - private final AbstractGoods abstractGoods; - private boolean partialChosen; - - private boolean fullChosen; - - /** - * Special flag for SHIFT+ALT drag functionality on - * {@code DefaultTransferHandler}. - */ - private boolean superFullChosen; + private AmountType amountType = AmountType.DEFAULT; /** @@ -76,24 +79,22 @@ public AbstractGoodsLabel(FreeColClient freeColClient, protected ImageLibrary getImageLibrary() { return this.lib; } - + /** - * Has the SHIFT-ALT been pressed on drag? - * - * @return True if this label was dragged with SHIFT-ALT + * Sets the type of transfer being used on drag. + * @param amountType */ - public boolean isSuperFullChosen() { - return superFullChosen; + public void setAmountType(AmountType amountType) { + this.amountType = (amountType == null) ? AmountType.DEFAULT : amountType; } /** - * Set DRAG-ALL functionality when SHIFT+ALT used on drag from - * {@code DragListener} + * Has the SHIFT-ALT been pressed on drag? * - * @param superFullChosen The new state of drag-all + * @return True if this label was dragged with SHIFT-ALT */ - public void setSuperFullChosen(boolean superFullChosen) { - this.superFullChosen = superFullChosen; + public boolean isSuperFullChosen() { + return amountType == AmountType.SUPER_FULL; } /** @@ -102,16 +103,7 @@ public void setSuperFullChosen(boolean superFullChosen) { * @return True if a partial amount has been selected. */ public boolean isPartialChosen() { - return partialChosen; - } - - /** - * Set the partial amount state. - * - * @param partialChosen The new partial amount state. - */ - public void setPartialChosen(boolean partialChosen) { - this.partialChosen = partialChosen; + return amountType == AmountType.PARTIAL; } /** @@ -120,16 +112,7 @@ public void setPartialChosen(boolean partialChosen) { * @return True if a full amount has been selected. */ public boolean isFullChosen() { - return fullChosen; - } - - /** - * Set the full amount state. - * - * @param fullChosen The new full amount state. - */ - public void setFullChosen(boolean fullChosen) { - this.fullChosen = fullChosen; + return amountType == AmountType.FULL; } /** @@ -188,9 +171,7 @@ public boolean equals(Object o) { if (o instanceof AbstractGoodsLabel) { AbstractGoodsLabel other = (AbstractGoodsLabel)o; return Utils.equals(this.abstractGoods, other.abstractGoods) - && this.partialChosen == other.partialChosen - && this.fullChosen == other.fullChosen - && this.superFullChosen == other.superFullChosen; + && this.amountType == other.amountType; } return false; } @@ -202,9 +183,7 @@ public boolean equals(Object o) { public int hashCode() { int hash = super.hashCode(); hash = 31 * hash + Utils.hashCode(this.abstractGoods); - hash = 31 * hash + ((this.partialChosen) ? 1 : 0) - + ((this.fullChosen) ? 2 : 0) - + ((this.superFullChosen) ? 4 : 0); + hash = 31 * hash + this.amountType.ordinal(); return hash; } } diff --git a/src/net/sf/freecol/client/gui/label/GoodsLabel.java b/src/net/sf/freecol/client/gui/label/GoodsLabel.java index b45625d8d3..1524c5582b 100644 --- a/src/net/sf/freecol/client/gui/label/GoodsLabel.java +++ b/src/net/sf/freecol/client/gui/label/GoodsLabel.java @@ -70,7 +70,9 @@ private void initialize() { final GoodsType type = goods.getType(); final Specification spec = goods.getGame().getSpecification(); - if (getAmount() < GoodsContainer.CARGO_SIZE) setPartialChosen(true); + if (getAmount() < GoodsContainer.CARGO_SIZE) { + setAmountType(AmountType.PARTIAL); + } setForeground(ImageLibrary.getGoodsColor(type, goods.getAmount(), location)); diff --git a/src/net/sf/freecol/client/gui/panel/DefaultTransferHandler.java b/src/net/sf/freecol/client/gui/panel/DefaultTransferHandler.java index debdef5adc..a9c85412f4 100644 --- a/src/net/sf/freecol/client/gui/panel/DefaultTransferHandler.java +++ b/src/net/sf/freecol/client/gui/panel/DefaultTransferHandler.java @@ -58,6 +58,7 @@ import net.sf.freecol.client.gui.label.GoodsTypeLabel; import net.sf.freecol.client.gui.label.MarketLabel; import net.sf.freecol.client.gui.label.UnitLabel; +import net.sf.freecol.client.gui.label.AbstractGoodsLabel.AmountType; import net.sf.freecol.common.model.Ability; import net.sf.freecol.common.model.AbstractGoods; import net.sf.freecol.common.model.Goods; @@ -76,6 +77,7 @@ public final class DefaultTransferHandler extends TransferHandler { private static final Logger logger = Logger.getLogger(DefaultTransferHandler.class.getName()); + /** * This is the default drag handler for drag and drop operations that @@ -89,7 +91,9 @@ private static class FreeColDragHandler private void updatePartialChosen(JComponent comp, boolean partial) { if (comp instanceof AbstractGoodsLabel) { - ((AbstractGoodsLabel)comp).setPartialChosen(partial); + if (partial) { + ((AbstractGoodsLabel)comp).setAmountType(AmountType.PARTIAL); + } } } diff --git a/src/net/sf/freecol/client/gui/panel/DragListener.java b/src/net/sf/freecol/client/gui/panel/DragListener.java index 333f566ab1..09a09fe0ba 100644 --- a/src/net/sf/freecol/client/gui/panel/DragListener.java +++ b/src/net/sf/freecol/client/gui/panel/DragListener.java @@ -34,6 +34,7 @@ import net.sf.freecol.client.gui.label.AbstractGoodsLabel; import net.sf.freecol.client.gui.label.GoodsTypeLabel; import net.sf.freecol.client.gui.label.UnitLabel; +import net.sf.freecol.client.gui.label.AbstractGoodsLabel.AmountType; import net.sf.freecol.common.model.Unit; import net.sf.freecol.common.util.OSUtils; @@ -146,17 +147,14 @@ public void mousePressed(MouseEvent e) { } } } - label.setSuperFullChosen(true); + label.setAmountType(AmountType.SUPER_FULL); label.setAmount(sum); } else if (e.isShiftDown()) { - label.setSuperFullChosen(false); - label.setPartialChosen(true); + label.setAmountType(AmountType.PARTIAL); } else if (e.isControlDown()) { - label.setSuperFullChosen(false); - label.setFullChosen(true); + label.setAmountType(AmountType.FULL); } else { - label.setSuperFullChosen(false); - label.setPartialChosen(false); + label.setAmountType(AmountType.DEFAULT); label.setDefaultAmount(); } } else if (comp instanceof UnitLabel) {