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. |
92 |
* This dialog is modal. The dialog has 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 |
|
101 |
private void init() { |
102 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
103 |
addWindowListener(new WindowAdapter() { |
104 |
|
105 |
public void windowClosing(WindowEvent e) { |
106 |
cancel(); |
107 |
} |
108 |
|
109 |
}); |
110 |
SwingUtil.centerFrameOnScreen(this); |
111 |
Box box = Box.createVerticalBox(); |
112 |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
113 |
box.add(panel); |
114 |
} |
115 |
JPanel cp = new JPanel(new BorderLayout()); |
116 |
cp.add(box, BorderLayout.CENTER); |
117 |
cp.add(getButtons(), BorderLayout.SOUTH); |
118 |
setContentPane(cp); |
119 |
|
120 |
// dialog.getRootPane().setDefaultButton(okButton); |
121 |
|
122 |
setTitle(RESOURCE.getString("translation_dialog_title")); // i8n |
123 |
setModal(true); |
124 |
pack(); |
125 |
} |
126 |
|
127 |
protected void cancel() { |
128 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
129 |
restore(); |
130 |
setVisible(false); |
131 |
dispose(); |
132 |
} |
133 |
|
134 |
private void restore() { |
135 |
int count = 0; |
136 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
137 |
if (component instanceof TranslationEditJPanel) { |
138 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
139 |
tep.getTranslation().fromOneLine(backup[count]); |
140 |
} |
141 |
count++; |
142 |
} |
143 |
} |
144 |
|
145 |
private JComponent getButtons() { |
146 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
147 |
if (okButton == null) { |
148 |
okButton = new OkButton(new AbstractAction() { |
149 |
{ |
150 |
// Set a mnemonic character. In most look and feels, this |
151 |
// causes the |
152 |
// specified character to be underlined This indicates that |
153 |
// if the component |
154 |
// using this action has the focus and In some look and |
155 |
// feels, this causes |
156 |
// the specified character in the label to be underlined and |
157 |
putValue(Action.MNEMONIC_KEY, new Integer( |
158 |
java.awt.event.KeyEvent.VK_E)); |
159 |
|
160 |
// Set tool tip text |
161 |
putValue(Action.SHORT_DESCRIPTION, |
162 |
"Accept the changes made to the translation."); |
163 |
|
164 |
} |
165 |
|
166 |
public void actionPerformed(ActionEvent evt) { |
167 |
TranslationAskJDialog.this.firePropertyChange( |
168 |
PROPERTY_APPLY_AND_CLOSE, null, null); |
169 |
setVisible(false); |
170 |
dispose(); |
171 |
System.out.println("OK button action performed"); |
172 |
} |
173 |
|
174 |
}); |
175 |
// okButton.addKeyListener( new KeyListener() { |
176 |
// |
177 |
// public void keyTyped(KeyEvent e) { |
178 |
// if () |
179 |
// okButton.action(new KEyPreEvent(), what) |
180 |
// } |
181 |
// |
182 |
// }); |
183 |
} |
184 |
jPanel.add(okButton); |
185 |
|
186 |
if (cancelButton == null) { |
187 |
cancelButton = new CancelButton(new AbstractAction("") { |
188 |
public void actionPerformed(ActionEvent evt) { |
189 |
// restore(); |
190 |
TranslationAskJDialog.this.firePropertyChange( |
191 |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
192 |
setVisible(false); |
193 |
setHasBeenCanceled(true); |
194 |
dispose(); |
195 |
} |
196 |
}); |
197 |
} |
198 |
jPanel.add(cancelButton); |
199 |
|
200 |
return jPanel; |
201 |
} |
202 |
|
203 |
public static void main(String[] args) { |
204 |
ArrayList<String> lang = new ArrayList<String>(); |
205 |
lang.add("de"); |
206 |
lang.add("en"); |
207 |
lang.add("fr"); |
208 |
|
209 |
Translation transe = new Translation(); |
210 |
transe.put("de", "Terciopelo-Lanzenotter"); |
211 |
TranslationEditJPanel p1 = new TranslationEditJPanel( |
212 |
"Name von New Group", transe, lang); |
213 |
|
214 |
Translation transe2 = new Translation(); |
215 |
transe2 |
216 |
.put( |
217 |
"de", |
218 |
"Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart."); |
219 |
TranslationEditJPanel p2 = new TranslationEditJPanel( |
220 |
"Description of Animal:", transe2, lang); |
221 |
|
222 |
// JFrame frame = new JFrame(); |
223 |
// frame.setContentPane(p1); |
224 |
// frame.pack(); |
225 |
// frame.setVisible(true); |
226 |
|
227 |
TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2); |
228 |
dialog.setVisible(true); |
229 |
} |
230 |
|
231 |
private void setHasBeenCanceled(boolean hasBeenCanceled) { |
232 |
this.hasBeenCanceled = hasBeenCanceled; |
233 |
} |
234 |
|
235 |
/** |
236 |
* After the modal dialog has been closed, this allows to find out, wether the dialog has been canceled. |
237 |
* @return |
238 |
*/ |
239 |
public boolean isHasBeenCanceled() { |
240 |
return hasBeenCanceled; |
241 |
} |
242 |
|
243 |
} |