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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 6.8 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.Toolkit;
14import java.awt.event.ActionEvent;
15import java.awt.event.ActionListener;
16import java.awt.event.KeyEvent;
17import java.awt.event.WindowAdapter;
18import java.awt.event.WindowEvent;
19
20import javax.swing.AbstractAction;
21import javax.swing.BorderFactory;
22import javax.swing.JCheckBox;
23import javax.swing.JComponent;
24import javax.swing.JDialog;
25import javax.swing.JOptionPane;
26import javax.swing.JPanel;
27import javax.swing.KeyStroke;
28
29import org.openstreetmap.josm.actions.ExpertToggleAction;
30import org.openstreetmap.josm.gui.SideButton;
31import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
32import org.openstreetmap.josm.gui.help.HelpUtil;
33import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
34import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
35import org.openstreetmap.josm.tools.GBC;
36import org.openstreetmap.josm.tools.ImageProvider;
37import org.openstreetmap.josm.tools.WindowGeometry;
38
39public class PreferenceDialog extends JDialog {
40
41 private PreferenceTabbedPane tpPreferences;
42 private boolean canceled;
43
44 protected JPanel buildActionPanel() {
45 JPanel pnl = new JPanel(new GridBagLayout());
46
47 JCheckBox expert = new JCheckBox(tr("Expert mode"));
48 expert.setSelected(ExpertToggleAction.isExpert());
49 expert.addActionListener(new ActionListener() {
50 @Override
51 public void actionPerformed(ActionEvent e) {
52 ExpertToggleAction.getInstance().actionPerformed(null);
53 }
54 });
55
56 JPanel btns = new JPanel(new FlowLayout(FlowLayout.CENTER));
57 btns.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
58 btns.add(new SideButton(new OKAction()));
59 btns.add(new SideButton(new CancelAction()));
60 btns.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Action/Preferences"))));
61 pnl.add(expert, GBC.std().insets(5,0,0,0));
62 pnl.add(btns, GBC.std().fill(GBC.HORIZONTAL));
63 return pnl;
64 }
65
66 protected void build() {
67 Container c = getContentPane();
68 c.setLayout(new BorderLayout());
69 c.add(tpPreferences = new PreferenceTabbedPane(), BorderLayout.CENTER);
70 tpPreferences.buildGui();
71 tpPreferences.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
72 c.add(buildActionPanel(), BorderLayout.SOUTH);
73
74 addWindowListener(new WindowEventHandler());
75
76 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
77 getRootPane().getActionMap().put("cancel", new CancelAction());
78 HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Action/Preferences"));
79 }
80
81 public PreferenceDialog(Component parent) {
82 super(JOptionPane.getFrameForComponent(parent), tr("Preferences"), ModalityType.DOCUMENT_MODAL);
83 build();
84 this.setMinimumSize(new Dimension(600, 350));
85 // set the maximum width to the current screen. If the dialog is opened on a
86 // smaller screen than before, this will reset the stored preference.
87 this.setMaximumSize( Toolkit.getDefaultToolkit().getScreenSize());
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 public void selectPreferencesTabByName(String name) {
131 tpPreferences.selectTabByName(name);
132 }
133
134 public void selectPreferencesTabByClass(Class<? extends TabPreferenceSetting> clazz) {
135 tpPreferences.selectTabByPref(clazz);
136 }
137
138 public void selectSubPreferencesTabByClass(Class<? extends SubPreferenceSetting> clazz) {
139 tpPreferences.selectSubTabByPref(clazz);
140 }
141
142 class CancelAction extends AbstractAction {
143 public CancelAction() {
144 putValue(NAME, tr("Cancel"));
145 putValue(SMALL_ICON, ImageProvider.get("cancel"));
146 putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates"));
147 }
148
149 public void cancel() {
150 setCanceled(true);
151 setVisible(false);
152 tpPreferences.validationListeners.clear();
153 }
154
155 @Override
156 public void actionPerformed(ActionEvent evt) {
157 cancel();
158 }
159 }
160
161 class OKAction extends AbstractAction {
162 public OKAction() {
163 putValue(NAME, tr("OK"));
164 putValue(SMALL_ICON, ImageProvider.get("ok"));
165 putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog"));
166 }
167
168 @Override
169 public void actionPerformed(ActionEvent evt) {
170 for (ValidationListener listener: tpPreferences.validationListeners) {
171 if (!listener.validatePreferences())
172 return;
173 }
174
175 tpPreferences.savePreferences();
176 tpPreferences.validationListeners.clear();
177 setCanceled(false);
178 setVisible(false);
179 }
180 }
181
182 class WindowEventHandler extends WindowAdapter {
183 @Override
184 public void windowClosing(WindowEvent arg0) {
185 new CancelAction().cancel();
186 }
187 }
188
189 public void selectMapPaintPreferenceTab() {
190 tpPreferences.selectSubTabByPref(MapPaintPreference.class);
191 }
192}
Note: See TracBrowser for help on using the repository browser.