1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Container;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.FlowLayout;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.Insets;
|
---|
13 | import java.awt.Toolkit;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.ActionListener;
|
---|
16 | import java.awt.event.KeyEvent;
|
---|
17 | import java.awt.event.WindowAdapter;
|
---|
18 | import java.awt.event.WindowEvent;
|
---|
19 |
|
---|
20 | import javax.swing.AbstractAction;
|
---|
21 | import javax.swing.BorderFactory;
|
---|
22 | import javax.swing.JCheckBox;
|
---|
23 | import javax.swing.JComponent;
|
---|
24 | import javax.swing.JDialog;
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.KeyStroke;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
30 | import org.openstreetmap.josm.gui.SideButton;
|
---|
31 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
32 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
33 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
|
---|
34 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
35 | import org.openstreetmap.josm.tools.GBC;
|
---|
36 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
37 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
38 |
|
---|
39 | public 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 | public void actionPerformed(ActionEvent e) {
|
---|
51 | ExpertToggleAction.getInstance().actionPerformed(null);
|
---|
52 | }
|
---|
53 | });
|
---|
54 |
|
---|
55 | JPanel btns = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
---|
56 | btns.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
57 | btns.add(new SideButton(new OKAction()));
|
---|
58 | btns.add(new SideButton(new CancelAction()));
|
---|
59 | btns.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Action/Preferences"))));
|
---|
60 | pnl.add(expert, GBC.std().insets(5,0,0,0));
|
---|
61 | pnl.add(btns, GBC.std().fill(GBC.HORIZONTAL));
|
---|
62 | return pnl;
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected void build() {
|
---|
66 | Container c = getContentPane();
|
---|
67 | c.setLayout(new BorderLayout());
|
---|
68 | c.add(tpPreferences = new PreferenceTabbedPane(), BorderLayout.CENTER);
|
---|
69 | tpPreferences.buildGui();
|
---|
70 | tpPreferences.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
71 | c.add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
72 |
|
---|
73 | addWindowListener(new WindowEventHandler());
|
---|
74 |
|
---|
75 | getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
|
---|
76 | getRootPane().getActionMap().put("cancel", new CancelAction());
|
---|
77 | HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Action/Preferences"));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public PreferenceDialog(Component parent) {
|
---|
81 | super(JOptionPane.getFrameForComponent(parent), tr("Preferences"), ModalityType.DOCUMENT_MODAL);
|
---|
82 | build();
|
---|
83 | this.setMinimumSize(new Dimension(600, 350));
|
---|
84 | // set the maximum width to the current screen. If the dialog is opened on a
|
---|
85 | // smaller screen than before, this will reset the stored preference.
|
---|
86 | this.setMaximumSize( Toolkit.getDefaultToolkit().getScreenSize());
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Replies the preferences tabbed pane.
|
---|
91 | * @return The preferences tabbed pane, or null if the dialog is not yet initialized.
|
---|
92 | * @since 5604
|
---|
93 | */
|
---|
94 | public PreferenceTabbedPane getTabbedPane() {
|
---|
95 | return tpPreferences;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean isCanceled() {
|
---|
99 | return canceled;
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void setCanceled(boolean canceled) {
|
---|
103 | this.canceled = canceled;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void setVisible(boolean visible) {
|
---|
108 | if (visible) {
|
---|
109 | // Make the pref window at most as large as the parent JOSM window
|
---|
110 | // Have to take window decorations into account or the windows will
|
---|
111 | // be too large
|
---|
112 | Insets i = this.getParent().getInsets();
|
---|
113 | Dimension p = this.getParent().getSize();
|
---|
114 | p = new Dimension(Math.min(p.width-i.left-i.right, 700),
|
---|
115 | Math.min(p.height-i.top-i.bottom, 800));
|
---|
116 | new WindowGeometry(
|
---|
117 | getClass().getName() + ".geometry",
|
---|
118 | WindowGeometry.centerInWindow(
|
---|
119 | getParent(),
|
---|
120 | p
|
---|
121 | )
|
---|
122 | ).applySafe(this);
|
---|
123 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
---|
124 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
125 | }
|
---|
126 | super.setVisible(visible);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void selectPreferencesTabByName(String name) {
|
---|
130 | tpPreferences.selectTabByName(name);
|
---|
131 | }
|
---|
132 |
|
---|
133 | public void selectPreferencesTabByClass(Class<? extends TabPreferenceSetting> clazz) {
|
---|
134 | tpPreferences.selectTabByPref(clazz);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void selectSubPreferencesTabByClass(Class<? extends SubPreferenceSetting> clazz) {
|
---|
138 | tpPreferences.selectSubTabByPref(clazz);
|
---|
139 | }
|
---|
140 |
|
---|
141 | class CancelAction extends AbstractAction {
|
---|
142 | public CancelAction() {
|
---|
143 | putValue(NAME, tr("Cancel"));
|
---|
144 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
145 | putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates"));
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void cancel() {
|
---|
149 | setCanceled(true);
|
---|
150 | setVisible(false);
|
---|
151 | tpPreferences.validationListeners.clear();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void actionPerformed(ActionEvent evt) {
|
---|
155 | cancel();
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | class OKAction extends AbstractAction {
|
---|
160 | public OKAction() {
|
---|
161 | putValue(NAME, tr("OK"));
|
---|
162 | putValue(SMALL_ICON, ImageProvider.get("ok"));
|
---|
163 | putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog"));
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void actionPerformed(ActionEvent evt) {
|
---|
167 | for (ValidationListener listener: tpPreferences.validationListeners) {
|
---|
168 | if (!listener.validatePreferences())
|
---|
169 | return;
|
---|
170 | }
|
---|
171 |
|
---|
172 | tpPreferences.savePreferences();
|
---|
173 | tpPreferences.validationListeners.clear();
|
---|
174 | setCanceled(false);
|
---|
175 | setVisible(false);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | class WindowEventHandler extends WindowAdapter {
|
---|
180 | @Override
|
---|
181 | public void windowClosing(WindowEvent arg0) {
|
---|
182 | new CancelAction().cancel();
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void selectMapPaintPreferenceTab() {
|
---|
187 | tpPreferences.selectSubTabByPref(MapPaintPreference.class);
|
---|
188 | }
|
---|
189 | }
|
---|