/[schmitzm]/trunk/src/skrueger/swing/TranslationEditJPanel.java
ViewVC logotype

Diff of /trunk/src/skrueger/swing/TranslationEditJPanel.java

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

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

Legend:
Removed from v.127  
changed lines
  Added in v.298

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26