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