Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add damage and icon for bombing target selection #2339

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/games/strategy/triplea/ui/TripleAFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
Expand Down Expand Up @@ -60,12 +61,14 @@
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JToolTip;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
Expand Down Expand Up @@ -112,6 +115,7 @@
import games.strategy.thread.ThreadPool;
import games.strategy.triplea.Properties;
import games.strategy.triplea.TripleAPlayer;
import games.strategy.triplea.TripleAUnit;
import games.strategy.triplea.ai.proAI.ProAI;
import games.strategy.triplea.attachments.AbstractConditionsAttachment;
import games.strategy.triplea.attachments.AbstractTriggerAttachment;
Expand Down Expand Up @@ -904,6 +908,7 @@ public Unit getStrategicBombingRaidTarget(final Territory territory, final Colle
final JList<Unit> list = new JList<>(new Vector<>(potentialTargets));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.setCellRenderer(new UnitRenderer());
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
if (bombers != null) {
Expand All @@ -924,6 +929,37 @@ public Unit getStrategicBombingRaidTarget(final Territory territory, final Colle
return selected.get();
}

@SuppressWarnings("serial")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have eclipse generate a serialVersionUID for you instead of supressing this warning

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

class UnitRenderer extends JLabel implements ListCellRenderer<Unit> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is only used within TripleAFrame and can thus be private. That would also allow you to give the constructor default accessibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to private


public UnitRenderer() {
setOpaque(true);
}

@Override
public Component getListCellRendererComponent(final JList<? extends Unit> list, final Unit unit, final int index,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick comment here might be good, summarize that we are building a dialog with the unit image next to name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment to class

final boolean isSelected, final boolean cellHasFocus) {

setText(unit.toString() + ", damage=" + TripleAUnit.get(unit).getUnitDamage());
final Optional<ImageIcon> icon = uiContext.getUnitImageFactory().getIcon(unit.getType(), unit.getOwner(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor, you can write the optional + if statement in one statement:

           Matches.unitHasTakenSomeBombingUnitDamage().match(unit), Matches.unitIsDisabled().match(unit)).ifPresent(icon -> setIcon(icon));```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna leave it on separate lines as I think its a little easier to read

Matches.unitHasTakenSomeBombingUnitDamage().match(unit), Matches.unitIsDisabled().match(unit));
if (icon.isPresent()) {
setIcon(icon.get());
}
setBorder(new EmptyBorder(0, 0, 0, 10));

if (isSelected) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand why we are setting foreground/background colors. Likely not terribly important, though a comment here may help to capture that context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment

setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}

return this;
}
}

public int[] selectFixedDice(final int numDice, final int hitAt, final boolean hitOnlyIfEquals, final String title,
final int diceSides) {
if (messageAndDialogThreadPool == null) {
Expand Down