/[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

revision 38 by alfonx, Sun Apr 5 15:06:56 2009 UTC revision 1137 by alfonx, Fri Oct 15 15:21:36 2010 UTC
# Line 1  Line 1 
1  package skrueger.swing;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import java.awt.BorderLayout;   *
4  import java.awt.Component;   * This file is part of the SCHMITZM library - a collection of utility
5  import java.awt.FlowLayout;   * classes based on Java 1.6, focusing (not only) on Java Swing
6  import java.awt.Window;   * and the Geotools library.
7  import java.awt.event.ActionEvent;   *
8  import java.awt.event.ActionListener;   * The SCHMITZM project is hosted at:
9  import java.awt.event.KeyEvent;   * http://wald.intevation.org/projects/schmitzm/
10  import java.awt.event.WindowAdapter;   *
11  import java.awt.event.WindowEvent;   * This program is free software; you can redistribute it and/or
12  import java.util.Locale;   * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14  import javax.swing.AbstractAction;   * of the License, or (at your option) any later version.
15  import javax.swing.Action;   *
16  import javax.swing.Box;   * This program is distributed in the hope that it will be useful,
17  import javax.swing.JComponent;   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  import javax.swing.JDialog;   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  import javax.swing.JOptionPane;   * GNU General Public License for more details.
20  import javax.swing.JPanel;   *
21  import javax.swing.JRootPane;   * You should have received a copy of the GNU Lesser General Public License (license.txt)
22  import javax.swing.KeyStroke;   * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  import schmitzm.lang.LangUtil;   * or try this link: http://www.gnu.org/licenses/lgpl.html
25  import schmitzm.lang.ResourceProvider;   *
26  import schmitzm.swing.SwingUtil;   * Contributors:
27  import skrueger.i8n.Translation;   *     Martin O. J. Schmitz - initial API and implementation
28     *     Stefan A. Tzeggai - additional utility classes
29  public class TranslationAskJDialog extends JDialog {   ******************************************************************************/
30    package skrueger.swing;
31          /**  
32           * {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des  import java.awt.BorderLayout;
33           * Package {@code skrueger.swing} zur Verfuegung stellt. Diese sind in  import java.awt.Component;
34           * properties-Datein unter {@code skrueger.swing.resource.locales}  import java.awt.FlowLayout;
35           * hinterlegt.  import java.awt.event.ActionEvent;
36           */  import java.awt.event.ActionListener;
37          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil  import java.awt.event.KeyEvent;
38                          .extendPackagePath(TranslationAskJDialog.class,  import java.awt.event.WindowAdapter;
39                                          "resource.locales.SwingResourceBundle"), Locale.ENGLISH);  import java.awt.event.WindowEvent;
40    
41          private String[] backup = new String[50]; // Maximum 50 languages ;-)  import javax.swing.AbstractAction;
42          private OkButton okButton;  import javax.swing.Action;
43          private CancelButton cancelButton;  import javax.swing.JButton;
44    import javax.swing.JComponent;
45          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";  import javax.swing.JDialog;
46          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";  import javax.swing.JPanel;
47    import javax.swing.JRootPane;
48          private JComponent[] translationEditJPanelsOrJustComponents;  import javax.swing.KeyStroke;
49    
50          private boolean hasBeenCanceled;  import schmitzm.swing.SwingUtil;
51    
52          /**  public class TranslationAskJDialog extends CancellableDialogAdapter{
53           * Since the registerKeyboardAction() method is part of the JComponent class  
54           * definition, you must define the Escape keystroke and register the          private OkButton okButton;
55           * keyboard action with a JComponent, not with a JDialog. The JRootPane for          private CancelButton cancelButton;
56           * the JDialog serves as an excellent choice to associate the registration,  
57           * as this will always be visible. If you override the protected          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
58           * createRootPane() method of JDialog, you can return your custom JRootPane          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
59           * with the keystroke enabled:  
60           */          private JComponent[] translationEditJPanelsOrJustComponents;
61          @Override  
62          protected JRootPane createRootPane() {          private boolean hasBeenCanceled;
63                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);  
64                  JRootPane rootPane = new JRootPane();          private JButton[] optionalButtons;
65                  rootPane.registerKeyboardAction(new ActionListener() {          private TranslationsAskJPanel translationsAskPane;
66    
67                          public void actionPerformed(ActionEvent e) {          /**
68                                  cancel();           * Since the registerKeyboardAction() method is part of the JComponent class
69                          }           * definition, you must define the Escape keystroke and register the
70             * keyboard action with a JComponent, not with a JDialog. The JRootPane for
71                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);           * the JDialog serves as an excellent choice to associate the registration,
72             * as this will always be visible. If you override the protected
73                  return rootPane;           * createRootPane() method of JDialog, you can return your custom JRootPane
74          }           * with the keystroke enabled:
75             */
76          /**          @Override
77           * The {@link TranslationAskJDialog} fills its content pane with an          protected JRootPane createRootPane() {
78           * arbitrary number of components. If these {@link Component}s are                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
79           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the                  JRootPane rootPane = new JRootPane();
80           * values and restore them if the dialog is canceled. Other                  rootPane.registerKeyboardAction(new ActionListener() {
81           * {@link JComponent}s are just displayed.<br/>  
82           * This class handles the cancel button itself. You may still want to listen                          public void actionPerformed(ActionEvent e) {
83           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has                                  cancel();
84           * to be set visible afterwards.<br/>                          }
85           */  
86          public TranslationAskJDialog(Window owner,                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
87                          final JComponent... translationEditJPanels) {  
88                  super(owner);                  return rootPane;
89                  setComponents(translationEditJPanels);          }
90          }  
91            /**
92          /**           * The {@link TranslationAskJDialog} fills its content pane with an
93           * The {@link TranslationAskJDialog} fills its content pane with an           * arbitrary number of components. If these {@link Component}s are
94           * arbitrary number of components. If these {@link Component}s are           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
95           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * values and restore them if the dialog is canceled. Other
96           * values and restore them if the dialog is canceled. Other           * {@link JComponent}s are just displayed.<br/>
97           * {@link JComponent}s are just displayed.<br/>           * This class handles the cancel button itself. You may still want to listen
98           * This class handles the cancel button itself. You may still want to listen           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
99           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           * to be set visible afterwards.<br/>
100           * to be set visible afterwards.<br/>           */
101           * Using this constructor, you have to call setComponents afterwards.          public TranslationAskJDialog(Component owner,
102           */                          final JComponent... translationEditJPanels) {
103          public TranslationAskJDialog(Window owner) {                  super(owner);
104                  super(owner);                  setComponents(translationEditJPanels);
105          }          }
106    
107          /**          /**
108           * The {@link TranslationAskJDialog} fills its content pane with an           * The {@link TranslationAskJDialog} fills its content pane with an
109           * arbitrary number of components. If these {@link Component}s are           * arbitrary number of components. If these {@link Component}s are
110           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
111           * values and restore them if the dialog is canceled. Other           * values and restore them if the dialog is canceled. Other
112           * {@link JComponent}s are just displayed.           * {@link JComponent}s are just displayed.<br/>
113           *           * This class handles the cancel button itself. You may still want to listen
114           * @param translationEditJPanels           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
115           *            Arbitrary list of {@link JComponent}s and           * to be set visible afterwards.<br/>
116           *            {@link TranslationEditJPanel}s.           * Using this constructor, you have to call setComponents afterwards.
117           */           */
118          public void setComponents(final JComponent... translationEditJPanels) {          public TranslationAskJDialog(Component owner) {
119                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                  super(owner);
120            }
121                  // Remember backups for all the TranslationEditJPanel  
122                  int count = 0;          /**
123                  for (JComponent component : translationEditJPanelsOrJustComponents) {           * The {@link TranslationAskJDialog} fills its content pane with an
124                          if (component instanceof TranslationEditJPanel) {           * arbitrary number of components. If these {@link Component}s are
125                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
126                                  Translation orig = tep.getTranslation();           * values and restore them if the dialog is canceled. Other
127             * {@link JComponent}s are just displayed.
128                                  // We don't want to overwrite the Translation object on           *
129                                  // restore(). We just want to change its value.           * @param translationEditJPanels
130                                  backup[count++] = orig.toOneLine();           *            Arbitrary list of {@link JComponent}s and
131                          }           *            {@link TranslationEditJPanel}s.
132                  }           */
133            public void setComponents(final JComponent... translationEditJPanels) {
134                  init();                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
135          }  
136                    init();
137          private void init() {          }
138                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);  
139                  addWindowListener(new WindowAdapter() {  
140            private void init() {
141                          public void windowClosing(WindowEvent e) {                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
142                                  cancel();                  addWindowListener(new WindowAdapter() {
143                          }  
144                            public void windowClosing(WindowEvent e) {
145                  });                                  cancel();
146                  SwingUtil.centerFrameOnScreen(this);                          }
147                  Box box = Box.createVerticalBox();  
148                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                  });
149                          box.add(panel);                  
150                  }                  translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents);
151                  JPanel cp = new JPanel(new BorderLayout());                  JPanel cp = new JPanel(new BorderLayout());
152                  cp.add(box, BorderLayout.CENTER);                  cp.add(translationsAskPane, BorderLayout.WEST);
153                  cp.add(getButtons(), BorderLayout.SOUTH);                  cp.add(getButtons(), BorderLayout.SOUTH);
154                  setContentPane(cp);                  setContentPane(cp);
155    
156                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
157                  setModal(true);                  setModal(true);
158                  pack();                  pack();
159          }                  SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5);
160            }
161          protected void cancel() {  
162                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);          public void setOptionalButtons(JButton... optionalButtons) {
163                  restore();                  this.optionalButtons = optionalButtons;
164                  setVisible(false);                  init();
165                  dispose();          }
166          }  
167            /**
168          protected void restore() {           * Called when the dilaog is closed using the cancel button. When
169                  int count = 0;           * overwriting this method, call super.cancel() after restoring your
170                  for (JComponent component : translationEditJPanelsOrJustComponents) {           * properties.
171                          if (component instanceof TranslationEditJPanel) {           */
172                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;          @Override
173                                  tep.getTranslation().fromOneLine(backup[count++]);          public void cancel() {
174                          }                  translationsAskPane.cancel();
175                  }                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
176          }                  hasBeenCanceled = true;
177                    setVisible(false);
178          private JComponent getButtons() {                  dispose();
179                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));          }
180                  if (okButton == null) {  
181                          okButton = new OkButton(new AbstractAction() {  
182                                  {          private JComponent getButtons() {
183                                          // Set a mnemonic character. In most look and feels, this                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
184                                          // causes the  
185                                          // specified character to be underlined This indicates that                  if (optionalButtons != null)
186                                          // if the component                          for (JButton b : optionalButtons) {
187                                          // using this action has the focus and In some look and                                  jPanel.add(b);
188                                          // feels, this causes                          }
189                                          // the specified character in the label to be underlined and  
190                                          putValue(Action.MNEMONIC_KEY, new Integer(                  if (okButton == null) {
191                                                          java.awt.event.KeyEvent.VK_E));                          okButton = new OkButton(new AbstractAction() {
192                                    {
193                                          // Set tool tip text                                          // Set a mnemonic character. In most look and feels, this
194                                          putValue(Action.SHORT_DESCRIPTION,                                          // causes the
195                                                          "Accept the changes made to the translation.");                                          // specified character to be underlined This indicates that
196                                            // if the component
197                                  }                                          // using this action has the focus and In some look and
198                                            // feels, this causes
199                                  public void actionPerformed(ActionEvent evt) {                                          // the specified character in the label to be underlined and
200                                          TranslationAskJDialog.this.firePropertyChange(                                          putValue(Action.MNEMONIC_KEY, new Integer(
201                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                                                          java.awt.event.KeyEvent.VK_E));
202                                            
203                                          if (!checkValidInputs()) return;                                          // // Set tool tip text
204                                                                                    // putValue(Action.SHORT_DESCRIPTION,
205                                          setVisible(false);                                          // "Accept the changes made to the translation."); //i8n
206                                          dispose();  
207                                  }                                  }
208    
209                          });                                  public void actionPerformed(ActionEvent evt) {
210    
211                  }                                          okClose();
212                  jPanel.add(okButton);  
213                                    }
214                  if (cancelButton == null) {  
215                          cancelButton = new CancelButton(new AbstractAction("") {                          });
216                                  public void actionPerformed(ActionEvent evt) {  
217                                          restore();                  }
218                                          TranslationAskJDialog.this.firePropertyChange(                  jPanel.add(okButton);
219                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);  
220                                          setVisible(false);                  if (cancelButton == null) {
221                                          setCancelled(true);                          cancelButton = new CancelButton(new AbstractAction("") {
222                                          dispose();                                  public void actionPerformed(ActionEvent evt) {
223                                  }                                          cancel();
224                          });                                  }
225                  }                          });
226                  jPanel.add(cancelButton);                  }
227                    jPanel.add(cancelButton);
228                  return jPanel;  
229          }                  return jPanel;
230            }
231          /**  
232           * @return <code>true</code> if none of the translations contains illegal characters.          /**
233           */           * This method is only called when the dialog is closed and not canceled.
234          protected boolean checkValidInputs() {           * Can be overwritten to do anything when the dialog has been accepted.
235                             */
236                  for (JComponent component : translationEditJPanelsOrJustComponents) {          public boolean okClose() {
237                          if (component instanceof TranslationEditJPanel) {  
238                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  if (!translationsAskPane.checkValidInputs())
239                                                            return false;
240                                  for (String l : tep.getTranslation().values()){                  
241                                          if ( l.contains("{") || l.contains("}")) {                  
242                                                  JOptionPane.showMessageDialog(this, RESOURCE.getString("ErrorMsg.InvalidCharacterInTranslation"));                  dispose();
243                                                  return false;                  
244                                          }                  TranslationAskJDialog.this.firePropertyChange(
245                                  }                                  PROPERTY_APPLY_AND_CLOSE, null, null);
246                                                    return true;
247                          }          }
248                  }  
249                    
250                    
251                  return true;          /**
252          }           * After the modal dialog has been closed, this allows to find out, whether
253             * the {@link Component} has been canceled.
254          private void setCancelled(boolean hasBeenCanceled) {           *
255                  this.hasBeenCanceled = hasBeenCanceled;           * @return <code>true</code> if the {@link JDialog} has been canceled.
256          }           */
257            public boolean isCancelled() {
258          /**                  return hasBeenCanceled;
259           * After the modal dialog has been closed, this allows to find out, whether          }
260           * the dialog has been canceled.  
261           *  }
          * @return <code>true</code> if the {@link JDialog} has been canceled.  
          */  
         public boolean isCancelled() {  
                 return hasBeenCanceled;  
         }  
   
 }  

Legend:
Removed from v.38  
changed lines
  Added in v.1137

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26