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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26