From 0a18a7bea6b4e1661f289413f69234cc22ffe156 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sun, 2 Feb 2014 21:57:02 +0100 Subject: [PATCH] Removed majority of memory leaks from game / editor / dialog handling. --- .../src/main/java/mage/client/MageFrame.java | 2 +- .../main/java/mage/client/cards/BigCard.java | 4 ++ .../main/java/mage/client/cards/CardArea.java | 25 ++++--- .../main/java/mage/client/cards/Cards.java | 11 ++- .../mage/client/dialog/AddLandDialog.java | 4 +- .../mage/client/dialog/ExileZoneDialog.java | 6 ++ .../mage/client/dialog/GameEndDialog.java | 3 +- .../java/mage/client/dialog/MageDialog.java | 20 +++--- .../mage/client/dialog/ShowCardsDialog.java | 22 +++--- .../client/dialog/TableWaitingDialog.form | 2 +- .../client/dialog/TableWaitingDialog.java | 30 ++++---- .../java/mage/client/draft/DraftPane.java | 8 +-- .../java/mage/client/draft/DraftPanel.java | 10 ++- .../mage/client/game/BattlefieldPanel.java | 49 +++++++------ .../java/mage/client/game/FeedbackPanel.java | 3 +- .../main/java/mage/client/game/GamePane.java | 8 +-- .../main/java/mage/client/game/GamePanel.java | 53 +++++++++----- .../main/java/mage/client/game/HandPanel.java | 4 ++ .../java/mage/client/game/PlayAreaPanel.java | 70 +++++++++++-------- .../java/mage/client/game/PlayerPanelExt.java | 5 +- .../java/mage/client/util/PhaseManager.java | 2 - .../mage/client/util/SettingsManager.java | 5 +- Mage/src/mage/game/GameState.java | 1 + Mage/src/mage/game/Revealed.java | 6 +- 24 files changed, 215 insertions(+), 138 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/MageFrame.java b/Mage.Client/src/main/java/mage/client/MageFrame.java index c7831e209d..a1023d507c 100644 --- a/Mage.Client/src/main/java/mage/client/MageFrame.java +++ b/Mage.Client/src/main/java/mage/client/MageFrame.java @@ -661,7 +661,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient { public void showGameEndDialog(GameEndView gameEndView) { GameEndDialog gameEndDialog = new GameEndDialog(gameEndView); - desktopPane.add(gameEndDialog, JLayeredPane.POPUP_LAYER); + desktopPane.add(gameEndDialog, JLayeredPane.MODAL_LAYER); gameEndDialog.showDialog(); } diff --git a/Mage.Client/src/main/java/mage/client/cards/BigCard.java b/Mage.Client/src/main/java/mage/client/cards/BigCard.java index f9290b19ae..766eb5a208 100644 --- a/Mage.Client/src/main/java/mage/client/cards/BigCard.java +++ b/Mage.Client/src/main/java/mage/client/cards/BigCard.java @@ -83,6 +83,10 @@ public class BigCard extends JComponent { this.getWidth()*1000/1142,this.getWidth()*1000/2539); } + public void clearUp() { + + } + public void setCard(UUID cardId, Image image, List strings, boolean foil) { if (this.cardId == null || !this.cardId.equals(cardId)) { if (this.panel != null) { diff --git a/Mage.Client/src/main/java/mage/client/cards/CardArea.java b/Mage.Client/src/main/java/mage/client/cards/CardArea.java index 02756b6e37..b35eccc96e 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardArea.java +++ b/Mage.Client/src/main/java/mage/client/cards/CardArea.java @@ -28,6 +28,16 @@ package mage.client.cards; +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Rectangle; +import java.awt.event.MouseListener; +import java.util.List; +import java.util.UUID; +import javax.swing.JLayeredPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; import mage.cards.CardDimensions; import mage.cards.MageCard; import mage.client.plugins.impl.Plugins; @@ -38,12 +48,6 @@ import mage.view.CardView; import mage.view.CardsView; import mage.view.SimpleCardsView; -import javax.swing.*; -import java.awt.*; -import java.awt.event.MouseListener; -import java.util.*; -import java.util.List; - public class CardArea extends JPanel { private boolean reloaded = false; @@ -71,10 +75,12 @@ public class CardArea extends JPanel { public void loadCards(CardsView showCards, BigCard bigCard, CardDimensions dimension, UUID gameId, MouseListener listener) { this.reloaded = true; cardArea.removeAll(); - if (showCards != null && showCards.size() < 10) + if (showCards != null && showCards.size() < 10) { loadCardsFew(showCards, bigCard, gameId, listener); - else + } + else { loadCardsMany(showCards, bigCard, gameId, listener, dimension); + } cardArea.revalidate(); this.revalidate(); @@ -113,8 +119,9 @@ public class CardArea extends JPanel { MageCard cardImg = Plugins.getInstance().getMageCard(card, bigCard, dimension, gameId, true); cardImg.setBounds(rectangle); - if (listener != null) + if (listener != null) { cardImg.addMouseListener(listener); + } cardArea.add(cardImg); cardArea.moveToFront(cardImg); cardImg.update(card); diff --git a/Mage.Client/src/main/java/mage/client/cards/Cards.java b/Mage.Client/src/main/java/mage/client/cards/Cards.java index 740305dcee..474acf3a36 100644 --- a/Mage.Client/src/main/java/mage/client/cards/Cards.java +++ b/Mage.Client/src/main/java/mage/client/cards/Cards.java @@ -44,6 +44,8 @@ import javax.swing.border.Border; import java.awt.*; import java.util.*; import java.util.Map.Entry; +import mage.cards.action.ActionCallback; +import mage.client.plugins.adapters.MageActionCallback; import org.apache.log4j.Logger; /** @@ -86,6 +88,13 @@ public class Cards extends javax.swing.JPanel { } } + public void cleanUp() { + ActionCallback actionCallback = Plugins.getInstance().getActionCallback(); + if (actionCallback instanceof MageActionCallback) { + ((MageActionCallback) actionCallback).setCardPreviewComponent(null); + } + } + /** * Sets components background color * @param color @@ -109,7 +118,7 @@ public class Cards extends javax.swing.JPanel { } } - public boolean loadCards(SimpleCardsView cardsView, BigCard bigCard, UUID gameId) { + public boolean loadCards(SimpleCardsView cardsView, BigCard bigCard, UUID gameId) { return loadCards(CardsViewUtil.convertSimple(cardsView), bigCard, gameId, null); } diff --git a/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java b/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java index 6817a5088a..2b2ee28d31 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java @@ -260,7 +260,7 @@ public class AddLandDialog extends MageDialog { }// //GEN-END:initComponents private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed - this.hideDialog(); + this.removeDialog(); }//GEN-LAST:event_btnCancelActionPerformed private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed @@ -275,7 +275,7 @@ public class AddLandDialog extends MageDialog { addLands("Mountain", nMountain); addLands("Plains", nPlains); addLands("Swamp", nSwamp); - this.hideDialog(); + this.removeDialog(); }//GEN-LAST:event_btnAddActionPerformed private void btnAutoAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAutoAddActionPerformed diff --git a/Mage.Client/src/main/java/mage/client/dialog/ExileZoneDialog.java b/Mage.Client/src/main/java/mage/client/dialog/ExileZoneDialog.java index c72a965d29..9b51b0bc0a 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/ExileZoneDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/ExileZoneDialog.java @@ -38,6 +38,7 @@ import java.beans.PropertyVetoException; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE; import mage.client.cards.BigCard; import mage.client.util.Config; import mage.view.ExileView; @@ -50,10 +51,15 @@ public class ExileZoneDialog extends MageDialog { /** Creates new form ExileZoneDialog */ public ExileZoneDialog() { + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); initComponents(); this.setModal(false); } + public void cleanUp() { + cards.cleanUp(); + } + public void loadCards(ExileView exile, BigCard bigCard, UUID gameId) { this.title = exile.getName(); this.setTitelBarToolTip(exile.getName()); diff --git a/Mage.Client/src/main/java/mage/client/dialog/GameEndDialog.java b/Mage.Client/src/main/java/mage/client/dialog/GameEndDialog.java index bb0bf1813f..3d912880b6 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/GameEndDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/GameEndDialog.java @@ -45,6 +45,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.swing.ImageIcon; +import javax.swing.JLayeredPane; import javax.swing.JOptionPane; import mage.client.MageFrame; import mage.client.game.GamePanel; @@ -120,8 +121,6 @@ public class GameEndDialog extends MageDialog { int winsNeeded = gameEndView.getWinsNeeded() - gameEndView.getWins(); lblMatchInfo.setText(new StringBuilder("You need ").append(winsNeeded == 1 ? "one win ":winsNeeded + " wins ").append("to win the match.").toString()); } - - } private void saveGameLog(GameEndView gameEndView) { diff --git a/Mage.Client/src/main/java/mage/client/dialog/MageDialog.java b/Mage.Client/src/main/java/mage/client/dialog/MageDialog.java index 9caf886692..36bba61e1a 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/MageDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/MageDialog.java @@ -33,15 +33,21 @@ */ package mage.client.dialog; -import org.apache.log4j.Logger; - -import javax.swing.*; -import java.awt.*; +import java.awt.AWTEvent; +import java.awt.ActiveEvent; +import java.awt.Component; +import java.awt.EventQueue; +import java.awt.KeyboardFocusManager; +import java.awt.MenuComponent; +import java.awt.TrayIcon; import java.awt.event.MouseEvent; import java.beans.PropertyVetoException; import java.lang.reflect.InvocationTargetException; import java.util.logging.Level; +import javax.swing.JComponent; +import javax.swing.SwingUtilities; import mage.client.MageFrame; +import org.apache.log4j.Logger; /** * @@ -174,11 +180,7 @@ public class MageDialog extends javax.swing.JInternalFrame { java.util.logging.Logger.getLogger(MageDialog.class.getName()).log(Level.SEVERE, "setClosed(false) failed", ex); } MageFrame.getDesktop().remove(this); - // this.ui.uninstallUI(this); - logger.warn("Remove Dialog " + this.getClass().getName() + " Components left: " + this.getComponentCount()); - for (Component comp : this.getComponents()) { - logger.warn("Existing Component: " + comp.getClass().getName()); - } + } /** diff --git a/Mage.Client/src/main/java/mage/client/dialog/ShowCardsDialog.java b/Mage.Client/src/main/java/mage/client/dialog/ShowCardsDialog.java index 5b41839675..3798251b8d 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/ShowCardsDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/ShowCardsDialog.java @@ -34,6 +34,14 @@ package mage.client.dialog; +import java.awt.Point; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.io.Serializable; +import java.util.Map; +import java.util.UUID; +import javax.swing.JLayeredPane; +import javax.swing.SwingUtilities; import mage.cards.CardDimensions; import mage.client.MageFrame; import mage.client.cards.BigCard; @@ -43,14 +51,7 @@ import mage.client.util.SettingsManager; import mage.client.util.gui.GuiDisplayUtil; import mage.view.CardsView; import mage.view.SimpleCardsView; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.io.Serializable; -import java.util.Map; -import java.util.UUID; +import org.apache.log4j.Logger; /** * @author BetaSteward_at_googlemail.com @@ -63,10 +64,15 @@ public class ShowCardsDialog extends MageDialog implements MouseListener { * Creates new form ShowCardsDialog */ public ShowCardsDialog() { + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); initComponents(); this.setModal(false); } + public void cleanUp() { + + } + public void loadCards(String name, SimpleCardsView showCards, BigCard bigCard, CardDimensions dimension, UUID gameId, boolean modal) { loadCards(name, CardsViewUtil.convertSimple(showCards), bigCard, dimension, gameId, modal); } diff --git a/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.form b/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.form index c50d514089..9f7fe2de9a 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.form +++ b/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.form @@ -40,7 +40,7 @@ - + diff --git a/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.java b/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.java index 7aa4f7003e..06525f2ce7 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/TableWaitingDialog.java @@ -34,22 +34,21 @@ package mage.client.dialog; -import mage.client.MageFrame; -import mage.client.chat.ChatPanel; -import mage.client.components.MageComponents; -import mage.client.components.tray.MageTray; -import mage.remote.Session; -import mage.view.SeatView; -import mage.view.TableView; -import org.apache.log4j.Logger; - -import javax.swing.*; -import javax.swing.table.AbstractTableModel; import java.util.List; import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; +import javax.swing.SwingWorker; +import javax.swing.table.AbstractTableModel; +import mage.client.MageFrame; +import mage.client.chat.ChatPanel; +import mage.client.components.MageComponents; +import mage.client.components.tray.MageTray; import mage.client.util.AudioManager; +import mage.remote.Session; +import mage.view.SeatView; +import mage.view.TableView; +import org.apache.log4j.Logger; /** * @@ -343,8 +342,9 @@ class TableWaitModel extends AbstractTableModel { public String getColumnName(int columnIndex) { String colName = ""; - if (columnIndex <= getColumnCount()) + if (columnIndex <= getColumnCount()) { colName = columnNames[columnIndex]; + } return colName; } @@ -411,15 +411,15 @@ class UpdateSeatsTask extends SwingWorker { } private int getPlayersCount(TableView tableView) { - int count = 0; + int playerCount = 0; if (tableView != null) { for (SeatView seatView: tableView.getSeats()) { if (seatView.getPlayerId() != null && seatView.getPlayerType().equals("Human")) { - count++; + playerCount++; } } } - return count; + return playerCount; } diff --git a/Mage.Client/src/main/java/mage/client/draft/DraftPane.java b/Mage.Client/src/main/java/mage/client/draft/DraftPane.java index ba296511f2..057ec0df0e 100644 --- a/Mage.Client/src/main/java/mage/client/draft/DraftPane.java +++ b/Mage.Client/src/main/java/mage/client/draft/DraftPane.java @@ -35,13 +35,10 @@ package mage.client.draft; import java.awt.Component; -import java.awt.KeyboardFocusManager; -import java.beans.PropertyVetoException; import java.util.HashMap; import java.util.Map; import java.util.UUID; import javax.swing.JComponent; -import mage.client.MageFrame; import mage.client.MagePane; import mage.client.plugins.impl.Plugins; @@ -76,8 +73,9 @@ public class DraftPane extends MagePane { this.draftPanel1.showDraft(draftId); } - public void hideDraft() { - this.hideFrame(); + public void hideDraft() { + draftPanel1.cleanUp(); + this.removeFrame(); } /** This method is called from within the constructor to diff --git a/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java b/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java index 98e4d220de..1b6629f3c5 100644 --- a/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java +++ b/Mage.Client/src/main/java/mage/client/draft/DraftPanel.java @@ -90,9 +90,16 @@ public class DraftPanel extends javax.swing.JPanel { ); } - public void clear() { + public void cleanUp() { draftPicks.clear(); draftBooster.clear(); + + if (countdown != null) { + countdown.stop(); + for (ActionListener al : countdown.getActionListeners()) { + countdown.removeActionListener(al); + } + } } public synchronized void showDraft(UUID draftId) { @@ -162,7 +169,6 @@ public class DraftPanel extends javax.swing.JPanel { } public void hideDraft() { - this.clear(); Component c = this.getParent(); while (c != null && !(c instanceof DraftPane)) { c = c.getParent(); diff --git a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java index 6c2e826f70..a5998ad06d 100644 --- a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java @@ -102,6 +102,11 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { this.bigCard = bigCard; } + public void cleanUp() { + permanents.clear(); + Plugins.getInstance().sortPermanents(ui, permanents.values()); + } + public void update(Map battlefield) { boolean changed = false; @@ -281,28 +286,28 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane { } } - private Rectangle findEmptySpace(Dimension size) { - int battlefieldWidth = this.getWidth(); - Rectangle r = new Rectangle(size); - boolean intersects; - while (true) { - intersects = false; - for (MagePermanent perm: permanents.values()) { - Rectangle pr = perm.getBounds(); - if (r.intersects(pr)) { - intersects = true; - if (pr.x + pr.width + r.width > battlefieldWidth) - r.setLocation(0, pr.y + pr.height + 1); - else - r.translate(pr.x + pr.width - r.x, 0); - break; - } - } - if (!intersects) - break; - } - return r; - } +// private Rectangle findEmptySpace(Dimension size) { +// int battlefieldWidth = this.getWidth(); +// Rectangle r = new Rectangle(size); +// boolean intersects; +// while (true) { +// intersects = false; +// for (MagePermanent perm: permanents.values()) { +// Rectangle pr = perm.getBounds(); +// if (r.intersects(pr)) { +// intersects = true; +// if (pr.x + pr.width + r.width > battlefieldWidth) +// r.setLocation(0, pr.y + pr.height + 1); +// else +// r.translate(pr.x + pr.width - r.x, 0); +// break; +// } +// } +// if (!intersects) +// break; +// } +// return r; +// } @Override public boolean isOptimizedDrawingEnabled () { diff --git a/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java b/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java index 9bb5b44062..9d19115680 100644 --- a/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/FeedbackPanel.java @@ -277,7 +277,8 @@ public class FeedbackPanel extends javax.swing.JPanel { private void btnRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRightActionPerformed this.selected = true; if (connectedDialog != null) { - connectedDialog.hideDialog(); + connectedDialog.removeDialog(); + connectedDialog = null; } if (mode == FeedbackMode.SELECT && (evt.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) { session.sendPlayerInteger(gameId, 0); diff --git a/Mage.Client/src/main/java/mage/client/game/GamePane.java b/Mage.Client/src/main/java/mage/client/game/GamePane.java index 9bb117251d..6a278bb302 100644 --- a/Mage.Client/src/main/java/mage/client/game/GamePane.java +++ b/Mage.Client/src/main/java/mage/client/game/GamePane.java @@ -38,8 +38,6 @@ import java.awt.KeyboardFocusManager; import java.util.UUID; import javax.swing.SwingUtilities; import mage.client.MagePane; -import mage.client.dialog.MageDialog; -import org.apache.log4j.Logger; /** * @@ -47,8 +45,6 @@ import org.apache.log4j.Logger; */ public class GamePane extends MagePane { - private static final Logger logger = Logger.getLogger(MageDialog.class); - /** Creates new form GamePane */ public GamePane() { this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); @@ -70,9 +66,7 @@ public class GamePane extends MagePane { } public void cleanUp() { - gamePanel.cleanUp(); - this.getInputMap().clear(); - this.getActionMap().clear(); + gamePanel.cleanUp(); } public void hideGame() { diff --git a/Mage.Client/src/main/java/mage/client/game/GamePanel.java b/Mage.Client/src/main/java/mage/client/game/GamePanel.java index 0574c215aa..5606d29d03 100644 --- a/Mage.Client/src/main/java/mage/client/game/GamePanel.java +++ b/Mage.Client/src/main/java/mage/client/game/GamePanel.java @@ -46,9 +46,11 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.Serializable; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -127,6 +129,7 @@ public final class GamePanel extends javax.swing.JPanel { private String chosenHandKey = "You"; private boolean smallMode = false; private boolean initialized = false; + private int lastUpdatedTurn; private Map loadedCards = new HashMap(); @@ -204,7 +207,9 @@ public final class GamePanel extends javax.swing.JPanel { this.removeListener(); - for(Map.Entry playAreaPanelEntry: players.entrySet()) { + this.handContainer.cleanUp(); + this.stack.cleanUp(); + for(Map.Entry playAreaPanelEntry: players.entrySet()) { playAreaPanelEntry.getValue().CleanUp(); } this.players.clear(); @@ -215,20 +220,20 @@ public final class GamePanel extends javax.swing.JPanel { jLayeredPane.remove(DialogManager.getManager(gameId)); DialogManager.removeGame(gameId); - this.getInputMap().clear(); - this.getActionMap().clear(); - - // this.pnlBattlefield.removeAll(); - // this.getUI().uninstallUI(this); - if (pickNumber != null) { pickNumber.removeDialog(); } - for (ExileZoneDialog exile: exiles.values()) { - exile.hideDialog(); + for (ExileZoneDialog exileDialog: exiles.values()) { + exileDialog.cleanUp(); + exileDialog.removeDialog(); } - for (ShowCardsDialog reveal: revealed.values()) { - reveal.hideDialog(); + for (ShowCardsDialog revealDialog: revealed.values()) { + revealDialog.cleanUp(); + revealDialog.removeDialog(); + } + for (ShowCardsDialog lookedAtDialog: lookedAt.values()) { + lookedAtDialog.cleanUp(); + lookedAtDialog.removeDialog(); } try { @@ -393,6 +398,7 @@ public final class GamePanel extends javax.swing.JPanel { public synchronized void init(GameView game) { addPlayers(game); updateGame(game); + lastUpdatedTurn = game.getTurn(); } private void addPlayers(GameView game) { @@ -598,16 +604,27 @@ public final class GamePanel extends javax.swing.JPanel { } private void showRevealed(GameView game) { - for (ShowCardsDialog reveal: revealed.values()) { - reveal.clearReloaded(); - } +// List toRemove = new ArrayList(); +// toRemove.addAll(revealed.keySet()); +// for (ShowCardsDialog reveal: revealed.values()) { +// reveal.clearReloaded(); // seems not to be used +// } for (RevealedView reveal: game.getRevealed()) { if (!revealed.containsKey(reveal.getName())) { ShowCardsDialog newReveal = new ShowCardsDialog(); revealed.put(reveal.getName(), newReveal); } revealed.get(reveal.getName()).loadCards("Revealed " + reveal.getName(), CardsViewUtil.convertSimple(reveal.getCards(), loadedCards), bigCard, Config.dimensions, gameId, false); +// toRemove.add(reveal.getName()); } +// for (String revealName: toRemove) { +// ShowCardsDialog revealDialog = revealed.get(revealName); +// if (revealDialog != null) { +// revealed.remove(revealName); +// revealDialog.cleanUp(); +// revealDialog.removeDialog(); +// } +// } } private void showLookedAt(GameView game) { @@ -616,8 +633,8 @@ public final class GamePanel extends javax.swing.JPanel { } for (LookedAtView looked: game.getLookedAt()) { if (!lookedAt.containsKey(looked.getName())) { - ShowCardsDialog newReveal = new ShowCardsDialog(); - lookedAt.put(looked.getName(), newReveal); + ShowCardsDialog newLookedAt = new ShowCardsDialog(); + lookedAt.put(looked.getName(), newLookedAt); } lookedAt.get(looked.getName()).loadCards("Looked at by " + looked.getName(), CardsViewUtil.convertSimple(looked.getCards(), loadedCards), bigCard, Config.dimensions, gameId, false); } @@ -675,7 +692,11 @@ public final class GamePanel extends javax.swing.JPanel { this.feedbackPanel.getFeedback(FeedbackMode.SELECT, messageToDisplay, gameView.getSpecial(), options, messageId); if (PhaseManager.getInstance().isSkip(gameView, message)) { this.feedbackPanel.doClick(); + logger.debug(new StringBuilder("Phase skipped: ").append(message).append(" id: ").append(messageId)); + } else { + logger.debug(new StringBuilder("Phase not skipped: ").append(message).append(" id: ").append(messageId)); } + } public void playMana(String message, GameView gameView, int messageId) { diff --git a/Mage.Client/src/main/java/mage/client/game/HandPanel.java b/Mage.Client/src/main/java/mage/client/game/HandPanel.java index 4f077bcfa2..5cbf9803f3 100644 --- a/Mage.Client/src/main/java/mage/client/game/HandPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/HandPanel.java @@ -58,6 +58,10 @@ public class HandPanel extends JPanel { hand.setZone(Zone.HAND.toString()); } + public void cleanUp() { + hand.cleanUp(); + } + public void loadCards(CardsView cards, BigCard bigCard, UUID gameId) { hand.loadCards(cards, bigCard, gameId, null); hand.sizeCards(getHandCardDimension()); diff --git a/Mage.Client/src/main/java/mage/client/game/PlayAreaPanel.java b/Mage.Client/src/main/java/mage/client/game/PlayAreaPanel.java index 648035972e..62f34bfe7e 100644 --- a/Mage.Client/src/main/java/mage/client/game/PlayAreaPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/PlayAreaPanel.java @@ -27,25 +27,29 @@ */ package mage.client.game; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.lang.reflect.Field; +import java.util.UUID; +import javax.swing.BorderFactory; +import javax.swing.GroupLayout; +import javax.swing.GroupLayout.Alignment; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JPopupMenu; +import javax.swing.LayoutStyle.ComponentPlacement; +import javax.swing.MenuSelectionManager; +import javax.swing.event.ChangeListener; import mage.cards.decks.importer.DeckImporterUtil; import mage.client.MageFrame; import mage.client.cards.BigCard; import mage.view.PlayerView; -import javax.swing.*; -import javax.swing.GroupLayout.Alignment; -import javax.swing.LayoutStyle.ComponentPlacement; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.util.UUID; -import javax.swing.event.MenuKeyListener; -import javax.swing.plaf.basic.BasicPopupMenuUI; - /** * * @author BetaSteward_at_googlemail.com @@ -67,12 +71,14 @@ public class PlayAreaPanel extends javax.swing.JPanel { initComponents(); setOpaque(false); battlefieldPanel.setOpaque(false); + popupMenu = new JPopupMenu(); if (isPlayer) { addPopupMenuPlayer(); } else { addPopupMenuWatcher(); } + this.add(popupMenu); } public PlayAreaPanel(PlayerView player, BigCard bigCard, UUID gameId, boolean me, int priorityTime, boolean isPlayer, GamePanel gamePanel) { @@ -83,28 +89,33 @@ public class PlayAreaPanel extends javax.swing.JPanel { } public void CleanUp() { - for (MouseListener ml :battlefieldPanel.getMainPanel().getMouseListeners()) { - battlefieldPanel.getMainPanel().removeMouseListener(ml); - } + battlefieldPanel.cleanUp(); playerPanel.cleanUp(); - for (KeyListener kl: popupMenu.getKeyListeners()) { - popupMenu.removeKeyListener(kl); - } - for (MenuKeyListener mkl: popupMenu.getMenuKeyListeners()) { - popupMenu.removeMenuKeyListener(mkl); - } + - for (Component child : popupMenu.getComponents()) { - if (child instanceof JMenuItem) { - JMenuItem menuItem = (JMenuItem) child; - for (ActionListener al: menuItem.getActionListeners()) { - menuItem.removeActionListener(al); + for (ActionListener al : btnCheat.getActionListeners() ) { + btnCheat.removeActionListener(al); + } + + // Taken form : https://community.oracle.com/thread/2183145 + // removed the internal focus of a popupMenu data to allow GC before another popup menu is selected + for(ChangeListener listener : MenuSelectionManager.defaultManager().getChangeListeners()) { + if (listener.getClass().getName().contains("MenuKeyboardHelper")) { + try { + Field field = listener.getClass().getDeclaredField("menuInputMap"); + field.setAccessible(true); + field.set(listener, null); + } catch (Exception e) { + // ignored } + break; } } + for (MouseListener ml :battlefieldPanel.getMainPanel().getMouseListeners()) { battlefieldPanel.getMainPanel().removeMouseListener(ml); } + popupMenu.getUI().uninstallUI(this); } @@ -199,6 +210,7 @@ public class PlayAreaPanel extends javax.swing.JPanel { } } }); + } private void addPopupMenuWatcher() { @@ -217,7 +229,6 @@ public class PlayAreaPanel extends javax.swing.JPanel { } } }); - battlefieldPanel.getMainPanel().addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent Me) { @@ -226,6 +237,7 @@ public class PlayAreaPanel extends javax.swing.JPanel { } } }); + } public final void init(PlayerView player, BigCard bigCard, UUID gameId, int priorityTime) { diff --git a/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java b/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java index 1a8370a18f..0b7b6d07b9 100644 --- a/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java +++ b/Mage.Client/src/main/java/mage/client/game/PlayerPanelExt.java @@ -153,7 +153,10 @@ public class PlayerPanelExt extends javax.swing.JPanel { } public void cleanUp() { - this.timer.cancel(); + if (timer != null) { + this.timer.cancel(); + } + topCardPanel.updateCallback(null, gameId); } public void update(PlayerView player) { diff --git a/Mage.Client/src/main/java/mage/client/util/PhaseManager.java b/Mage.Client/src/main/java/mage/client/util/PhaseManager.java index b087ca34d4..0e866b4d8c 100644 --- a/Mage.Client/src/main/java/mage/client/util/PhaseManager.java +++ b/Mage.Client/src/main/java/mage/client/util/PhaseManager.java @@ -104,8 +104,6 @@ public class PhaseManager { if (prefKey != null) { String prop = prefs.get(prefKey, PHASE_ON); return !prop.equals(PHASE_ON); - } else { - logger.warn(new StringBuilder("Message not found: ").append(message)); } return false; } diff --git a/Mage.Client/src/main/java/mage/client/util/SettingsManager.java b/Mage.Client/src/main/java/mage/client/util/SettingsManager.java index a5b529691f..3feaab54e5 100644 --- a/Mage.Client/src/main/java/mage/client/util/SettingsManager.java +++ b/Mage.Client/src/main/java/mage/client/util/SettingsManager.java @@ -1,10 +1,9 @@ package mage.client.util; - +import java.awt.Point; +import java.awt.Rectangle; import org.mage.card.arcane.CardPanel; -import java.awt.*; - /** * Contains dynamic settings for client. * diff --git a/Mage/src/mage/game/GameState.java b/Mage/src/mage/game/GameState.java index cb86bd6a3c..3e3b0abb01 100644 --- a/Mage/src/mage/game/GameState.java +++ b/Mage/src/mage/game/GameState.java @@ -88,6 +88,7 @@ public class GameState implements Serializable, Copyable { private final Players players; private final PlayerList playerList; private final Turn turn; + // revealed cards >, will be reset if all players pass priority private final Revealed revealed; private final Map lookedAt = new HashMap(); private final DelayedTriggeredAbilities delayed; diff --git a/Mage/src/mage/game/Revealed.java b/Mage/src/mage/game/Revealed.java index 472ae193b9..fd0df46ce2 100644 --- a/Mage/src/mage/game/Revealed.java +++ b/Mage/src/mage/game/Revealed.java @@ -56,8 +56,9 @@ public class Revealed extends HashMap implements Serializable, Co } public void add(String name, Cards cards) { - if (!this.containsKey(name)) + if (!this.containsKey(name)) { createRevealed(name); + } this.put(name, cards.copy()); } @@ -78,8 +79,9 @@ public class Revealed extends HashMap implements Serializable, Co public Card getCard(UUID cardId, Game game) { for (Cards cards: this.values()) { - if (cards.contains(cardId)) + if (cards.contains(cardId)) { return game.getCard(cardId); + } } return null; }