/[schmitzm]/trunk/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Diff of /trunk/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/swing/TranslationAskJDialog.java revision 244 by alfonx, Wed Jul 29 09:33:33 2009 UTC branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java revision 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC
# Line 2  Line 2 
2   * Copyright (c) 2009 Martin O. J. Schmitz.   * Copyright (c) 2009 Martin O. J. Schmitz.
3   *   *
4   * This file is part of the SCHMITZM library - a collection of utility   * This file is part of the SCHMITZM library - a collection of utility
5   * classes based on Java 1.6, focussing (not only) on Java Swing   * classes based on Java 1.6, focusing (not only) on Java Swing
6   * and the Geotools library.   * and the Geotools library.
7   *   *
8   * The SCHMITZM project is hosted at:   * The SCHMITZM project is hosted at:
# Line 38  import java.awt.event.ActionListener; Line 38  import java.awt.event.ActionListener;
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;
# Line 52  import javax.swing.JPanel; Line 49  import javax.swing.JPanel;
49  import javax.swing.JRootPane;  import javax.swing.JRootPane;
50  import javax.swing.KeyStroke;  import javax.swing.KeyStroke;
51    
 import schmitzm.geotools.styling.StylingUtil;  
 import schmitzm.lang.LangUtil;  
 import schmitzm.lang.ResourceProvider;  
52  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
 import skrueger.i8n.Translation;  
