/branches/cleka_acquire_etc/Colossus/core/src/main/java/net/sf/colossus/client/LegionInfoPanel.java
# · Java · 256 lines · 212 code · 26 blank · 18 comment · 32 complexity · 0219a2b318fc89668703ad77b7e658a7 MD5 · raw file
- package net.sf.colossus.client;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import javax.swing.Box;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import net.sf.colossus.util.HTMLColor;
- import net.sf.colossus.util.Options;
- /**
- * Creates a JPanel displaying one legion,
- * used by AutoInspector and ShowLegion (right-click on legion)
- */
- public final class LegionInfoPanel extends JPanel
- {
- private String valueText = "";
- public LegionInfoPanel(LegionClientSide legion, int scale, int margin,
- int padding, boolean usePlayerColor, int viewMode,
- boolean dubiousAsBlanks, boolean showLegionValue)
- {
- boolean contentCertain = false;
- boolean hideAll = false;
- if (viewMode == Options.viewableAllNum)
- {
- contentCertain = true;
- viewAll(legion, usePlayerColor, scale, margin, padding,
- dubiousAsBlanks, hideAll, showLegionValue);
- }
- else if (viewMode == Options.viewableOwnNum)
- {
- if (legion.isMyLegion())
- {
- contentCertain = true;
- viewAll(legion, usePlayerColor, scale, margin, padding,
- dubiousAsBlanks, hideAll, showLegionValue);
- }
- else
- {
- hideAll = true;
- viewAll(legion, usePlayerColor, scale, margin, padding, false,
- hideAll, showLegionValue);
- }
- }
- else if (viewMode == Options.viewableEverNum)
- {
- // for this mode, in Game/Server broadcasting of revealed info
- // is limited to those that are entitled to know;
- // thus we can use the splitPrediction to decide what is
- // "has ever been shown or can be concluded".
- viewAll(legion, usePlayerColor, scale, margin, padding,
- dubiousAsBlanks, hideAll, showLegionValue);
- }
- else
- {
- viewOtherText("not implemented...");
- }
- if (contentCertain)
- {
- int value = legion.getPointValue();
- valueText = " (" + value + " points)";
- }
- else
- {
- int value;
- int numUC;
- if (viewMode == Options.viewableOwnNum)
- {
- value = 0;
- numUC = legion.getHeight();
- }
- else
- {
- value = legion.getCertainPointValue();
- numUC = legion.numUncertainCreatures();
- }
- String ucString = "";
- if (numUC > 0)
- {
- StringBuffer uncertainIndicator = new StringBuffer(8);
- uncertainIndicator.append("+");
- while (numUC > 0)
- {
- uncertainIndicator.append("?");
- numUC--;
- }
- // substring so that StringBuffer gets released.
- ucString = uncertainIndicator.substring(0);
- }
- valueText = " (" + value + ucString + " points)";
- }
- }
- public String getValueText()
- {
- return valueText;
- }
- private void viewOtherText(String text)
- {
- add(new JLabel(text));
- }
- private void viewAll(LegionClientSide legion, boolean usePlayerColor,
- int scale, int margin, int padding, boolean dubiousAsBlanks,
- boolean hideAll, boolean showLegionValue)
- {
- setLayout(null);
- if (usePlayerColor)
- {
- Color playerColor = HTMLColor.stringToColor(legion.getPlayer()
- .getColor()
- + "Colossus");
- setBackground(playerColor);
- }
- int i = 0;
- int effectiveChitSize = 0; // Chit treats scale as a hint,
- // actual size might differ
- // We could add the marker, if we want:
- boolean showMarker = false;
- if (showMarker)
- {
- Chit marker = new Chit(scale, legion.getMarkerId(), false, true,
- false);
- if (effectiveChitSize == 0)
- {
- // they should be all the same size
- effectiveChitSize = marker.getWidth();
- }
- add(marker);
- marker.setLocation(i * (effectiveChitSize + padding) + margin,
- margin);
- i++;
- }
- List<String> imageNames = legion.getImageNames();
- List<Boolean> certain = legion.getCertainties();
- boolean allCertain = !hideAll;
- // if uncertain shall be shown ones only as blanks, then
- // also sort the blanks all to the end:
- // (just unnecessary work if hideAll is set.)
- if (dubiousAsBlanks && !hideAll)
- {
- Iterator<String> iIt = imageNames.iterator();
- Iterator<Boolean> cIt = certain.iterator();
- List<String> cNames = new ArrayList<String>();
- List<Boolean> cCertain = new ArrayList<Boolean>();
- List<String> ucNames = new ArrayList<String>();
- List<Boolean> ucCertain = new ArrayList<Boolean>();
- while (iIt.hasNext())
- {
- String imageName = iIt.next();
- Boolean sure = cIt.next();
- if (sure.booleanValue())
- {
- cNames.add(imageName);
- cCertain.add(sure);
- }
- else
- {
- ucNames.add(imageName);
- ucCertain.add(sure);
- }
- }
- imageNames.clear();
- imageNames.addAll(cNames);
- imageNames.addAll(ucNames);
- cNames.clear();
- ucNames.clear();
- certain.clear();
- certain.addAll(cCertain);
- certain.addAll(ucCertain);
- cCertain.clear();
- ucCertain.clear();
- }
- Iterator<String> it = imageNames.iterator();
- Iterator<Boolean> it2 = certain.iterator();
- // now add the chits one by one to the panel:
- while (it.hasNext())
- {
- String imageName = it.next();
- Chit chit;
- boolean sure = it2.next().booleanValue();
- if (!sure)
- {
- allCertain = false;
- }
- if (hideAll)
- {
- chit = new Chit(scale, "QuestionMarkMask", false, true, false);
- }
- else
- {
- chit = new Chit(scale, imageName, false, !sure,
- dubiousAsBlanks);
- }
- if (effectiveChitSize == 0)
- {
- // they should be all the same size
- effectiveChitSize = chit.getWidth();
- }
- add(chit);
- chit.setLocation(i * (effectiveChitSize + padding) + margin,
- margin);
- i++;
- }
- if (showLegionValue && allCertain)
- {
- JLabel sizeLabel = new JLabel(String.valueOf(legion
- .getPointValue()));
- sizeLabel.setForeground(Color.WHITE);
- add(sizeLabel);
- sizeLabel.setLocation(i * (effectiveChitSize + padding) + margin,
- margin);
- sizeLabel.setSize(new Dimension(effectiveChitSize,
- effectiveChitSize));
- i++;
- }
- // This fixes a repaint bug under Linux.
- if (imageNames.size() == 1)
- {
- add(Box.createRigidArea(new Dimension(scale, scale)));
- }
- setSize(
- (legion.getImageNames().size() + (showMarker ? 1 : 0) + (showLegionValue
- && allCertain ? 1 : 0))
- * (effectiveChitSize + padding) - padding + 2 * margin,
- effectiveChitSize + 2 * margin);
- setMinimumSize(getSize());
- setPreferredSize(getSize());
- setMaximumSize(getSize());
- }
- }