1 |
package skrueger.swing; |
package skrueger.swing; |
2 |
|
|
3 |
import java.awt.BorderLayout; |
import java.awt.BorderLayout; |
4 |
import java.awt.Dialog; |
import java.awt.Component; |
5 |
import java.awt.FlowLayout; |
import java.awt.FlowLayout; |
6 |
import java.awt.Window; |
import java.awt.Window; |
7 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.KeyEvent; |
import java.awt.event.KeyEvent; |
10 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
11 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
|
import java.util.ArrayList; |
|
12 |
import java.util.Locale; |
import java.util.Locale; |
13 |
|
|
14 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
16 |
import javax.swing.Box; |
import javax.swing.Box; |
17 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
18 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
19 |
|
import javax.swing.JOptionPane; |
20 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
21 |
import javax.swing.JRootPane; |
import javax.swing.JRootPane; |
22 |
import javax.swing.KeyStroke; |
import javax.swing.KeyStroke; |
45 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
46 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
47 |
|
|
48 |
private final JComponent[] translationEditJPanelsOrJustComponents; |
private JComponent[] translationEditJPanelsOrJustComponents; |
49 |
|
|
50 |
private boolean hasBeenCanceled; |
private boolean hasBeenCanceled; |
51 |
|
|
74 |
} |
} |
75 |
|
|
76 |
/** |
/** |
77 |
|
* The {@link TranslationAskJDialog} fills its content pane with an |
78 |
|
* arbitrary number of components. If these {@link Component}s are |
79 |
|
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
80 |
|
* values and restore them if the dialog is canceled. Other |
81 |
|
* {@link JComponent}s are just displayed.<br/> |
82 |
* This class handles the cancel button itself. You may still want to listen |
* This class handles the cancel button itself. You may still want to listen |
83 |
* to PROPERTY_APPLY_AND_CLOSE events. |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
84 |
* |
* to be set visible afterwards.<br/> |
|
* This dialog is modal. The dialog has to be set visible afterwards. |
|
85 |
*/ |
*/ |
86 |
public TranslationAskJDialog(Dialog owner, |
public TranslationAskJDialog(Window owner, |
87 |
final JComponent... translationEditJPanels) { |
final JComponent... translationEditJPanels) { |
88 |
super(owner); |
super(owner); |
89 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
setComponents(translationEditJPanels); |
|
init(); |
|
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
93 |
|
* 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 |
* This class handles the cancel button itself. You may still want to listen |
* This class handles the cancel button itself. You may still want to listen |
99 |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
100 |
* to be set visible afterwards. |
* to be set visible afterwards.<br/> |
101 |
|
* Using this constructor, you have to call setComponents afterwards. |
102 |
*/ |
*/ |
103 |
public TranslationAskJDialog(Window owner, |
public TranslationAskJDialog(Window owner) { |
|
final JComponent... translationEditJPanels) { |
|
104 |
super(owner); |
super(owner); |
105 |
|
} |
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 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
|
init(); |
|
120 |
|
|
121 |
// Rememebr backups for all the jtextpanels |
// Remember backups for all the TranslationEditJPanel |
122 |
int count = 0; |
int count = 0; |
123 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
124 |
if (component instanceof TranslationEditJPanel) { |
if (component instanceof TranslationEditJPanel) { |
125 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
126 |
Translation orig = tep.getTranslation(); |
Translation orig = tep.getTranslation(); |
127 |
|
|
128 |
// We don't want to overwrite the Translation object on |
// We don't want to overwrite the Translation object on |
129 |
// restore(). We just want to change its value. |
// restore(). We just want to change its value. |
|
|
|
130 |
backup[count++] = orig.toOneLine(); |
backup[count++] = orig.toOneLine(); |
131 |
} |
} |
132 |
} |
} |
133 |
|
|
134 |
|
init(); |
135 |
} |
} |
136 |
|
|
137 |
private void init() { |
private void init() { |
153 |
cp.add(getButtons(), BorderLayout.SOUTH); |
cp.add(getButtons(), BorderLayout.SOUTH); |
154 |
setContentPane(cp); |
setContentPane(cp); |
155 |
|
|
|
// dialog.getRootPane().setDefaultButton(okButton); |
|
|
|
|
156 |
setTitle(RESOURCE.getString("translation_dialog_title")); // i8n |
setTitle(RESOURCE.getString("translation_dialog_title")); // i8n |
157 |
setModal(true); |
setModal(true); |
158 |
pack(); |
pack(); |
165 |
dispose(); |
dispose(); |
166 |
} |
} |
167 |
|
|
168 |
private void restore() { |
protected void restore() { |
169 |
int count = 0; |
int count = 0; |
170 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
171 |
if (component instanceof TranslationEditJPanel) { |
if (component instanceof TranslationEditJPanel) { |
199 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
200 |
TranslationAskJDialog.this.firePropertyChange( |
TranslationAskJDialog.this.firePropertyChange( |
201 |
PROPERTY_APPLY_AND_CLOSE, null, null); |
PROPERTY_APPLY_AND_CLOSE, null, null); |
202 |
|
|
203 |
|
if (!checkValidInputs()) return; |
204 |
|
|
205 |
setVisible(false); |
setVisible(false); |
206 |
dispose(); |
dispose(); |
|
System.out.println("OK button action performed"); |
|
207 |
} |
} |
208 |
|
|
209 |
}); |
}); |
210 |
// okButton.addKeyListener( new KeyListener() { |
|
|
// |
|
|
// public void keyTyped(KeyEvent e) { |
|
|
// if () |
|
|
// okButton.action(new KEyPreEvent(), what) |
|
|
// } |
|
|
// |
|
|
// }); |
|
211 |
} |
} |
212 |
jPanel.add(okButton); |
jPanel.add(okButton); |
213 |
|
|
214 |
if (cancelButton == null) { |
if (cancelButton == null) { |
215 |
cancelButton = new CancelButton(new AbstractAction("") { |
cancelButton = new CancelButton(new AbstractAction("") { |
216 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
217 |
// restore(); |
restore(); |
218 |
TranslationAskJDialog.this.firePropertyChange( |
TranslationAskJDialog.this.firePropertyChange( |
219 |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
220 |
setVisible(false); |
setVisible(false); |
221 |
setHasBeenCanceled(true); |
setCancelled(true); |
222 |
dispose(); |
dispose(); |
223 |
} |
} |
224 |
}); |
}); |
228 |
return jPanel; |
return jPanel; |
229 |
} |
} |
230 |
|
|
231 |
public static void main(String[] args) { |
/** |
232 |
ArrayList<String> lang = new ArrayList<String>(); |
* @return <code>true</code> if none of the translations contains illegal characters. |
233 |
lang.add("de"); |
*/ |
234 |
lang.add("en"); |
protected boolean checkValidInputs() { |
235 |
lang.add("fr"); |
|
236 |
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
237 |
Translation transe = new Translation(); |
if (component instanceof TranslationEditJPanel) { |
238 |
transe.put("de", "Terciopelo-Lanzenotter"); |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
239 |
TranslationEditJPanel p1 = new TranslationEditJPanel( |
|
240 |
"Name von New Group", transe, lang); |
for (String l : tep.getTranslation().values()){ |
241 |
|
if ( l.contains("{") || l.contains("}")) { |
242 |
Translation transe2 = new Translation(); |
JOptionPane.showMessageDialog(this, RESOURCE.getString("ErrorMsg.InvalidCharacterInTranslation")); |
243 |
transe2 |
return false; |
244 |
.put( |
} |
245 |
"de", |
} |
246 |
"Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart."); |
|
247 |
TranslationEditJPanel p2 = new TranslationEditJPanel( |
} |
248 |
"Description of Animal:", transe2, lang); |
} |
249 |
|
|
250 |
// JFrame frame = new JFrame(); |
|
251 |
// frame.setContentPane(p1); |
return true; |
|
// frame.pack(); |
|
|
// frame.setVisible(true); |
|
|
|
|
|
TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2); |
|
|
dialog.setVisible(true); |
|
252 |
} |
} |
253 |
|
|
254 |
private void setHasBeenCanceled(boolean hasBeenCanceled) { |
private void setCancelled(boolean hasBeenCanceled) { |
255 |
this.hasBeenCanceled = hasBeenCanceled; |
this.hasBeenCanceled = hasBeenCanceled; |
256 |
} |
} |
257 |
|
|
261 |
* |
* |
262 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
263 |
*/ |
*/ |
264 |
public boolean isHasBeenCanceled() { |
public boolean isCancelled() { |
265 |
return hasBeenCanceled; |
return hasBeenCanceled; |
266 |
} |
} |
267 |
|
|