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

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

apply patches from xeen for #1977.

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