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; |
15 |
import javax.swing.Action; |
import javax.swing.Action; |
16 |
|
import javax.swing.BorderFactory; |
17 |
import javax.swing.Box; |
import javax.swing.Box; |
18 |
|
import javax.swing.JButton; |
19 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
20 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
21 |
|
import javax.swing.JOptionPane; |
22 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
23 |
import javax.swing.JRootPane; |
import javax.swing.JRootPane; |
24 |
import javax.swing.KeyStroke; |
import javax.swing.KeyStroke; |
25 |
|
|
26 |
|
import schmitzm.geotools.styling.StylingUtil; |
27 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
28 |
import schmitzm.lang.ResourceProvider; |
import schmitzm.lang.ResourceProvider; |
29 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
31 |
|
|
32 |
public class TranslationAskJDialog extends JDialog { |
public class TranslationAskJDialog extends JDialog { |
33 |
|
|
|
/** |
|
|
* {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des |
|
|
* Package {@code skrueger.swing} zur Verfuegung stellt. Diese sind in |
|
|
* properties-Datein unter {@code skrueger.swing.resource.locales} |
|
|
* hinterlegt. |
|
|
*/ |
|
|
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
|
|
.extendPackagePath(TranslationAskJDialog.class, |
|
|
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
|
|
|
|
34 |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
35 |
private OkButton okButton; |
private OkButton okButton; |
36 |
private CancelButton cancelButton; |
private CancelButton cancelButton; |
38 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
39 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
40 |
|
|
41 |
private final JComponent[] translationEditJPanelsOrJustComponents; |
private JComponent[] translationEditJPanelsOrJustComponents; |
42 |
|
|
43 |
private boolean hasBeenCanceled; |
private boolean hasBeenCanceled; |
44 |
|
|
45 |
|
private JButton[] optionalButtons; |
46 |
|
|
47 |
/** |
/** |
48 |
* Since the registerKeyboardAction() method is part of the JComponent class |
* Since the registerKeyboardAction() method is part of the JComponent class |
49 |
* definition, you must define the Escape keystroke and register the |
* definition, you must define the Escape keystroke and register the |
69 |
} |
} |
70 |
|
|
71 |
/** |
/** |
72 |
|
* 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 |
* 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 |
78 |
* to PROPERTY_APPLY_AND_CLOSE events. |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
79 |
|
* to be set visible afterwards.<br/> |
80 |
* |
* |
81 |
* This dialog is modal. The dialog has to be set visible afterwards. |
* @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 |
*/ |
*/ |
86 |
public TranslationAskJDialog(Dialog owner, |
public TranslationAskJDialog(Component owner, |
87 |
final JComponent... translationEditJPanels) { |
final JComponent... translationEditJPanels) { |
88 |
super(owner); |
super(SwingUtil.getParentWindow(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(Component owner) { |
104 |
final JComponent... translationEditJPanels) { |
this(owner, new JComponent[] {}); |
105 |
super(owner); |
} |
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() { |
146 |
SwingUtil.centerFrameOnScreen(this); |
SwingUtil.centerFrameOnScreen(this); |
147 |
Box box = Box.createVerticalBox(); |
Box box = Box.createVerticalBox(); |
148 |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
149 |
|
panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); |
150 |
|
panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6)); |
151 |
box.add(panel); |
box.add(panel); |
152 |
|
|
153 |
} |
} |
154 |
JPanel cp = new JPanel(new BorderLayout()); |
JPanel cp = new JPanel(new BorderLayout()); |
155 |
cp.add(box, BorderLayout.CENTER); |
cp.add(box, BorderLayout.WEST); |
156 |
cp.add(getButtons(), BorderLayout.SOUTH); |
cp.add(getButtons(), BorderLayout.SOUTH); |
157 |
setContentPane(cp); |
setContentPane(cp); |
158 |
|
|
159 |
// dialog.getRootPane().setDefaultButton(okButton); |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
|
|
|
|
setTitle(RESOURCE.getString("translation_dialog_title")); // i8n |
|
160 |
setModal(true); |
setModal(true); |
161 |
pack(); |
pack(); |
162 |
} |
} |
163 |
|
|
164 |
|
public void setButtons(JButton... optionalButtons) { |
165 |
|
this.optionalButtons = optionalButtons; |
166 |
|
init(); |
167 |
|
} |
168 |
|
|
169 |
protected void cancel() { |
protected void cancel() { |
170 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
171 |
restore(); |
restore(); |
173 |
dispose(); |
dispose(); |
174 |
} |
} |
175 |
|
|
176 |
private void restore() { |
protected void restore() { |
177 |
int count = 0; |
int count = 0; |
178 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
179 |
if (component instanceof TranslationEditJPanel) { |
if (component instanceof TranslationEditJPanel) { |
185 |
|
|
186 |
private JComponent getButtons() { |
private JComponent getButtons() { |
187 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
188 |
|
|
189 |
|
if (optionalButtons != null) |
190 |
|
for (JButton b : optionalButtons) { |
191 |
|
jPanel.add(b); |
192 |
|
} |
193 |
|
|
194 |
if (okButton == null) { |
if (okButton == null) { |
195 |
okButton = new OkButton(new AbstractAction() { |
okButton = new OkButton(new AbstractAction() { |
196 |
{ |
{ |
213 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
214 |
TranslationAskJDialog.this.firePropertyChange( |
TranslationAskJDialog.this.firePropertyChange( |
215 |
PROPERTY_APPLY_AND_CLOSE, null, null); |
PROPERTY_APPLY_AND_CLOSE, null, null); |
216 |
|
|
217 |
|
if (!checkValidInputs()) |
218 |
|
return; |
219 |
|
|
220 |
setVisible(false); |
setVisible(false); |
221 |
dispose(); |
dispose(); |
|
System.out.println("OK button action performed"); |
|
222 |
} |
} |
223 |
|
|
224 |
}); |
}); |
225 |
// okButton.addKeyListener( new KeyListener() { |
|
|
// |
|
|
// public void keyTyped(KeyEvent e) { |
|
|
// if () |
|
|
// okButton.action(new KEyPreEvent(), what) |
|
|
// } |
|
|
// |
|
|
// }); |
|
226 |
} |
} |
227 |
jPanel.add(okButton); |
jPanel.add(okButton); |
228 |
|
|
229 |
if (cancelButton == null) { |
if (cancelButton == null) { |
230 |
cancelButton = new CancelButton(new AbstractAction("") { |
cancelButton = new CancelButton(new AbstractAction("") { |
231 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
232 |
// restore(); |
restore(); |
233 |
TranslationAskJDialog.this.firePropertyChange( |
TranslationAskJDialog.this.firePropertyChange( |
234 |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
235 |
setVisible(false); |
setVisible(false); |
236 |
setHasBeenCanceled(true); |
setCancelled(true); |
237 |
dispose(); |
dispose(); |
238 |
} |
} |
239 |
}); |
}); |
243 |
return jPanel; |
return jPanel; |
244 |
} |
} |
245 |
|
|
246 |
public static void main(String[] args) { |
/** |
247 |
ArrayList<String> lang = new ArrayList<String>(); |
* @return <code>true</code> if none of the translations contains illegal |
248 |
lang.add("de"); |
* characters. |
249 |
lang.add("en"); |
*/ |
250 |
lang.add("fr"); |
protected boolean checkValidInputs() { |
251 |
|
|
252 |
Translation transe = new Translation(); |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
253 |
transe.put("de", "Terciopelo-Lanzenotter"); |
if (component instanceof TranslationEditJPanel) { |
254 |
TranslationEditJPanel p1 = new TranslationEditJPanel( |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
255 |
"Name von New Group", transe, lang); |
|
256 |
|
for (String l : tep.getTranslation().values()) { |
257 |
Translation transe2 = new Translation(); |
if (l.contains("{") || l.contains("}")) { |
258 |
transe2 |
JOptionPane |
259 |
.put( |
.showMessageDialog( |
260 |
"de", |
this, |
261 |
"Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart."); |
SwingUtil.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation")); |
262 |
TranslationEditJPanel p2 = new TranslationEditJPanel( |
return false; |
263 |
"Description of Animal:", transe2, lang); |
} |
264 |
|
} |
265 |
// JFrame frame = new JFrame(); |
|
266 |
// frame.setContentPane(p1); |
} |
267 |
// frame.pack(); |
} |
|
// frame.setVisible(true); |
|
268 |
|
|
269 |
TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2); |
return true; |
|
dialog.setVisible(true); |
|
270 |
} |
} |
271 |
|
|
272 |
private void setHasBeenCanceled(boolean hasBeenCanceled) { |
private void setCancelled(boolean hasBeenCanceled) { |
273 |
this.hasBeenCanceled = hasBeenCanceled; |
this.hasBeenCanceled = hasBeenCanceled; |
274 |
} |
} |
275 |
|
|
279 |
* |
* |
280 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
281 |
*/ |
*/ |
282 |
public boolean isHasBeenCanceled() { |
public boolean isCancelled() { |
283 |
return hasBeenCanceled; |
return hasBeenCanceled; |
284 |
} |
} |
285 |
|
|