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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26