/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

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