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

Last change on this file since 1373 was 1373, checked in by stoecker, 15 years ago

fixed #1425. modified patch by xeen

File size: 4.9 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import java.awt.Component;
4import java.awt.Dimension;
5import java.awt.event.ActionEvent;
6import java.awt.event.ComponentEvent;
7import java.awt.event.ComponentListener;
8import java.awt.GridBagLayout;
9
10import javax.swing.AbstractAction;
11import javax.swing.Action;
12import javax.swing.BorderFactory;
13import javax.swing.BoundedRangeModel;
14import javax.swing.InputMap;
15import javax.swing.JButton;
16import javax.swing.JComponent;
17import javax.swing.JDialog;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JRootPane;
21import javax.swing.JScrollPane;
22import javax.swing.KeyStroke;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.tools.GBC;
26import org.openstreetmap.josm.tools.I18n;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29
30public class ExtendedDialog extends JDialog {
31 private int result = 0;
32 private final String[] bTexts;
33
34 /**
35 * Sets up the dialog. The first button is always the default.
36 * @param Component The parent element that will be used for position and maximum size
37 * @param String The text that will be shown in the window titlebar
38 * @param Component Any component that should be show above the buttons (e.g. JLabel)
39 * @param String[] The labels that will be displayed on the buttons
40 * @param String[] 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);
44 bTexts = buttonTexts;
45 setupDialog(parent, title, content, buttonTexts, buttonIcons);
46 }
47
48 public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts) {
49 super(JOptionPane.getFrameForComponent(parent), title, true);
50 bTexts = buttonTexts;
51 setupDialog(parent, title, content, buttonTexts, null);
52 }
53
54 private void setupDialog(Component parent, String title, Component content, String[] buttonTexts, String[] buttonIcons) {
55 JButton button;
56 JPanel buttonsPanel = new JPanel(new GridBagLayout());
57
58 for(int i=0; i < bTexts.length; i++) {
59 Action action = new AbstractAction(bTexts[i]) {
60 public void actionPerformed(ActionEvent evt) {
61 String a = evt.getActionCommand();
62 for(int i=0; i < bTexts.length; i++)
63 if(bTexts[i].equals(a)) {
64 result = i+1;
65 break;
66 }
67
68 setVisible(false);
69 }
70 };
71
72 button = new JButton(action);
73 if(buttonIcons != null && buttonIcons[i] != null)
74 button.setIcon(ImageProvider.get(buttonIcons[i]));
75
76 if(i == 0) rootPane.setDefaultButton(button);
77 buttonsPanel.add(button, GBC.std().insets(2,2,2,2));
78 }
79
80 JPanel cp = new JPanel(new GridBagLayout());
81 cp.add(content, GBC.eol().anchor(GBC.CENTER).insets(0,10,0,0)); //fill(GBC.HORIZONTAL).
82 cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5));
83
84 JScrollPane pane = new JScrollPane(cp);
85 pane.setBorder(null);
86 setContentPane(pane);
87
88 pack();
89
90 // Try to make it not larger than the parent window or at least not larger than a reasonable value
91 Dimension d = getSize();
92 Dimension x = new Dimension(700, 500);
93 try {
94
95 if(parent != null)
96 x = JOptionPane.getFrameForComponent(parent).getSize();
97 } catch(NullPointerException e) { }
98
99 if(x.width > 0 && d.width > x.width) d.width = x.width;
100 if(x.height > 0 && d.height > x.height) d.height = x.height;
101 setSize(d);
102
103 setLocationRelativeTo(parent);
104
105 setupEscListener();
106 setVisible(true);
107 }
108
109 /**
110 * @return int The selected button. The count starts with 1.
111 * A return value of 0 means the dialog has been closed otherwise.
112 */
113 public int getValue() {
114 return result;
115 }
116
117 /**
118 * Makes the dialog listen to ESC keypressed
119 */
120 private void setupEscListener() {
121 Action actionListener = new AbstractAction() {
122 public void actionPerformed(ActionEvent actionEvent) {
123 setVisible(false);
124 }
125 };
126
127 rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
128 .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
129 rootPane.getActionMap().put("ESCAPE", actionListener);
130 }
131}
Note: See TracBrowser for help on using the repository browser.