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