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.event.ActionEvent;
|
---|
12 | import java.awt.event.KeyEvent;
|
---|
13 | import java.awt.event.WindowAdapter;
|
---|
14 | import java.awt.event.WindowEvent;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.BorderFactory;
|
---|
18 | import javax.swing.JComponent;
|
---|
19 | import javax.swing.JDialog;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.KeyStroke;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.gui.SideButton;
|
---|
25 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
26 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
|
---|
28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
29 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
30 |
|
---|
31 | public class PreferenceDialog extends JDialog {
|
---|
32 |
|
---|
33 | private PreferenceTabbedPane tpPreferences;
|
---|
34 | private boolean canceled;
|
---|
35 |
|
---|
36 | protected JPanel buildActionPanel() {
|
---|
37 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
---|
38 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
39 | pnl.add(new SideButton(new OKAction()));
|
---|
40 | pnl.add(new SideButton(new CancelAction()));
|
---|
41 | pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Action/Preferences"))));
|
---|
42 | return pnl;
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected void build() {
|
---|
46 | Container c = getContentPane();
|
---|
47 | c.setLayout(new BorderLayout());
|
---|
48 | c.add(tpPreferences = new PreferenceTabbedPane(), BorderLayout.CENTER);
|
---|
49 | tpPreferences.buildGui();
|
---|
50 | tpPreferences.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
51 | c.add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
52 |
|
---|
53 | addWindowListener(new WindowEventHandler());
|
---|
54 |
|
---|
55 | getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
|
---|
56 | getRootPane().getActionMap().put("cancel", new CancelAction());
|
---|
57 | HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Action/Preferences"));
|
---|
58 | }
|
---|
59 |
|
---|
60 | public PreferenceDialog(Component parent) {
|
---|
61 | super(JOptionPane.getFrameForComponent(parent), tr("Preferences"), true /* modal */);
|
---|
62 | build();
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean isCanceled() {
|
---|
66 | return canceled;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void setCanceled(boolean canceled) {
|
---|
70 | this.canceled = canceled;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void setVisible(boolean visible) {
|
---|
75 | if (visible) {
|
---|
76 | new WindowGeometry(
|
---|
77 | getClass().getName() + ".geometry",
|
---|
78 | WindowGeometry.centerInWindow(
|
---|
79 | getParent(),
|
---|
80 | new Dimension(600,800)
|
---|
81 | )
|
---|
82 | ).applySafe(this);
|
---|
83 | } else if (!visible && isShowing()){
|
---|
84 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
85 | }
|
---|
86 | super.setVisible(visible);
|
---|
87 | }
|
---|
88 |
|
---|
89 | class CancelAction extends AbstractAction {
|
---|
90 | public CancelAction() {
|
---|
91 | putValue(NAME, tr("Cancel"));
|
---|
92 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
93 | putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates"));
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void cancel() {
|
---|
97 | setCanceled(true);
|
---|
98 | setVisible(false);
|
---|
99 | tpPreferences.validationListeners.clear();
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void actionPerformed(ActionEvent evt) {
|
---|
103 | cancel();
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | class OKAction extends AbstractAction {
|
---|
108 | public OKAction() {
|
---|
109 | putValue(NAME, tr("OK"));
|
---|
110 | putValue(SMALL_ICON, ImageProvider.get("ok"));
|
---|
111 | putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog"));
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void actionPerformed(ActionEvent evt) {
|
---|
115 | for (ValidationListener listener: tpPreferences.validationListeners) {
|
---|
116 | if (!listener.validatePreferences())
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | tpPreferences.savePreferences();
|
---|
121 | tpPreferences.validationListeners.clear();
|
---|
122 | setCanceled(false);
|
---|
123 | setVisible(false);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | class WindowEventHandler extends WindowAdapter {
|
---|
128 | @Override
|
---|
129 | public void windowClosing(WindowEvent arg0) {
|
---|
130 | new CancelAction().cancel();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|