1 |
package skrueger.swing; |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
import java.awt.BorderLayout; |
* |
4 |
import java.awt.Dialog; |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
import java.awt.FlowLayout; |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
import java.awt.Window; |
* and the Geotools library. |
7 |
import java.awt.event.ActionEvent; |
* |
8 |
import java.awt.event.ActionListener; |
* The SCHMITZM project is hosted at: |
9 |
import java.awt.event.KeyEvent; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import java.awt.event.WindowAdapter; |
* |
11 |
import java.awt.event.WindowEvent; |
* This program is free software; you can redistribute it and/or |
12 |
import java.util.ArrayList; |
* modify it under the terms of the GNU Lesser General Public License |
13 |
import java.util.Locale; |
* as published by the Free Software Foundation; either version 3 |
14 |
|
* of the License, or (at your option) any later version. |
15 |
import javax.swing.AbstractAction; |
* |
16 |
import javax.swing.Action; |
* This program is distributed in the hope that it will be useful, |
17 |
import javax.swing.Box; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import javax.swing.JComponent; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import javax.swing.JDialog; |
* GNU General Public License for more details. |
20 |
import javax.swing.JPanel; |
* |
21 |
import javax.swing.JRootPane; |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
import javax.swing.KeyStroke; |
* 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 |
import schmitzm.lang.LangUtil; |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
import schmitzm.lang.ResourceProvider; |
* |
26 |
import schmitzm.swing.SwingUtil; |
* Contributors: |
27 |
import skrueger.i8n.Translation; |
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
* Stefan A. Krüger - additional utility classes |
29 |
public class TranslationAskJDialog extends JDialog { |
******************************************************************************/ |
30 |
|
package skrueger.swing; |
31 |
/** |
|
32 |
* {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des |
import java.awt.BorderLayout; |
33 |
* Package {@code skrueger.swing} zur Verfuegung stellt. Diese sind in |
import java.awt.Component; |
34 |
* properties-Datein unter {@code skrueger.swing.resource.locales} |
import java.awt.FlowLayout; |
35 |
* hinterlegt. |
import java.awt.Window; |
36 |
*/ |
import java.awt.event.ActionEvent; |
37 |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
import java.awt.event.ActionListener; |
38 |
.extendPackagePath(TranslationAskJDialog.class, |
import java.awt.event.KeyEvent; |
39 |
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
import java.awt.event.WindowAdapter; |
40 |
|
import java.awt.event.WindowEvent; |
41 |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
|
42 |
private OkButton okButton; |
import javax.swing.AbstractAction; |
43 |
private CancelButton cancelButton; |
import javax.swing.Action; |
44 |
|
import javax.swing.BorderFactory; |
45 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
import javax.swing.Box; |
46 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
import javax.swing.JButton; |
47 |
|
import javax.swing.JComponent; |
48 |
private final JComponent[] translationEditJPanelsOrJustComponents; |
import javax.swing.JDialog; |
49 |
|
import javax.swing.JOptionPane; |
50 |
private boolean hasBeenCanceled; |
import javax.swing.JPanel; |
51 |
|
import javax.swing.JRootPane; |
52 |
/** |
import javax.swing.KeyStroke; |
53 |
* Since the registerKeyboardAction() method is part of the JComponent class |
|
54 |
* definition, you must define the Escape keystroke and register the |
import schmitzm.swing.SwingUtil; |
55 |
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
import skrueger.i8n.Translation; |
56 |
* the JDialog serves as an excellent choice to associate the registration, |
|
57 |
* as this will always be visible. If you override the protected |
public class TranslationAskJDialog extends JDialog { |
58 |
* createRootPane() method of JDialog, you can return your custom JRootPane |
|
59 |
* with the keystroke enabled: |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
60 |
*/ |
private OkButton okButton; |
61 |
@Override |
private CancelButton cancelButton; |
62 |
protected JRootPane createRootPane() { |
|
63 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
64 |
JRootPane rootPane = new JRootPane(); |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
65 |
rootPane.registerKeyboardAction(new ActionListener() { |
|
66 |
|
private JComponent[] translationEditJPanelsOrJustComponents; |
67 |
public void actionPerformed(ActionEvent e) { |
|
68 |
cancel(); |
private boolean hasBeenCanceled; |
69 |
} |
|
70 |
|
private JButton[] optionalButtons; |
71 |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
|
72 |
|
/** |
73 |
return rootPane; |
* Since the registerKeyboardAction() method is part of the JComponent class |
74 |
} |
* definition, you must define the Escape keystroke and register the |
75 |
|
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
76 |
/** |
* the JDialog serves as an excellent choice to associate the registration, |
77 |
* This class handles the cancel button itself. You may still want to listen |
* as this will always be visible. If you override the protected |
78 |
* to PROPERTY_APPLY_AND_CLOSE events. |
* createRootPane() method of JDialog, you can return your custom JRootPane |
79 |
* |
* with the keystroke enabled: |
80 |
* This dialog is modal. The dialog has to be set visible afterwards. |
*/ |
81 |
*/ |
@Override |
82 |
public TranslationAskJDialog(Dialog owner, |
protected JRootPane createRootPane() { |
83 |
final JComponent... translationEditJPanels) { |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
84 |
super(owner); |
JRootPane rootPane = new JRootPane(); |
85 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
rootPane.registerKeyboardAction(new ActionListener() { |
86 |
init(); |
|
87 |
} |
public void actionPerformed(ActionEvent e) { |
88 |
|
cancel(); |
89 |
/** |
} |
90 |
* This class handles the cancel button itself. You may still want to listen |
|
91 |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
92 |
* to be set visible afterwards. |
|
93 |
*/ |
return rootPane; |
94 |
public TranslationAskJDialog(Window owner, |
} |
95 |
final JComponent... translationEditJPanels) { |
|
96 |
super(owner); |
/** |
97 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
* The {@link TranslationAskJDialog} fills its content pane with an |
98 |
init(); |
* arbitrary number of components. If these {@link Component}s are |
99 |
|
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
100 |
// Rememebr backups for all the jtextpanels |
* values and restore them if the dialog is canceled. Other |
101 |
int count = 0; |
* {@link JComponent}s are just displayed.<br/> |
102 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
* This class handles the cancel button itself. You may still want to listen |
103 |
if (component instanceof TranslationEditJPanel) { |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
104 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
* to be set visible afterwards.<br/> |
105 |
Translation orig = tep.getTranslation(); |
* |
106 |
// We dont' want to overwrite the Translation object on |
* @param owner |
107 |
// restore(). We just want to change its values. |
* A component of the GUI that this dialog is related to. If no |
108 |
|
* {@link Window} is passed, SwingUtil.getParentWindow(owner) is |
109 |
backup[count++] = orig.toOneLine(); |
* called. |
110 |
} |
*/ |
111 |
} |
public TranslationAskJDialog(Component owner, |
112 |
} |
final JComponent... translationEditJPanels) { |
113 |
|
super(SwingUtil.getParentWindow(owner)); |
114 |
private void init() { |
setComponents(translationEditJPanels); |
115 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
} |
116 |
addWindowListener(new WindowAdapter() { |
|
117 |
|
/** |
118 |
public void windowClosing(WindowEvent e) { |
* The {@link TranslationAskJDialog} fills its content pane with an |
119 |
cancel(); |
* arbitrary number of components. If these {@link Component}s are |
120 |
} |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
121 |
|
* values and restore them if the dialog is canceled. Other |
122 |
}); |
* {@link JComponent}s are just displayed.<br/> |
123 |
SwingUtil.centerFrameOnScreen(this); |
* This class handles the cancel button itself. You may still want to listen |
124 |
Box box = Box.createVerticalBox(); |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
125 |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
* to be set visible afterwards.<br/> |
126 |
box.add(panel); |
* Using this constructor, you have to call setComponents afterwards. |
127 |
} |
*/ |
128 |
JPanel cp = new JPanel(new BorderLayout()); |
public TranslationAskJDialog(Component owner) { |
129 |
cp.add(box, BorderLayout.CENTER); |
super(SwingUtil.getParentWindow(owner)); |
130 |
cp.add(getButtons(), BorderLayout.SOUTH); |
} |
131 |
setContentPane(cp); |
|
132 |
|
/** |
133 |
// dialog.getRootPane().setDefaultButton(okButton); |
* The {@link TranslationAskJDialog} fills its content pane with an |
134 |
|
* arbitrary number of components. If these {@link Component}s are |
135 |
setTitle(RESOURCE.getString("translation_dialog_title")); // i8n |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
136 |
setModal(true); |
* values and restore them if the dialog is canceled. Other |
137 |
pack(); |
* {@link JComponent}s are just displayed. |
138 |
} |
* |
139 |
|
* @param translationEditJPanels |
140 |
protected void cancel() { |
* Arbitrary list of {@link JComponent}s and |
141 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
* {@link TranslationEditJPanel}s. |
142 |
restore(); |
*/ |
143 |
setVisible(false); |
public void setComponents(final JComponent... translationEditJPanels) { |
144 |
dispose(); |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
145 |
} |
|
146 |
|
backup(); |
147 |
private void restore() { |
|
148 |
int count = 0; |
init(); |
149 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
} |
150 |
if (component instanceof TranslationEditJPanel) { |
|
151 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
/** |
152 |
tep.getTranslation().fromOneLine(backup[count++]); |
* Stores the original values of all {@link TranslationEditJPanel}s so |
153 |
} |
* cancel works. |
154 |
} |
*/ |
155 |
} |
protected void backup() { |
156 |
|
// Remember backups for all the TranslationEditJPanel |
157 |
private JComponent getButtons() { |
int count = 0; |
158 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
159 |
if (okButton == null) { |
if (component instanceof TranslationEditJPanel) { |
160 |
okButton = new OkButton(new AbstractAction() { |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
161 |
{ |
Translation orig = tep.getTranslation(); |
162 |
// Set a mnemonic character. In most look and feels, this |
|
163 |
// causes the |
// We don't want to overwrite the Translation object on |
164 |
// specified character to be underlined This indicates that |
// restore(). We just want to change its value. |
165 |
// if the component |
backup[count++] = orig.toOneLine(); |
166 |
// using this action has the focus and In some look and |
} |
167 |
// feels, this causes |
} |
168 |
// the specified character in the label to be underlined and |
} |
169 |
putValue(Action.MNEMONIC_KEY, new Integer( |
|
170 |
java.awt.event.KeyEvent.VK_E)); |
private void init() { |
171 |
|
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
172 |
// Set tool tip text |
addWindowListener(new WindowAdapter() { |
173 |
putValue(Action.SHORT_DESCRIPTION, |
|
174 |
"Accept the changes made to the translation."); |
public void windowClosing(WindowEvent e) { |
175 |
|
cancel(); |
176 |
} |
} |
177 |
|
|
178 |
public void actionPerformed(ActionEvent evt) { |
}); |
179 |
TranslationAskJDialog.this.firePropertyChange( |
SwingUtil.centerFrameOnScreen(this); |
180 |
PROPERTY_APPLY_AND_CLOSE, null, null); |
Box box = Box.createVerticalBox(); |
181 |
setVisible(false); |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
182 |
dispose(); |
panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); |
183 |
System.out.println("OK button action performed"); |
panel.setBorder(BorderFactory.createEmptyBorder(5, 6, 5, 6)); |
184 |
} |
box.add(panel); |
185 |
|
|
186 |
}); |
} |
187 |
// okButton.addKeyListener( new KeyListener() { |
JPanel cp = new JPanel(new BorderLayout()); |
188 |
// |
cp.add(box, BorderLayout.WEST); |
189 |
// public void keyTyped(KeyEvent e) { |
cp.add(getButtons(), BorderLayout.SOUTH); |
190 |
// if () |
setContentPane(cp); |
191 |
// okButton.action(new KEyPreEvent(), what) |
|
192 |
// } |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
193 |
// |
setModal(true); |
194 |
// }); |
pack(); |
195 |
} |
} |
196 |
jPanel.add(okButton); |
|
197 |
|
public void setButtons(JButton... optionalButtons) { |
198 |
if (cancelButton == null) { |
this.optionalButtons = optionalButtons; |
199 |
cancelButton = new CancelButton(new AbstractAction("") { |
init(); |
200 |
public void actionPerformed(ActionEvent evt) { |
} |
201 |
// restore(); |
|
202 |
TranslationAskJDialog.this.firePropertyChange( |
/** |
203 |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
* Called when the dilaog is closed using the cancel button. |
204 |
setVisible(false); |
*/ |
205 |
setHasBeenCanceled(true); |
protected void cancel() { |
206 |
dispose(); |
restore(); |
207 |
} |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
208 |
}); |
setCancelled(true); |
209 |
} |
setVisible(false); |
210 |
jPanel.add(cancelButton); |
dispose(); |
211 |
|
} |
212 |
return jPanel; |
|
213 |
} |
/** |
214 |
|
* Used to restore all the values when cancel has been pressed. |
215 |
public static void main(String[] args) { |
*/ |
216 |
ArrayList<String> lang = new ArrayList<String>(); |
protected void restore() { |
217 |
lang.add("de"); |
int count = 0; |
218 |
lang.add("en"); |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
219 |
lang.add("fr"); |
if (component instanceof TranslationEditJPanel) { |
220 |
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
221 |
Translation transe = new Translation(); |
tep.getTranslation().fromOneLine(backup[count++]); |
222 |
transe.put("de", "Terciopelo-Lanzenotter"); |
} |
223 |
TranslationEditJPanel p1 = new TranslationEditJPanel( |
} |
224 |
"Name von New Group", transe, lang); |
} |
225 |
|
|
226 |
Translation transe2 = new Translation(); |
private JComponent getButtons() { |
227 |
transe2 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
228 |
.put( |
|
229 |
"de", |
if (optionalButtons != null) |
230 |
"Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart."); |
for (JButton b : optionalButtons) { |
231 |
TranslationEditJPanel p2 = new TranslationEditJPanel( |
jPanel.add(b); |
232 |
"Description of Animal:", transe2, lang); |
} |
233 |
|
|
234 |
// JFrame frame = new JFrame(); |
if (okButton == null) { |
235 |
// frame.setContentPane(p1); |
okButton = new OkButton(new AbstractAction() { |
236 |
// frame.pack(); |
{ |
237 |
// frame.setVisible(true); |
// Set a mnemonic character. In most look and feels, this |
238 |
|
// causes the |
239 |
TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2); |
// specified character to be underlined This indicates that |
240 |
dialog.setVisible(true); |
// if the component |
241 |
} |
// using this action has the focus and In some look and |
242 |
|
// feels, this causes |
243 |
private void setHasBeenCanceled(boolean hasBeenCanceled) { |
// the specified character in the label to be underlined and |
244 |
this.hasBeenCanceled = hasBeenCanceled; |
putValue(Action.MNEMONIC_KEY, new Integer( |
245 |
} |
java.awt.event.KeyEvent.VK_E)); |
246 |
|
|
247 |
/** |
// // Set tool tip text |
248 |
* After the modal dialog has been closed, this allows to find out, wether |
// putValue(Action.SHORT_DESCRIPTION, |
249 |
* the dialog has been canceled. |
// "Accept the changes made to the translation."); //i8n |
250 |
* |
|
251 |
* @return |
} |
252 |
*/ |
|
253 |
public boolean isHasBeenCanceled() { |
public void actionPerformed(ActionEvent evt) { |
254 |
return hasBeenCanceled; |
TranslationAskJDialog.this.firePropertyChange( |
255 |
} |
PROPERTY_APPLY_AND_CLOSE, null, null); |
256 |
|
|
257 |
} |
if (!checkValidInputs()) |
258 |
|
return; |
259 |
|
|
260 |
|
okClose(); |
261 |
|
|
262 |
|
} |
263 |
|
|
264 |
|
}); |
265 |
|
|
266 |
|
} |
267 |
|
jPanel.add(okButton); |
268 |
|
|
269 |
|
if (cancelButton == null) { |
270 |
|
cancelButton = new CancelButton(new AbstractAction("") { |
271 |
|
public void actionPerformed(ActionEvent evt) { |
272 |
|
// restore(); |
273 |
|
// TranslationAskJDialog.this.firePropertyChange( |
274 |
|
// PROPERTY_CANCEL_AND_CLOSE, null, null); |
275 |
|
// setVisible(false); |
276 |
|
// setCancelled(true); |
277 |
|
// dispose(); |
278 |
|
cancel(); |
279 |
|
} |
280 |
|
}); |
281 |
|
} |
282 |
|
jPanel.add(cancelButton); |
283 |
|
|
284 |
|
return jPanel; |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
|
* This method is only called when the dialog is closed and not cancelled. |
289 |
|
* Can be overwritten to do anything when the dialog has been accepted. |
290 |
|
*/ |
291 |
|
protected void okClose() { |
292 |
|
setVisible(false); |
293 |
|
dispose(); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
|
* @return <code>true</code> if none of the translations contains illegal |
298 |
|
* characters. |
299 |
|
*/ |
300 |
|
protected boolean checkValidInputs() { |
301 |
|
|
302 |
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
303 |
|
if (component instanceof TranslationEditJPanel) { |
304 |
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
305 |
|
|
306 |
|
for (String l : tep.getTranslation().values()) { |
307 |
|
if (l.contains("{") || l.contains("}")) { |
308 |
|
JOptionPane |
309 |
|
.showMessageDialog( |
310 |
|
this, |
311 |
|
SwingUtil |
312 |
|
.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation")); |
313 |
|
return false; |
314 |
|
} |
315 |
|
} |
316 |
|
|
317 |
|
} |
318 |
|
} |
319 |
|
|
320 |
|
return true; |
321 |
|
} |
322 |
|
|
323 |
|
private void setCancelled(boolean hasBeenCanceled) { |
324 |
|
this.hasBeenCanceled = hasBeenCanceled; |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
|
* After the modal dialog has been closed, this allows to find out, whether |
329 |
|
* the dialog has been canceled. |
330 |
|
* |
331 |
|
* @return <code>true</code> if the {@link JDialog} has been canceled. |
332 |
|
*/ |
333 |
|
public boolean isCancelled() { |
334 |
|
return hasBeenCanceled; |
335 |
|
} |
336 |
|
|
337 |
|
} |