use of games.strategy.util.Tuple in project triplea by triplea-game.
the class TabbedProductionPanel method initLayout.
@Override
protected void initLayout() {
this.removeAll();
this.setLayout(new GridBagLayout());
add(new JLabel("Attack/Defense/Movement"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(8, 8, 8, 0), 0, 0));
final JTabbedPane tabs = new JTabbedPane();
add(tabs, new GridBagConstraints(0, 1, 1, 1, 100, 100, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(8, 8, 8, 8), 0, 0));
final ProductionTabsProperties properties = ProductionTabsProperties.getInstance(id, rules);
final List<Tuple<String, List<Rule>>> ruleLists = getRuleLists(properties);
calculateRowsAndColumns(properties, largestList(ruleLists));
for (final Tuple<String, List<Rule>> ruleList : ruleLists) {
if (ruleList.getSecond().size() > 0) {
tabs.addTab(ruleList.getFirst(), new JScrollPane(getRulesPanel(ruleList.getSecond())));
}
}
final JPanel totals = new JPanel();
totals.add(left);
totals.add(remainingResources);
add(totals, new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 8, 0, 12), 0, 0));
done = new JButton(doneAction);
add(done, new GridBagConstraints(0, 3, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 8, 0), 0, 0));
tabs.validate();
this.validate();
}
use of games.strategy.util.Tuple in project triplea by triplea-game.
the class TripleAFrame method selectTerritoryForAirToLand.
@Nullable
public Territory selectTerritoryForAirToLand(final Collection<Territory> candidates, final Territory currentTerritory, final String unitMessage) {
if (candidates == null || candidates.isEmpty()) {
return null;
}
if (candidates.size() == 1 || messageAndDialogThreadPool == null) {
return candidates.iterator().next();
}
messageAndDialogThreadPool.waitForAll();
final Supplier<Tuple<JPanel, JList<Territory>>> action = () -> {
mapPanel.centerOn(currentTerritory);
final JList<Territory> list = new JList<>(SwingComponents.newListModel(candidates));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
final JScrollPane scroll = new JScrollPane(list);
final JTextArea text = new JTextArea(unitMessage, 8, 30);
text.setLineWrap(true);
text.setEditable(false);
text.setWrapStyleWord(true);
panel.add(text, BorderLayout.NORTH);
panel.add(scroll, BorderLayout.CENTER);
return Tuple.of(panel, list);
};
return Interruptibles.awaitResult(() -> SwingAction.invokeAndWaitResult(action)).result.map(comps -> {
final JPanel panel = comps.getFirst();
final JList<?> list = comps.getSecond();
final String[] options = { "OK" };
final String title = "Select territory for air units to land, current territory is " + currentTerritory.getName();
EventThreadJOptionPane.showOptionDialog(this, panel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null, getUiContext().getCountDownLatchHandler());
return (Territory) list.getSelectedValue();
}).orElse(null);
}
use of games.strategy.util.Tuple in project triplea by triplea-game.
the class TripleAFrame method scrambleUnitsQuery.
public HashMap<Territory, Collection<Unit>> scrambleUnitsQuery(final Territory scrambleTo, final Map<Territory, Tuple<Collection<Unit>, Collection<Unit>>> possibleScramblers) {
if (messageAndDialogThreadPool == null) {
return null;
}
messageAndDialogThreadPool.waitForAll();
if (SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("Should not be called from dispatch thread");
}
final CountDownLatch continueLatch = new CountDownLatch(1);
final HashMap<Territory, Collection<Unit>> selection = new HashMap<>();
final Collection<Tuple<Territory, UnitChooser>> choosers = new ArrayList<>();
SwingUtilities.invokeLater(() -> {
mapPanel.centerOn(scrambleTo);
final JDialog dialog = new JDialog(this, "Select units to scramble to " + scrambleTo.getName());
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
final String optionScramble = "Scramble";
final String optionNone = "None";
final JButton scrambleButton = new JButton(optionScramble);
scrambleButton.addActionListener(e -> getOptionPane((JComponent) e.getSource()).setValue(scrambleButton));
final JButton noneButton = new JButton(optionNone);
noneButton.addActionListener(e -> getOptionPane((JComponent) e.getSource()).setValue(noneButton));
final Object[] options = { scrambleButton, noneButton };
final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[1]);
final JLabel whereTo = new JLabel("Scramble To: " + scrambleTo.getName());
whereTo.setFont(new Font("Arial", Font.ITALIC, 12));
panel.add(whereTo, BorderLayout.NORTH);
final JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createEmptyBorder());
panel2.setLayout(new FlowLayout());
final JPanel fuelCostPanel = new JPanel(new GridBagLayout());
panel.add(fuelCostPanel, BorderLayout.SOUTH);
for (final Territory from : possibleScramblers.keySet()) {
final JPanel panelChooser = new JPanel();
panelChooser.setLayout(new BoxLayout(panelChooser, BoxLayout.Y_AXIS));
panelChooser.setBorder(BorderFactory.createLineBorder(getBackground()));
final JLabel whereFrom = new JLabel("From: " + from.getName());
whereFrom.setHorizontalAlignment(SwingConstants.LEFT);
whereFrom.setFont(new Font("Arial", Font.BOLD, 12));
panelChooser.add(whereFrom);
panelChooser.add(new JLabel(" "));
final Collection<Unit> possible = possibleScramblers.get(from).getSecond();
final int maxAllowed = Math.min(BattleDelegate.getMaxScrambleCount(possibleScramblers.get(from).getFirst()), possible.size());
final UnitChooser chooser = new UnitChooser(possible, Collections.emptyMap(), false, uiContext);
chooser.setMaxAndShowMaxButton(maxAllowed);
chooser.addChangeListener(field -> {
final Collection<Unit> units = new HashSet<>();
final Map<PlayerID, ResourceCollection> playerFuelCost = new HashMap<>();
for (final Tuple<Territory, UnitChooser> tuple : choosers) {
units.addAll(tuple.getSecond().getSelected(false));
final Map<PlayerID, ResourceCollection> map = Route.getScrambleFuelCostCharge(tuple.getSecond().getSelected(false), tuple.getFirst(), scrambleTo, data);
for (final Entry<PlayerID, ResourceCollection> playerAndCost : map.entrySet()) {
if (playerFuelCost.containsKey(playerAndCost.getKey())) {
playerFuelCost.get(playerAndCost.getKey()).add(playerAndCost.getValue());
} else {
playerFuelCost.put(playerAndCost.getKey(), playerAndCost.getValue());
}
}
}
fuelCostPanel.removeAll();
boolean hasEnoughFuel = true;
int count = 0;
for (final Entry<PlayerID, ResourceCollection> entry : playerFuelCost.entrySet()) {
final JLabel label = new JLabel(entry.getKey().getName() + ": ");
fuelCostPanel.add(label, new GridBagConstraints(0, count, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
fuelCostPanel.add(uiContext.getResourceImageFactory().getResourcesPanel(entry.getValue(), data), new GridBagConstraints(1, count++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
if (!entry.getKey().getResources().has(entry.getValue().getResourcesCopy())) {
hasEnoughFuel = false;
label.setForeground(Color.RED);
}
}
scrambleButton.setEnabled(hasEnoughFuel);
dialog.pack();
});
choosers.add(Tuple.of(from, chooser));
panelChooser.add(chooser);
final JScrollPane chooserScrollPane = new JScrollPane(panelChooser);
panel2.add(chooserScrollPane);
}
panel.add(panel2, BorderLayout.CENTER);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setLocationRelativeTo(getParent());
dialog.setAlwaysOnTop(true);
dialog.pack();
dialog.setVisible(true);
dialog.requestFocusInWindow();
optionPane.addPropertyChangeListener(e -> {
if (!dialog.isVisible()) {
return;
}
final String option = ((JButton) optionPane.getValue()).getText();
if (option.equals(optionNone)) {
choosers.clear();
selection.clear();
dialog.setVisible(false);
dialog.removeAll();
dialog.dispose();
continueLatch.countDown();
} else if (option.equals(optionScramble)) {
for (final Tuple<Territory, UnitChooser> terrChooser : choosers) {
selection.put(terrChooser.getFirst(), terrChooser.getSecond().getSelected());
}
dialog.setVisible(false);
dialog.removeAll();
dialog.dispose();
continueLatch.countDown();
}
});
});
mapPanel.getUiContext().addShutdownLatch(continueLatch);
Interruptibles.await(continueLatch);
mapPanel.getUiContext().removeShutdownLatch(continueLatch);
return selection;
}
use of games.strategy.util.Tuple in project triplea by triplea-game.
the class TripleAFrame method getStrategicBombingRaidTarget.
@Nullable
public Unit getStrategicBombingRaidTarget(final Territory territory, final Collection<Unit> potentialTargets, final Collection<Unit> bombers) {
if (potentialTargets.size() == 1 || messageAndDialogThreadPool == null) {
return potentialTargets.iterator().next();
}
messageAndDialogThreadPool.waitForAll();
final AtomicReference<Unit> selected = new AtomicReference<>();
final String message = "Select bombing target in " + territory.getName();
final Supplier<Tuple<JPanel, JList<Unit>>> action = () -> {
final JList<Unit> list = new JList<>(SwingComponents.newListModel(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) {
panel.add(new JLabel("For Units: " + MyFormatter.unitsToTextNoOwner(bombers)), BorderLayout.NORTH);
}
final JScrollPane scroll = new JScrollPane(list);
panel.add(scroll, BorderLayout.CENTER);
return Tuple.of(panel, list);
};
return Interruptibles.awaitResult(() -> SwingAction.invokeAndWaitResult(action)).result.map(comps -> {
final JPanel panel = comps.getFirst();
final JList<?> list = comps.getSecond();
final String[] options = { "OK" };
final int selection = EventThreadJOptionPane.showOptionDialog(this, panel, message, JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null, getUiContext().getCountDownLatchHandler());
if (selection == 0) {
selected.set((Unit) list.getSelectedValue());
}
return selected.get();
}).orElse(null);
}
Aggregations