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.Insets;
|
---|
12 | import java.awt.Toolkit;
|
---|
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.JComponent;
|
---|
21 | import javax.swing.JDialog;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.KeyStroke;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.gui.SideButton;
|
---|
27 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
28 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
|
---|
30 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
31 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
32 |
|
---|
33 | public class PreferenceDialog extends JDialog {
|
---|
34 |
|
---|
35 | private PreferenceTabbedPane tpPreferences;
|
---|
36 | private boolean canceled;
|
---|
37 |
|
---|
38 | protected JPanel buildActionPanel() {
|
---|
39 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
---|
40 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
41 | pnl.add(new SideButton(new OKAction()));
|
---|
42 | pnl.add(new SideButton(new CancelAction()));
|
---|
43 | pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Action/Preferences"))));
|
---|
44 | return pnl;
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected void build() {
|
---|
48 | Container c = getContentPane();
|
---|
49 | c.setLayout(new BorderLayout());
|
---|
50 | c.add(tpPreferences = new PreferenceTabbedPane(), BorderLayout.CENTER);
|
---|
51 | tpPreferences.buildGui();
|
---|
52 | tpPreferences.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
53 | c.add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
54 |
|
---|
55 | addWindowListener(new WindowEventHandler());
|
---|
56 |
|
---|
57 | getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
|
---|
58 | getRootPane().getActionMap().put("cancel", new CancelAction());
|
---|
59 | HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Action/Preferences"));
|
---|
60 | }
|
---|
61 |
|
---|
62 | public PreferenceDialog(Component parent) {
|
---|
63 | super(JOptionPane.getFrameForComponent(parent), tr("Preferences"), ModalityType.DOCUMENT_MODAL);
|
---|
64 | build();
|
---|
65 | this.setMinimumSize(new Dimension(600, 350));
|
---|
66 | // set the maximum width to the current screen. If the dialog is opened on a
|
---|
67 | // smaller screen than before, this will reset the stored preference.
|
---|
68 | this.setMaximumSize( Toolkit.getDefaultToolkit().getScreenSize());
|
---|
69 | }
|
---|
70 |
|
---|
71 | public boolean isCanceled() {
|
---|
72 | return canceled;
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void setCanceled(boolean canceled) {
|
---|
76 | this.canceled = canceled;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void setVisible(boolean visible) {
|
---|
81 | if (visible) {
|
---|
82 | // Make the pref window at most as large as the parent JOSM window
|
---|
83 | // Have to take window decorations into account or the windows will
|
---|
84 | // be too large
|
---|
85 | Insets i = this.getParent().getInsets();
|
---|
86 | Dimension p = this.getParent().getSize();
|
---|
87 | p = new Dimension(Math.min(p.width-i.left-i.right, 700),
|
---|
88 | Math.min(p.height-i.top-i.bottom, 800));
|
---|
89 | new WindowGeometry(
|
---|
90 | getClass().getName() + ".geometry",
|
---|
91 | WindowGeometry.centerInWindow(
|
---|
92 | getParent(),
|
---|
93 | p
|
---|
94 | )
|
---|
95 | ).applySafe(this);
|
---|
96 | } else if (!visible && isShowing()){
|
---|
97 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
98 | }
|
---|
99 | super.setVisible(visible);
|
---|
100 | }
|
---|
101 |
|
---|
102 | class CancelAction extends AbstractAction {
|
---|
103 | public CancelAction() {
|
---|
104 | putValue(NAME, tr("Cancel"));
|
---|
105 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
106 | putValue(SHORT_DESCRIPTION, tr("Close the preferences dialog and discard preference updates"));
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void cancel() {
|
---|
110 | setCanceled(true);
|
---|
111 | setVisible(false);
|
---|
112 | tpPreferences.validationListeners.clear();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void actionPerformed(ActionEvent evt) {
|
---|
116 | cancel();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | class OKAction extends AbstractAction {
|
---|
121 | public OKAction() {
|
---|
122 | putValue(NAME, tr("OK"));
|
---|
123 | putValue(SMALL_ICON, ImageProvider.get("ok"));
|
---|
124 | putValue(SHORT_DESCRIPTION, tr("Save the preferences and close the dialog"));
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void actionPerformed(ActionEvent evt) {
|
---|
128 | for (ValidationListener listener: tpPreferences.validationListeners) {
|
---|
129 | if (!listener.validatePreferences())
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | tpPreferences.savePreferences();
|
---|
134 | tpPreferences.validationListeners.clear();
|
---|
135 | setCanceled(false);
|
---|
136 | setVisible(false);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | class WindowEventHandler extends WindowAdapter {
|
---|
141 | @Override
|
---|
142 | public void windowClosing(WindowEvent arg0) {
|
---|
143 | new CancelAction().cancel();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public void selectMapPaintPreferenceTab() {
|
---|
148 | tpPreferences.setSelectedComponent(tpPreferences.map);
|
---|
149 | tpPreferences.mapcontent.setSelectedIndex(1);
|
---|
150 | }
|
---|
151 | }
|
---|