/[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 34 by alfonx, Sat Mar 28 18:08:42 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.Dialog;   * 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.ArrayList;   * modify it under the terms of the GNU Lesser General Public License
13  import java.util.Locale;   * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15  import javax.swing.AbstractAction;   *
16  import javax.swing.Action;   * 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.JComponent;   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  import javax.swing.JDialog;   * 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.Window;
36           */  import java.awt.event.ActionEvent;
37          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil  import java.awt.event.ActionListener;
38                          .extendPackagePath(TranslationAskJDialog.class,  import java.awt.event.KeyEvent;
39                                          "resource.locales.SwingResourceBundle"), Locale.ENGLISH);  import java.awt.event.WindowAdapter;
40    import java.awt.event.WindowEvent;
41          private String[] backup = new String[50]; // Maximum 50 languages ;-)  
42          private OkButton okButton;  import javax.swing.AbstractAction;
43          private CancelButton cancelButton;  import javax.swing.Action;
44    import javax.swing.JButton;
45          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";  import javax.swing.JComponent;
46          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";  import javax.swing.JDialog;
47    import javax.swing.JPanel;
48          private final JComponent[] translationEditJPanelsOrJustComponents;  import javax.swing.JRootPane;
49    import javax.swing.KeyStroke;
50          private boolean hasBeenCanceled;  
51    import schmitzm.swing.SwingUtil;
52          /**  
53           * Since the registerKeyboardAction() method is part of the JComponent class  public class TranslationAskJDialog extends CancellableDialogAdapter{
54           * definition, you must define the Escape keystroke and register the  
55           * keyboard action with a JComponent, not with a JDialog. The JRootPane for          private OkButton okButton;
56           * the JDialog serves as an excellent choice to associate the registration,          private CancelButton cancelButton;
57           * as this will always be visible. If you override the protected  
58           * createRootPane() method of JDialog, you can return your custom JRootPane          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
59           * with the keystroke enabled:          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
60           */  
61          @Override          private JComponent[] translationEditJPanelsOrJustComponents;
62          protected JRootPane createRootPane() {  
63                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);          private boolean hasBeenCanceled;
64                  JRootPane rootPane = new JRootPane();  
65                  rootPane.registerKeyboardAction(new ActionListener() {          private JButton[] optionalButtons;
66            private TranslationsAskJPanel translationsAskPane;
67                          public void actionPerformed(ActionEvent e) {  
68                                  cancel();          /**
69                          }           * Since the registerKeyboardAction() method is part of the JComponent class
70             * definition, you must define the Escape keystroke and register the
71                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);           * keyboard action with a JComponent, not with a JDialog. The JRootPane for
72             * the JDialog serves as an excellent choice to associate the registration,
73                  return rootPane;           * as this will always be visible. If you override the protected
74          }           * createRootPane() method of JDialog, you can return your custom JRootPane
75             * with the keystroke enabled:
76          /**           */
77           * This class handles the cancel button itself. You may still want to listen          @Override
78           * to PROPERTY_APPLY_AND_CLOSE events.          protected JRootPane createRootPane() {
79           *                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
80           * This dialog is modal. The dialog has to be set visible afterwards.                  JRootPane rootPane = new JRootPane();
81           */                  rootPane.registerKeyboardAction(new ActionListener() {
82          public TranslationAskJDialog(Dialog owner,  
83                          final JComponent... translationEditJPanels) {                          public void actionPerformed(ActionEvent e) {
84                  super(owner);                                  cancel();
85                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                          }
86                  init();  
87          }                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
88    
89          /**                  return rootPane;
90           * This class handles the cancel button itself. You may still want to listen          }
91           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has  
92           * to be set visible afterwards.          /**
93           */           * The {@link TranslationAskJDialog} fills its content pane with an
94          public TranslationAskJDialog(Window owner,           * arbitrary number of components. If these {@link Component}s are
95                          final JComponent... translationEditJPanels) {           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
96                  super(owner);           * values and restore them if the dialog is canceled. Other
97                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * {@link JComponent}s are just displayed.<br/>
98                  init();           * 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
100                  // Rememebr backups for all the jtextpanels           * to be set visible afterwards.<br/>
101                  int count = 0;           */
102                  for (JComponent component : translationEditJPanelsOrJustComponents) {          public TranslationAskJDialog(Component owner,
103                          if (component instanceof TranslationEditJPanel) {                          final JComponent... translationEditJPanels) {
104                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  super(owner);
105                                  Translation orig = tep.getTranslation();                  setComponents(translationEditJPanels);
106                                            }
107                                  // We don't want to overwrite the Translation object on  
108                                  // restore(). We just want to change its value.          /**
109             * The {@link TranslationAskJDialog} fills its content pane with an
110                                  backup[count++] = orig.toOneLine();           * arbitrary number of components. If these {@link Component}s are
111                          }           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
112                  }           * values and restore them if the dialog is canceled. Other
113          }           * {@link JComponent}s are just displayed.<br/>
114             * This class handles the cancel button itself. You may still want to listen
115          private void init() {           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
116                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           * to be set visible afterwards.<br/>
117                  addWindowListener(new WindowAdapter() {           * Using this constructor, you have to call setComponents afterwards.
118             */
119                          public void windowClosing(WindowEvent e) {          public TranslationAskJDialog(Component owner) {
120                                  cancel();                  super(owner);
121                          }          }
122    
123                  });          /**
124                  SwingUtil.centerFrameOnScreen(this);           * The {@link TranslationAskJDialog} fills its content pane with an
125                  Box box = Box.createVerticalBox();           * arbitrary number of components. If these {@link Component}s are
126                  for (JComponent panel : translationEditJPanelsOrJustComponents) {           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
127                          box.add(panel);           * values and restore them if the dialog is canceled. Other
128                  }           * {@link JComponent}s are just displayed.
129                  JPanel cp = new JPanel(new BorderLayout());           *
130                  cp.add(box, BorderLayout.CENTER);           * @param translationEditJPanels
131                  cp.add(getButtons(), BorderLayout.SOUTH);           *            Arbitrary list of {@link JComponent}s and
132                  setContentPane(cp);           *            {@link TranslationEditJPanel}s.
133             */
134                  // dialog.getRootPane().setDefaultButton(okButton);          public void setComponents(final JComponent... translationEditJPanels) {
135                    this.translationEditJPanelsOrJustComponents = translationEditJPanels;
136                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n  
137                  setModal(true);                  init();
138                  pack();          }
139          }  
140    
141          protected void cancel() {          private void init() {
142                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
143                  restore();                  addWindowListener(new WindowAdapter() {
144                  setVisible(false);  
145                  dispose();                          public void windowClosing(WindowEvent e) {
146          }                                  cancel();
147                            }
148          private void restore() {  
149                  int count = 0;                  });
150                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  
151                          if (component instanceof TranslationEditJPanel) {                  translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents);
152                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  JPanel cp = new JPanel(new BorderLayout());
153                                  tep.getTranslation().fromOneLine(backup[count++]);                  cp.add(translationsAskPane, BorderLayout.WEST);
154                          }                  cp.add(getButtons(), BorderLayout.SOUTH);
155                  }                  setContentPane(cp);
156          }  
157                    setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
158          private JComponent getButtons() {                  setModal(true);
159                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  pack();
160                  if (okButton == null) {                  SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5);
161                          okButton = new OkButton(new AbstractAction() {          }
162                                  {  
163                                          // Set a mnemonic character. In most look and feels, this          public void setOptionalButtons(JButton... optionalButtons) {
164                                          // causes the                  this.optionalButtons = optionalButtons;
165                                          // specified character to be underlined This indicates that                  init();
166                                          // if the component          }
167                                          // using this action has the focus and In some look and  
168                                          // feels, this causes          /**
169                                          // the specified character in the label to be underlined and           * Called when the dilaog is closed using the cancel button. When
170                                          putValue(Action.MNEMONIC_KEY, new Integer(           * overwriting this method, call super.cancel() after restoring your
171                                                          java.awt.event.KeyEvent.VK_E));           * properties.
172             */
173                                          // Set tool tip text          @Override
174                                          putValue(Action.SHORT_DESCRIPTION,          public void cancel() {
175                                                          "Accept the changes made to the translation.");                  translationsAskPane.cancel();
176                    firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
177                                  }                  hasBeenCanceled = true;
178                    setVisible(false);
179                                  public void actionPerformed(ActionEvent evt) {                  dispose();
180                                          TranslationAskJDialog.this.firePropertyChange(          }
181                                                          PROPERTY_APPLY_AND_CLOSE, null, null);  
182                                          setVisible(false);  
183                                          dispose();          private JComponent getButtons() {
184                                          System.out.println("OK button action performed");                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
185                                  }  
186                    if (optionalButtons != null)
187                          });                          for (JButton b : optionalButtons) {
188                          // okButton.addKeyListener( new KeyListener() {                                  jPanel.add(b);
189                          //                          }
190                          // public void keyTyped(KeyEvent e) {  
191                          // if ()                  if (okButton == null) {
192                          // okButton.action(new KEyPreEvent(), what)                          okButton = new OkButton(new AbstractAction() {
193                          // }                                  {
194                          //                                                                        // Set a mnemonic character. In most look and feels, this
195                          // });                                          // causes the
196                  }                                          // specified character to be underlined This indicates that
197                  jPanel.add(okButton);                                          // if the component
198                                            // using this action has the focus and In some look and
199                  if (cancelButton == null) {                                          // feels, this causes
200                          cancelButton = new CancelButton(new AbstractAction("") {                                          // the specified character in the label to be underlined and
201                                  public void actionPerformed(ActionEvent evt) {                                          putValue(Action.MNEMONIC_KEY, new Integer(
202                                          // restore();                                                          java.awt.event.KeyEvent.VK_E));
203                                          TranslationAskJDialog.this.firePropertyChange(  
204                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                                          // // Set tool tip text
205                                          setVisible(false);                                          // putValue(Action.SHORT_DESCRIPTION,
206                                          setHasBeenCanceled(true);                                          // "Accept the changes made to the translation."); //i8n
207                                          dispose();  
208                                  }                                  }
209                          });  
210                  }                                  public void actionPerformed(ActionEvent evt) {
211                  jPanel.add(cancelButton);  
212                                            okClose();
213                  return jPanel;  
214          }                                  }
215    
216          public static void main(String[] args) {                          });
217                  ArrayList<String> lang = new ArrayList<String>();  
218                  lang.add("de");                  }
219                  lang.add("en");                  jPanel.add(okButton);
220                  lang.add("fr");  
221                    if (cancelButton == null) {
222                  Translation transe = new Translation();                          cancelButton = new CancelButton(new AbstractAction("") {
223                  transe.put("de", "Terciopelo-Lanzenotter");                                  public void actionPerformed(ActionEvent evt) {
224                  TranslationEditJPanel p1 = new TranslationEditJPanel(                                          cancel();
225                                  "Name von New Group", transe, lang);                                  }
226                            });
227                  Translation transe2 = new Translation();                  }
228                  transe2                  jPanel.add(cancelButton);
229                                  .put(  
230                                                  "de",                  return jPanel;
231                                                  "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");          }
232                  TranslationEditJPanel p2 = new TranslationEditJPanel(  
233                                  "Description of Animal:", transe2, lang);          /**
234             * This method is only called when the dialog is closed and not canceled.
235                  // JFrame frame = new JFrame();           * Can be overwritten to do anything when the dialog has been accepted.
236                  // frame.setContentPane(p1);           */
237                  // frame.pack();          public boolean okClose() {
238                  // frame.setVisible(true);  
239                    if (!translationsAskPane.checkValidInputs())
240                  TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);                          return false;
241                  dialog.setVisible(true);                  
242          }                  
243                    dispose();
244          private void setHasBeenCanceled(boolean hasBeenCanceled) {                  
245                  this.hasBeenCanceled = hasBeenCanceled;                  TranslationAskJDialog.this.firePropertyChange(
246          }                                  PROPERTY_APPLY_AND_CLOSE, null, null);
247                    return true;
248          /**          }
249           * After the modal dialog has been closed, this allows to find out, whether  
250           * the dialog has been canceled.  
251           *  
252           * @return <code>true</code> if the {@link JDialog} has been canceled.          /**
253           */           * After the modal dialog has been closed, this allows to find out, whether
254          public boolean isHasBeenCanceled() {           * the {@link Component} has been canceled.
255                  return hasBeenCanceled;           *
256          }           * @return <code>true</code> if the {@link JDialog} has been canceled.
257             */
258  }          public boolean isCancelled() {
259                    return hasBeenCanceled;
260            }
261    
262    }

Legend:
Removed from v.34  
changed lines
  Added in v.1136

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26