source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java@ 4248

Last change on this file since 4248 was 4248, checked in by xeen, 13 years ago

fix #6556

When the prefs are opened for the very first time, the window is now at most
as large as the parent window. After that, the dimensions are stored in JOSM's
preferences, so it won't work for existing installations.

As an additional measure, the dialog cannot be larger than the screen dimension
and as far as my testing goes this setting overwrites the stored dimension.
Unfortunately there is no way to determine the space occupied by application
launchers/taskbars across all systems and the pref dialog may be hidden behind
them.

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Container;
9import java.awt.Dimension;
10import java.awt.FlowLayout;
11import java.awt.Insets;
12import java.awt.Toolkit;
13import java.awt.event.ActionEvent;
14import java.awt.event.KeyEvent;
15import java.awt.event.WindowAdapter;
16import java.awt.event.WindowEvent;
17
18import javax.swing.AbstractAction;
19import javax.swing.BorderFactory;
20import javax.swing.JComponent;
21import javax.swing.JDialog;
22import javax.swing.JOptionPane;
23import javax.swing.JPanel;
24import javax.swing.KeyStroke;
25
26import org.openstreetmap.josm.gui.SideButton;
27import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
28import org.openstreetmap.josm.gui.help.HelpUtil;
29import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
30import org.openstreetmap.josm.tools.ImageProvider;
31import org.openstreetmap.josm.tools.WindowGeometry;
32
33public 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}
Note: See TracBrowser for help on using the repository browser.