diff --git a/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/UserPreferences.java b/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/UserPreferences.java index 6c235abd..29c56c02 100644 --- a/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/UserPreferences.java +++ b/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/UserPreferences.java @@ -40,6 +40,11 @@ public class UserPreferences { private static final String SAVE_DIRECTORY = "saveDirectory"; private static final String IMPORT_DIRECTORY = "importDirectory"; private static final String BACKUP_STRATEGY = "backupStrategy"; + private static final String SAVE_WINDOW_SIZE = "saveWindowSize"; + private static final String SAVED_WINDOW_HEIGHT = "savedWindowHeight"; + private static final String SAVED_WINDOW_WIDTH = "savedWindowWidth"; + private static final String SAVED_WINDOW_MAXIMIZED = "savedWindowMaximized"; + private static final String CENTER_WINDOW = "centerWindow"; private static UserPreferences instance; private final Properties properties = new Properties(); /** @@ -128,6 +133,59 @@ public class UserPreferences { public BackupStrategy createBackupStrategy() { return BackupStrategy.create(getBackupStrategy()); } + /** + * Indicates if we are saving the window size. Used when windows are RESIZED. + */ + public boolean getSaveWindowSize() { + return toBoolean(SAVE_WINDOW_SIZE); + } + /** + * Get the saved window height. 0 means not saved. + */ + public int getSavedWindowHeight() { + return toInt(SAVED_WINDOW_HEIGHT); + } + /** + * Get the saved window width. 0 means not saved. + */ + public int getSavedWindowWidth() { + return toInt(SAVED_WINDOW_WIDTH); + } + /** + * Indicates if the window was maximized when saved. + */ + public boolean getSavedWindowMaximized() { + return toBoolean(SAVED_WINDOW_MAXIMIZED); + } + /** + * Indicates if the window should be centered on the screen when created. + */ + public boolean getCenterWindow() { + return toBoolean(CENTER_WINDOW); + } + + /** + * Convert existing string to boolean. This is excessively paranoid, but we catch any exceptions + * and then default to false. + */ + private boolean toBoolean(final String propertyName) { + try { + return Boolean.parseBoolean(properties.getProperty(propertyName, "false")); + } catch (Exception _) { + return false; + } + } + /** + * Convert existing string to an int. This is excessively paranoid, but we catch any exceptions + * and then default to 0. + */ + private int toInt(final String propertyName) { + try { + return Integer.parseInt(properties.getProperty(propertyName, "0")); + } catch (Exception _) { + return 0; + } + } /** * Set the disk image directory. @@ -159,4 +217,34 @@ public class UserPreferences { public void setBackupStrategy(String backupStrategy) { properties.setProperty(BACKUP_STRATEGY, backupStrategy); } + /** + * Indicates if we are saving the window size. Used when windows are RESIZED. + */ + public void setSaveWindowSize(boolean flag) { + properties.setProperty(SAVE_WINDOW_SIZE, Boolean.toString(flag)); + } + /** + * Get the saved window height. 0 means not saved. + */ + public void setSavedWindowHeight(int height) { + properties.setProperty(SAVED_WINDOW_HEIGHT, Integer.toString(height)); + } + /** + * Get the saved window width. 0 means not saved. + */ + public void setSavedWindowWidth(int width) { + properties.setProperty(SAVED_WINDOW_WIDTH, Integer.toString(width)); + } + /** + * Indicates if the window was maximized when saved. + */ + public void setSavedWindowMaximized(boolean maximized) { + properties.setProperty(SAVED_WINDOW_MAXIMIZED, Boolean.toString(maximized)); + } + /** + * Indicates if the window should be centered on the screen when created. + */ + public void setCenterWindow(boolean flag) { + properties.setProperty(CENTER_WINDOW, Boolean.toString(flag)); + } } diff --git a/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/DiskWindow.java b/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/DiskWindow.java index cac4a275..e8ba3b13 100644 --- a/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/DiskWindow.java +++ b/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/DiskWindow.java @@ -23,6 +23,7 @@ import com.webcodepro.applecommander.storage.DiskCorruptException; import com.webcodepro.applecommander.storage.DiskException; import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.ui.UiBundle; +import com.webcodepro.applecommander.ui.UserPreferences; import com.webcodepro.applecommander.ui.swt.util.ImageManager; import com.webcodepro.applecommander.ui.swt.util.SwtUtil; import com.webcodepro.applecommander.util.TextBundle; @@ -41,6 +42,7 @@ import org.eclipse.swt.widgets.Shell; * @author Rob Greene */ public class DiskWindow { + private final UserPreferences preferences = UserPreferences.getInstance(); private final Shell parentShell; private final ImageManager imageManager; @@ -56,7 +58,7 @@ public class DiskWindow { /** * Construct the disk window. */ - public DiskWindow(Shell parentShell, FormattedDisk[] disks, ImageManager imageManager) { + public DiskWindow(Shell shell, FormattedDisk[] disks, ImageManager imageManager) { this.parentShell = shell; this.disks = disks; this.imageManager = imageManager; @@ -70,6 +72,10 @@ public class DiskWindow { shell.setLayout(new FillLayout()); shell.setImage(imageManager.get(ImageManager.ICON_DISK)); shell.setMinimumSize(400, 300); + if (preferences.getSavedWindowHeight() != 0 && preferences.getSavedWindowWidth() != 0) { + shell.setSize(preferences.getSavedWindowWidth(), preferences.getSavedWindowHeight()); + } + shell.setMaximized(preferences.getSavedWindowMaximized()); setStandardWindowTitle(); shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent event) { @@ -83,6 +89,14 @@ public class DiskWindow { event.doit = button == SWT.YES; } }); + shell.addListener(SWT.Resize, e -> { + // It doesn't look like the event itself has this info, so using the primary shell + if (preferences.getSaveWindowSize()) { + preferences.setSavedWindowHeight(shell.getSize().y); + preferences.setSavedWindowWidth(shell.getSize().x); + preferences.setSavedWindowMaximized(shell.getMaximized()); + } + }); CTabFolder tabFolder = new CTabFolder(shell, SWT.BOTTOM); diskExplorerTab = new DiskExplorerTab(tabFolder, disks, imageManager, this); @@ -94,7 +108,10 @@ public class DiskWindow { } diskInfoTab = new DiskInfoTab(tabFolder, disks); tabFolder.setSelection(tabFolder.getItems()[0]); - + + if (preferences.getCenterWindow()) { + SwtUtil.center(parentShell, shell); + } shell.open(); } @@ -113,9 +130,9 @@ public class DiskWindow { protected void handle(final DiskCorruptException e) { Shell finalShell = shell; MessageBox box = new MessageBox(finalShell, SWT.ICON_ERROR | SWT.OK); - box.setText(textBundle.get("SwtAppleCommander." + e.kind + ".Title")); //$NON-NLS-1$ + box.setText(textBundle.get("SwtAppleCommander." + e.kind + ".Title")); box.setMessage( - textBundle.format("SwtAppleCommander.DiskCorruptException.Message", //$NON-NLS-1$ + textBundle.format("SwtAppleCommander.DiskCorruptException.Message", e.imagepath )); box.open(); @@ -132,9 +149,9 @@ public class DiskWindow { return; } final MessageBox box = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK | SWT.MODELESS); - box.setText(textBundle.get("SwtAppleCommander.DiskException.Title")); //$NON-NLS-1$ + box.setText(textBundle.get("SwtAppleCommander.DiskException.Title")); box.setMessage( - textBundle.format("SwtAppleCommander.DiskException.Message", //$NON-NLS-1$ + textBundle.format("SwtAppleCommander.DiskException.Message", e.imagepath, e.toString())); box.open(); } @@ -148,9 +165,9 @@ public class DiskWindow { final TextBundle textBundle = UiBundle.getInstance(); Shell finalShell = shell; MessageBox box = new MessageBox(finalShell, SWT.ICON_ERROR | SWT.OK); - box.setText(textBundle.get("SwtAppleCommander.UnexpectedErrorTitle")); //$NON-NLS-1$ + box.setText(textBundle.get("SwtAppleCommander.UnexpectedErrorTitle")); box.setMessage( - textBundle.get("SwtAppleCommander.UnexpectedErrorMessage" //$NON-NLS-1$ + textBundle.get("SwtAppleCommander.UnexpectedErrorMessage" )); box.open(); e.printStackTrace(); @@ -162,7 +179,7 @@ public class DiskWindow { */ public void setStandardWindowTitle() { shell.setText(UiBundle.getInstance().format( - "DiskWindow.Title", disks[0].getFilename())); //$NON-NLS-1$ + "DiskWindow.Title", disks[0].getFilename())); } /** diff --git a/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/SettingsDialog.java b/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/SettingsDialog.java index 61271d62..fd89c67c 100644 --- a/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/SettingsDialog.java +++ b/lib/ac-swt-common/src/main/java/com/webcodepro/applecommander/ui/swt/SettingsDialog.java @@ -32,6 +32,7 @@ import org.eclipse.swt.widgets.*; import java.util.Map; import java.util.Optional; +import java.util.function.Consumer; /** * The settings dialog allows some management of AppleCommander behavior. @@ -45,6 +46,10 @@ public class SettingsDialog { private Button backupBrowseButton; private Text backupDirectoryText; + // Temporary state of the window settings + private boolean saveWindowSize; + private boolean centerWindow; + public SettingsDialog(Shell parent) { this.parent = parent; } @@ -78,6 +83,7 @@ public class SettingsDialog { GridData rowSpanGridData = new GridData(GridData.FILL_HORIZONTAL); rowSpanGridData.horizontalSpan = 2; + createWindowTab(tabFolder, tabItemLayout); createBackupTab(tabFolder, tabItemLayout, rowSpanGridData); createInfoTab(tabFolder, tabItemLayout, rowSpanGridData); @@ -100,12 +106,7 @@ public class SettingsDialog { dialog.setDefaultButton(button); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { - String strategy = switch (backupStrategyCombo.getSelectionIndex()) { - case 1 -> "bak"; - case 2 -> backupDirectoryText.getText(); - default -> ""; - }; - preference.setBackupStrategy(strategy); + savePreferences(); dialog.close(); } }); @@ -115,6 +116,63 @@ public class SettingsDialog { dialog.open(); } + public void savePreferences() { + // Window tab + preference.setSaveWindowSize(saveWindowSize); + preference.setCenterWindow(centerWindow); + + // Backup tab + String strategy = switch (backupStrategyCombo.getSelectionIndex()) { + case 1 -> "bak"; + case 2 -> backupDirectoryText.getText(); + default -> ""; + }; + preference.setBackupStrategy(strategy); + + preference.save(); + } + + public void createWindowTab(TabFolder tabFolder, GridLayout tabItemLayout) { + TabItem item = new TabItem(tabFolder, SWT.NONE); + item.setText("Window"); + Composite control = new Composite(tabFolder, SWT.NONE); + control.setLayout(tabItemLayout); + item.setControl(control); + + Label label = new Label(control, SWT.NONE); + label.setText("Save Window Size?"); + saveWindowSize = preference.getSaveWindowSize(); + addYesNoRadio(control, saveWindowSize, flag -> saveWindowSize = flag); + + label = new Label(control, SWT.NONE); + label.setText("Center Window on Screen?"); + centerWindow = preference.getCenterWindow(); + addYesNoRadio(control, centerWindow, flag -> centerWindow = flag); + } + + private void addYesNoRadio(Composite control, boolean value, Consumer callback) { + Composite ynPanel = new Composite(control, SWT.NONE); + ynPanel.setLayout(new FillLayout(SWT.HORIZONTAL)); + + Button button = new Button(ynPanel, SWT.RADIO); + button.setText("Yes"); + button.setSelection(value); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + callback.accept(true); + } + }); + + button = new Button(ynPanel, SWT.RADIO); + button.setText("No"); + button.setSelection(!value); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + callback.accept(false); + } + }); + } + public void createBackupTab(TabFolder tabFolder, GridLayout tabItemLayout, GridData rowSpanGridData) { TabItem item = new TabItem(tabFolder, SWT.NONE); item.setText("Backup");