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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations)
Sat Mar 28 17:06:27 2009 UTC (15 years, 11 months ago) by alfonx
File size: 7829 byte(s)
The cancel mechanism of these didn't work. Thanks to AR for reporting this.
1 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 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
92 * to be set visible afterwards.
93 */
94 public TranslationAskJDialog(Window owner,
95 final JComponent... translationEditJPanels) {
96 super(owner);
97 this.translationEditJPanelsOrJustComponents = translationEditJPanels;
98 init();
99
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 // We dont' want to overwrite the Translation object on
107 // restore(). We just want to change its values.
108
109 backup[count++] = orig.toOneLine();
110 }
111 }
112 }
113
114 private void init() {
115 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
116 addWindowListener(new WindowAdapter() {
117
118 public void windowClosing(WindowEvent e) {
119 cancel();
120 }
121
122 });
123 SwingUtil.centerFrameOnScreen(this);
124 Box box = Box.createVerticalBox();
125 for (JComponent panel : translationEditJPanelsOrJustComponents) {
126 box.add(panel);
127 }
128 JPanel cp = new JPanel(new BorderLayout());
129 cp.add(box, BorderLayout.CENTER);
130 cp.add(getButtons(), BorderLayout.SOUTH);
131 setContentPane(cp);
132
133 // dialog.getRootPane().setDefaultButton(okButton);
134
135 setTitle(RESOURCE.getString("translation_dialog_title")); // i8n
136 setModal(true);
137 pack();
138 }
139
140 protected void cancel() {
141 firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
142 restore();
143 setVisible(false);
144 dispose();
145 }
146
147 private void restore() {
148 int count = 0;
149 for (JComponent component : translationEditJPanelsOrJustComponents) {
150 if (component instanceof TranslationEditJPanel) {
151 TranslationEditJPanel tep = (TranslationEditJPanel) component;
152 tep.getTranslation().fromOneLine(backup[count++]);
153 }
154 }
155 }
156
157 private JComponent getButtons() {
158 JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
159 if (okButton == null) {
160 okButton = new OkButton(new AbstractAction() {
161 {
162 // Set a mnemonic character. In most look and feels, this
163 // causes the
164 // specified character to be underlined This indicates that
165 // if the component
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));
171
172 // Set tool tip text
173 putValue(Action.SHORT_DESCRIPTION,
174 "Accept the changes made to the translation.");
175
176 }
177
178 public void actionPerformed(ActionEvent evt) {
179 TranslationAskJDialog.this.firePropertyChange(
180 PROPERTY_APPLY_AND_CLOSE, null, null);
181 setVisible(false);
182 dispose();
183 System.out.println("OK button action performed");
184 }
185
186 });
187 // okButton.addKeyListener( new KeyListener() {
188 //
189 // public void keyTyped(KeyEvent e) {
190 // if ()
191 // okButton.action(new KEyPreEvent(), what)
192 // }
193 //
194 // });
195 }
196 jPanel.add(okButton);
197
198 if (cancelButton == null) {
199 cancelButton = new CancelButton(new AbstractAction("") {
200 public void actionPerformed(ActionEvent evt) {
201 // restore();
202 TranslationAskJDialog.this.firePropertyChange(
203 PROPERTY_CANCEL_AND_CLOSE, null, null);
204 setVisible(false);
205 setHasBeenCanceled(true);
206 dispose();
207 }
208 });
209 }
210 jPanel.add(cancelButton);
211
212 return jPanel;
213 }
214
215 public static void main(String[] args) {
216 ArrayList<String> lang = new ArrayList<String>();
217 lang.add("de");
218 lang.add("en");
219 lang.add("fr");
220
221 Translation transe = new Translation();
222 transe.put("de", "Terciopelo-Lanzenotter");
223 TranslationEditJPanel p1 = new TranslationEditJPanel(
224 "Name von New Group", transe, lang);
225
226 Translation transe2 = new Translation();
227 transe2
228 .put(
229 "de",
230 "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");
231 TranslationEditJPanel p2 = new TranslationEditJPanel(
232 "Description of Animal:", transe2, lang);
233
234 // JFrame frame = new JFrame();
235 // frame.setContentPane(p1);
236 // frame.pack();
237 // frame.setVisible(true);
238
239 TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);
240 dialog.setVisible(true);
241 }
242
243 private void setHasBeenCanceled(boolean hasBeenCanceled) {
244 this.hasBeenCanceled = hasBeenCanceled;
245 }
246
247 /**
248 * After the modal dialog has been closed, this allows to find out, wether
249 * the dialog has been canceled.
250 *
251 * @return
252 */
253 public boolean isHasBeenCanceled() {
254 return hasBeenCanceled;
255 }
256
257 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26