Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload the preferences panel after resetting preferences, fixes #168 #1213

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PreferencesDialogPanel extends JPanel implements Disposable {

public static final String RESET_PREFERENCES_CONFIRMATION_DIALOG_TITLE = "Reset preferences?";

public static final String RESET_PREFERENCES_CONFIRMATION_DIALOG_MESSAGE = "Are you sure you want to reset all preferences to their default settings";
public static final String RESET_PREFERENCES_CONFIRMATION_DIALOG_MESSAGE = "Are you sure you want to reset ALL preferences to their default settings?";

public static final int DIALOG_DEFAULT_WIDTH = 850;

Expand All @@ -40,15 +40,26 @@ public class PreferencesDialogPanel extends JPanel implements Disposable {

private static final String PREFS_HISTORY_PANEL_KEY = "prefs.history.panel";

// A bit messy
private boolean wasReset = false;

private final Logger logger = LoggerFactory.getLogger(PreferencesDialogPanel.class);

private final EditorKit editorKit;

public PreferencesDialogPanel(EditorKit editorKit) {
this.editorKit = editorKit;
setLayout(new BorderLayout());

PreferencesPanelPluginLoader loader = new PreferencesPanelPluginLoader(editorKit);
add(tabbedPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int margin = 300;
int prefWidth = Math.min(screenSize.width - margin, DIALOG_DEFAULT_WIDTH);
int prefHeight = Math.min(screenSize.height - margin, DIALOG_DEFAULT_HEIGHT);
setPreferredSize(new Dimension(prefWidth, prefHeight));
reload();
}

private void reload() {
dispose();
tabbedPane.removeAll();
PreferencesPanelPluginLoader loader = new PreferencesPanelPluginLoader(editorKit);
Set<PreferencesPanelPlugin> plugins = new TreeSet<>((o1, o2) -> {
String s1 = o1.getLabel();
String s2 = o2.getLabel();
Expand All @@ -70,13 +81,6 @@ public PreferencesDialogPanel(EditorKit editorKit) {
logger.warn("An error occurred whilst trying to instantiate the preferences panel plugin '{}': {}", plugin.getLabel(), e);
}
}
add(tabbedPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int margin = 300;
int prefWidth = Math.min(screenSize.width - margin, DIALOG_DEFAULT_WIDTH);
int prefHeight = Math.min(screenSize.height - margin, DIALOG_DEFAULT_HEIGHT);
setPreferredSize(new Dimension(prefWidth, prefHeight));

}

public void dispose() {
Expand Down Expand Up @@ -114,21 +118,25 @@ protected String getSelectedPanel() {
}
return null;
}

protected void setSelectedPanel(String tabName) {
Component c = componentMap.get(tabName);
if (c != null) {
tabbedPane.setSelectedComponent(c);
}
}

public static void showPreferencesDialog(String selectedPanel, EditorKit editorKit) {
final PreferencesDialogPanel preferencesPanel = new PreferencesDialogPanel(editorKit);

JPanel holder = new JPanel(new BorderLayout(4, 4));
holder.add(preferencesPanel);

final Preferences prefs = PreferencesManager.getInstance().getApplicationPreferences(PreferencesDialogPanel.class);

if (selectedPanel == null){
Preferences prefs = PreferencesManager.getInstance().getApplicationPreferences(PreferencesDialogPanel.class);
selectedPanel = prefs.getString(PREFS_HISTORY_PANEL_KEY, null);
}
Component c = preferencesPanel.componentMap.get(selectedPanel);
if (c != null) {
preferencesPanel.tabbedPane.setSelectedComponent(c);
}
preferencesPanel.setSelectedPanel(selectedPanel);

JPanel resetPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton resetPreferencesButton = new JButton(new AbstractAction("Reset preferences...") {
Expand All @@ -143,25 +151,28 @@ public void actionPerformed(ActionEvent e) {
JDialog dlg = op.createDialog(editorKit.getWorkspace(), "Preferences");
dlg.setResizable(true);
dlg.setVisible(true);
if (!preferencesPanel.wasReset) {
Object o = op.getValue();
if (o != null){
int ret = (Integer)o;
if (ret == JOptionPane.OK_OPTION) {
preferencesPanel.applyPreferences();
}
Object o = op.getValue();
if (o != null){
int ret = (Integer)o;
if (ret == JOptionPane.OK_OPTION) {
preferencesPanel.applyPreferences();
}
prefs.putString(PREFS_HISTORY_PANEL_KEY, preferencesPanel.getSelectedPanel());
}

PreferencesManager.getInstance()
.getApplicationPreferences(PreferencesDialogPanel.class)
.putString(PREFS_HISTORY_PANEL_KEY, preferencesPanel.getSelectedPanel());

preferencesPanel.dispose();
}

private static void handleResetPreferences(PreferencesDialogPanel panel) {
String selectedPanel = panel.getSelectedPanel();
int ret = JOptionPane.showConfirmDialog(panel, RESET_PREFERENCES_CONFIRMATION_DIALOG_MESSAGE, RESET_PREFERENCES_CONFIRMATION_DIALOG_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION) {
PreferencesManager.getInstance().resetPreferencesToFactorySettings();
panel.wasReset = true;
}
}
panel.reload();
panel.setSelectedPanel(selectedPanel);
}
}