/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26