/[schmitzm]/branches/2.0-RC2/src/skrueger/swing/TranslationEditJPanel.java
ViewVC logotype

Diff of /branches/2.0-RC2/src/skrueger/swing/TranslationEditJPanel.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/swing/TranslationEditJPanel.java revision 38 by alfonx, Sun Apr 5 15:06:56 2009 UTC branches/2.0-RC2/src/skrueger/swing/TranslationEditJPanel.java revision 621 by alfonx, Thu Jan 28 10:06:05 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.Dimension;   * This file is part of the SCHMITZM library - a collection of utility
5  import java.util.List;   * classes based on Java 1.6, focusing (not only) on Java Swing
6     * and the Geotools library.
7  import javax.swing.BorderFactory;   *
8  import javax.swing.JDialog;   * The SCHMITZM project is hosted at:
9  import javax.swing.JLabel;   * http://wald.intevation.org/projects/schmitzm/
10  import javax.swing.JPanel;   *
11  import javax.swing.SpringLayout;   * This program is free software; you can redistribute it and/or
12  import javax.swing.SwingConstants;   * 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 org.apache.log4j.Logger;   * of the License, or (at your option) any later version.
15     *
16  import schmitzm.swing.SpringUtilities;   * This program is distributed in the hope that it will be useful,
17  import skrueger.i8n.Translation;   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  /**   * GNU General Public License for more details.
20   * A {@link JPanel} that asks the user for translation of several strings   *
21   * @author Stefan Alfons Krüger   * You should have received a copy of the GNU Lesser General Public License (license.txt)
22   *   * 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  public class TranslationEditJPanel extends JPanel {   * or try this link: http://www.gnu.org/licenses/lgpl.html
25          static final protected Logger LOGGER = Logger.getLogger(TranslationEditJPanel.class);   *
26             * Contributors:
27          private final List<String> languages;   *     Martin O. J. Schmitz - initial API and implementation
28          private JPanel translationGrid;   *     Stefan A. Krüger - additional utility classes
29          private Translation trans;   ******************************************************************************/
30    package skrueger.swing;
31          public TranslationEditJPanel(Translation trans, List<String> languages_) {  
32                  this(null, trans, languages_);  import java.awt.BorderLayout;
33          }  import java.awt.Dimension;
34    import java.awt.event.ActionEvent;
35          public TranslationEditJPanel(String question, Translation trans,  import java.awt.event.ActionListener;
36                          List<String> languages_) {  import java.util.HashSet;
37                  super(new BorderLayout());  import java.util.List;
38                    import java.util.Random;
39                  if (trans == null) trans = new Translation();  import java.util.Set;
40    import java.util.WeakHashMap;
41                  this.trans = trans;  
42                  this.languages = languages_;  import javax.swing.BorderFactory;
43                    import javax.swing.JLabel;
44                  add(getTranslationGrid(), BorderLayout.CENTER);  import javax.swing.JTextField;
45    import javax.swing.SpringLayout;
46                  if (question != null) {  import javax.swing.SwingConstants;
47                          JLabel questionLable = new JLabel(question);  
48                          questionLable.setBorder(BorderFactory.createEmptyBorder(  import org.apache.log4j.Logger;
49                                          6, 6, 6, 6));  
50                          add(questionLable, BorderLayout.NORTH);  import schmitzm.swing.JPanel;
51                  }  import schmitzm.swing.SpringUtilities;
52          }  import schmitzm.swing.SwingUtil;
53    import skrueger.i8n.Translation;
54          private JPanel getTranslationGrid() {  
55                  if (translationGrid == null) {  /**
56                          translationGrid = new JPanel(new SpringLayout());   * A {@link JPanel} that asks the user for the translations of a String in
57     * several languages. Use {@link TranslationAskJDialog} to display.<br/>
58                          for (String langId : languages) {   * The class does not implement any backup/clong strategies. The
59     * {@link Translation} object is manipulated directly.<br/>
60                                  // language code : entry field for translation   * {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a
61                                  JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n   * transparent Apply/Cancel logic.
62                                  langDesc.setHorizontalAlignment(SwingConstants.RIGHT);   *
63                                  langDesc.setVerticalAlignment(SwingConstants.NORTH);   * @author Stefan Alfons Krüger
64     */
65                                  TranslationJTextField langTextField = new TranslationJTextField(  public class TranslationEditJPanel extends JPanel {
66                                                  trans, langId);          static final protected Logger LOGGER = Logger
67                                  // Setting a size                          .getLogger(TranslationEditJPanel.class);
68                                  langTextField.setPreferredSize( new Dimension(360,22));  
69                                  langDesc.setLabelFor(langTextField);          private final List<String> languages;
70                                  translationGrid.add(langDesc);          private JPanel translationGrid;
71                                  translationGrid.add(langTextField);          private Translation trans;
72                          }  
73            /**
74                          // Lay out the panel.           * Remembers all {@link JTextField} that have been created.
75                          SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows,           */
76                                          2, // cols           private Set<TranslationJTextField> langTextFields = new HashSet<TranslationJTextField>();
77                                          6, 6, // initX, initY          private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>();
78                                          6, 6); // xPad, yPad  
79            /**
80                  }           * Creates a {@link JPanel} that asks the user for the translation of a
81                  return translationGrid;           * String in several languages
82          }           */
83            public TranslationEditJPanel(Translation trans, List<String> languages_) {
84  //      /**                  this(null, trans, languages_);
85  //       * Merges a few {@link TranslationEditJPanel}s and shows them together..          }
86  //       * So far this is working on the Translation object directly, Cancel not possible.  
87  //       * @param translationEditJPanel          /**
88  //       *           * Creates a {@link JPanel} that asks the user for the translation of a
89  //       * @deprecated Use {@link TranslationAskJDialog}           * String in several languages and additionally puts a {@link JLabel} with a
90  //       */           * question at the {@link JPanel}'s first row.
91  //      public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) {           */
92  //              dialog = new JDialog(parentFrame);          public TranslationEditJPanel(String question, Translation trans,
93  //      //      backup( translationEditJPanels );                          List<String> languages_) {
94  //              showDialog(dialog, translationEditJPanels);                  super(new BorderLayout());
95  //      }                  
96  //                  SwingUtil.setMinimumWidth(this, 400);
97  //        
98  //      /**                  if (trans == null)
99  //       * Merges a few {@link TranslationEditJPanel}s and shows them together..                          trans = new Translation();
100  //       * So far this is working on the Translation object directly, Cancel not possible.  
101  //       * @param translationEditJPanel                  this.trans = trans;
102  //                  this.languages = languages_;
103  //       * @deprecated User {@link TranslationAskJDialog}        *  
104  //       */                  add(getTranslationGrid(), BorderLayout.CENTER);
105  //      public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) {  
106  //              dialog = new JDialog(parentWindow);                  if (question != null) {
107  //      //      backup( translationEditJPanels );                          JLabel questionLable = new JLabel(question);
108  //              showDialog(dialog, translationEditJPanels);                          questionLable
109  //      }                                          .setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
110  //                                add(questionLable, BorderLayout.NORTH);
111  //      /**                  }
112  //       * Merges a few {@link TranslationEditJPanel}s and shows them together..  
113  //       * So far this is working on the Translation object directly, Cancel not possible.                  /**
114  //       * @param translationEditJPanel                   * Add listeners
115  //       *                   */
116  //       *       * @deprecated User {@link TranslationAskJDialog}          }
117  //       */  
118  //      public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) {          /**
119  //              dialog = new JDialog(parentDialog);           * Creates a {@link JPanel} that asks the user for the translation of a
120  //      //      backup( translationEditJPanels );           * String in several languages and additionally puts a {@link JLabel} with a
121  //              showDialog(dialog, translationEditJPanels);           * question at the {@link JPanel}'s first row.
122  //      }           *
123  //                 * <br/>
124  //                 * This constructor also sets a TitledBorder with the given title.
125  //      /**           */
126  //       *       * @deprecated User {@link TranslationAskJDialog}          public TranslationEditJPanel(Translation title, List<String> languages2,
127  //       * @param d                          String borderTitle) {
128  //       * @param translationEditJPanels                  this(title, languages2);
129  //       * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>  
130  //       */                  setBorder(BorderFactory.createTitledBorder(borderTitle));
131  //      public static void showDialog(JDialog d, JComponent... translationEditJPanels) {          }
132  //              dialog = d;  
133  //              dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);          private JPanel getTranslationGrid() {
134  //              SwingUtil.centerFrameOnScreen(dialog);                  if (translationGrid == null) {
135  //              Box box = Box.createVerticalBox();                          translationGrid = new JPanel(new SpringLayout());
136  //              for (JComponent panel : translationEditJPanels) {  
137  //                      box.add(panel);                          for (String langId : languages) {
138  //              }  
139  //              JPanel cp = new JPanel( new BorderLayout());                                  // language code : entry field for translation
140  //              cp.add( box, BorderLayout.CENTER);                                  JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n
141  //              cp.add( getButtons(), BorderLayout.SOUTH  );                                  langDesc.setHorizontalAlignment(SwingConstants.RIGHT);
142  //              dialog.setContentPane(cp);                                  langDesc.setVerticalAlignment(SwingConstants.NORTH);
143  //                
144  //              // dialog.getRootPane().setDefaultButton(okButton);                                  TranslationJTextField langTextField = new TranslationJTextField(
145  //                                                                trans, langId);
146  //              dialog.setTitle("Please translate"); //i8n                                  // Setting a size
147  //              dialog.setModal(true);                                  langTextField.setPreferredSize(new Dimension(360, 22));
148  //              dialog.pack();                                  langDesc.setLabelFor(langTextField);
149  //              dialog.setVisible(true);                                  translationGrid.add(langDesc);
150  //      }                                  translationGrid.add(langTextField);
151  //  
152  //      /**                                   langTextFields.add(langTextField);
153  //       * @deprecated Use TranslationAskJDialog                          }
154  //       * @return  
155  //       * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>                          // Lay out the panel.
156  //       */                          SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows,
157  //      private static JComponent getButtons() {                                          2, // cols
158  //              JPanel jPanel = new JPanel();                                          6, 6, // initX, initY
159  //              if (okButton == null) {                                          6, 6); // xPad, yPad
160  //                      okButton = new OkButton(new AbstractAction("enter") {  
161  //                  public void actionPerformed(ActionEvent evt) {                  }
162  //                      firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null);                  return translationGrid;
163  //                      dialog.dispose();          }
164  //                  }  
165  //              } );          /**
166  //              }           * @return The {@link Translation} that this {@link TranslationEditJPanel}
167  //              jPanel.add(okButton);           *         deals with.
168  //           */
169  //              if (cancelButton == null) {          public Translation getTranslation() {
170  //                      cancelButton = new CancelButton(new AbstractAction("") {                  return trans;
171  //                  public void actionPerformed(ActionEvent evt) {          }
172  //                      // restore();  
173  //                      firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);          /**
174  //                      dialog.dispose();           * TODO Is never called?!
175  //                  }           */
176  //              } );          public void dispose() {
177  //              }                  for (TranslationJTextField f: langTextFields) {
178  //              jPanel.add(okButton);                          f.dispose();
179  //                  }
180  //              return jPanel;          }
181  //      }  
182                    public void addTranslationChangeListener(final ActionListener al) {
183          /**                  final ActionListener actionListener = new ActionListener() {
184           * @return The {@link Translation} that this {@link TranslationEditJPanel} deals with.  
185           */                          @Override
186          public Translation getTranslation() {                          public void actionPerformed(ActionEvent e) {
187                  return trans;                                  al.actionPerformed(new ActionEvent(TranslationEditJPanel.this,
188          }                                                  new Random().nextInt(), ""));
189                            }
190  }  
191                    };
192                    actionListeners.put(al, actionListener);
193                    getTranslation().addTranslationChangeListener(actionListener);
194            }
195    
196            public void removeTranslationChangeListener(ActionListener al) {
197                    if (actionListeners.get(al) != null) {
198                            ActionListener actionListener = actionListeners.get(al);
199                            getTranslation().removeTranslationChangeListener(actionListener);
200                            actionListeners.remove(actionListener);
201                    }
202            }
203    
204    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26