1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
alfonx |
862 |
* Stefan A. Tzeggai - additional utility classes |
29 |
alfonx |
244 |
******************************************************************************/ |
30 |
|
|
package skrueger.swing; |
31 |
|
|
|
32 |
|
|
import java.awt.BorderLayout; |
33 |
|
|
import java.awt.Component; |
34 |
|
|
import java.awt.FlowLayout; |
35 |
|
|
import java.awt.Window; |
36 |
|
|
import java.awt.event.ActionEvent; |
37 |
|
|
import java.awt.event.ActionListener; |
38 |
|
|
import java.awt.event.KeyEvent; |
39 |
|
|
import java.awt.event.WindowAdapter; |
40 |
|
|
import java.awt.event.WindowEvent; |
41 |
|
|
|
42 |
|
|
import javax.swing.AbstractAction; |
43 |
|
|
import javax.swing.Action; |
44 |
|
|
import javax.swing.JButton; |
45 |
|
|
import javax.swing.JComponent; |
46 |
|
|
import javax.swing.JDialog; |
47 |
|
|
import javax.swing.JPanel; |
48 |
|
|
import javax.swing.JRootPane; |
49 |
|
|
import javax.swing.KeyStroke; |
50 |
|
|
|
51 |
|
|
import schmitzm.swing.SwingUtil; |
52 |
|
|
|
53 |
alfonx |
420 |
public class TranslationAskJDialog extends CancellableDialogAdapter{ |
54 |
alfonx |
244 |
|
55 |
|
|
private OkButton okButton; |
56 |
|
|
private CancelButton cancelButton; |
57 |
|
|
|
58 |
|
|
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
59 |
|
|
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
60 |
|
|
|
61 |
|
|
private JComponent[] translationEditJPanelsOrJustComponents; |
62 |
|
|
|
63 |
|
|
private boolean hasBeenCanceled; |
64 |
|
|
|
65 |
|
|
private JButton[] optionalButtons; |
66 |
alfonx |
420 |
private TranslationsAskJPanel translationsAskPane; |
67 |
alfonx |
244 |
|
68 |
|
|
/** |
69 |
|
|
* Since the registerKeyboardAction() method is part of the JComponent class |
70 |
|
|
* definition, you must define the Escape keystroke and register the |
71 |
|
|
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
72 |
|
|
* the JDialog serves as an excellent choice to associate the registration, |
73 |
|
|
* as this will always be visible. If you override the protected |
74 |
|
|
* createRootPane() method of JDialog, you can return your custom JRootPane |
75 |
|
|
* with the keystroke enabled: |
76 |
|
|
*/ |
77 |
|
|
@Override |
78 |
|
|
protected JRootPane createRootPane() { |
79 |
|
|
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
80 |
|
|
JRootPane rootPane = new JRootPane(); |
81 |
|
|
rootPane.registerKeyboardAction(new ActionListener() { |
82 |
|
|
|
83 |
|
|
public void actionPerformed(ActionEvent e) { |
84 |
|
|
cancel(); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
88 |
|
|
|
89 |
|
|
return rootPane; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* The {@link TranslationAskJDialog} fills its content pane with an |
94 |
|
|
* arbitrary number of components. If these {@link Component}s are |
95 |
|
|
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
96 |
|
|
* values and restore them if the dialog is canceled. Other |
97 |
|
|
* {@link JComponent}s are just displayed.<br/> |
98 |
|
|
* This class handles the cancel button itself. You may still want to listen |
99 |
|
|
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
100 |
|
|
* to be set visible afterwards.<br/> |
101 |
|
|
* |
102 |
|
|
* @param owner |
103 |
|
|
* A component of the GUI that this dialog is related to. If no |
104 |
|
|
* {@link Window} is passed, SwingUtil.getParentWindow(owner) is |
105 |
|
|
* called. |
106 |
|
|
*/ |
107 |
|
|
public TranslationAskJDialog(Component owner, |
108 |
|
|
final JComponent... translationEditJPanels) { |
109 |
|
|
super(SwingUtil.getParentWindow(owner)); |
110 |
|
|
setComponents(translationEditJPanels); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* The {@link TranslationAskJDialog} fills its content pane with an |
115 |
|
|
* arbitrary number of components. If these {@link Component}s are |
116 |
|
|
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
117 |
|
|
* values and restore them if the dialog is canceled. Other |
118 |
|
|
* {@link JComponent}s are just displayed.<br/> |
119 |
|
|
* This class handles the cancel button itself. You may still want to listen |
120 |
|
|
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
121 |
|
|
* to be set visible afterwards.<br/> |
122 |
|
|
* Using this constructor, you have to call setComponents afterwards. |
123 |
|
|
*/ |
124 |
|
|
public TranslationAskJDialog(Component owner) { |
125 |
alfonx |
302 |
super(SwingUtil.getParentWindow(owner)); |
126 |
alfonx |
244 |
} |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* The {@link TranslationAskJDialog} fills its content pane with an |
130 |
|
|
* arbitrary number of components. If these {@link Component}s are |
131 |
|
|
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
132 |
|
|
* values and restore them if the dialog is canceled. Other |
133 |
|
|
* {@link JComponent}s are just displayed. |
134 |
|
|
* |
135 |
|
|
* @param translationEditJPanels |
136 |
|
|
* Arbitrary list of {@link JComponent}s and |
137 |
|
|
* {@link TranslationEditJPanel}s. |
138 |
|
|
*/ |
139 |
|
|
public void setComponents(final JComponent... translationEditJPanels) { |
140 |
|
|
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
141 |
|
|
|
142 |
alfonx |
302 |
init(); |
143 |
|
|
} |
144 |
|
|
|
145 |
alfonx |
244 |
|
146 |
|
|
private void init() { |
147 |
|
|
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
148 |
|
|
addWindowListener(new WindowAdapter() { |
149 |
|
|
|
150 |
|
|
public void windowClosing(WindowEvent e) { |
151 |
|
|
cancel(); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
}); |
155 |
alfonx |
420 |
|
156 |
|
|
translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents); |
157 |
alfonx |
244 |
JPanel cp = new JPanel(new BorderLayout()); |
158 |
alfonx |
420 |
cp.add(translationsAskPane, BorderLayout.WEST); |
159 |
alfonx |
244 |
cp.add(getButtons(), BorderLayout.SOUTH); |
160 |
|
|
setContentPane(cp); |
161 |
alfonx |
302 |
|
162 |
|
|
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
163 |
alfonx |
244 |
setModal(true); |
164 |
|
|
pack(); |
165 |
alfonx |
617 |
SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5); |
166 |
alfonx |
244 |
} |
167 |
|
|
|
168 |
alfonx |
420 |
public void setOptionalButtons(JButton... optionalButtons) { |
169 |
alfonx |
244 |
this.optionalButtons = optionalButtons; |
170 |
|
|
init(); |
171 |
|
|
} |
172 |
|
|
|
173 |
alfonx |
302 |
/** |
174 |
alfonx |
419 |
* Called when the dilaog is closed using the cancel button. When |
175 |
|
|
* overwriting this method, call super.cancel() after restoring your |
176 |
|
|
* properties. |
177 |
alfonx |
302 |
*/ |
178 |
alfonx |
420 |
@Override |
179 |
alfonx |
419 |
public void cancel() { |
180 |
alfonx |
420 |
translationsAskPane.cancel(); |
181 |
alfonx |
244 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
182 |
alfonx |
420 |
hasBeenCanceled = true; |
183 |
alfonx |
244 |
setVisible(false); |
184 |
|
|
dispose(); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
|
private JComponent getButtons() { |
189 |
|
|
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
190 |
|
|
|
191 |
|
|
if (optionalButtons != null) |
192 |
|
|
for (JButton b : optionalButtons) { |
193 |
|
|
jPanel.add(b); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
if (okButton == null) { |
197 |
|
|
okButton = new OkButton(new AbstractAction() { |
198 |
|
|
{ |
199 |
|
|
// Set a mnemonic character. In most look and feels, this |
200 |
|
|
// causes the |
201 |
|
|
// specified character to be underlined This indicates that |
202 |
|
|
// if the component |
203 |
|
|
// using this action has the focus and In some look and |
204 |
|
|
// feels, this causes |
205 |
|
|
// the specified character in the label to be underlined and |
206 |
|
|
putValue(Action.MNEMONIC_KEY, new Integer( |
207 |
|
|
java.awt.event.KeyEvent.VK_E)); |
208 |
|
|
|
209 |
alfonx |
302 |
// // Set tool tip text |
210 |
|
|
// putValue(Action.SHORT_DESCRIPTION, |
211 |
|
|
// "Accept the changes made to the translation."); //i8n |
212 |
alfonx |
244 |
|
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
public void actionPerformed(ActionEvent evt) { |
216 |
|
|
|
217 |
alfonx |
302 |
okClose(); |
218 |
|
|
|
219 |
alfonx |
244 |
} |
220 |
|
|
|
221 |
|
|
}); |
222 |
|
|
|
223 |
|
|
} |
224 |
|
|
jPanel.add(okButton); |
225 |
|
|
|
226 |
|
|
if (cancelButton == null) { |
227 |
|
|
cancelButton = new CancelButton(new AbstractAction("") { |
228 |
|
|
public void actionPerformed(ActionEvent evt) { |
229 |
alfonx |
302 |
cancel(); |
230 |
alfonx |
244 |
} |
231 |
|
|
}); |
232 |
|
|
} |
233 |
|
|
jPanel.add(cancelButton); |
234 |
|
|
|
235 |
|
|
return jPanel; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
alfonx |
420 |
* This method is only called when the dialog is closed and not canceled. |
240 |
alfonx |
302 |
* Can be overwritten to do anything when the dialog has been accepted. |
241 |
|
|
*/ |
242 |
alfonx |
420 |
public boolean okClose() { |
243 |
|
|
|
244 |
|
|
if (!translationsAskPane.checkValidInputs()) |
245 |
|
|
return false; |
246 |
|
|
|
247 |
|
|
|
248 |
alfonx |
302 |
dispose(); |
249 |
alfonx |
420 |
|
250 |
|
|
TranslationAskJDialog.this.firePropertyChange( |
251 |
|
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
252 |
|
|
return true; |
253 |
alfonx |
302 |
} |
254 |
|
|
|
255 |
alfonx |
244 |
|
256 |
|
|
|
257 |
|
|
/** |
258 |
|
|
* After the modal dialog has been closed, this allows to find out, whether |
259 |
alfonx |
420 |
* the {@link Component} has been canceled. |
260 |
alfonx |
244 |
* |
261 |
|
|
* @return <code>true</code> if the {@link JDialog} has been canceled. |
262 |
|
|
*/ |
263 |
|
|
public boolean isCancelled() { |
264 |
|
|
return hasBeenCanceled; |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
} |