53    
54  public class TranslationAskJDialog extends JDialog {  public class TranslationAskJDialog extends CancellableDialogAdapter{
55    
         private String[] backup = new String[50]; // Maximum 50 languages ;-)  
56          private OkButton okButton;          private OkButton okButton;
57          private CancelButton cancelButton;          private CancelButton cancelButton;
58    
# Line 72  public class TranslationAskJDialog exten Line 64  public class TranslationAskJDialog exten
64          private boolean hasBeenCanceled;          private boolean hasBeenCanceled;
65    
66          private JButton[] optionalButtons;          private JButton[] optionalButtons;
67            private TranslationsAskJPanel translationsAskPane;
68    
69          /**          /**
70           * Since the registerKeyboardAction() method is part of the JComponent class           * Since the registerKeyboardAction() method is part of the JComponent class
# Line 130  public class TranslationAskJDialog exten Line 123  public class TranslationAskJDialog exten
123           * Using this constructor, you have to call setComponents afterwards.           * Using this constructor, you have to call setComponents afterwards.
124           */           */
125          public TranslationAskJDialog(Component owner) {          public TranslationAskJDialog(Component owner) {
126                  this(owner, new JComponent[] {});                  super(SwingUtil.getParentWindow(owner));
127          }          }
128    
129          /**          /**
# Line 147  public class TranslationAskJDialog exten Line 140  public class TranslationAskJDialog exten
140          public void setComponents(final JComponent... translationEditJPanels) {          public void setComponents(final JComponent... translationEditJPanels) {
141                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
142    
                 // 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();  
                         }  
                 }  
   
143                  init();                  init();
144          }          }
145    
146    
147          private void init() {          private void init() {
148                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
149                  addWindowListener(new WindowAdapter() {                  addWindowListener(new WindowAdapter() {
# Line 172  public class TranslationAskJDialog exten Line 153  public class TranslationAskJDialog exten
153                          }                          }
154    
155                  });                  });
156                  SwingUtil.centerFrameOnScreen(this);                  
157                  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);  
                           
                 }  
158                  JPanel cp = new JPanel(new BorderLayout());                  JPanel cp = new JPanel(new BorderLayout());
159                  cp.add(box, BorderLayout.WEST);                  cp.add(translationsAskPane, BorderLayout.WEST);
160                  cp.add(getButtons(), BorderLayout.SOUTH);                  cp.add(getButtons(), BorderLayout.SOUTH);
161                  setContentPane(cp);                  setContentPane(cp);
162                    
163                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
164                  setModal(true);                  setModal(true);
165                  pack();                  pack();
166                    SwingUtil.centerFrameOnScreen(this);
167          }          }
168    
169          public void setButtons(JButton... optionalButtons) {          public void setOptionalButtons(JButton... optionalButtons) {
170                  this.optionalButtons = optionalButtons;                  this.optionalButtons = optionalButtons;
171                  init();                  init();
172          }          }
173    
174          protected void cancel() {          /**
175             * Called when the dilaog is closed using the cancel button. When
176             * overwriting this method, call super.cancel() after restoring your
177             * properties.
178             */
179            @Override
180            public void cancel() {
181                    translationsAskPane.cancel();
182                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
183                  restore();                  hasBeenCanceled = true;
184                  setVisible(false);                  setVisible(false);
185                  dispose();                  dispose();
186          }          }
187    
         protected void restore() {  
                 int count = 0;  
                 for (JComponent component : translationEditJPanelsOrJustComponents) {  
                         if (component instanceof TranslationEditJPanel) {  
                                 TranslationEditJPanel tep = (TranslationEditJPanel) component;  
                                 tep.getTranslation().fromOneLine(backup[count++]);  
                         }  
                 }  
         }  
188    
189          private JComponent getButtons() {          private JComponent getButtons() {
190                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
# Line 233  public class TranslationAskJDialog exten Line 207  public class TranslationAskJDialog exten
207                                          putValue(Action.MNEMONIC_KEY, new Integer(                                          putValue(Action.MNEMONIC_KEY, new Integer(
208                                                          java.awt.event.KeyEvent.VK_E));                                                          java.awt.event.KeyEvent.VK_E));
209    
210                                          // Set tool tip text                                          // // Set tool tip text
211                                          putValue(Action.SHORT_DESCRIPTION,                                          // putValue(Action.SHORT_DESCRIPTION,
212                                                          "Accept the changes made to the translation.");                                          // "Accept the changes made to the translation."); //i8n
213    
214                                  }                                  }
215    
216                                  public void actionPerformed(ActionEvent evt) {                                  public void actionPerformed(ActionEvent evt) {
                                         TranslationAskJDialog.this.firePropertyChange(  
                                                         PROPERTY_APPLY_AND_CLOSE, null, null);  
217    
218                                          if (!checkValidInputs())                                          okClose();
                                                 return;  
219    
                                         setVisible(false);  
                                         dispose();  
220                                  }                                  }
221    
222                          });                          });
# Line 258  public class TranslationAskJDialog exten Line 227  public class TranslationAskJDialog exten
227                  if (cancelButton == null) {                  if (cancelButton == null) {
228                          cancelButton = new CancelButton(new AbstractAction("") {                          cancelButton = new CancelButton(new AbstractAction("") {
229                                  public void actionPerformed(ActionEvent evt) {                                  public void actionPerformed(ActionEvent evt) {
230                                          restore();                                          cancel();
                                         TranslationAskJDialog.this.firePropertyChange(  
                                                         PROPERTY_CANCEL_AND_CLOSE, null, null);  
                                         setVisible(false);  
                                         setCancelled(true);  
                                         dispose();  
231                                  }                                  }
232                          });                          });
233                  }                  }
# Line 273  public class TranslationAskJDialog exten Line 237  public class TranslationAskJDialog exten
237          }          }
238    
239          /**          /**
240           * @return <code>true</code> if none of the translations contains illegal           * This method is only called when the dialog is closed and not canceled.
241           *         characters.           * Can be overwritten to do anything when the dialog has been accepted.
242           */           */
243          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;  
                                         }  
                                 }  
   
                         }  
                 }  
244    
245                    if (!translationsAskPane.checkValidInputs())
246                            return false;
247                    
248                    
249                    dispose();
250                    
251                    TranslationAskJDialog.this.firePropertyChange(
252                                    PROPERTY_APPLY_AND_CLOSE, null, null);
253                  return true;                  return true;
254          }          }
255    
256          private void setCancelled(boolean hasBeenCanceled) {  
                 this.hasBeenCanceled = hasBeenCanceled;  
         }  
257    
258          /**          /**
259           * 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
260           * the dialog has been canceled.           * the {@link Component} has been canceled.
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           */           */

Legend:
Removed from v.244  
changed lines
  Added in v.420

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26