PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/MWClient/src/main/java/mw/client/gui/MWReveal.java

http://magicwars.googlecode.com/
Java | 357 lines | 268 code | 70 blank | 19 comment | 36 complexity | 9b127ff77399ed8d68b2d51fe2c921ef MD5 | raw file
  1. package mw.client.gui;
  2. import java.awt.Dimension;
  3. import java.awt.Rectangle;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseMotionAdapter;
  10. import java.awt.event.MouseMotionListener;
  11. import java.rmi.RemoteException;
  12. import java.util.ArrayList;
  13. import javax.swing.AbstractAction;
  14. import javax.swing.BoxLayout;
  15. import javax.swing.JComponent;
  16. import javax.swing.JInternalFrame;
  17. import javax.swing.JLayeredPane;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javax.swing.KeyStroke;
  21. import org.apache.log4j.Logger;
  22. import mw.client.managers.ConnectionManager;
  23. import mw.client.managers.ProfileManager;
  24. import mw.client.managers.WindowManager;
  25. import mw.client.utils.cache.SaveObjectUtil;
  26. import mw.mtgforge.AllZone;
  27. import mw.server.list.CardBeanList;
  28. import mw.server.model.MagicWarsModel.GameZone;
  29. import mw.server.model.bean.CardBean;
  30. import mw.server.socket.ZippedObjectImpl;
  31. @SuppressWarnings("serial")
  32. public class MWReveal extends JInternalFrame implements MouseMotionListener {
  33. private static final Logger log = Logger.getLogger(MWReveal.class);
  34. public enum RevealType {
  35. Reveal,
  36. OpponentHand,
  37. LookAt
  38. };
  39. private int headsize;
  40. private JPanel contentPane;
  41. private JLayeredPane layeredPane;
  42. private int revealedSize;
  43. private CardBeanList revealedCards;
  44. private Object cardIsBeingDisplayed;
  45. private double aspectRatio = 1.0;
  46. private RevealType revealType;
  47. public MWReveal(RevealType revealType) {
  48. super("Reveal", false, // resizable
  49. !revealType.equals(RevealType.OpponentHand), // closable if canPlay is false
  50. false, // maximizable
  51. false);// iconifiable
  52. // set the window size
  53. //setSize((int)(140 * aspectRatio), 130);
  54. this.revealType = revealType;
  55. headsize = 16;
  56. // Create and set up the content pane.
  57. contentPane = new JPanel();
  58. contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  59. contentPane.setOpaque(true);
  60. // Create and set up the layered pane.
  61. layeredPane = new JLayeredPane();
  62. layeredPane.setPreferredSize(new Dimension(200, 120));
  63. showCards();
  64. contentPane.add(layeredPane);
  65. setContentPane(contentPane);
  66. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  67. if (revealType.equals(RevealType.OpponentHand)) {
  68. layeredPane.addMouseListener(new MouseAdapter() {
  69. @Override
  70. public void mousePressed(MouseEvent e) {
  71. int count = e.getClickCount();
  72. if (count == 2) {
  73. Object o = layeredPane.getComponentAt(e.getPoint());
  74. if (o instanceof MWCardImpl) {
  75. MWCardImpl mtgCard = (MWCardImpl) o;
  76. WindowManager.getManager().getFlowHand().getInputControl().selectCard(mtgCard.getCard(), GameZone.Hand);
  77. }
  78. }
  79. }
  80. });
  81. }
  82. layeredPane.addMouseMotionListener(new MouseMotionAdapter() {
  83. @Override
  84. public void mouseMoved(MouseEvent e) {
  85. Object o = layeredPane.getComponentAt(e.getPoint());
  86. if (o instanceof MWCardImpl) {
  87. if (!o.equals(cardIsBeingDisplayed)) {
  88. cardIsBeingDisplayed = o;
  89. MWCardImpl mtgCard = (MWCardImpl) o;
  90. mtgCard.updateDescription();
  91. }
  92. }
  93. }
  94. });
  95. layeredPane.addMouseListener(new MouseAdapter() {
  96. @Override
  97. public void mouseExited(MouseEvent e) {
  98. cardIsBeingDisplayed = null;
  99. }
  100. });
  101. /**
  102. * Commands for cycling the cards in the hand
  103. */
  104. getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,
  105. ActionEvent.ALT_MASK), "up");
  106. getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
  107. ActionEvent.ALT_MASK), "down");
  108. getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,
  109. ActionEvent.ALT_MASK), "left");
  110. getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
  111. ActionEvent.ALT_MASK), "right");
  112. getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT,
  113. 0), "alt");
  114. addKeyListener(new KeyAdapter() {
  115. public void keyPressed(KeyEvent e) {
  116. if (e.getKeyCode() == 18) {
  117. JOptionPane.showMessageDialog(null, "Alt");
  118. }
  119. }
  120. });
  121. getActionMap().put("left", new AbstractAction() {
  122. public void actionPerformed(ActionEvent e) {
  123. cycleLeft();
  124. }
  125. private static final long serialVersionUID = 1L;
  126. });
  127. getActionMap().put("right", new AbstractAction() {
  128. public void actionPerformed(ActionEvent e) {
  129. cycleRight();
  130. }
  131. private static final long serialVersionUID = 1L;
  132. });
  133. getActionMap().put("up", new AbstractAction() {
  134. public void actionPerformed(ActionEvent e) {
  135. //cycleLeft();
  136. }
  137. private static final long serialVersionUID = 1L;
  138. });
  139. getActionMap().put("down", new AbstractAction() {
  140. public void actionPerformed(ActionEvent e) {
  141. //cycleRight();
  142. }
  143. private static final long serialVersionUID = 1L;
  144. });
  145. getActionMap().put("alt", new AbstractAction() {
  146. public void actionPerformed(ActionEvent e) {
  147. //cycleRight();
  148. JOptionPane.showMessageDialog(null, "Alt!");
  149. }
  150. private static final long serialVersionUID = 1L;
  151. });
  152. }
  153. private void addCard(CardBean card, int zOrder, JLayeredPane layeredPane) {
  154. MWCardImpl mtgCard = new MWCardImpl(card);
  155. Rectangle rec = new Rectangle(0, headsize * (zOrder),
  156. (int) (130 * aspectRatio), (int) (130 * aspectRatio));
  157. mtgCard.setBounds(rec);
  158. layeredPane.add(mtgCard, Integer.valueOf(zOrder));
  159. }
  160. protected void reorganizeOrders(int cardCount) {
  161. int i = 0;
  162. for (int j = 0; j < revealedSize ; j++) {
  163. CardBean card = revealedCards.get(j);
  164. int k = 0;
  165. while (k < layeredPane.getComponentCount()) {
  166. Object o = layeredPane.getComponent(k);
  167. if (o instanceof MWCardImpl) {
  168. CardBean c = ((MWCardImpl)o).getCard();
  169. if (c.equals(card)) {
  170. break;
  171. }
  172. }
  173. k++;
  174. }
  175. MWCardImpl mtgCard = (MWCardImpl) layeredPane.getComponent(k);
  176. layeredPane.setComponentZOrder(mtgCard, j);
  177. Rectangle rec = new Rectangle(0, headsize * (cardCount - j - 1),
  178. (int)(130 * aspectRatio),
  179. (int)(130 * aspectRatio));
  180. mtgCard.setBounds(rec);
  181. i++;
  182. }
  183. }
  184. public void showCards() {
  185. try {
  186. ZippedObjectImpl<CardBeanList> zipped = null;
  187. if (revealType.equals(RevealType.OpponentHand)) {
  188. zipped = ConnectionManager.getRMIConnection().getRevealedCardsToPlay(ProfileManager.getMyId());
  189. } else if (revealType.equals(RevealType.Reveal)) {
  190. zipped = ConnectionManager.getRMIConnection().getRevealedCards(ProfileManager.getMyId());
  191. } else if (revealType.equals(RevealType.LookAt)) {
  192. zipped = ConnectionManager.getRMIConnection().getViewedCards(ProfileManager.getMyId());
  193. }
  194. if (zipped != null) {
  195. revealedCards = zipped.unzip();
  196. } else {
  197. log.error("reveal: zipped: null.");
  198. }
  199. revealedSize = revealedCards.size();
  200. SaveObjectUtil.saveObject(zipped, ProfileManager.getMyId(), "revealed_viewed");
  201. /**
  202. * Merge hand list changes.
  203. */
  204. mergeCardChanges(revealedCards, layeredPane);
  205. reorganizeOrders(revealedSize);
  206. if (revealType.equals(RevealType.OpponentHand)) {
  207. setTitle("Opp.hand (" + revealedSize + ")");
  208. } else if (revealType.equals(RevealType.Reveal)) {
  209. setTitle("Revealed (" + revealedSize + ")");
  210. } else if (revealType.equals(RevealType.LookAt)) {
  211. setTitle("Look at");
  212. }
  213. /**
  214. * change visual hand size - pack
  215. */
  216. if (revealedSize > 0) {
  217. setSize((int)(139 * aspectRatio),
  218. (int)(130 * aspectRatio) + (revealedSize + 1) * headsize);
  219. }
  220. else {
  221. setSize((int)(140 * aspectRatio), 0);
  222. }
  223. } catch (RemoteException ex) {
  224. ex.printStackTrace();
  225. }
  226. }
  227. /**
  228. * Remove cards from JLayeredPane.
  229. */
  230. protected void mergeCardChanges(CardBeanList cardList, JLayeredPane pane) {
  231. int paneCount = pane.getComponentCount();
  232. int arrayCount = cardList.size();
  233. ArrayList<CardBean> paneCards = new ArrayList<CardBean>();
  234. for (int i = paneCount - 1; i >= 0; i--) {
  235. if (pane.getComponent(i) instanceof MWCardImpl) {
  236. MWCardImpl mtgCard = (MWCardImpl)pane.getComponent(i);
  237. CardBean card = mtgCard.getCard();
  238. paneCards.add(card);
  239. if (!cardList.contains(card)) {
  240. pane.remove(mtgCard);
  241. }
  242. }
  243. }
  244. for (int i = 0; i < arrayCount; i++) {
  245. CardBean card = cardList.get(i);
  246. if (!paneCards.contains(card)) {
  247. addCard(card, i + 1, pane);
  248. }
  249. }
  250. }
  251. public void mouseEntered(MouseEvent e) {
  252. }
  253. public void mouseExited(MouseEvent e) {
  254. }
  255. public void mouseDragged(MouseEvent e) {
  256. }
  257. public void mouseMoved(MouseEvent e) {
  258. }
  259. public void cycleRight() {
  260. MWCardImpl card = (MWCardImpl) layeredPane.getComponent(0);
  261. int i = 0;
  262. while (!revealedCards.get(i).equals(card.getCard()) && i < revealedSize) {
  263. i++;
  264. }
  265. CardBean c = revealedCards.remove(i);
  266. mergeCardChanges(revealedCards, layeredPane);
  267. revealedCards.add(c);
  268. mergeCardChanges(revealedCards, layeredPane);
  269. reorganizeOrders(revealedSize);
  270. revalidate();
  271. repaint();
  272. }
  273. public void cycleLeft() {
  274. MWCardImpl card = (MWCardImpl) layeredPane.getComponent(revealedCards.size() - 1);
  275. int i = 0;
  276. while (!revealedCards.get(i).equals(card.getCard()) && i < revealedSize) {
  277. i++;
  278. }
  279. CardBean c = revealedCards.remove(i);
  280. mergeCardChanges(revealedCards, layeredPane);
  281. revealedCards.add(0, c);
  282. mergeCardChanges(revealedCards, layeredPane);
  283. reorganizeOrders(revealedSize);
  284. revalidate();
  285. repaint();
  286. }
  287. }