25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.swing; |
package skrueger.swing; |
31 |
|
|
32 |
import java.awt.BorderLayout; |
import java.awt.BorderLayout; |
33 |
import java.awt.Component; |
import java.awt.Component; |
34 |
import java.awt.FlowLayout; |
import java.awt.FlowLayout; |
|
import java.awt.Window; |
|
35 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
36 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
37 |
import java.awt.event.KeyEvent; |
import java.awt.event.KeyEvent; |
40 |
|
|
41 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
42 |
import javax.swing.Action; |
import javax.swing.Action; |
|
import javax.swing.BorderFactory; |
|
|
import javax.swing.Box; |
|
43 |
import javax.swing.JButton; |
import javax.swing.JButton; |
44 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
45 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
|
import javax.swing.JOptionPane; |
|
46 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
47 |
import javax.swing.JRootPane; |
import javax.swing.JRootPane; |
48 |
import javax.swing.KeyStroke; |
import javax.swing.KeyStroke; |
49 |
|
|
50 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
|
import skrueger.i8n.Translation; |
|
51 |
|
|
52 |
public class TranslationAskJDialog extends JDialog { |
public class TranslationAskJDialog extends CancellableDialogAdapter{ |
53 |
|
|
|
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
|
54 |
private OkButton okButton; |
private OkButton okButton; |
55 |
private CancelButton cancelButton; |
private CancelButton cancelButton; |
56 |
|
|
62 |
private boolean hasBeenCanceled; |
private boolean hasBeenCanceled; |
63 |
|
|
64 |
private JButton[] optionalButtons; |
private JButton[] optionalButtons; |
65 |
|
private TranslationsAskJPanel translationsAskPane; |
66 |
|
|
67 |
/** |
/** |
68 |
* Since the registerKeyboardAction() method is part of the JComponent class |
* Since the registerKeyboardAction() method is part of the JComponent class |
97 |
* 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 |
98 |
* 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 |
99 |
* to be set visible afterwards.<br/> |
* to be set visible afterwards.<br/> |
|
* |
|
|
* @param owner |
|
|
* A component of the GUI that this dialog is related to. If no |
|
|
* {@link Window} is passed, SwingUtil.getParentWindow(owner) is |
|
|
* called. |
|
100 |
*/ |
*/ |
101 |
public TranslationAskJDialog(Component owner, |
public TranslationAskJDialog(Component owner, |
102 |
final JComponent... translationEditJPanels) { |
final JComponent... translationEditJPanels) { |
103 |
super(SwingUtil.getParentWindow(owner)); |
super(owner); |
104 |
setComponents(translationEditJPanels); |
setComponents(translationEditJPanels); |
105 |
} |
} |
106 |
|
|
116 |
* Using this constructor, you have to call setComponents afterwards. |
* Using this constructor, you have to call setComponents afterwards. |
117 |
*/ |
*/ |
118 |
public TranslationAskJDialog(Component owner) { |
public TranslationAskJDialog(Component owner) { |
119 |
super(SwingUtil.getParentWindow(owner)); |
super(owner); |
120 |
} |
} |
121 |
|
|
122 |
/** |
/** |
133 |
public void setComponents(final JComponent... translationEditJPanels) { |
public void setComponents(final JComponent... translationEditJPanels) { |
134 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
135 |
|
|
|
backup(); |
|
|
|
|
136 |
init(); |
init(); |
137 |
} |
} |
138 |
|
|
|
/** |
|
|
* Stores the original values of all {@link TranslationEditJPanel}s so |
|
|
* cancel works. |
|
|
*/ |
|
|
protected void backup() { |
|
|
// Remember backups for all the TranslationEditJPanel |
|
|
int count = 0; |
|
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
|
|
if (component instanceof TranslationEditJPanel) { |
|
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
|
|
Translation orig = tep.getTranslation(); |
|
|
|
|
|
// We don't want to overwrite the Translation object on |
|
|
// restore(). We just want to change its value. |
|
|
backup[count++] = orig.toOneLine(); |
|
|
} |
|
|
} |
|
|
} |
|
139 |
|
|
140 |
private void init() { |
private void init() { |
141 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
146 |
} |
} |
147 |
|
|
148 |
}); |
}); |
149 |
SwingUtil.centerFrameOnScreen(this); |
|
150 |
Box box = Box.createVerticalBox(); |
translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents); |
|
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
|
|
panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); |
|
|
panel.setBorder(BorderFactory.createEmptyBorder(5, 6, 5, 6)); |
|
|
box.add(panel); |
|
|
|
|
|
} |
|
151 |
JPanel cp = new JPanel(new BorderLayout()); |
JPanel cp = new JPanel(new BorderLayout()); |
152 |
cp.add(box, BorderLayout.WEST); |
cp.add(translationsAskPane, BorderLayout.WEST); |
153 |
cp.add(getButtons(), BorderLayout.SOUTH); |
cp.add(getButtons(), BorderLayout.SOUTH); |
154 |
setContentPane(cp); |
setContentPane(cp); |
155 |
|
|
156 |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
157 |
setModal(true); |
setModal(true); |
158 |
pack(); |
pack(); |
159 |
|
SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5); |
160 |
} |
} |
161 |
|
|
162 |
public void setButtons(JButton... optionalButtons) { |
public void setOptionalButtons(JButton... optionalButtons) { |
163 |
this.optionalButtons = optionalButtons; |
this.optionalButtons = optionalButtons; |
164 |
init(); |
init(); |
165 |
} |
} |
166 |
|
|
167 |
/** |
/** |
168 |
* Called when the dilaog is closed using the cancel button. |
* Called when the dilaog is closed using the cancel button. When |
169 |
|
* overwriting this method, call super.cancel() after restoring your |
170 |
|
* properties. |
171 |
*/ |
*/ |
172 |
protected void cancel() { |
@Override |
173 |
restore(); |
public void cancel() { |
174 |
|
translationsAskPane.cancel(); |
175 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
176 |
setCancelled(true); |
hasBeenCanceled = true; |
177 |
setVisible(false); |
setVisible(false); |
178 |
dispose(); |
dispose(); |
179 |
} |
} |
180 |
|
|
|
/** |
|
|
* Used to restore all the values when cancel has been pressed. |
|
|
*/ |
|
|
protected void restore() { |
|
|
int count = 0; |
|
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
|
|
if (component instanceof TranslationEditJPanel) { |
|
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
|
|
tep.getTranslation().fromOneLine(backup[count++]); |
|
|
} |
|
|
} |
|
|
} |
|
181 |
|
|
182 |
private JComponent getButtons() { |
private JComponent getButtons() { |
183 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
207 |
} |
} |
208 |
|
|
209 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
|
TranslationAskJDialog.this.firePropertyChange( |
|
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
|
|
|
|
|
if (!checkValidInputs()) |
|
|
return; |
|
210 |
|
|
211 |
okClose(); |
okClose(); |
212 |
|
|
220 |
if (cancelButton == null) { |
if (cancelButton == null) { |
221 |
cancelButton = new CancelButton(new AbstractAction("") { |
cancelButton = new CancelButton(new AbstractAction("") { |
222 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
|
// restore(); |
|
|
// TranslationAskJDialog.this.firePropertyChange( |
|
|
// PROPERTY_CANCEL_AND_CLOSE, null, null); |
|
|
// setVisible(false); |
|
|
// setCancelled(true); |
|
|
// dispose(); |
|
223 |
cancel(); |
cancel(); |
224 |
} |
} |
225 |
}); |
}); |
230 |
} |
} |
231 |
|
|
232 |
/** |
/** |
233 |
* This method is only called when the dialog is closed and not cancelled. |
* This method is only called when the dialog is closed and not canceled. |
234 |
* Can be overwritten to do anything when the dialog has been accepted. |
* Can be overwritten to do anything when the dialog has been accepted. |
235 |
*/ |
*/ |
236 |
protected void okClose() { |
public boolean okClose() { |
|
setVisible(false); |
|
|
dispose(); |
|
|
} |
|
|
|
|
|
/** |
|
|
* @return <code>true</code> if none of the translations contains illegal |
|
|
* characters. |
|
|
*/ |
|
|
protected boolean checkValidInputs() { |
|
|
|
|
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
|
|
if (component instanceof TranslationEditJPanel) { |
|
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
|
|
|
|
|
for (String l : tep.getTranslation().values()) { |
|
|
if (l.contains("{") || l.contains("}")) { |
|
|
JOptionPane |
|
|
.showMessageDialog( |
|
|
this, |
|
|
SwingUtil |
|
|
.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation")); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
237 |
|
|
238 |
|
if (!translationsAskPane.checkValidInputs()) |
239 |
|
return false; |
240 |
|
|
241 |
|
|
242 |
|
dispose(); |
243 |
|
|
244 |
|
TranslationAskJDialog.this.firePropertyChange( |
245 |
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
246 |
return true; |
return true; |
247 |
} |
} |
248 |
|
|
249 |
private void setCancelled(boolean hasBeenCanceled) { |
|
|
this.hasBeenCanceled = hasBeenCanceled; |
|
|
} |
|
250 |
|
|
251 |
/** |
/** |
252 |
* After the modal dialog has been closed, this allows to find out, whether |
* After the modal dialog has been closed, this allows to find out, whether |
253 |
* the dialog has been canceled. |
* the {@link Component} has been canceled. |
254 |
* |
* |
255 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
256 |
*/ |
*/ |