use SwingWorker instead of Timer - greatly reduces lag

This commit is contained in:
BetaSteward 2011-05-10 23:54:29 -04:00
parent 346e6d3e8f
commit 0dff1ac743
3 changed files with 71 additions and 50 deletions

View file

@ -2,6 +2,7 @@
<Form version="1.6" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JInternalFrameFormInfo"> <Form version="1.6" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JInternalFrameFormInfo">
<Properties> <Properties>
<Property name="resizable" type="boolean" value="true"/>
<Property name="title" type="java.lang.String" value="Waiting for players"/> <Property name="title" type="java.lang.String" value="Waiting for players"/>
</Properties> </Properties>
<SyntheticProperties> <SyntheticProperties>
@ -39,7 +40,7 @@
<DimensionLayout dim="1"> <DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<Component id="jSplitPane1" pref="255" max="32767" attributes="0"/> <Component id="jSplitPane1" pref="259" max="32767" attributes="0"/>
<EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/> <EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0"> <Group type="103" groupAlignment="3" attributes="0">
<Component id="btnMoveDown" alignment="3" min="-2" max="-2" attributes="0"/> <Component id="btnMoveDown" alignment="3" min="-2" max="-2" attributes="0"/>

View file

@ -35,18 +35,12 @@
package mage.client.dialog; package mage.client.dialog;
import mage.client.*; import mage.client.*;
import java.awt.event.ActionEvent; import java.util.List;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import mage.Constants.TableState;
import mage.client.components.MageComponents; import mage.client.components.MageComponents;
import mage.client.remote.Session; import mage.client.remote.Session;
import mage.util.Logging;
import mage.view.SeatView; import mage.view.SeatView;
import mage.view.TableView; import mage.view.TableView;
@ -54,16 +48,16 @@ import mage.view.TableView;
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public class TableWaitingDialog extends MageDialog implements Observer { public class TableWaitingDialog extends MageDialog {
private final static Logger logger = Logging.getLogger(TableWaitingDialog.class.getName()); // private final static Logger logger = Logging.getLogger(TableWaitingDialog.class.getName());
private UUID tableId; private UUID tableId;
private UUID roomId; private UUID roomId;
private boolean isTournament; private boolean isTournament;
private Session session; private Session session;
private TableWaitModel tableWaitModel; private TableWaitModel tableWaitModel;
private SeatsWatchdog seatsWatchdog = new SeatsWatchdog(); private UpdateSeatsTask updateTask;
/** Creates new form TableWaitingDialog */ /** Creates new form TableWaitingDialog */
public TableWaitingDialog() { public TableWaitingDialog() {
@ -77,10 +71,9 @@ public class TableWaitingDialog extends MageDialog implements Observer {
session.getUI().addButton(MageComponents.TABLE_WAITING_START_BUTTON, btnStart); session.getUI().addButton(MageComponents.TABLE_WAITING_START_BUTTON, btnStart);
} }
@Override // @Override
public void update(Observable arg0, Object arg1) { public void update(TableView table) {
try { try {
TableView table = session.getTable(roomId, tableId);
if (table != null) { if (table != null) {
switch (table.getTableState()) { switch (table.getTableState()) {
case STARTING: case STARTING:
@ -115,6 +108,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
this.tableId = tableId; this.tableId = tableId;
this.isTournament = isTournament; this.isTournament = isTournament;
session = MageFrame.getSession(); session = MageFrame.getSession();
updateTask = new UpdateSeatsTask(session, roomId, tableId, this);
if (session.isTableOwner(roomId, tableId)) { if (session.isTableOwner(roomId, tableId)) {
this.btnStart.setVisible(true); this.btnStart.setVisible(true);
this.btnMoveDown.setVisible(true); this.btnMoveDown.setVisible(true);
@ -127,7 +121,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
UUID chatId = session.getTableChatId(tableId); UUID chatId = session.getTableChatId(tableId);
if (chatId != null) { if (chatId != null) {
this.chatPanel.connect(chatId); this.chatPanel.connect(chatId);
seatsWatchdog.addObserver(this); updateTask.execute();
this.setModal(true); this.setModal(true);
this.setLocation(100, 100); this.setLocation(100, 100);
this.setVisible(true); this.setVisible(true);
@ -138,7 +132,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
} }
public void closeDialog() { public void closeDialog() {
seatsWatchdog.deleteObservers(); updateTask.cancel(true);
this.chatPanel.disconnect(); this.chatPanel.disconnect();
setVisible(false); setVisible(false);
} }
@ -163,6 +157,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
tableSeats = new javax.swing.JTable(); tableSeats = new javax.swing.JTable();
chatPanel = new mage.client.chat.ChatPanel(); chatPanel = new mage.client.chat.ChatPanel();
setResizable(true);
setTitle("Waiting for players"); setTitle("Waiting for players");
btnMoveUp.setText("Move Up"); btnMoveUp.setText("Move Up");
@ -226,7 +221,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
layout.setVerticalGroup( layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE) .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnMoveDown) .addComponent(btnMoveDown)
@ -288,7 +283,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
} }
class TableWaitModel extends AbstractTableModel { class TableWaitModel extends AbstractTableModel {
private String[] columnNames = new String[]{"Seat Num", "Player Id", "Name", "Player Type"}; private String[] columnNames = new String[]{"Seat Num", "Player Name", "Player Type"};
private SeatView[] seats = new SeatView[0]; private SeatView[] seats = new SeatView[0];
public void loadData(TableView table) { public void loadData(TableView table) {
@ -318,10 +313,8 @@ class TableWaitModel extends AbstractTableModel {
case 0: case 0:
return Integer.toString(arg0 + 1); return Integer.toString(arg0 + 1);
case 1: case 1:
return seats[arg0].getPlayerId().toString();
case 2:
return seats[arg0].getPlayerName(); return seats[arg0].getPlayerName();
case 3: case 2:
return seats[arg0].getPlayerType(); return seats[arg0].getPlayerType();
} }
} }
@ -345,25 +338,37 @@ class TableWaitModel extends AbstractTableModel {
@Override @Override
public boolean isCellEditable(int rowIndex, int columnIndex) { public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex != 4) return false;
return false;
return true;
} }
} }
class SeatsWatchdog extends Observable implements ActionListener { class UpdateSeatsTask extends SwingWorker<Void, TableView> {
Timer t = new Timer(1000, this); // check every second private Session session;
private UUID roomId;
private UUID tableId;
private TableWaitingDialog dialog;
public SeatsWatchdog() { UpdateSeatsTask(Session session, UUID roomId, UUID tableId, TableWaitingDialog dialog) {
t.start(); this.session = session;
this.roomId = roomId;
this.tableId = tableId;
this.dialog = dialog;
} }
@Override @Override
public void actionPerformed(ActionEvent arg0) { protected Void doInBackground() throws Exception {
setChanged(); while (!isCancelled()) {
notifyObservers(); this.publish(session.getTable(roomId, tableId));
Thread.sleep(1000);
}
return null;
}
@Override
protected void process(List<TableView> view) {
dialog.update(view.get(0));
} }
} }

View file

@ -34,6 +34,8 @@
package mage.client.table; package mage.client.table;
import java.awt.Color;
import java.awt.Component;
import mage.Constants.MultiplayerAttackOption; import mage.Constants.MultiplayerAttackOption;
import mage.Constants.RangeOfInfluence; import mage.Constants.RangeOfInfluence;
import mage.client.MageFrame; import mage.client.MageFrame;
@ -50,12 +52,13 @@ import mage.util.Logging;
import mage.view.TableView; import mage.view.TableView;
import javax.swing.*; import javax.swing.*;
import javax.swing.Timer;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.util.Collection;
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.client.dialog.NewTournamentDialog; import mage.client.dialog.NewTournamentDialog;
@ -65,13 +68,13 @@ import mage.client.dialog.NewTournamentDialog;
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public class TablesPanel extends javax.swing.JPanel implements Observer { public class TablesPanel extends javax.swing.JPanel {
private final static Logger logger = Logging.getLogger(TablesPanel.class.getName()); private final static Logger logger = Logging.getLogger(TablesPanel.class.getName());
private TableTableModel tableModel; private TableTableModel tableModel;
private UUID roomId; private UUID roomId;
private TablesWatchdog tablesWatchdog = new TablesWatchdog(); private UpdateTablesTask updateTask;
private JoinTableDialog joinTableDialog; private JoinTableDialog joinTableDialog;
private NewTableDialog newTableDialog; private NewTableDialog newTableDialog;
private NewTournamentDialog newTournamentDialog; private NewTournamentDialog newTournamentDialog;
@ -139,10 +142,9 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
return components; return components;
} }
@Override public void update(Collection<TableView> tables) {
public void update(Observable arg0, Object arg1) {
try { try {
tableModel.loadData(MageFrame.getSession().getTables(roomId)); tableModel.loadData(tables);
this.tableTables.repaint(); this.tableTables.repaint();
} catch (Exception ex) { } catch (Exception ex) {
hideTables(); hideTables();
@ -153,6 +155,7 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
this.roomId = roomId; this.roomId = roomId;
session = MageFrame.getSession(); session = MageFrame.getSession();
updateTask = new UpdateTablesTask(session, roomId, this);
if (session != null) { if (session != null) {
btnQuickStart.setVisible(session.isTestMode()); btnQuickStart.setVisible(session.isTestMode());
} }
@ -175,7 +178,7 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
UUID chatRoomId = session.getRoomChatId(roomId); UUID chatRoomId = session.getRoomChatId(roomId);
if (chatRoomId != null) { if (chatRoomId != null) {
this.chatPanel.connect(chatRoomId); this.chatPanel.connect(chatRoomId);
tablesWatchdog.addObserver(this); updateTask.execute();
this.setVisible(true); this.setVisible(true);
this.repaint(); this.repaint();
} }
@ -190,7 +193,7 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
if (tableWaitingDialog.isVisible()) { if (tableWaitingDialog.isVisible()) {
tableWaitingDialog.closeDialog(); tableWaitingDialog.closeDialog();
} }
tablesWatchdog.deleteObservers(); updateTask.cancel(true);
this.chatPanel.disconnect(); this.chatPanel.disconnect();
Component c = this.getParent(); Component c = this.getParent();
@ -423,18 +426,30 @@ class TableTableModel extends AbstractTableModel {
} }
class TablesWatchdog extends Observable implements ActionListener { class UpdateTablesTask extends SwingWorker<Void, Collection<TableView>> {
Timer t = new Timer(1000, this); // check every second private Session session;
private UUID roomId;
private TablesPanel panel;
public TablesWatchdog() { UpdateTablesTask(Session session, UUID roomId, TablesPanel panel) {
t.start(); this.session = session;
this.roomId = roomId;
this.panel = panel;
} }
@Override @Override
public void actionPerformed(ActionEvent arg0) { protected Void doInBackground() throws Exception {
setChanged(); while (!isCancelled()) {
notifyObservers(); this.publish(session.getTables(roomId));
Thread.sleep(1000);
}
return null;
}
@Override
protected void process(List<Collection<TableView>> view) {
panel.update(view.get(0));
} }
} }