38 |
import java.awt.event.KeyEvent; |
import java.awt.event.KeyEvent; |
39 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
40 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
|
import java.util.Locale; |
|
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 |
|
|
|
import schmitzm.geotools.styling.StylingUtil; |
|
|
import schmitzm.lang.LangUtil; |
|
|
import schmitzm.lang.ResourceProvider; |
|
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 |
122 |
* Using this constructor, you have to call setComponents afterwards. |
* Using this constructor, you have to call setComponents afterwards. |
123 |
*/ |
*/ |
124 |
public TranslationAskJDialog(Component owner) { |
public TranslationAskJDialog(Component owner) { |
125 |
this(owner, new JComponent[] {}); |
super(SwingUtil.getParentWindow(owner)); |
126 |
} |
} |
127 |
|
|
128 |
/** |
/** |
139 |
public void setComponents(final JComponent... translationEditJPanels) { |
public void setComponents(final JComponent... translationEditJPanels) { |
140 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
141 |
|
|
|
// 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(); |
|
|
} |
|
|
} |
|
|
|
|
142 |
init(); |
init(); |
143 |
} |
} |
144 |
|
|
145 |
|
|
146 |
private void init() { |
private void init() { |
147 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
148 |
addWindowListener(new WindowAdapter() { |
addWindowListener(new WindowAdapter() { |
152 |
} |
} |
153 |
|
|
154 |
}); |
}); |
155 |
SwingUtil.centerFrameOnScreen(this); |
|
156 |
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); |
|
|
|
|
|
} |
|
157 |
JPanel cp = new JPanel(new BorderLayout()); |
JPanel cp = new JPanel(new BorderLayout()); |
158 |
cp.add(box, BorderLayout.WEST); |
cp.add(translationsAskPane, BorderLayout.WEST); |
159 |
cp.add(getButtons(), BorderLayout.SOUTH); |
cp.add(getButtons(), BorderLayout.SOUTH); |
160 |
setContentPane(cp); |
setContentPane(cp); |
161 |
|
|
162 |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
163 |
setModal(true); |
setModal(true); |
164 |
pack(); |
pack(); |
165 |
|
SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5); |
166 |
} |
} |
167 |
|
|
168 |
public void setButtons(JButton... optionalButtons) { |
public void setOptionalButtons(JButton... optionalButtons) { |
169 |
this.optionalButtons = optionalButtons; |
this.optionalButtons = optionalButtons; |
170 |
init(); |
init(); |
171 |
} |
} |
172 |
|
|
173 |
protected void cancel() { |
/** |
174 |
|
* Called when the dilaog is closed using the cancel button. When |
175 |
|
* overwriting this method, call super.cancel() after restoring your |
176 |
|
* properties. |
177 |
|
*/ |
178 |
|
@Override |
179 |
|
public void cancel() { |
180 |
|
translationsAskPane.cancel(); |
181 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
182 |
restore(); |
hasBeenCanceled = true; |
183 |
setVisible(false); |
setVisible(false); |
184 |
dispose(); |
dispose(); |
185 |
} |
} |
186 |
|
|
|
protected void restore() { |
|
|
int count = 0; |
|
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
|
|
if (component instanceof TranslationEditJPanel) { |
|
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
|
|
tep.getTranslation().fromOneLine(backup[count++]); |
|
|
} |
|
|
} |
|
|
} |
|
187 |
|
|
188 |
private JComponent getButtons() { |
private JComponent getButtons() { |
189 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
206 |
putValue(Action.MNEMONIC_KEY, new Integer( |
putValue(Action.MNEMONIC_KEY, new Integer( |
207 |
java.awt.event.KeyEvent.VK_E)); |
java.awt.event.KeyEvent.VK_E)); |
208 |
|
|
209 |
// Set tool tip text |
// // Set tool tip text |
210 |
putValue(Action.SHORT_DESCRIPTION, |
// putValue(Action.SHORT_DESCRIPTION, |
211 |
"Accept the changes made to the translation."); |
// "Accept the changes made to the translation."); //i8n |
212 |
|
|
213 |
} |
} |
214 |
|
|
215 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
|
TranslationAskJDialog.this.firePropertyChange( |
|
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
|
216 |
|
|
217 |
if (!checkValidInputs()) |
okClose(); |
|
return; |
|
218 |
|
|
|
setVisible(false); |
|
|
dispose(); |
|
219 |
} |
} |
220 |
|
|
221 |
}); |
}); |
226 |
if (cancelButton == null) { |
if (cancelButton == null) { |
227 |
cancelButton = new CancelButton(new AbstractAction("") { |
cancelButton = new CancelButton(new AbstractAction("") { |
228 |
public void actionPerformed(ActionEvent evt) { |
public void actionPerformed(ActionEvent evt) { |
229 |
restore(); |
cancel(); |
|
TranslationAskJDialog.this.firePropertyChange( |
|
|
PROPERTY_CANCEL_AND_CLOSE, null, null); |
|
|
setVisible(false); |
|
|
setCancelled(true); |
|
|
dispose(); |
|
230 |
} |
} |
231 |
}); |
}); |
232 |
} |
} |
236 |
} |
} |
237 |
|
|
238 |
/** |
/** |
239 |
* @return <code>true</code> if none of the translations contains illegal |
* This method is only called when the dialog is closed and not canceled. |
240 |
* characters. |
* Can be overwritten to do anything when the dialog has been accepted. |
241 |
*/ |
*/ |
242 |
protected boolean checkValidInputs() { |
public boolean okClose() { |
|
|
|
|
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; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
243 |
|
|
244 |
|
if (!translationsAskPane.checkValidInputs()) |
245 |
|
return false; |
246 |
|
|
247 |
|
|
248 |
|
dispose(); |
249 |
|
|
250 |
|
TranslationAskJDialog.this.firePropertyChange( |
251 |
|
PROPERTY_APPLY_AND_CLOSE, null, null); |
252 |
return true; |
return true; |
253 |
} |
} |
254 |
|
|
255 |
private void setCancelled(boolean hasBeenCanceled) { |
|
|
this.hasBeenCanceled = hasBeenCanceled; |
|
|
} |
|
256 |
|
|
257 |
/** |
/** |
258 |
* 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 |
259 |
* the dialog has been canceled. |
* the {@link Component} has been canceled. |
260 |
* |
* |
261 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
262 |
*/ |
*/ |