source: josm/trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java@ 2003

Last change on this file since 2003 was 2003, checked in by Gubaer, 15 years ago

applied #3312: patch by plaicy: Set this.parent of ExtendedDialog

File size: 7.1 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import java.awt.Component;
4import java.awt.Dimension;
5import java.awt.GridBagLayout;
6import java.awt.Toolkit;
7import java.awt.event.ActionEvent;
8import java.util.ArrayList;
9
10import javax.swing.AbstractAction;
11import javax.swing.Action;
12import javax.swing.JButton;
13import javax.swing.JComponent;
14import javax.swing.JDialog;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JScrollBar;
18import javax.swing.JScrollPane;
19import javax.swing.KeyStroke;
20
21import org.openstreetmap.josm.tools.GBC;
22import org.openstreetmap.josm.tools.ImageProvider;
23
24
25public class ExtendedDialog extends JDialog {
26 private int result = 0;
27 private Component parent;
28 private final String[] bTexts;
29
30 // For easy access when inherited
31 protected Object contentConstraints = GBC.eol().anchor(GBC.CENTER).fill(GBC.HORIZONTAL).insets(5,10,5,0);
32 protected ArrayList<JButton> buttons = new ArrayList<JButton>();
33
34 /**
35 * Sets up the dialog. The first button is always the default.
36 * @param parent The parent element that will be used for position and maximum size
37 * @param title The text that will be shown in the window titlebar
38 * @param content Any component that should be show above the buttons (e.g. JLabel)
39 * @param buttonTexts The labels that will be displayed on the buttons
40 * @param buttonIcons The path to the icons that will be displayed on the buttons. Path is relative to JOSM's image directory. File extensions need to be included. If a button should not have an icon pass null.
41 */
42 public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts, String[] buttonIcons) {
43 super(JOptionPane.getFrameForComponent(parent), title, true /* modal */);
44 this.parent = parent;
45 bTexts = buttonTexts;
46 setupDialog(content, buttonIcons);
47 setVisible(true);
48 }
49
50 public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts) {
51 this(parent, title, content, buttonTexts, null);
52 }
53
54 /**
55 * Sets up the dialog and displays the given message in a breakable label
56 */
57 public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts, String[] buttonIcons) {
58 super(JOptionPane.getFrameForComponent(parent), title, true);
59
60 JMultilineLabel lbl = new JMultilineLabel(message);
61 // Make it not wider than 2/3 of the screen
62 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
63 lbl.setMaxWidth(Math.round(screenSize.width*2/3));
64
65 this.parent = parent;
66 bTexts = buttonTexts;
67 setupDialog(lbl, buttonIcons);
68 setVisible(true);
69 }
70
71 public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts) {
72 this(parent, title, message, buttonTexts, null);
73 }
74
75 /**
76 * Constructor that doesn't make the dialog visible immediately. Intended for when inheriting.
77 */
78 public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
79 super(JOptionPane.getFrameForComponent(parent), title, modal);
80 this.parent = parent;
81 bTexts = buttonTexts;
82 }
83
84 protected void setupDialog(Component content, String[] buttonIcons) {
85 setupEscListener();
86
87 JButton button;
88 JPanel buttonsPanel = new JPanel(new GridBagLayout());
89
90 for(int i=0; i < bTexts.length; i++) {
91 Action action = new AbstractAction(bTexts[i]) {
92 public void actionPerformed(ActionEvent evt) {
93 buttonAction(evt);
94 }
95 };
96
97 button = new JButton(action);
98 if(buttonIcons != null && buttonIcons[i] != null) {
99 button.setIcon(ImageProvider.get(buttonIcons[i]));
100 }
101
102 if(i == 0) {
103 rootPane.setDefaultButton(button);
104 }
105 buttonsPanel.add(button, GBC.std().insets(2,2,2,2));
106 buttons.add(button);
107 }
108
109 JPanel cp = new JPanel(new GridBagLayout());
110 cp.add(content, contentConstraints);
111 cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5));
112
113 JScrollPane pane = new JScrollPane(cp);
114 pane.setBorder(null);
115 setContentPane(pane);
116
117 pack();
118
119 // Try to make it not larger than the parent window or at least not larger than 2/3 of the screen
120 Dimension d = getSize();
121 Dimension x = findMaxDialogSize();
122
123 boolean limitedInWidth = d.width > x.width;
124 boolean limitedInHeight = d.height > x.height;
125
126 if(x.width > 0 && d.width > x.width) {
127 d.width = x.width;
128 }
129 if(x.height > 0 && d.height > x.height) {
130 d.height = x.height;
131 }
132
133 // We have a vertical scrollbar and enough space to prevent a horizontal one
134 if(!limitedInWidth && limitedInHeight) {
135 d.width += new JScrollBar().getPreferredSize().width;
136 }
137
138 setSize(d);
139 setLocationRelativeTo(parent);
140 }
141
142 /**
143 * @return int The selected button. The count starts with 1.
144 * A return value of 0 means the dialog has been closed otherwise.
145 */
146 public int getValue() {
147 return result;
148 }
149
150 /**
151 * This gets performed whenever a button is clicked or activated
152 * @param evt the button event
153 */
154 protected void buttonAction(ActionEvent evt) {
155 String a = evt.getActionCommand();
156 for(int i=0; i < bTexts.length; i++)
157 if(bTexts[i].equals(a)) {
158 result = i+1;
159 break;
160 }
161
162 setVisible(false);
163 }
164
165 /**
166 * Tries to find a good value of how large the dialog should be
167 * @return Dimension Size of the parent Component or 2/3 of screen size if not available
168 */
169 protected Dimension findMaxDialogSize() {
170 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
171 Dimension x = new Dimension(Math.round(screenSize.width*2/3),
172 Math.round(screenSize.height*2/3));
173 try {
174 if(parent != null) {
175 x = JOptionPane.getFrameForComponent(parent).getSize();
176 }
177 } catch(NullPointerException e) { }
178 return x;
179 }
180
181 /**
182 * Makes the dialog listen to ESC keypressed
183 */
184 private void setupEscListener() {
185 Action actionListener = new AbstractAction() {
186 public void actionPerformed(ActionEvent actionEvent) {
187 // 0 means that the dialog has been closed otherwise.
188 // We need to set it to zero again, in case the dialog has been re-used
189 // and the result differs from its default value
190 result = 0;
191 setVisible(false);
192 }
193 };
194
195 rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
196 .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
197 rootPane.getActionMap().put("ESCAPE", actionListener);
198 }
199
200 @Override
201 public void setVisible(boolean visible) {
202 super.setVisible(visible);
203 if (visible) {
204 toFront();
205 }
206 }
207}
Note: See TracBrowser for help on using the repository browser.