/tags/20071224/Colossus/core/src/main/java/net/sf/colossus/client/LegionInfoPanel.java

# · Java · 259 lines · 212 code · 27 blank · 20 comment · 32 complexity · bf13c55ae30f803953facd6dc6906bd7 MD5 · raw file

  1. package net.sf.colossus.client;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import javax.swing.Box;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import net.sf.colossus.util.HTMLColor;
  11. import net.sf.colossus.util.Options;
  12. /**
  13. * Creates a JPanel displaying one legion,
  14. * used by AutoInspector and ShowLegion (right-click on legion)
  15. *
  16. * @version $Id$
  17. */
  18. public final class LegionInfoPanel extends JPanel
  19. {
  20. private String valueText = "";
  21. public LegionInfoPanel(LegionInfo legion, int scale, int margin,
  22. int padding, boolean usePlayerColor, int viewMode, String playerName,
  23. boolean dubiousAsBlanks, boolean showLegionValue)
  24. {
  25. boolean contentCertain = false;
  26. boolean hideAll = false;
  27. if (viewMode == Options.viewableAllNum)
  28. {
  29. contentCertain = true;
  30. viewAll(legion, usePlayerColor, scale, margin, padding,
  31. dubiousAsBlanks, hideAll, showLegionValue);
  32. }
  33. else if (viewMode == Options.viewableOwnNum)
  34. {
  35. String legionOwner = legion.getPlayerName();
  36. if (playerName.equals(legionOwner))
  37. {
  38. contentCertain = true;
  39. viewAll(legion, usePlayerColor, scale, margin, padding,
  40. dubiousAsBlanks, hideAll, showLegionValue);
  41. }
  42. else
  43. {
  44. hideAll = true;
  45. viewAll(legion, usePlayerColor, scale, margin, padding, false,
  46. hideAll, showLegionValue);
  47. }
  48. }
  49. else if (viewMode == Options.viewableEverNum)
  50. {
  51. // for this mode, in Game/Server broadcasting of revealed info
  52. // is limited to those that are entitled to know;
  53. // thus we can use the splitPrediction to decide what is
  54. // "has ever been shown or can be concluded".
  55. viewAll(legion, usePlayerColor, scale, margin, padding,
  56. dubiousAsBlanks, hideAll, showLegionValue);
  57. }
  58. else
  59. {
  60. viewOtherText("not implemented...");
  61. }
  62. if (contentCertain)
  63. {
  64. int value = legion.getPointValue();
  65. valueText = " (" + value + " points)";
  66. }
  67. else
  68. {
  69. int value;
  70. int numUC;
  71. if (viewMode == Options.viewableOwnNum)
  72. {
  73. value = 0;
  74. numUC = legion.getHeight();
  75. }
  76. else
  77. {
  78. value = legion.getCertainPointValue();
  79. numUC = legion.numUncertainCreatures();
  80. }
  81. String ucString = "";
  82. if (numUC > 0)
  83. {
  84. StringBuffer uncertainIndicator = new StringBuffer(8);
  85. uncertainIndicator.append("+");
  86. while (numUC > 0)
  87. {
  88. uncertainIndicator.append("?");
  89. numUC--;
  90. }
  91. // substring so that StringBuffer gets released.
  92. ucString = uncertainIndicator.substring(0);
  93. }
  94. valueText = " (" + value + ucString + " points)";
  95. }
  96. }
  97. public String getValueText()
  98. {
  99. return valueText;
  100. }
  101. private void viewOtherText(String text)
  102. {
  103. add(new JLabel(text));
  104. }
  105. private void viewAll(LegionInfo legion, boolean usePlayerColor,
  106. int scale, int margin, int padding,
  107. boolean dubiousAsBlanks, boolean hideAll, boolean showLegionValue)
  108. {
  109. setLayout(null);
  110. if (usePlayerColor)
  111. {
  112. Color playerColor =
  113. HTMLColor.stringToColor(legion.getPlayerInfo().getColor() +
  114. "Colossus");
  115. setBackground(playerColor);
  116. }
  117. int i = 0;
  118. int effectiveChitSize = 0; // Chit treats scale as a hint,
  119. // actual size might differ
  120. // We could add the marker, if we want:
  121. boolean showMarker = false;
  122. if (showMarker)
  123. {
  124. Chit marker = new Chit(scale, legion.getMarkerId(),
  125. false, true, false);
  126. if (effectiveChitSize == 0)
  127. {
  128. // they should be all the same size
  129. effectiveChitSize = marker.getWidth();
  130. }
  131. add(marker);
  132. marker.setLocation(i * (effectiveChitSize + padding) + margin,
  133. margin);
  134. i++;
  135. }
  136. List imageNames = legion.getImageNames();
  137. List certain = legion.getCertainties();
  138. boolean allCertain = !hideAll;
  139. // if uncertain shall be shown ones only as blanks, then
  140. // also sort the blanks all to the end:
  141. // (just unnecessary work if hideAll is set.)
  142. if (dubiousAsBlanks && !hideAll)
  143. {
  144. Iterator iIt = imageNames.iterator();
  145. Iterator cIt = certain.iterator();
  146. List cNames = new ArrayList();
  147. List cCertain = new ArrayList();
  148. List ucNames = new ArrayList();
  149. List ucCertain = new ArrayList();
  150. while (iIt.hasNext())
  151. {
  152. String imageName = (String)iIt.next();
  153. Boolean sure = (Boolean)cIt.next();
  154. if (sure.booleanValue())
  155. {
  156. cNames.add(imageName);
  157. cCertain.add(sure);
  158. }
  159. else
  160. {
  161. ucNames.add(imageName);
  162. ucCertain.add(sure);
  163. }
  164. }
  165. imageNames.clear();
  166. imageNames.addAll(cNames);
  167. imageNames.addAll(ucNames);
  168. cNames.clear();
  169. ucNames.clear();
  170. certain.clear();
  171. certain.addAll(cCertain);
  172. certain.addAll(ucCertain);
  173. cCertain.clear();
  174. ucCertain.clear();
  175. }
  176. Iterator it = imageNames.iterator();
  177. Iterator it2 = certain.iterator();
  178. // now add the chits one by one to the panel:
  179. while (it.hasNext())
  180. {
  181. String imageName = (String)it.next();
  182. Chit chit;
  183. boolean sure = ((Boolean)it2.next()).booleanValue();
  184. if (!sure)
  185. {
  186. allCertain = false;
  187. }
  188. if (hideAll)
  189. {
  190. chit = new Chit(scale, "QuestionMarkMask", false, true, false);
  191. }
  192. else
  193. {
  194. chit = new Chit(scale, imageName, false, !sure,
  195. dubiousAsBlanks);
  196. }
  197. if (effectiveChitSize == 0)
  198. {
  199. // they should be all the same size
  200. effectiveChitSize = chit.getWidth();
  201. }
  202. add(chit);
  203. chit.setLocation(i * (effectiveChitSize + padding) + margin,
  204. margin);
  205. i++;
  206. }
  207. if (showLegionValue && allCertain)
  208. {
  209. JLabel sizeLabel =
  210. new JLabel(String.valueOf(legion.getPointValue()));
  211. sizeLabel.setForeground(Color.WHITE);
  212. add(sizeLabel);
  213. sizeLabel.setLocation(i * (effectiveChitSize + padding) + margin,
  214. margin);
  215. sizeLabel.setSize(new Dimension(effectiveChitSize,
  216. effectiveChitSize));
  217. i++;
  218. }
  219. // This fixes a repaint bug under Linux.
  220. if (imageNames.size() == 1)
  221. {
  222. add(Box.createRigidArea(new Dimension(scale, scale)));
  223. }
  224. setSize((legion.getImageNames().size() + (showMarker ? 1 : 0)
  225. + (showLegionValue && allCertain ? 1 : 0))
  226. * (effectiveChitSize + padding) - padding + 2 * margin,
  227. effectiveChitSize + 2 * margin);
  228. setMinimumSize(getSize());
  229. setPreferredSize(getSize());
  230. setMaximumSize(getSize());
  231. }
  232. }