/[schmitzm]/trunk/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Annotation of /trunk/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (hide annotations)
Sat May 16 21:25:44 2009 UTC (15 years, 9 months ago) by alfonx
File size: 9209 byte(s)
* Possibility to ass more buttons to the button panel by #setButtons(JButton... )
1 mojays 2 package skrueger.swing;
2    
3     import java.awt.BorderLayout;
4 alfonx 38 import java.awt.Component;
5 mojays 2 import java.awt.FlowLayout;
6     import java.awt.Window;
7     import java.awt.event.ActionEvent;
8     import java.awt.event.ActionListener;
9     import java.awt.event.KeyEvent;
10     import java.awt.event.WindowAdapter;
11     import java.awt.event.WindowEvent;
12     import java.util.Locale;
13    
14     import javax.swing.AbstractAction;
15     import javax.swing.Action;
16     import javax.swing.Box;
17 alfonx 120 import javax.swing.JButton;
18 mojays 2 import javax.swing.JComponent;
19     import javax.swing.JDialog;
20 alfonx 38 import javax.swing.JOptionPane;
21 mojays 2 import javax.swing.JPanel;
22     import javax.swing.JRootPane;
23     import javax.swing.KeyStroke;
24    
25     import schmitzm.lang.LangUtil;
26     import schmitzm.lang.ResourceProvider;
27     import schmitzm.swing.SwingUtil;
28     import skrueger.i8n.Translation;
29    
30     public class TranslationAskJDialog extends JDialog {
31    
32     /**
33     * {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des
34     * Package {@code skrueger.swing} zur Verfuegung stellt. Diese sind in
35     * properties-Datein unter {@code skrueger.swing.resource.locales}
36     * hinterlegt.
37     */
38     public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
39     .extendPackagePath(TranslationAskJDialog.class,
40     "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
41    
42     private String[] backup = new String[50]; // Maximum 50 languages ;-)
43     private OkButton okButton;
44     private CancelButton cancelButton;
45    
46     public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
47     public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
48    
49 alfonx 38 private JComponent[] translationEditJPanelsOrJustComponents;
50 mojays 2
51     private boolean hasBeenCanceled;
52    
53 alfonx 120 private JButton[] optionalButtons;
54    
55 mojays 2 /**
56     * Since the registerKeyboardAction() method is part of the JComponent class
57     * definition, you must define the Escape keystroke and register the
58     * keyboard action with a JComponent, not with a JDialog. The JRootPane for
59     * the JDialog serves as an excellent choice to associate the registration,
60     * as this will always be visible. If you override the protected
61     * createRootPane() method of JDialog, you can return your custom JRootPane
62     * with the keystroke enabled:
63     */
64     @Override
65     protected JRootPane createRootPane() {
66     KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
67     JRootPane rootPane = new JRootPane();
68     rootPane.registerKeyboardAction(new ActionListener() {
69    
70     public void actionPerformed(ActionEvent e) {
71     cancel();
72     }
73    
74     }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
75    
76     return rootPane;
77     }
78    
79     /**
80 alfonx 38 * The {@link TranslationAskJDialog} fills its content pane with an
81     * arbitrary number of components. If these {@link Component}s are
82     * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
83     * values and restore them if the dialog is canceled. Other
84     * {@link JComponent}s are just displayed.<br/>
85 mojays 2 * This class handles the cancel button itself. You may still want to listen
86 alfonx 38 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
87     * to be set visible afterwards.<br/>
88 alfonx 116 *
89     * @param owner
90     * A component of the GUI that this dialog is related to. If no
91     * {@link Window} is passed, SwingUtil.getParentWindow(owner) is
92     * called.
93 mojays 2 */
94 alfonx 116 public TranslationAskJDialog(Component owner,
95 mojays 2 final JComponent... translationEditJPanels) {
96 alfonx 116 super(owner instanceof Window ? (Window) owner : SwingUtil
97     .getParentWindow(owner));
98 alfonx 38 setComponents(translationEditJPanels);
99 mojays 2 }
100    
101     /**
102 alfonx 38 * The {@link TranslationAskJDialog} fills its content pane with an
103     * arbitrary number of components. If these {@link Component}s are
104     * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
105     * values and restore them if the dialog is canceled. Other
106     * {@link JComponent}s are just displayed.<br/>
107 mojays 2 * This class handles the cancel button itself. You may still want to listen
108 alfonx 33 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
109 alfonx 38 * to be set visible afterwards.<br/>
110     * Using this constructor, you have to call setComponents afterwards.
111 mojays 2 */
112 alfonx 38 public TranslationAskJDialog(Window owner) {
113 alfonx 116 this(owner, new JComponent[] {});
114 alfonx 38 }
115    
116     /**
117     * The {@link TranslationAskJDialog} fills its content pane with an
118     * arbitrary number of components. If these {@link Component}s are
119     * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
120     * values and restore them if the dialog is canceled. Other
121     * {@link JComponent}s are just displayed.
122     *
123     * @param translationEditJPanels
124     * Arbitrary list of {@link JComponent}s and
125     * {@link TranslationEditJPanel}s.
126     */
127     public void setComponents(final JComponent... translationEditJPanels) {
128 mojays 2 this.translationEditJPanelsOrJustComponents = translationEditJPanels;
129 alfonx 33
130 alfonx 38 // Remember backups for all the TranslationEditJPanel
131 alfonx 33 int count = 0;
132     for (JComponent component : translationEditJPanelsOrJustComponents) {
133     if (component instanceof TranslationEditJPanel) {
134     TranslationEditJPanel tep = (TranslationEditJPanel) component;
135     Translation orig = tep.getTranslation();
136 alfonx 38
137 alfonx 34 // We don't want to overwrite the Translation object on
138     // restore(). We just want to change its value.
139 alfonx 33 backup[count++] = orig.toOneLine();
140     }
141     }
142 alfonx 38
143     init();
144 mojays 2 }
145    
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     SwingUtil.centerFrameOnScreen(this);
156     Box box = Box.createVerticalBox();
157     for (JComponent panel : translationEditJPanelsOrJustComponents) {
158     box.add(panel);
159     }
160     JPanel cp = new JPanel(new BorderLayout());
161     cp.add(box, BorderLayout.CENTER);
162     cp.add(getButtons(), BorderLayout.SOUTH);
163     setContentPane(cp);
164    
165     setTitle(RESOURCE.getString("translation_dialog_title")); // i8n
166     setModal(true);
167     pack();
168     }
169    
170 alfonx 120 public void setButtons(JButton... optionalButtons) {
171     this.optionalButtons = optionalButtons;
172     init();
173     }
174    
175 mojays 2 protected void cancel() {
176     firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
177     restore();
178     setVisible(false);
179     dispose();
180     }
181    
182 alfonx 38 protected void restore() {
183 mojays 2 int count = 0;
184     for (JComponent component : translationEditJPanelsOrJustComponents) {
185     if (component instanceof TranslationEditJPanel) {
186     TranslationEditJPanel tep = (TranslationEditJPanel) component;
187 alfonx 33 tep.getTranslation().fromOneLine(backup[count++]);
188 mojays 2 }
189     }
190     }
191    
192     private JComponent getButtons() {
193     JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
194 alfonx 120
195     if (optionalButtons != null)
196     for (JButton b : optionalButtons) {
197     jPanel.add(b);
198     }
199    
200 mojays 2 if (okButton == null) {
201     okButton = new OkButton(new AbstractAction() {
202     {
203     // Set a mnemonic character. In most look and feels, this
204     // causes the
205     // specified character to be underlined This indicates that
206     // if the component
207     // using this action has the focus and In some look and
208     // feels, this causes
209     // the specified character in the label to be underlined and
210     putValue(Action.MNEMONIC_KEY, new Integer(
211     java.awt.event.KeyEvent.VK_E));
212    
213     // Set tool tip text
214     putValue(Action.SHORT_DESCRIPTION,
215     "Accept the changes made to the translation.");
216    
217     }
218    
219     public void actionPerformed(ActionEvent evt) {
220     TranslationAskJDialog.this.firePropertyChange(
221     PROPERTY_APPLY_AND_CLOSE, null, null);
222 alfonx 116
223     if (!checkValidInputs())
224     return;
225    
226 mojays 2 setVisible(false);
227     dispose();
228     }
229    
230     });
231 alfonx 38
232 mojays 2 }
233     jPanel.add(okButton);
234    
235     if (cancelButton == null) {
236     cancelButton = new CancelButton(new AbstractAction("") {
237     public void actionPerformed(ActionEvent evt) {
238 alfonx 38 restore();
239 mojays 2 TranslationAskJDialog.this.firePropertyChange(
240     PROPERTY_CANCEL_AND_CLOSE, null, null);
241     setVisible(false);
242 alfonx 38 setCancelled(true);
243 mojays 2 dispose();
244     }
245     });
246     }
247     jPanel.add(cancelButton);
248    
249     return jPanel;
250     }
251    
252 alfonx 38 /**
253 alfonx 116 * @return <code>true</code> if none of the translations contains illegal
254     * characters.
255 alfonx 38 */
256     protected boolean checkValidInputs() {
257 alfonx 116
258 alfonx 38 for (JComponent component : translationEditJPanelsOrJustComponents) {
259     if (component instanceof TranslationEditJPanel) {
260     TranslationEditJPanel tep = (TranslationEditJPanel) component;
261 alfonx 116
262     for (String l : tep.getTranslation().values()) {
263     if (l.contains("{") || l.contains("}")) {
264     JOptionPane
265     .showMessageDialog(
266     this,
267     RESOURCE
268     .getString("ErrorMsg.InvalidCharacterInTranslation"));
269 alfonx 38 return false;
270     }
271     }
272 alfonx 116
273 alfonx 38 }
274     }
275 alfonx 116
276 alfonx 38 return true;
277 mojays 2 }
278    
279 alfonx 38 private void setCancelled(boolean hasBeenCanceled) {
280 mojays 2 this.hasBeenCanceled = hasBeenCanceled;
281     }
282    
283     /**
284 alfonx 34 * After the modal dialog has been closed, this allows to find out, whether
285 alfonx 33 * the dialog has been canceled.
286     *
287 alfonx 34 * @return <code>true</code> if the {@link JDialog} has been canceled.
288 mojays 2 */
289 alfonx 38 public boolean isCancelled() {
290 mojays 2 return hasBeenCanceled;
291     }
292    
293     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26