refactor divider setting and saving into utility functions

refactor setting and saving filters into utility functions
This commit is contained in:
GrayedFox 2018-05-22 20:11:07 +02:00
parent d0c30e11bf
commit 5822a7d94d
No known key found for this signature in database
GPG key ID: 7FF4748DBF28C93F
3 changed files with 84 additions and 62 deletions

View file

@ -56,6 +56,11 @@ import mage.client.components.MageComponents;
import mage.client.dialog.*;
import static mage.client.dialog.PreferencesDialog.KEY_TABLES_COLUMNS_ORDER;
import static mage.client.dialog.PreferencesDialog.KEY_TABLES_COLUMNS_WIDTH;
import static mage.client.dialog.PreferencesDialog.KEY_TABLES_FILTER_SETTINGS;
import static mage.client.dialog.PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_1;
import static mage.client.dialog.PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_2;
import static mage.client.dialog.PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_3;
import static mage.client.dialog.PreferencesDialog.KEY_MAGE_PANEL_LAST_SIZE;
import mage.client.util.ButtonColumn;
import mage.client.util.GUISizeHelper;
import mage.client.util.IgnoreList;
@ -227,8 +232,7 @@ public class TablesPanel extends javax.swing.JPanel {
jScrollPaneTablesActive.getViewport().setBackground(new Color(255, 255, 255, 50));
jScrollPaneTablesFinished.getViewport().setBackground(new Color(255, 255, 255, 50));
restoreSettings();
saveActiveFiltersToPrefs();
setGUISize();
Action openTableAction;
@ -343,7 +347,7 @@ public class TablesPanel extends javax.swing.JPanel {
}
public void cleanUp() {
saveSettings();
saveGuiSettings();
chatPanelMain.cleanUp();
}
@ -406,66 +410,35 @@ public class TablesPanel extends javax.swing.JPanel {
}
private void saveDividerLocations() {
// save panel sizes and divider locations.
// save desktop bounds and divider locations
Rectangle rec = MageFrame.getDesktop().getBounds();
String sb = Double.toString(rec.getWidth()) + 'x' + Double.toString(rec.getHeight());
PreferencesDialog.saveValue(PreferencesDialog.KEY_MAGE_PANEL_LAST_SIZE, sb);
PreferencesDialog.saveValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_1, Integer.toString(this.jSplitPane1.getDividerLocation()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_2, Integer.toString(this.jSplitPaneTables.getDividerLocation()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_3, Integer.toString(chatPanelMain.getSplitDividerLocation()));
String currentBounds = Double.toString(rec.getWidth()) + 'x' + Double.toString(rec.getHeight());
PreferencesDialog.saveValue(KEY_MAGE_PANEL_LAST_SIZE, currentBounds);
GuiDisplayUtil.setDividerLocation(KEY_TABLES_DIVIDER_LOCATION_1, this.jSplitPane1.getDividerLocation());
GuiDisplayUtil.setDividerLocation(KEY_TABLES_DIVIDER_LOCATION_2, this.jSplitPaneTables.getDividerLocation());
GuiDisplayUtil.setDividerLocation(KEY_TABLES_DIVIDER_LOCATION_3, chatPanelMain.getSplitDividerLocation());
}
private void restoreSettings() {
// filter settings
String formatSettings = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_TABLES_FILTER_SETTINGS, "");
int i = 0;
for (JToggleButton component : filterButtons) {
if (formatSettings.length() > i) {
component.setSelected(formatSettings.substring(i, i + 1).equals("x"));
} else {
component.setSelected(true);
}
i++;
}
private void saveActiveFiltersToPrefs() {
TableUtil.setActiveFilters(KEY_TABLES_FILTER_SETTINGS, filterButtons);
setTableFilter();
}
private void saveSettings() {
// Filters
StringBuilder formatSettings = new StringBuilder();
for (JToggleButton component : filterButtons) {
formatSettings.append(component.isSelected() ? "x" : "-");
}
PreferencesDialog.saveValue(PreferencesDialog.KEY_TABLES_FILTER_SETTINGS, formatSettings.toString());
private void saveGuiSettings() {
TableUtil.saveActiveFiltersToPrefs(KEY_TABLES_FILTER_SETTINGS, filterButtons);
TableUtil.saveColumnWidthAndOrderToPrefs(tableTables, KEY_TABLES_COLUMNS_WIDTH, KEY_TABLES_COLUMNS_ORDER);
}
private void restoreDividerLocations() {
Rectangle rec = MageFrame.getDesktop().getBounds();
if (rec != null) {
String size = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_MAGE_PANEL_LAST_SIZE, null);
String sb = Double.toString(rec.getWidth()) + 'x' + Double.toString(rec.getHeight());
// use divider positions only if screen size is the same as it was the time the settings were saved
if (size != null && size.equals(sb)) {
String location = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_1, null);
if (location != null && jSplitPane1 != null) {
jSplitPane1.setDividerLocation(Integer.parseInt(location));
}
if (this.btnStateFinished.isSelected()) {
this.jSplitPaneTables.setDividerLocation(-1);
} else {
location = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_2, null);
if (location != null && jSplitPaneTables != null) {
jSplitPaneTables.setDividerLocation(Integer.parseInt(location));
}
}
location = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_3, null);
if (location != null && chatPanelMain != null) {
chatPanelMain.setSplitDividerLocation(Integer.parseInt(location));
}
}
}
private void restoreDividers() {
Rectangle currentBounds = MageFrame.getDesktop().getBounds();
if (currentBounds != null) {
String firstDivider = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_1, null);
String tableDivider = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_TABLES_DIVIDER_LOCATION_2, null);
String chatDivider = PreferencesDialog.getCachedValue(PreferencesDialog.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() {
@ -565,7 +538,7 @@ public class TablesPanel extends javax.swing.JPanel {
MageFrame.getUI().addButton(MageComponents.NEW_GAME_BUTTON, btnNewTable);
// divider locations have to be set with delay else values set are overwritten with system defaults
Executors.newSingleThreadScheduledExecutor().schedule(() -> restoreDividerLocations(), 300, TimeUnit.MILLISECONDS);
Executors.newSingleThreadScheduledExecutor().schedule(() -> restoreDividers(), 300, TimeUnit.MILLISECONDS);
}
@ -1284,7 +1257,7 @@ public class TablesPanel extends javax.swing.JPanel {
if (currentMessage >= messages.size()) {
currentMessage = 0;
}
URLHandler.RemoveMouseAdapter(jLabelFooterText);
URLHandler.handleMessage(messages.get(currentMessage), this.jLabelFooterText);
}
@ -1447,7 +1420,7 @@ class TableTableModel extends AbstractTableModel {
if (tables[arg0].isTournament()) {
return "Show";
} else {
owner = tables[arg0].getControllerName();
owner = tables[arg0].getControllerName();
if (SessionHandler.getSession() != null && owner.equals(SessionHandler.getUserName())) {
return "";
}

View file

@ -4,8 +4,11 @@ import java.awt.*;
import java.util.ArrayList;
import java.util.Locale;
import javax.swing.*;
import mage.client.dialog.PreferencesDialog;
import static mage.client.dialog.PreferencesDialog.KEY_MAGE_PANEL_LAST_SIZE;
import mage.client.MageFrame;
import mage.client.util.GUISizeHelper;
import mage.client.table.*;
import mage.constants.*;
import mage.view.CardView;
import mage.view.CounterView;
@ -26,6 +29,29 @@ public final class GuiDisplayUtil {
public ArrayList<String> lines;
}
public static void restoreDividerLocations(Rectangle bounds, String lastDividerLocation, JComponent component) {
String currentBounds = Double.toString(bounds.getWidth()) + 'x' + Double.toString(bounds.getHeight());
String savedBounds = PreferencesDialog.getCachedValue(KEY_MAGE_PANEL_LAST_SIZE, null);
// use divider positions only if screen size is the same as it was the time the settings were saved
if (savedBounds != null && savedBounds.equals(currentBounds)) {
if (lastDividerLocation != null && component != null) {
if (component instanceof JSplitPane) {
JSplitPane jSplitPane = (JSplitPane) component;
jSplitPane.setDividerLocation(Integer.parseInt(lastDividerLocation));
}
if (component instanceof PlayersChatPanel) {
PlayersChatPanel playerChatPanel = (PlayersChatPanel) component;
playerChatPanel.setSplitDividerLocation(Integer.parseInt(lastDividerLocation));
}
}
}
}
public static void setDividerLocation(String dividerPrefKey, int position) {
PreferencesDialog.saveValue(dividerPrefKey, Integer.toString(position));
}
public static JXPanel getDescription(CardView card, int width, int height) {
JXPanel descriptionPanel = new JXPanel();

View file

@ -7,6 +7,7 @@ package mage.client.util.gui;
import java.util.Arrays;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.table.TableColumn;
import org.apache.log4j.Logger;
import mage.client.dialog.PreferencesDialog;
@ -27,7 +28,29 @@ public final class TableUtil {
private static final Logger LOGGER = Logger.getLogger(TableUtil.class);
static public void setColumnWidthAndOrder(JTable table, int[] defaultColumnsWidth, String widthPrefKey, String orderPrefKey) {
public static void saveActiveFiltersToPrefs(String filterPrefKey, JToggleButton[] buttons) {
StringBuilder currentFilters = new StringBuilder();
for (JToggleButton component : buttons) {
currentFilters.append(component.isSelected() ? "x" : "-");
}
PreferencesDialog.saveValue(filterPrefKey, currentFilters.toString());
}
public static void setActiveFilters(String filterPrefKey, JToggleButton[] buttons) {
String formatSettings = PreferencesDialog.getCachedValue(filterPrefKey, "");
int i = 0;
for (JToggleButton component : buttons) {
if (formatSettings.length() > i) {
component.setSelected(formatSettings.substring(i, i + 1).equals("x"));
} else {
component.setSelected(true);
}
i++;
}
}
public static void setColumnWidthAndOrder(JTable table, int[] defaultColumnsWidth, String widthPrefKey, String orderPrefKey) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// set the column width from saved value or defaults
@ -58,11 +81,11 @@ public final class TableUtil {
}
static public void saveColumnWidthAndOrderToPrefs(JTable table, String widthPrefKey, String orderPrefKey) {
// Column width
public static void saveColumnWidthAndOrderToPrefs(JTable table, String widthPrefKey, String orderPrefKey) {
StringBuilder columnWidthSettings = new StringBuilder();
StringBuilder columnOrderSettings = new StringBuilder();
boolean firstValue = true;
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
TableColumn column = table.getColumnModel().getColumn(table.convertColumnIndexToView(i));
if (!firstValue) {
@ -74,14 +97,14 @@ public final class TableUtil {
columnWidthSettings.append(column.getWidth());
columnOrderSettings.append(table.convertColumnIndexToModel(i));
}
PreferencesDialog.saveValue(widthPrefKey, columnWidthSettings.toString());
PreferencesDialog.saveValue(orderPrefKey, columnOrderSettings.toString());
LOGGER.info("saving column widths: " + columnWidthSettings.toString());
LOGGER.info("saving column order: " + columnOrderSettings.toString());
}
public static int[] getIntArrayFromString(String stringData) {
private static int[] getIntArrayFromString(String stringData) {
int[] intArray = null;
if (stringData != null && !stringData.isEmpty()) {
String[] items = stringData.split(",");