From 32557fdf64af8c32576ca29c778bb4fba7ce5ee8 Mon Sep 17 00:00:00 2001 From: lodici Date: Tue, 7 Feb 2017 12:54:25 +0000 Subject: [PATCH] query PWL stats only once per deck using PwlWorker. --- src/magic/ui/screen/deck/DeckScreenPanel.java | 16 +++++- .../ui/screen/deck/editor/ContentPanel.java | 9 ++++ .../ui/screen/deck/editor/DeckSideBar.java | 12 +++-- .../ui/screen/deck/editor/MainViewsPanel.java | 17 ++++--- .../ui/screen/decks/DecksScreenPanel.java | 18 ++++--- src/magic/ui/screen/decks/SidebarPanel.java | 14 +++-- .../ui/screen/duel/decks/DuelDecksPanel.java | 19 ++++++- src/magic/ui/widget/deck/DeckInfoPanel.java | 4 ++ .../deck/stats/DeckStatisticsViewer.java | 34 +------------ .../widget/deck/stats/IPwlWorkerListener.java | 5 ++ src/magic/ui/widget/deck/stats/PwlWorker.java | 51 +++++++++++++++++++ 11 files changed, 142 insertions(+), 57 deletions(-) create mode 100644 src/magic/ui/widget/deck/stats/IPwlWorkerListener.java create mode 100644 src/magic/ui/widget/deck/stats/PwlWorker.java diff --git a/src/magic/ui/screen/deck/DeckScreenPanel.java b/src/magic/ui/screen/deck/DeckScreenPanel.java index 9c4c1c8410..afc5f405a7 100644 --- a/src/magic/ui/screen/deck/DeckScreenPanel.java +++ b/src/magic/ui/screen/deck/DeckScreenPanel.java @@ -7,15 +7,18 @@ import magic.model.MagicCardDefinition; import magic.model.MagicDeck; import magic.ui.screen.deck.editor.DeckSideBar; import magic.ui.widget.cards.table.CardTablePanelA; +import magic.ui.widget.deck.stats.IPwlWorkerListener; +import magic.ui.widget.deck.stats.PwlWorker; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") -class DeckScreenPanel extends JPanel { +class DeckScreenPanel extends JPanel implements IPwlWorkerListener { private MagicDeck deck; private final MigLayout migLayout = new MigLayout(); private final DeckSideBar sideBarPanel; private final CardTablePanelA deckTable; + private PwlWorker pwlWorker; DeckScreenPanel(final MagicDeck aDeck, final MagicCardDefinition selectedCard) { @@ -86,6 +89,17 @@ class DeckScreenPanel extends JPanel { void setDeck(MagicDeck aDeck) { this.deck = aDeck; + doPWLStatsQuery(deck); } + @Override + public void setPlayedWonLost(String pwl) { + sideBarPanel.setPlayedWonLost(pwl); + } + + private void doPWLStatsQuery(MagicDeck deck) { + pwlWorker = new PwlWorker(deck); + pwlWorker.setListeners(sideBarPanel); + pwlWorker.execute(); + } } diff --git a/src/magic/ui/screen/deck/editor/ContentPanel.java b/src/magic/ui/screen/deck/editor/ContentPanel.java index 0f4e83ec85..296ecf4947 100644 --- a/src/magic/ui/screen/deck/editor/ContentPanel.java +++ b/src/magic/ui/screen/deck/editor/ContentPanel.java @@ -7,6 +7,7 @@ import magic.model.MagicDeck; import magic.model.MagicDeckConstructionRule; import magic.translate.MText; import magic.ui.ScreenController; +import magic.ui.widget.deck.stats.PwlWorker; import magic.utility.MagicSystem; import net.miginfocom.swing.MigLayout; @@ -24,6 +25,7 @@ class ContentPanel extends JPanel implements IDeckEditorListener { private final MainViewsPanel viewsPanel; // RHS private final IDeckEditorListener listener; private boolean isStandalone = true; + private PwlWorker pwlWorker; ContentPanel(IDeckEditorListener aListener) { @@ -96,6 +98,7 @@ class ContentPanel extends JPanel implements IDeckEditorListener { CONFIG.save(); } listener.deckUpdated(deck); + doPWLStatsQuery(deck); } @Override @@ -114,4 +117,10 @@ class ContentPanel extends JPanel implements IDeckEditorListener { throw new UnsupportedOperationException("Not supported yet."); } + private void doPWLStatsQuery(MagicDeck deck) { + pwlWorker = new PwlWorker(deck); + pwlWorker.setListeners(sideBarPanel, viewsPanel); + pwlWorker.execute(); + } + } diff --git a/src/magic/ui/screen/deck/editor/DeckSideBar.java b/src/magic/ui/screen/deck/editor/DeckSideBar.java index 3067283b82..dddee38946 100644 --- a/src/magic/ui/screen/deck/editor/DeckSideBar.java +++ b/src/magic/ui/screen/deck/editor/DeckSideBar.java @@ -6,14 +6,15 @@ import javax.swing.BorderFactory; import magic.model.MagicCardDefinition; import magic.model.MagicDeck; import magic.ui.widget.M.MScrollPane; -import magic.ui.widget.duel.viewer.CardViewer; -import magic.ui.widget.duel.viewer.DeckEditorCardViewer; import magic.ui.widget.TexturedPanel; import magic.ui.widget.deck.DeckInfoPanel; +import magic.ui.widget.deck.stats.IPwlWorkerListener; +import magic.ui.widget.duel.viewer.CardViewer; +import magic.ui.widget.duel.viewer.DeckEditorCardViewer; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") -public class DeckSideBar extends TexturedPanel { +public class DeckSideBar extends TexturedPanel implements IPwlWorkerListener { private final MigLayout migLayout = new MigLayout(); private final MScrollPane cardScrollPane = new MScrollPane(); @@ -68,4 +69,9 @@ public class DeckSideBar extends TexturedPanel { return cardViewer; } + @Override + public void setPlayedWonLost(String pwl) { + deckInfo.setPlayedWonLost(pwl); + } + } diff --git a/src/magic/ui/screen/deck/editor/MainViewsPanel.java b/src/magic/ui/screen/deck/editor/MainViewsPanel.java index 2ee337160a..3dfa3c3598 100644 --- a/src/magic/ui/screen/deck/editor/MainViewsPanel.java +++ b/src/magic/ui/screen/deck/editor/MainViewsPanel.java @@ -6,7 +6,6 @@ import javax.swing.AbstractAction; import javax.swing.JPanel; import javax.swing.JToggleButton; import magic.data.GeneralConfig; -import magic.data.stats.MagicStats; import magic.model.MagicCardDefinition; import magic.model.MagicDeck; import magic.translate.MText; @@ -15,10 +14,12 @@ import magic.ui.ScreenController; import magic.ui.helpers.MouseHelper; import magic.ui.screen.deck.editor.stats.DeckStatsPanel; import magic.ui.widget.cards.table.CardsJTable; +import magic.ui.widget.deck.stats.IPwlWorkerListener; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") -class MainViewsPanel extends JPanel implements IDeckEditorListener { +class MainViewsPanel extends JPanel + implements IDeckEditorListener, IPwlWorkerListener { // translatable strings private static final String _S1 = "Deck"; @@ -132,7 +133,7 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener { }); } - statsToggleButton = toggleButtonsPanel.addToggleButton(getStatsTabCaption(), new AbstractAction() { + statsToggleButton = toggleButtonsPanel.addToggleButton("", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { MouseHelper.showBusyCursor((Component) e.getSource()); @@ -146,10 +147,6 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener { toggleButtonsPanel.refreshLayout(); } - private String getStatsTabCaption() { - return MagicStats.getPlayedWonLost(controller.getDeck()); - } - private void addPropertyChangeListeners() { // DeckPanel @@ -242,7 +239,6 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener { deckPanel.doRefreshView(); legalityPanel.setDeck(controller.getDeck()); statsPanel.setDeck(controller.getDeck()); - statsToggleButton.setText(getStatsTabCaption()); activeView.notifyShowing(); } @@ -267,4 +263,9 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener { doRefreshViews(); } + @Override + public void setPlayedWonLost(String pwl) { + statsToggleButton.setText(pwl); + } + } diff --git a/src/magic/ui/screen/decks/DecksScreenPanel.java b/src/magic/ui/screen/decks/DecksScreenPanel.java index e33cf5d43d..49a0414019 100644 --- a/src/magic/ui/screen/decks/DecksScreenPanel.java +++ b/src/magic/ui/screen/decks/DecksScreenPanel.java @@ -9,6 +9,7 @@ import magic.model.MagicDeck; import magic.ui.screen.interfaces.IDeckConsumer; import magic.ui.widget.cards.table.CardTablePanelB; import magic.ui.widget.deck.DeckStatusPanel; +import magic.ui.widget.deck.stats.PwlWorker; import magic.ui.widget.duel.viewer.CardViewer; import magic.utility.DeckUtils; import net.miginfocom.swing.MigLayout; @@ -23,6 +24,7 @@ class DecksScreenPanel extends JPanel implements IDeckConsumer { private final JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT); private final DeckStatusPanel deckStatusPanel; private final SidebarPanel sidebar; + private PwlWorker pwlWorker; DecksScreenPanel(DeckStatusPanel deckStatusPanel) { @@ -74,7 +76,7 @@ class DecksScreenPanel extends JPanel implements IDeckConsumer { @Override public void setDeck(String deckName, DeckType deckType) { - System.out.println("setDeck(" + deckName + ", " + deckType + ")"); + throw new UnsupportedOperationException("setDeck(" + deckName + ", " + deckType + ")"); } @Override @@ -94,17 +96,19 @@ class DecksScreenPanel extends JPanel implements IDeckConsumer { deckStatusPanel.setDeck(null, false); splitter.setVisible(false); } + doPWLStatsQuery(deck); return true; } @Override public void setDeck(MagicDeck deck) { - selectedDeck = deck; - deckFilePath = DeckUtils.getDeckPath(deck); - sidebar.setDeck(deck); - deckTable.setCards(deck); - deckStatusPanel.setDeck(deck, deck.isValid() || deck.size() > 0); - splitter.setVisible(deck.isValid() || deck.size() > 0); + setDeck(deck, DeckUtils.getDeckPath(deck)); + } + + private void doPWLStatsQuery(MagicDeck deck) { + pwlWorker = new PwlWorker(deck); + pwlWorker.setListeners(sidebar); + pwlWorker.execute(); } } diff --git a/src/magic/ui/screen/decks/SidebarPanel.java b/src/magic/ui/screen/decks/SidebarPanel.java index df60d79c09..91d0f5da6a 100644 --- a/src/magic/ui/screen/decks/SidebarPanel.java +++ b/src/magic/ui/screen/decks/SidebarPanel.java @@ -4,16 +4,17 @@ import java.awt.Color; import java.awt.Dimension; import javax.swing.BorderFactory; import magic.model.MagicDeck; +import magic.ui.FontsAndBorders; +import magic.ui.screen.interfaces.IDeckConsumer; +import magic.ui.widget.TexturedPanel; import magic.ui.widget.deck.DeckInfoPanel; import magic.ui.widget.deck.DeckPicker; +import magic.ui.widget.deck.stats.IPwlWorkerListener; import magic.ui.widget.duel.viewer.CardViewer; -import magic.ui.screen.interfaces.IDeckConsumer; -import magic.ui.FontsAndBorders; -import magic.ui.widget.TexturedPanel; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") -class SidebarPanel extends TexturedPanel { +class SidebarPanel extends TexturedPanel implements IPwlWorkerListener { private final DeckPicker deckPicker; private final DeckInfoPanel deckInfo; @@ -53,4 +54,9 @@ class SidebarPanel extends TexturedPanel { deckInfo.setDeck(deck); } + @Override + public void setPlayedWonLost(String pwl) { + deckInfo.setPlayedWonLost(pwl); + } + } diff --git a/src/magic/ui/screen/duel/decks/DuelDecksPanel.java b/src/magic/ui/screen/duel/decks/DuelDecksPanel.java index 198d6dbb97..425235c3ee 100644 --- a/src/magic/ui/screen/duel/decks/DuelDecksPanel.java +++ b/src/magic/ui/screen/duel/decks/DuelDecksPanel.java @@ -30,10 +30,13 @@ import magic.ui.screen.widget.ActionBarButton; import magic.ui.screen.widget.MenuButton; import magic.ui.widget.TexturedPanel; import magic.ui.widget.cards.table.CardTablePanelB; +import magic.ui.widget.deck.stats.IPwlWorkerListener; +import magic.ui.widget.deck.stats.PwlWorker; import net.miginfocom.swing.MigLayout; @SuppressWarnings("serial") -class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener { +class DuelDecksPanel extends TexturedPanel + implements IPlayerProfileListener, IPwlWorkerListener { // translatable strings private static final String _S7 = "Swap Decks"; @@ -50,8 +53,8 @@ class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener { private final CardTablePanelB[] cardTables; private final DeckSideBar sidebar; private final ActionBarButton newDeckButton; - private boolean isTabChanged = false; + private PwlWorker pwlWorker; DuelDecksPanel(final MagicDuel duel) { @@ -128,6 +131,7 @@ class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener { DuelPlayerConfig player = getSelectedPlayer(); MagicDeck deck = player.getDeck(); sidebar.setDeck(deck); + doPWLStatsQuery(deck); sidebar.setCard(deck.get(0)); boolean isRandomDeck = player.getDeckProfile().getDeckType() == DeckType.Random; newDeckButton.setEnabled(isRandomDeck && duel.getGamesPlayed() == 0); @@ -275,4 +279,15 @@ class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener { cardTables[0].setStyle(); cardTables[1].setStyle(); } + + @Override + public void setPlayedWonLost(String pwl) { + sidebar.setPlayedWonLost(pwl); + } + + private void doPWLStatsQuery(MagicDeck deck) { + pwlWorker = new PwlWorker(deck); + pwlWorker.setListeners(sidebar); + pwlWorker.execute(); + } } diff --git a/src/magic/ui/widget/deck/DeckInfoPanel.java b/src/magic/ui/widget/deck/DeckInfoPanel.java index 1cec609eab..52a0999b76 100644 --- a/src/magic/ui/widget/deck/DeckInfoPanel.java +++ b/src/magic/ui/widget/deck/DeckInfoPanel.java @@ -49,4 +49,8 @@ public class DeckInfoPanel extends TexturedPanel { descViewer.setDeckDescription(aDeck); } + public void setPlayedWonLost(String pwl) { + statsViewer.setPlayedWonLost(pwl); + } + } diff --git a/src/magic/ui/widget/deck/stats/DeckStatisticsViewer.java b/src/magic/ui/widget/deck/stats/DeckStatisticsViewer.java index a536743ce5..028dab5de9 100644 --- a/src/magic/ui/widget/deck/stats/DeckStatisticsViewer.java +++ b/src/magic/ui/widget/deck/stats/DeckStatisticsViewer.java @@ -3,21 +3,16 @@ package magic.ui.widget.deck.stats; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CancellationException; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; -import javax.swing.SwingWorker; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import magic.data.CardStatistics; import magic.data.GeneralConfig; import magic.data.MagicIcon; -import magic.data.stats.MagicStats; import magic.model.DuelPlayerConfig; import magic.model.MagicDeck; import magic.translate.MText; @@ -36,13 +31,10 @@ public class DeckStatisticsViewer extends JPanel implements ChangeListener { private static final String _S1 = "Deck Statistics"; private static final String _S2 = "%d card deck"; - private static final Logger LOGGER = Logger.getLogger(DeckStatisticsViewer.class.getName()); - private final ActionButtonTitleBar titleBar; private final ManaCurvePanel manaCurvePanel; private final ActionBarButton titlebarButton; private final StatsTable statsTable; - private SwingWorker pwlWorker; private String pwl = ""; private CardStatistics statistics; private MagicDeck thisDeck; @@ -127,39 +119,17 @@ public class DeckStatisticsViewer extends JPanel implements ChangeListener { manaCurvePanel.setStats(statistics); if (isNewDeck(aDeck)) { this.thisDeck = aDeck; - doStatsQueryPWL(aDeck); } - setPlayedWinLost(pwl); + setPlayedWonLost(pwl); } - private void setPlayedWinLost(String newPWL) { + public void setPlayedWonLost(String newPWL) { pwl = newPWL; titleBar.setText(MText.get(_S2, statistics.totalCards) + (!newPWL.isEmpty() ? " • " + newPWL : "") ); } - private void doStatsQueryPWL(MagicDeck aDeck) { - final MagicDeck deckCopy = new MagicDeck(aDeck); - pwlWorker = new SwingWorker() { - @Override - protected String doInBackground() throws Exception { - return MagicStats.getPlayedWonLost(deckCopy); - } - @Override - protected void done() { - try { - setPlayedWinLost(get()); - } catch (CancellationException ex) { - LOGGER.log(Level.INFO, "pwlWorker cancelled."); - } catch (Exception ex) { - LOGGER.log(Level.SEVERE, null, ex); - } - } - }; - pwlWorker.execute(); - } - @Override public void stateChanged(final ChangeEvent event) { setDeck(((DuelPlayerConfig)event.getSource()).getDeck()); diff --git a/src/magic/ui/widget/deck/stats/IPwlWorkerListener.java b/src/magic/ui/widget/deck/stats/IPwlWorkerListener.java new file mode 100644 index 0000000000..4f8791aa6f --- /dev/null +++ b/src/magic/ui/widget/deck/stats/IPwlWorkerListener.java @@ -0,0 +1,5 @@ +package magic.ui.widget.deck.stats; + +public interface IPwlWorkerListener { + public void setPlayedWonLost(String pwl); +} diff --git a/src/magic/ui/widget/deck/stats/PwlWorker.java b/src/magic/ui/widget/deck/stats/PwlWorker.java new file mode 100644 index 0000000000..34e97ae6c8 --- /dev/null +++ b/src/magic/ui/widget/deck/stats/PwlWorker.java @@ -0,0 +1,51 @@ +package magic.ui.widget.deck.stats; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CancellationException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; +import magic.data.stats.MagicStats; +import magic.model.MagicDeck; + +public class PwlWorker extends SwingWorker { + + private static final Logger LOGGER = Logger.getLogger(PwlWorker.class.getName()); + + private final MagicDeck deckCopy; + private final List listeners = new ArrayList<>(); + + public PwlWorker(MagicDeck deck) { + deckCopy = new MagicDeck(deck); + } + + public void setListeners(IPwlWorkerListener... newListeners) { + listeners.clear(); + listeners.addAll(Arrays.asList(newListeners)); + } + + @Override + protected String doInBackground() throws Exception { + return MagicStats.getPlayedWonLost(deckCopy); + } + + @Override + protected void done() { + try { + final String pwl = get(); + for (IPwlWorkerListener listener : listeners) { + SwingUtilities.invokeLater(() -> { + listener.setPlayedWonLost(pwl); + }); + } + System.out.println("PwlWorker.done() : " + pwl); + } catch (CancellationException ex) { + LOGGER.log(Level.INFO, "pwlWorker cancelled."); + } catch (Exception ex) { + LOGGER.log(Level.SEVERE, null, ex); + } + } +}