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

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

see #15182 - move WindowGeometry from tools to gui.util

  • Property svn:eol-style set to native
File size: 6.9 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 boolean canceled;
45
46 /**
47 * Constructs a new {@code PreferenceDialog}.
48 * @param parent parent component
49 */
50 public PreferenceDialog(Component parent) {
51 super(GuiHelper.getFrameForComponent(parent), tr("Preferences"), ModalityType.DOCUMENT_MODAL);
52 build();
53 this.setMinimumSize(new Dimension(600, 350));
54 // set the maximum width to the current screen. If the dialog is opened on a
55 // smaller screen than before, this will reset the stored preference.
56 this.setMaximumSize(GuiHelper.getScreenSize());
57 }
58
59 protected JPanel buildActionPanel() {
60 JPanel pnl = new JPanel(new GridBagLayout());
61
62 JCheckBox expert = new JCheckBox(tr("Expert mode"));
63 expert.setSelected(ExpertToggleAction.isExpert());
64 expert.addActionListener(e -> ExpertToggleAction.getInstance().actionPerformed(null));
65
66 JPanel btns = new JPanel(new FlowLayout(FlowLayout.CENTER));
67 btns.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
68 btns.add(new JButton(new OKAction()));
69 btns.add(new JButton(new CancelAction()));
70 btns.add(new JButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Action/Preferences"))));
71 pnl.add(expert, GBC.std().insets(5, 0, 0, 0));
72 pnl.add(btns, GBC.std().fill(GBC.HORIZONTAL));
73 return pnl;
74 }
75
76 protected final void build() {
77 Container c = getContentPane();
78 c.setLayout(new BorderLayout());
79 c.add(tpPreferences, BorderLayout.CENTER);
80 tpPreferences.buildGui();
81 tpPreferences.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
82 c.add(buildActionPanel(), BorderLayout.SOUTH);
83
84 addWindowListener(new WindowEventHandler());
85
86 InputMapUtils.addEscapeAction(getRootPane(), new CancelAction());
87 HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Action/Preferences"));
88 }
89
90 /**
91 * Replies the preferences tabbed pane.
92 * @return The preferences tabbed pane, or null if the dialog is not yet initialized.
93 * @since 5604
94 */
95 public PreferenceTabbedPane getTabbedPane() {
96 return tpPreferences;
97 }
98
99 public boolean isCanceled() {
100 return canceled;
101 }
102
103 protected void setCanceled(boolean canceled) {
104 this.canceled = canceled;
105 }
106
107 @Override
108 public void setVisible(boolean visible) {
109 if (visible) {
110 // Make the pref window at most as large as the parent JOSM window
111 // Have to take window decorations into account or the windows will
112 // be too large
113 Insets i = this.getParent().getInsets();
114 Dimension p = this.getParent().getSize();
115 p = new Dimension(Math.min(p.width-i.left-i.right, 700),
116 Math.min(p.height-i.top-i.bottom, 800));
117 new WindowGeometry(
118 getClass().getName() + ".geometry",
119 WindowGeometry.centerInWindow(
120 getParent(),
121 p
122 )
123 ).applySafe(this);
124 } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
125 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
126 }
127 super.setVisible(visible);
128 }
129
130 /**
131 * Select preferences tab by name.
132 * @param name preferences tab name (icon)
133 */
134 public void selectPreferencesTabByName(String name) {
135 tpPreferences.selectTabByName(name);
136 }
137
138 /**
139 * Select preferences tab by class.
140 * @param clazz preferences tab class
141 */
142 public void selectPreferencesTabByClass(Class<? extends TabPreferenceSetting> clazz) {
143 tpPreferences.selectTabByPref(clazz);
144 }
145
146 /**
147 * Select preferences sub-tab by class.
148 * @param clazz preferences sub-tab class
149 */
150 public void selectSubPreferencesTabByClass(Class<? extends SubPreferenceSetting> clazz) {
151 tpPreferences.selectSubTabByPref(clazz);
152 }
153
154 class CancelAction extends AbstractAction {
155 CancelAction() {
156 putValue(NAME, tr("Cancel"));
157 new ImageProvider("cancel").getResource().attachImageIcon(this);
158 putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates"));
159 }
160
161 public void cancel() {
162 setCanceled(true);
163 setVisible(false);
164 tpPreferences.validationListeners.clear();
165 }
166
167 @Override
168 public void actionPerformed(ActionEvent evt) {
169 cancel();
170 }
171 }
172
173 class OKAction extends AbstractAction {
174 OKAction() {
175 putValue(NAME, tr("OK"));
176 new ImageProvider("ok").getResource().attachImageIcon(this);
177 putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog"));
178 }
179
180 @Override
181 public void actionPerformed(ActionEvent evt) {
182 for (ValidationListener listener: tpPreferences.validationListeners) {
183 if (!listener.validatePreferences())
184 return;
185 }
186
187 tpPreferences.savePreferences();
188 tpPreferences.validationListeners.clear();
189 setCanceled(false);
190 setVisible(false);
191 }
192 }
193
194 class WindowEventHandler extends WindowAdapter {
195 @Override
196 public void windowClosing(WindowEvent arg0) {
197 new CancelAction().cancel();
198 }
199 }
200}
Note: See TracBrowser for help on using the repository browser.