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