/[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 34 - (hide annotations)
Sat Mar 28 18:08:42 2009 UTC (15 years, 11 months ago) by alfonx
File size: 7895 byte(s)
Added Listener support to Translation. This allows to register a swing  PropertyChangeListener for Locales changes. 
1 mojays 2 package skrueger.swing;
2    
3     import java.awt.BorderLayout;
4     import java.awt.Dialog;
5     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.ArrayList;
13     import java.util.Locale;
14    
15     import javax.swing.AbstractAction;
16     import javax.swing.Action;
17     import javax.swing.Box;
18     import javax.swing.JComponent;
19     import javax.swing.JDialog;
20     import javax.swing.JPanel;
21     import javax.swing.JRootPane;
22     import javax.swing.KeyStroke;
23    
24     import schmitzm.lang.LangUtil;
25     import schmitzm.lang.ResourceProvider;
26     import schmitzm.swing.SwingUtil;
27     import skrueger.i8n.Translation;
28    
29     public class TranslationAskJDialog extends JDialog {
30    
31     /**
32     * {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des
33     * Package {@code skrueger.swing} zur Verfuegung stellt. Diese sind in
34     * properties-Datein unter {@code skrueger.swing.resource.locales}
35     * hinterlegt.
36     */
37     public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
38     .extendPackagePath(TranslationAskJDialog.class,
39     "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
40    
41     private String[] backup = new String[50]; // Maximum 50 languages ;-)
42     private OkButton okButton;
43     private CancelButton cancelButton;
44    
45     public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
46     public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
47    
48     private final JComponent[] translationEditJPanelsOrJustComponents;
49    
50     private boolean hasBeenCanceled;
51    
52     /**
53     * Since the registerKeyboardAction() method is part of the JComponent class
54     * definition, you must define the Escape keystroke and register the
55     * keyboard action with a JComponent, not with a JDialog. The JRootPane for
56     * the JDialog serves as an excellent choice to associate the registration,
57     * as this will always be visible. If you override the protected
58     * createRootPane() method of JDialog, you can return your custom JRootPane
59     * with the keystroke enabled:
60     */
61     @Override
62     protected JRootPane createRootPane() {
63     KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
64     JRootPane rootPane = new JRootPane();
65     rootPane.registerKeyboardAction(new ActionListener() {
66    
67     public void actionPerformed(ActionEvent e) {
68     cancel();
69     }
70    
71     }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
72    
73     return rootPane;
74     }
75    
76     /**
77     * This class handles the cancel button itself. You may still want to listen
78     * to PROPERTY_APPLY_AND_CLOSE events.
79     *
80     * This dialog is modal. The dialog has to be set visible afterwards.
81     */
82     public TranslationAskJDialog(Dialog owner,
83     final JComponent... translationEditJPanels) {
84     super(owner);
85     this.translationEditJPanelsOrJustComponents = translationEditJPanels;
86     init();
87     }
88    
89     /**
90     * This class handles the cancel button itself. You may still want to listen
91 alfonx 33 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
92     * to be set visible afterwards.
93 mojays 2 */
94     public TranslationAskJDialog(Window owner,
95     final JComponent... translationEditJPanels) {
96     super(owner);
97     this.translationEditJPanelsOrJustComponents = translationEditJPanels;
98     init();
99 alfonx 33
100     // Rememebr backups for all the jtextpanels
101     int count = 0;
102     for (JComponent component : translationEditJPanelsOrJustComponents) {
103     if (component instanceof TranslationEditJPanel) {
104     TranslationEditJPanel tep = (TranslationEditJPanel) component;
105     Translation orig = tep.getTranslation();
106 alfonx 34
107     // We don't want to overwrite the Translation object on
108     // restore(). We just want to change its value.
109 alfonx 33
110     backup[count++] = orig.toOneLine();
111     }
112     }
113 mojays 2 }
114    
115     private void init() {
116     setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
117     addWindowListener(new WindowAdapter() {
118    
119     public void windowClosing(WindowEvent e) {
120     cancel();
121     }
122    
123     });
124     SwingUtil.centerFrameOnScreen(this);
125     Box box = Box.createVerticalBox();
126     for (JComponent panel : translationEditJPanelsOrJustComponents) {
127     box.add(panel);
128     }
129     JPanel cp = new JPanel(new BorderLayout());
130     cp.add(box, BorderLayout.CENTER);
131     cp.add(getButtons(), BorderLayout.SOUTH);
132     setContentPane(cp);
133    
134     // dialog.getRootPane().setDefaultButton(okButton);
135    
136     setTitle(RESOURCE.getString("translation_dialog_title")); // i8n
137     setModal(true);
138     pack();
139     }
140    
141     protected void cancel() {
142     firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
143     restore();
144     setVisible(false);
145     dispose();
146     }
147    
148     private void restore() {
149     int count = 0;
150     for (JComponent component : translationEditJPanelsOrJustComponents) {
151     if (component instanceof TranslationEditJPanel) {
152     TranslationEditJPanel tep = (TranslationEditJPanel) component;
153 alfonx 33 tep.getTranslation().fromOneLine(backup[count++]);
154 mojays 2 }
155     }
156     }
157    
158     private JComponent getButtons() {
159     JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
160     if (okButton == null) {
161     okButton = new OkButton(new AbstractAction() {
162     {
163     // Set a mnemonic character. In most look and feels, this
164     // causes the
165     // specified character to be underlined This indicates that
166     // if the component
167     // using this action has the focus and In some look and
168     // feels, this causes
169     // the specified character in the label to be underlined and
170     putValue(Action.MNEMONIC_KEY, new Integer(
171     java.awt.event.KeyEvent.VK_E));
172    
173     // Set tool tip text
174     putValue(Action.SHORT_DESCRIPTION,
175     "Accept the changes made to the translation.");
176    
177     }
178    
179     public void actionPerformed(ActionEvent evt) {
180     TranslationAskJDialog.this.firePropertyChange(
181     PROPERTY_APPLY_AND_CLOSE, null, null);
182     setVisible(false);
183     dispose();
184     System.out.println("OK button action performed");
185     }
186    
187     });
188     // okButton.addKeyListener( new KeyListener() {
189     //
190     // public void keyTyped(KeyEvent e) {
191     // if ()
192     // okButton.action(new KEyPreEvent(), what)
193     // }
194     //
195     // });
196     }
197     jPanel.add(okButton);
198    
199     if (cancelButton == null) {
200     cancelButton = new CancelButton(new AbstractAction("") {
201     public void actionPerformed(ActionEvent evt) {
202     // restore();
203     TranslationAskJDialog.this.firePropertyChange(
204     PROPERTY_CANCEL_AND_CLOSE, null, null);
205     setVisible(false);
206     setHasBeenCanceled(true);
207     dispose();
208     }
209     });
210     }
211     jPanel.add(cancelButton);
212    
213     return jPanel;
214     }
215    
216     public static void main(String[] args) {
217     ArrayList<String> lang = new ArrayList<String>();
218     lang.add("de");
219     lang.add("en");
220     lang.add("fr");
221    
222     Translation transe = new Translation();
223     transe.put("de", "Terciopelo-Lanzenotter");
224     TranslationEditJPanel p1 = new TranslationEditJPanel(
225     "Name von New Group", transe, lang);
226    
227     Translation transe2 = new Translation();
228     transe2
229     .put(
230     "de",
231     "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");
232     TranslationEditJPanel p2 = new TranslationEditJPanel(
233     "Description of Animal:", transe2, lang);
234    
235     // JFrame frame = new JFrame();
236     // frame.setContentPane(p1);
237     // frame.pack();
238     // frame.setVisible(true);
239    
240     TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);
241     dialog.setVisible(true);
242     }
243    
244     private void setHasBeenCanceled(boolean hasBeenCanceled) {
245     this.hasBeenCanceled = hasBeenCanceled;
246     }
247    
248     /**
249 alfonx 34 * After the modal dialog has been closed, this allows to find out, whether
250 alfonx 33 * the dialog has been canceled.
251     *
252 alfonx 34 * @return <code>true</code> if the {@link JDialog} has been canceled.
253 mojays 2 */
254     public boolean isHasBeenCanceled() {
255     return hasBeenCanceled;
256     }
257    
258     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26