source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java@ 13431

Last change on this file since 13431 was 13431, checked in by Don-vip, 7 years ago

fix #15950 - Preferences Help button should go directly to selected Preferences tab Help page

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Container;
9import java.awt.Dimension;
10import java.awt.FlowLayout;
11import java.awt.GridBagLayout;
12import java.awt.Insets;
13import java.awt.event.ActionEvent;
14import java.awt.event.WindowAdapter;
15import java.awt.event.WindowEvent;
16
17import javax.swing.AbstractAction;
18import javax.swing.BorderFactory;
19import javax.swing.JButton;
20import javax.swing.JCheckBox;
21import javax.swing.JDialog;
22import javax.swing.JPanel;
23
24import org.openstreetmap.josm.actions.ExpertToggleAction;
25import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
26import org.openstreetmap.josm.gui.help.HelpUtil;
27import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
28import org.openstreetmap.josm.gui.util.GuiHelper;
29import org.openstreetmap.josm.gui.util.WindowGeometry;
30import org.openstreetmap.josm.tools.GBC;
31import org.openstreetmap.josm.tools.ImageProvider;
32import org.openstreetmap.josm.tools.InputMapUtils;
33
34/**
35 * The main preferences dialog.
36 *
37 * Dialog window where the user can change various settings. Organized in main
38 * tabs to the left ({@link TabPreferenceSetting}) and (optional) sub-pages
39 * ({@link SubPreferenceSetting}).
40 */
41public class PreferenceDialog extends JDialog {
42
43 private final PreferenceTabbedPane tpPreferences = new PreferenceTabbedPane();
44 private final ContextSensitiveHelpAction helpAction = new ContextSensitiveHelpAction();
45 private boolean canceled;
46
47 /**
48 * Constructs a new {@code PreferenceDialog}.
49 * @param parent parent component
50 */
51 public PreferenceDialog(Component parent) {
52 super(GuiHelper.getFrameForComponent(parent), tr("Preferences"), ModalityType.DOCUMENT_MODAL);
53 build();
54 this.setMinimumSize(new Dimension(600, 350));
55 // set the maximum width to the current screen. If the dialog is opened on a
56 // smaller screen than before, this will reset the stored preference.
57 this.setMaximumSize(GuiHelper.getScreenSize());
58 }
59
60 protected JPanel buildActionPanel() {
61 JPanel pnl = new JPanel(new GridBagLayout());
62
63 JCheckBox expert = new JCheckBox(tr("Expert mode"));
64 expert.setSelected(ExpertToggleAction.isExpert());
65 expert.addActionListener(e -> ExpertToggleAction.getInstance().actionPerformed(null));
66
67 JPanel btns = new JPanel(new FlowLayout(FlowLayout.CENTER));
68 btns.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
69 btns.add(new JButton(new OKAction()));
70 btns.add(new JButton(new CancelAction()));
71 btns.add(new JButton(helpAction));
72 pnl.add(expert, GBC.std().insets(5, 0, 0, 0));
73 pnl.add(btns, GBC.std().fill(GBC.HORIZONTAL));
74 return pnl;
75 }
76
77 protected final void build() {
78 Container c = getContentPane();
79 c.setLayout(new BorderLayout());
80 c.add(tpPreferences, BorderLayout.CENTER);
81 tpPreferences.buildGui();
82 tpPreferences.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
83 c.add(buildActionPanel(), BorderLayout.SOUTH);
84
85 addWindowListener(new WindowEventHandler());
86
87 InputMapUtils.addEscapeAction(getRootPane(), new CancelAction());
88 setHelpContext(HelpUtil.ht("/Action/Preferences"));
89 }
90
91 /**
92 * Sets the help context of the preferences dialog.
93 * @param helpContext new help context
94 * @since 13431
95 */
96 public final void setHelpContext(String helpContext) {
97 helpAction.setHelpTopic(helpContext);
98 HelpUtil.setHelpContext(getRootPane(), helpContext);
99 }
100
101 /**
102 * Replies the preferences tabbed pane.
103 * @return The preferences tabbed pane, or null if the dialog is not yet initialized.
104 * @since 5604
105 */
106 public PreferenceTabbedPane getTabbedPane() {
107 return tpPreferences;
108 }
109
110 /**
111 * Determines if preferences changes have been canceled.
112 * @return {@code true} if preferences changes have been canceled
113 */
114 public boolean isCanceled() {
115 return canceled;
116 }
117
118 protected void setCanceled(boolean canceled) {
119 this.canceled = canceled;
120 }
121
122 @Override
123 public void setVisible(boolean visible) {
124 if (visible) {
125 // Make the pref window at most as large as the parent JOSM window
126 // Have to take window decorations into account or the windows will be too large
127 Insets i = this.getParent().getInsets();
128 Dimension p = this.getParent().getSize();
129 p = new Dimension(Math.min(p.width-i.left-i.right, 700),
130 Math.min(p.height-i.top-i.bottom, 800));
131 new WindowGeometry(
132 getClass().getName() + ".geometry",
133 WindowGeometry.centerInWindow(
134 getParent(),
135 p
136 )
137 ).applySafe(this);
138 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
139 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
140 }
141 super.setVisible(visible);
142 }
143
144 /**
145 * Select preferences tab by name.
146 * @param name preferences tab name (icon)
147 */
148 public void selectPreferencesTabByName(String name) {
149 tpPreferences.selectTabByName(name);
150 }
151
152 /**
153 * Select preferences tab by class.
154 * @param clazz preferences tab class
155 */
156 public void selectPreferencesTabByClass(Class<? extends TabPreferenceSetting> clazz) {
157 tpPreferences.selectTabByPref(clazz);
158 }
159
160 /**
161 * Select preferences sub-tab by class.
162 * @param clazz preferences sub-tab class
163 */
164 public void selectSubPreferencesTabByClass(Class<? extends SubPreferenceSetting> clazz) {
165 tpPreferences.selectSubTabByPref(clazz);
166 }
167
168 class CancelAction extends AbstractAction {
169 CancelAction() {
170 putValue(NAME, tr("Cancel"));
171 new ImageProvider("cancel").getResource().attachImageIcon(this);
172 putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates"));
173 }
174
175 public void cancel() {
176 setCanceled(true);
177 setVisible(false);
178 tpPreferences.validationListeners.clear();
179 }
180
181 @Override
182 public void actionPerformed(ActionEvent evt) {
183 cancel();
184 }
185 }
186
187 class OKAction extends AbstractAction {
188 OKAction() {
189 putValue(NAME, tr("OK"));
190 new ImageProvider("ok").getResource().attachImageIcon(this);
191 putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog"));
192 }
193
194 @Override
195 public void actionPerformed(ActionEvent evt) {
196 for (ValidationListener listener: tpPreferences.validationListeners) {
197 if (!listener.validatePreferences())
198 return;
199 }
200
201 tpPreferences.savePreferences();
202 tpPreferences.validationListeners.clear();
203 setCanceled(false);
204 setVisible(false);
205 }
206 }
207
208 class WindowEventHandler extends WindowAdapter {
209 @Override
210 public void windowClosing(WindowEvent arg0) {
211 new CancelAction().cancel();
212 }
213 }
214}
Note: See TracBrowser for help on using the repository browser.