/[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 33 by alfonx, Sat Mar 28 17:06:27 2009 UTC revision 862 by alfonx, Sat May 22 01:24: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) {           * @param owner
103                          if (component instanceof TranslationEditJPanel) {           *            A component of the GUI that this dialog is related to. If no
104                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
105                                  Translation orig = tep.getTranslation();           *            called.
106                                  // We dont' want to overwrite the Translation object on           */
107                                  // restore(). We just want to change its values.          public TranslationAskJDialog(Component owner,
108                            final JComponent... translationEditJPanels) {
109                                  backup[count++] = orig.toOneLine();                  super(SwingUtil.getParentWindow(owner));
110                          }                  setComponents(translationEditJPanels);
111                  }          }
112          }  
113            /**
114          private void init() {           * The {@link TranslationAskJDialog} fills its content pane with an
115                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           * arbitrary number of components. If these {@link Component}s are
116                  addWindowListener(new WindowAdapter() {           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
117             * values and restore them if the dialog is canceled. Other
118                          public void windowClosing(WindowEvent e) {           * {@link JComponent}s are just displayed.<br/>
119                                  cancel();           * This class handles the cancel button itself. You may still want to listen
120                          }           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
121             * to be set visible afterwards.<br/>
122                  });           * Using this constructor, you have to call setComponents afterwards.
123                  SwingUtil.centerFrameOnScreen(this);           */
124                  Box box = Box.createVerticalBox();          public TranslationAskJDialog(Component owner) {
125                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                  super(SwingUtil.getParentWindow(owner));
126                          box.add(panel);          }
127                  }  
128                  JPanel cp = new JPanel(new BorderLayout());          /**
129                  cp.add(box, BorderLayout.CENTER);           * The {@link TranslationAskJDialog} fills its content pane with an
130                  cp.add(getButtons(), BorderLayout.SOUTH);           * arbitrary number of components. If these {@link Component}s are
131                  setContentPane(cp);           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
132             * values and restore them if the dialog is canceled. Other
133                  // dialog.getRootPane().setDefaultButton(okButton);           * {@link JComponent}s are just displayed.
134             *
135                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n           * @param translationEditJPanels
136                  setModal(true);           *            Arbitrary list of {@link JComponent}s and
137                  pack();           *            {@link TranslationEditJPanel}s.
138          }           */
139            public void setComponents(final JComponent... translationEditJPanels) {
140          protected void cancel() {                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
141                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);  
142                  restore();                  init();
143                  setVisible(false);          }
144                  dispose();  
145          }  
146            private void init() {
147          private void restore() {                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
148                  int count = 0;                  addWindowListener(new WindowAdapter() {
149                  for (JComponent component : translationEditJPanelsOrJustComponents) {  
150                          if (component instanceof TranslationEditJPanel) {                          public void windowClosing(WindowEvent e) {
151                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                  cancel();
152                                  tep.getTranslation().fromOneLine(backup[count++]);                          }
153                          }  
154                  }                  });
155          }                  
156                    translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents);
157          private JComponent getButtons() {                  JPanel cp = new JPanel(new BorderLayout());
158                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  cp.add(translationsAskPane, BorderLayout.WEST);
159                  if (okButton == null) {                  cp.add(getButtons(), BorderLayout.SOUTH);
160                          okButton = new OkButton(new AbstractAction() {                  setContentPane(cp);
161                                  {  
162                                          // Set a mnemonic character. In most look and feels, this                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
163                                          // causes the                  setModal(true);
164                                          // specified character to be underlined This indicates that                  pack();
165                                          // if the component                  SwingUtil.setRelativeFramePosition(this, getParent(), .5, .5);
166                                          // using this action has the focus and In some look and          }
167                                          // feels, this causes  
168                                          // the specified character in the label to be underlined and          public void setOptionalButtons(JButton... optionalButtons) {
169                                          putValue(Action.MNEMONIC_KEY, new Integer(                  this.optionalButtons = optionalButtons;
170                                                          java.awt.event.KeyEvent.VK_E));                  init();
171            }
172                                          // Set tool tip text  
173                                          putValue(Action.SHORT_DESCRIPTION,          /**
174                                                          "Accept the changes made to the translation.");           * Called when the dilaog is closed using the cancel button. When
175             * overwriting this method, call super.cancel() after restoring your
176                                  }           * properties.
177             */
178                                  public void actionPerformed(ActionEvent evt) {          @Override
179                                          TranslationAskJDialog.this.firePropertyChange(          public void cancel() {
180                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                  translationsAskPane.cancel();
181                                          setVisible(false);                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
182                                          dispose();                  hasBeenCanceled = true;
183                                          System.out.println("OK button action performed");                  setVisible(false);
184                                  }                  dispose();
185            }
186                          });  
187                          // okButton.addKeyListener( new KeyListener() {  
188                          //          private JComponent getButtons() {
189                          // public void keyTyped(KeyEvent e) {                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
190                          // if ()  
191                          // okButton.action(new KEyPreEvent(), what)                  if (optionalButtons != null)
192                          // }                          for (JButton b : optionalButtons) {
193                          //                                                                jPanel.add(b);
194                          // });                          }
195                  }  
196                  jPanel.add(okButton);                  if (okButton == null) {
197                            okButton = new OkButton(new AbstractAction() {
198                  if (cancelButton == null) {                                  {
199                          cancelButton = new CancelButton(new AbstractAction("") {                                          // Set a mnemonic character. In most look and feels, this
200                                  public void actionPerformed(ActionEvent evt) {                                          // causes the
201                                          // restore();                                          // specified character to be underlined This indicates that
202                                          TranslationAskJDialog.this.firePropertyChange(                                          // if the component
203                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                                          // using this action has the focus and In some look and
204                                          setVisible(false);                                          // feels, this causes
205                                          setHasBeenCanceled(true);                                          // the specified character in the label to be underlined and
206                                          dispose();                                          putValue(Action.MNEMONIC_KEY, new Integer(
207                                  }                                                          java.awt.event.KeyEvent.VK_E));
208                          });  
209                  }                                          // // Set tool tip text
210                  jPanel.add(cancelButton);                                          // putValue(Action.SHORT_DESCRIPTION,
211                                            // "Accept the changes made to the translation."); //i8n
212                  return jPanel;  
213          }                                  }
214    
215          public static void main(String[] args) {                                  public void actionPerformed(ActionEvent evt) {
216                  ArrayList<String> lang = new ArrayList<String>();  
217                  lang.add("de");                                          okClose();
218                  lang.add("en");  
219                  lang.add("fr");                                  }
220    
221                  Translation transe = new Translation();                          });
222                  transe.put("de", "Terciopelo-Lanzenotter");  
223                  TranslationEditJPanel p1 = new TranslationEditJPanel(                  }
224                                  "Name von New Group", transe, lang);                  jPanel.add(okButton);
225    
226                  Translation transe2 = new Translation();                  if (cancelButton == null) {
227                  transe2                          cancelButton = new CancelButton(new AbstractAction("") {
228                                  .put(                                  public void actionPerformed(ActionEvent evt) {
229                                                  "de",                                          cancel();
230                                                  "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");                                  }
231                  TranslationEditJPanel p2 = new TranslationEditJPanel(                          });
232                                  "Description of Animal:", transe2, lang);                  }
233                    jPanel.add(cancelButton);
234                  // JFrame frame = new JFrame();  
235                  // frame.setContentPane(p1);                  return jPanel;
236                  // frame.pack();          }
237                  // frame.setVisible(true);  
238            /**
239                  TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);           * This method is only called when the dialog is closed and not canceled.
240                  dialog.setVisible(true);           * Can be overwritten to do anything when the dialog has been accepted.
241          }           */
242            public boolean okClose() {
243          private void setHasBeenCanceled(boolean hasBeenCanceled) {  
244                  this.hasBeenCanceled = hasBeenCanceled;                  if (!translationsAskPane.checkValidInputs())
245          }                          return false;
246                    
247          /**                  
248           * After the modal dialog has been closed, this allows to find out, wether                  dispose();
249           * the dialog has been canceled.                  
250           *                  TranslationAskJDialog.this.firePropertyChange(
251           * @return                                  PROPERTY_APPLY_AND_CLOSE, null, null);
252           */                  return true;
253          public boolean isHasBeenCanceled() {          }
254                  return hasBeenCanceled;  
255          }  
256    
257  }          /**
258             * After the modal dialog has been closed, this allows to find out, whether
259             * the {@link Component} has been canceled.
260             *
261             * @return <code>true</code> if the {@link JDialog} has been canceled.
262             */
263            public boolean isCancelled() {
264                    return hasBeenCanceled;
265            }
266    
267    }

Legend:
Removed from v.33  
changed lines
  Added in v.862

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26