mirror of
https://github.com/magefree/mage.git
synced 2025-12-23 12:02:01 -08:00
use SwingWorker instead of Timer - greatly reduces lag
This commit is contained in:
parent
346e6d3e8f
commit
0dff1ac743
3 changed files with 71 additions and 50 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
<Form version="1.6" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JInternalFrameFormInfo">
|
||||
<Properties>
|
||||
<Property name="resizable" type="boolean" value="true"/>
|
||||
<Property name="title" type="java.lang.String" value="Waiting for players"/>
|
||||
</Properties>
|
||||
<SyntheticProperties>
|
||||
|
|
@ -39,7 +40,7 @@
|
|||
<DimensionLayout dim="1">
|
||||
<Group type="103" groupAlignment="0" 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"/>
|
||||
<Group type="103" groupAlignment="3" attributes="0">
|
||||
<Component id="btnMoveDown" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
|
|
|
|||
|
|
@ -35,18 +35,12 @@
|
|||
package mage.client.dialog;
|
||||
|
||||
import mage.client.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.Timer;
|
||||
import javax.swing.SwingWorker;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import mage.Constants.TableState;
|
||||
import mage.client.components.MageComponents;
|
||||
import mage.client.remote.Session;
|
||||
import mage.util.Logging;
|
||||
import mage.view.SeatView;
|
||||
import mage.view.TableView;
|
||||
|
||||
|
|
@ -54,16 +48,16 @@ import mage.view.TableView;
|
|||
*
|
||||
* @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 roomId;
|
||||
private boolean isTournament;
|
||||
private Session session;
|
||||
private TableWaitModel tableWaitModel;
|
||||
private SeatsWatchdog seatsWatchdog = new SeatsWatchdog();
|
||||
private UpdateSeatsTask updateTask;
|
||||
|
||||
/** Creates new form TableWaitingDialog */
|
||||
public TableWaitingDialog() {
|
||||
|
|
@ -77,10 +71,9 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
session.getUI().addButton(MageComponents.TABLE_WAITING_START_BUTTON, btnStart);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable arg0, Object arg1) {
|
||||
// @Override
|
||||
public void update(TableView table) {
|
||||
try {
|
||||
TableView table = session.getTable(roomId, tableId);
|
||||
if (table != null) {
|
||||
switch (table.getTableState()) {
|
||||
case STARTING:
|
||||
|
|
@ -115,6 +108,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
this.tableId = tableId;
|
||||
this.isTournament = isTournament;
|
||||
session = MageFrame.getSession();
|
||||
updateTask = new UpdateSeatsTask(session, roomId, tableId, this);
|
||||
if (session.isTableOwner(roomId, tableId)) {
|
||||
this.btnStart.setVisible(true);
|
||||
this.btnMoveDown.setVisible(true);
|
||||
|
|
@ -127,7 +121,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
UUID chatId = session.getTableChatId(tableId);
|
||||
if (chatId != null) {
|
||||
this.chatPanel.connect(chatId);
|
||||
seatsWatchdog.addObserver(this);
|
||||
updateTask.execute();
|
||||
this.setModal(true);
|
||||
this.setLocation(100, 100);
|
||||
this.setVisible(true);
|
||||
|
|
@ -138,7 +132,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
}
|
||||
|
||||
public void closeDialog() {
|
||||
seatsWatchdog.deleteObservers();
|
||||
updateTask.cancel(true);
|
||||
this.chatPanel.disconnect();
|
||||
setVisible(false);
|
||||
}
|
||||
|
|
@ -163,6 +157,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
tableSeats = new javax.swing.JTable();
|
||||
chatPanel = new mage.client.chat.ChatPanel();
|
||||
|
||||
setResizable(true);
|
||||
setTitle("Waiting for players");
|
||||
|
||||
btnMoveUp.setText("Move Up");
|
||||
|
|
@ -226,7 +221,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.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)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(btnMoveDown)
|
||||
|
|
@ -288,7 +283,7 @@ public class TableWaitingDialog extends MageDialog implements Observer {
|
|||
}
|
||||
|
||||
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];
|
||||
|
||||
public void loadData(TableView table) {
|
||||
|
|
@ -318,10 +313,8 @@ class TableWaitModel extends AbstractTableModel {
|
|||
case 0:
|
||||
return Integer.toString(arg0 + 1);
|
||||
case 1:
|
||||
return seats[arg0].getPlayerId().toString();
|
||||
case 2:
|
||||
return seats[arg0].getPlayerName();
|
||||
case 3:
|
||||
case 2:
|
||||
return seats[arg0].getPlayerType();
|
||||
}
|
||||
}
|
||||
|
|
@ -345,25 +338,37 @@ class TableWaitModel extends AbstractTableModel {
|
|||
|
||||
@Override
|
||||
public boolean isCellEditable(int rowIndex, int columnIndex) {
|
||||
if (columnIndex != 4)
|
||||
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() {
|
||||
t.start();
|
||||
UpdateSeatsTask(Session session, UUID roomId, UUID tableId, TableWaitingDialog dialog) {
|
||||
this.session = session;
|
||||
this.roomId = roomId;
|
||||
this.tableId = tableId;
|
||||
this.dialog = dialog;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
protected Void doInBackground() throws Exception {
|
||||
while (!isCancelled()) {
|
||||
this.publish(session.getTable(roomId, tableId));
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<TableView> view) {
|
||||
dialog.update(view.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -34,6 +34,8 @@
|
|||
|
||||
package mage.client.table;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.client.MageFrame;
|
||||
|
|
@ -50,12 +52,13 @@ import mage.util.Logging;
|
|||
import mage.view.TableView;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.Timer;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
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.Logger;
|
||||
import mage.client.dialog.NewTournamentDialog;
|
||||
|
|
@ -65,13 +68,13 @@ import mage.client.dialog.NewTournamentDialog;
|
|||
*
|
||||
* @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 TableTableModel tableModel;
|
||||
private UUID roomId;
|
||||
private TablesWatchdog tablesWatchdog = new TablesWatchdog();
|
||||
private UpdateTablesTask updateTask;
|
||||
private JoinTableDialog joinTableDialog;
|
||||
private NewTableDialog newTableDialog;
|
||||
private NewTournamentDialog newTournamentDialog;
|
||||
|
|
@ -139,10 +142,9 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
|
|||
return components;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable arg0, Object arg1) {
|
||||
public void update(Collection<TableView> tables) {
|
||||
try {
|
||||
tableModel.loadData(MageFrame.getSession().getTables(roomId));
|
||||
tableModel.loadData(tables);
|
||||
this.tableTables.repaint();
|
||||
} catch (Exception ex) {
|
||||
hideTables();
|
||||
|
|
@ -153,6 +155,7 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
|
|||
|
||||
this.roomId = roomId;
|
||||
session = MageFrame.getSession();
|
||||
updateTask = new UpdateTablesTask(session, roomId, this);
|
||||
if (session != null) {
|
||||
btnQuickStart.setVisible(session.isTestMode());
|
||||
}
|
||||
|
|
@ -175,7 +178,7 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
|
|||
UUID chatRoomId = session.getRoomChatId(roomId);
|
||||
if (chatRoomId != null) {
|
||||
this.chatPanel.connect(chatRoomId);
|
||||
tablesWatchdog.addObserver(this);
|
||||
updateTask.execute();
|
||||
this.setVisible(true);
|
||||
this.repaint();
|
||||
}
|
||||
|
|
@ -190,7 +193,7 @@ public class TablesPanel extends javax.swing.JPanel implements Observer {
|
|||
if (tableWaitingDialog.isVisible()) {
|
||||
tableWaitingDialog.closeDialog();
|
||||
}
|
||||
tablesWatchdog.deleteObservers();
|
||||
updateTask.cancel(true);
|
||||
this.chatPanel.disconnect();
|
||||
|
||||
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() {
|
||||
t.start();
|
||||
UpdateTablesTask(Session session, UUID roomId, TablesPanel panel) {
|
||||
this.session = session;
|
||||
this.roomId = roomId;
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
protected Void doInBackground() throws Exception {
|
||||
while (!isCancelled()) {
|
||||
this.publish(session.getTables(roomId));
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<Collection<TableView>> view) {
|
||||
panel.update(view.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue