GUI: fixed that server's lobby do not remember divider position after app restart (tables, matches and chat sizes)

This commit is contained in:
Oleg Agafonov 2024-06-23 15:55:46 +04:00
parent f78177a540
commit f0c38cdb87
7 changed files with 36 additions and 32 deletions

View file

@ -324,9 +324,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
// create default server lobby and hide it until connect // create default server lobby and hide it until connect
tablesPane = new TablesPane(); tablesPane = new TablesPane();
desktopPane.add(tablesPane, javax.swing.JLayeredPane.DEFAULT_LAYER); desktopPane.add(tablesPane, javax.swing.JLayeredPane.DEFAULT_LAYER);
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(this::hideServerLobby);
this.hideServerLobby();
});
addTooltipContainer(); addTooltipContainer();
setBackground(); setBackground();
@ -1608,8 +1606,9 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
} }
@Override @Override
public void disconnected(final boolean askToReconnect) { public void disconnected(boolean askToReconnect, boolean keepMySessionActive) {
if (SwingUtilities.isEventDispatchThread()) { if (SwingUtilities.isEventDispatchThread()) {
// TODO: need research, it can generate wrong logs due diff threads source (doInBackground, swing, server events, etc)
// REMOTE task, e.g. connecting // REMOTE task, e.g. connecting
LOGGER.info("Disconnected from server side"); LOGGER.info("Disconnected from server side");
} else { } else {
@ -1617,11 +1616,11 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
LOGGER.info("Disconnected from client side"); LOGGER.info("Disconnected from client side");
} }
SwingUtilities.invokeLater(() -> { Runnable runOnExit = () -> {
// user already disconnected, can't do any online actions like quite chat // user already disconnected, can't do any online actions like quite chat
// but try to keep session // but try to keep session
// TODO: why it ignore askToReconnect here, but use custom reconnect dialog later?! Need research // TODO: why it ignore askToReconnect here, but use custom reconnect dialog later?! Need research
SessionHandler.disconnect(false, true); SessionHandler.disconnect(false, keepMySessionActive);
setConnectButtonText(NOT_CONNECTED_BUTTON); setConnectButtonText(NOT_CONNECTED_BUTTON);
disableButtons(); disableButtons();
hideGames(); hideGames();
@ -1632,7 +1631,13 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
message.setButton2("Yes", PlayerAction.CLIENT_RECONNECT); message.setButton2("Yes", PlayerAction.CLIENT_RECONNECT);
showUserRequestDialog(message); showUserRequestDialog(message);
} }
}); };
if (SwingUtilities.isEventDispatchThread()) {
runOnExit.run();
} else {
SwingUtilities.invokeLater(runOnExit);
}
} }
@Override @Override

View file

@ -30,8 +30,6 @@ import org.apache.log4j.Logger;
import org.mage.card.arcane.CardRendererUtils; import org.mage.card.arcane.CardRendererUtils;
import org.ocpsoft.prettytime.Duration; import org.ocpsoft.prettytime.Duration;
import org.ocpsoft.prettytime.PrettyTime; import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.TimeFormat;
import org.ocpsoft.prettytime.units.JustNow;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
@ -645,8 +643,24 @@ public class TablesPanel extends javax.swing.JPanel {
btnQuickStartMCTS.setFont(GUISizeHelper.menuFont); btnQuickStartMCTS.setFont(GUISizeHelper.menuFont);
} }
private void restoreDividerLocations() {
Rectangle currentBounds = MageFrame.getDesktop().getBounds();
if (currentBounds != null) {
String firstDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_1, null);
String tableDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_2, null);
String chatDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_3, null);
GuiDisplayUtil.restoreDividerLocations(currentBounds, firstDivider, jSplitPane1);
GuiDisplayUtil.restoreDividerLocations(currentBounds, tableDivider, jSplitPaneTables);
GuiDisplayUtil.restoreDividerLocations(currentBounds, chatDivider, chatPanelMain);
}
}
private void saveDividerLocations() { private void saveDividerLocations() {
// save divider locations and divider saveDividerLocations // save divider locations
if (this.jSplitPane1.getDividerLocation() == -1) {
// server lobby hidden by default, so ignore that settings
return;
}
GuiDisplayUtil.saveCurrentBoundsToPrefs(); GuiDisplayUtil.saveCurrentBoundsToPrefs();
GuiDisplayUtil.saveDividerLocationToPrefs(KEY_TABLES_DIVIDER_LOCATION_1, this.jSplitPane1.getDividerLocation()); GuiDisplayUtil.saveDividerLocationToPrefs(KEY_TABLES_DIVIDER_LOCATION_1, this.jSplitPane1.getDividerLocation());
GuiDisplayUtil.saveDividerLocationToPrefs(KEY_TABLES_DIVIDER_LOCATION_2, this.jSplitPaneTables.getDividerLocation()); GuiDisplayUtil.saveDividerLocationToPrefs(KEY_TABLES_DIVIDER_LOCATION_2, this.jSplitPaneTables.getDividerLocation());
@ -663,18 +677,6 @@ public class TablesPanel extends javax.swing.JPanel {
TableUtil.saveColumnWidthAndOrderToPrefs(tableTables, KEY_TABLES_COLUMNS_WIDTH, KEY_TABLES_COLUMNS_ORDER); TableUtil.saveColumnWidthAndOrderToPrefs(tableTables, KEY_TABLES_COLUMNS_WIDTH, KEY_TABLES_COLUMNS_ORDER);
} }
private void restoreDividers() {
Rectangle currentBounds = MageFrame.getDesktop().getBounds();
if (currentBounds != null) {
String firstDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_1, null);
String tableDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_2, null);
String chatDivider = PreferencesDialog.getCachedValue(KEY_TABLES_DIVIDER_LOCATION_3, null);
GuiDisplayUtil.restoreDividerLocations(currentBounds, firstDivider, jSplitPane1);
GuiDisplayUtil.restoreDividerLocations(currentBounds, tableDivider, jSplitPaneTables);
GuiDisplayUtil.restoreDividerLocations(currentBounds, chatDivider, chatPanelMain);
}
}
public Map<String, JComponent> getUIComponents() { public Map<String, JComponent> getUIComponents() {
Map<String, JComponent> components = new HashMap<>(); Map<String, JComponent> components = new HashMap<>();
@ -778,12 +780,9 @@ public class TablesPanel extends javax.swing.JPanel {
//tableModel.setSession(session); //tableModel.setSession(session);
reloadServerMessages(); reloadServerMessages();
MageFrame.getUI().addButton(MageComponents.NEW_GAME_BUTTON, btnNewTable); MageFrame.getUI().addButton(MageComponents.NEW_GAME_BUTTON, btnNewTable);
// divider locations have to be set with delay else values set are overwritten with system defaults restoreDividerLocations();
Executors.newSingleThreadScheduledExecutor().schedule(() -> restoreDividers(), 300, TimeUnit.MILLISECONDS);
} }
protected void reloadServerMessages() { protected void reloadServerMessages() {

View file

@ -71,7 +71,7 @@ public class MultiConnectTest {
} }
@Override @Override
public void disconnected(boolean askToReconnect) { public void disconnected(boolean askToReconnect, boolean keepMySessionActive) {
logger.info("disconnected"); logger.info("disconnected");
} }

View file

@ -14,7 +14,7 @@ public interface MageClient extends CallbackClient {
void connected(String message); void connected(String message);
void disconnected(boolean askToReconnect); void disconnected(boolean askToReconnect, boolean keepMySessionActive);
void showMessage(String message); void showMessage(String message);

View file

@ -552,7 +552,7 @@ public class SessionImpl implements Session {
if (askForReconnect) { if (askForReconnect) {
client.showError("Network error. Can't connect to " + connection.getHost()); client.showError("Network error. Can't connect to " + connection.getHost());
} }
client.disconnected(askForReconnect); // MageFrame with check to reconnect client.disconnected(askForReconnect, keepMySessionActive); // MageFrame with check to reconnect
pingTime.clear(); pingTime.clear();
} }
@ -568,7 +568,7 @@ public class SessionImpl implements Session {
@Override @Override
public synchronized void connectReconnect(Throwable throwable) { public synchronized void connectReconnect(Throwable throwable) {
client.disconnected(true); client.disconnected(true, true);
} }
@Override @Override

View file

@ -243,7 +243,7 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient {
} }
@Override @Override
public void disconnected(boolean askToReconnect) { public void disconnected(boolean askToReconnect, boolean keepMySessionActive) {
if (SwingUtilities.isEventDispatchThread()) { if (SwingUtilities.isEventDispatchThread()) {
consolePanel1.stop(); consolePanel1.stop();
setStatusText("Not connected"); setStatusText("Not connected");

View file

@ -39,7 +39,7 @@ public class SimpleMageClient implements MageClient {
} }
@Override @Override
public void disconnected(boolean askToReconnect) { public void disconnected(boolean askToReconnect, boolean keepMySessionActive) {
// do nothing // do nothing
} }