Skip to content

Commit

Permalink
Some code cleaning of amount types when dragging.
Browse files Browse the repository at this point in the history
  • Loading branch information
stiangre committed Dec 28, 2023
1 parent 4d5efcb commit d2d0baa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 56 deletions.
73 changes: 26 additions & 47 deletions src/net/sf/freecol/client/gui/label/AbstractGoodsLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
4 changes: 3 additions & 1 deletion src/net/sf/freecol/client/gui/label/GoodsLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/net/sf/freecol/client/gui/panel/DragListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d2d0baa

Please sign in to comment.