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 |
|
|
41 |
|
|
42 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
43 |
import javax.swing.Action; |
import javax.swing.Action; |
|
import javax.swing.BorderFactory; |
|
|
import javax.swing.Box; |
|
44 |
import javax.swing.JButton; |
import javax.swing.JButton; |
45 |
import javax.swing.JComponent; |
import javax.swing.JComponent; |
46 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
|
import javax.swing.JOptionPane; |
|
47 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
48 |
import javax.swing.JRootPane; |
import javax.swing.JRootPane; |
49 |
import javax.swing.KeyStroke; |
import javax.swing.KeyStroke; |
50 |
|
|
51 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
|
import skrueger.i8n.Translation; |
|
52 |
|
|
53 |
public class TranslationAskJDialog extends JDialog { |
public class TranslationAskJDialog extends CancellableDialogAdapter{ |
54 |
|
|
|
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
|
55 |
private OkButton okButton; |
private OkButton okButton; |
56 |
private CancelButton cancelButton; |
private CancelButton cancelButton; |
57 |
|
|
63 |
private boolean hasBeenCanceled; |
private boolean hasBeenCanceled; |
64 |
|
|
65 |
private JButton[] optionalButtons; |
private JButton[] optionalButtons; |
66 |
|
private TranslationsAskJPanel translationsAskPane; |
67 |
|
|
68 |
/** |
/** |
69 |
* Since the registerKeyboardAction() method is part of the JComponent class |
* Since the registerKeyboardAction() method is part of the JComponent class |
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.<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. |
|
101 |
*/ |
*/ |
102 |
public TranslationAskJDialog(Component owner, |
public TranslationAskJDialog(Component owner, |
103 |
final JComponent... translationEditJPanels) { |
final JComponent... translationEditJPanels) { |
104 |
super(SwingUtil.getParentWindow(owner)); |
super(owner); |
105 |
setComponents(translationEditJPanels); |
setComponents(translationEditJPanels); |
106 |
} |
} |
107 |
|
|
117 |
* Using this constructor, you have to call setComponents afterwards. |
* Using this constructor, you have to call setComponents afterwards. |
118 |
*/ |
*/ |
119 |
public TranslationAskJDialog(Component owner) { |
public TranslationAskJDialog(Component owner) { |
120 |
super(SwingUtil.getParentWindow(owner)); |
super(owner); |
121 |
} |
} |
122 |
|
|
123 |
/** |
/** |
134 |
public void setComponents(final JComponent... translationEditJPanels) { |
public void setComponents(final JComponent... translationEditJPanels) { |
135 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
136 |
|
|
|
backup(); |
|
|
|
|
137 |
init(); |
init(); |
138 |
} |
} |
139 |
|
|
|
/** |
|
|
* 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(); |
|
|
} |
|
|
} |
|
|
} |
|
140 |
|
|
141 |
private void init() { |
private void init() { |
142 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
147 |
} |
} |
148 |
|
|
149 |
}); |
}); |
150 |
SwingUtil.centerFrameOnScreen(this); |
|
151 |
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); |
|
|
|
|
|
} |
|
152 |
JPanel cp = new JPanel(new BorderLayout()); |
JPanel cp = new JPanel(new BorderLayout()); |
153 |
cp.add(box, BorderLayout.WEST); |
cp.add(translationsAskPane, BorderLayout.WEST); |
154 |
cp.add(getButtons(), BorderLayout.SOUTH); |
cp.add(getButtons(), BorderLayout.SOUTH); |
155 |
setContentPane(cp); |
setContentPane(cp); |
156 |
|
|
157 |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
158 |
setModal(true); |
setModal(true); |
159 |
pack(); |
pack(); |
160 |
|
SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5); |
161 |
} |
} |
162 |
|
|
163 |
public void setButtons(JButton... optionalButtons) { |
public void setOptionalButtons(JButton... optionalButtons) { |
164 |
this.optionalButtons = optionalButtons; |
this.optionalButtons = optionalButtons; |
165 |
init(); |
init(); |
166 |
} |
} |
170 |
* overwriting this method, call super.cancel() after restoring your |
* overwriting this method, call super.cancel() after restoring your |
171 |
* properties. |
* properties. |
172 |
*/ |
*/ |
173 |
|
@Override |
174 |
public void cancel() { |
public void cancel() { |
175 |
restore(); |
translationsAskPane.cancel(); |
176 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
177 |
setCancelled(true); |
hasBeenCanceled = true; |
178 |
setVisible(false); |
setVisible(false); |
179 |
dispose(); |
dispose(); |
180 |
} |
} |
181 |
|
|
|
/** |
|
|
* Used to restore all the values when cancel has been pressed. |
|
|
*/ |
|
|
private void restore() { |
|
|
int count = 0; |
|
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
|
|
if (component instanceof TranslationEditJPanel) { |
|
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
|
|
tep.getTranslation().fromOneLine(backup[count++]); |
|
|
} |
|
|
} |
|
|
} |
|
182 |
|
|
183 |
private JComponent getButtons() { |
private JComponent getButtons() { |
184 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
208 |
} |
} |
209 |
|
|
210 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
|
TranslationAskJDialog.this.firePropertyChange( |
|
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
|
|
|
|
|
if (!checkValidInputs()) |
|
|
return; |
|
211 |
|
|
212 |
okClose(); |
okClose(); |
213 |
|
|
221 |
if (cancelButton == null) { |
if (cancelButton == null) { |
222 |
cancelButton = new CancelButton(new AbstractAction("") { |
cancelButton = new CancelButton(new AbstractAction("") { |
223 |
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(); |
|
224 |
cancel(); |
cancel(); |
225 |
} |
} |
226 |
}); |
}); |
231 |
} |
} |
232 |
|
|
233 |
/** |
/** |
234 |
* 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. |
235 |
* Can be overwritten to do anything when the dialog has been accepted. |
* Can be overwritten to do anything when the dialog has been accepted. |
236 |
*/ |
*/ |
237 |
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; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
238 |
|
|
239 |
|
if (!translationsAskPane.checkValidInputs()) |
240 |
|
return false; |
241 |
|
|
242 |
|
|
243 |
|
dispose(); |
244 |
|
|
245 |
|
TranslationAskJDialog.this.firePropertyChange( |
246 |
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
247 |
return true; |
return true; |
248 |
} |
} |
249 |
|
|
250 |
private void setCancelled(boolean hasBeenCanceled) { |
|
|
this.hasBeenCanceled = hasBeenCanceled; |
|
|
} |
|
251 |
|
|
252 |
/** |
/** |
253 |
* 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 |
254 |
* the dialog has been canceled. |
* the {@link Component} has been canceled. |
255 |
* |
* |
256 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
257 |
*/ |
*/ |