/[schmitzm]/branches/2.2.x/src/skrueger/swing/TranslationEditJPanel.java
ViewVC logotype

Annotation of /branches/2.2.x/src/skrueger/swing/TranslationEditJPanel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 897 - (hide annotations)
Mon Jun 7 14:14:39 2010 UTC (14 years, 8 months ago) by alfonx
File size: 6635 byte(s)
Das ist 2.2-SNAPSHOT

1 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * 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     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * 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     * GNU General Public License for more details.
20     *
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     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     * Martin O. J. Schmitz - initial API and implementation
28 alfonx 862 * Stefan A. Tzeggai - additional utility classes
29 alfonx 244 ******************************************************************************/
30     package skrueger.swing;
31    
32     import java.awt.BorderLayout;
33     import java.awt.Dimension;
34     import java.awt.event.ActionEvent;
35     import java.awt.event.ActionListener;
36 alfonx 711 import java.util.HashMap;
37 alfonx 244 import java.util.List;
38 alfonx 292 import java.util.Random;
39     import java.util.WeakHashMap;
40 alfonx 244
41     import javax.swing.BorderFactory;
42     import javax.swing.JLabel;
43     import javax.swing.JTextField;
44     import javax.swing.SpringLayout;
45     import javax.swing.SwingConstants;
46    
47     import org.apache.log4j.Logger;
48    
49 mojays 289 import schmitzm.swing.JPanel;
50 alfonx 244 import schmitzm.swing.SpringUtilities;
51 alfonx 490 import schmitzm.swing.SwingUtil;
52 alfonx 244 import skrueger.i8n.Translation;
53    
54     /**
55     * A {@link JPanel} that asks the user for the translations of a String in
56     * several languages. Use {@link TranslationAskJDialog} to display.<br/>
57     * The class does not implement any backup/clong strategies. The
58     * {@link Translation} object is manipulated directly.<br/>
59     * {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a
60     * transparent Apply/Cancel logic.
61     *
62 alfonx 862 * @author Stefan Alfons Tzeggai
63 alfonx 244 */
64 alfonx 292 public class TranslationEditJPanel extends JPanel {
65 alfonx 244 static final protected Logger LOGGER = Logger
66     .getLogger(TranslationEditJPanel.class);
67    
68     private final List<String> languages;
69     private JPanel translationGrid;
70     private Translation trans;
71    
72     /**
73     * Remembers all {@link JTextField} that have been created.
74     */
75 alfonx 711 private HashMap<String, TranslationJTextField> langTextFields = new HashMap<String, TranslationJTextField>();
76 alfonx 292 private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>();
77    
78 alfonx 711 private final String question;
79    
80 alfonx 244 /**
81     * Creates a {@link JPanel} that asks the user for the translation of a
82     * String in several languages
83     */
84     public TranslationEditJPanel(Translation trans, List<String> languages_) {
85     this(null, trans, languages_);
86     }
87    
88     /**
89     * Creates a {@link JPanel} that asks the user for the translation of a
90     * String in several languages and additionally puts a {@link JLabel} with a
91     * question at the {@link JPanel}'s first row.
92     */
93     public TranslationEditJPanel(String question, Translation trans,
94     List<String> languages_) {
95     super(new BorderLayout());
96 alfonx 711 this.question = question;
97    
98 alfonx 490 SwingUtil.setMinimumWidth(this, 400);
99 alfonx 244
100     if (trans == null)
101     trans = new Translation();
102    
103     this.trans = trans;
104     this.languages = languages_;
105    
106 alfonx 711 initGUI();
107     }
108    
109     private void initGUI() {
110 alfonx 244 add(getTranslationGrid(), BorderLayout.CENTER);
111    
112     if (question != null) {
113     JLabel questionLable = new JLabel(question);
114     questionLable
115     .setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
116     add(questionLable, BorderLayout.NORTH);
117     }
118     }
119    
120 alfonx 298 /**
121     * Creates a {@link JPanel} that asks the user for the translation of a
122     * String in several languages and additionally puts a {@link JLabel} with a
123     * question at the {@link JPanel}'s first row.
124     *
125     * <br/>
126     * This constructor also sets a TitledBorder with the given title.
127     */
128     public TranslationEditJPanel(Translation title, List<String> languages2,
129     String borderTitle) {
130     this(title, languages2);
131    
132     setBorder(BorderFactory.createTitledBorder(borderTitle));
133     }
134    
135 alfonx 244 private JPanel getTranslationGrid() {
136     if (translationGrid == null) {
137     translationGrid = new JPanel(new SpringLayout());
138    
139     for (String langId : languages) {
140    
141     // language code : entry field for translation
142     JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n
143     langDesc.setHorizontalAlignment(SwingConstants.RIGHT);
144     langDesc.setVerticalAlignment(SwingConstants.NORTH);
145    
146 alfonx 711 TranslationJTextField langTextField = langTextFields
147     .get(langId);
148    
149     if (langTextField == null) {
150     langTextField = new TranslationJTextField(trans, langId);
151     // Setting a size
152     langTextField.setPreferredSize(new Dimension(360, 22));
153     langDesc.setLabelFor(langTextField);
154     }
155    
156 alfonx 244 translationGrid.add(langDesc);
157     translationGrid.add(langTextField);
158 alfonx 292
159 alfonx 711 langTextFields.put(langId, langTextField);
160 alfonx 244 }
161    
162     // Lay out the panel.
163     SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows,
164     2, // cols
165     6, 6, // initX, initY
166     6, 6); // xPad, yPad
167    
168     }
169     return translationGrid;
170     }
171    
172     /**
173     * @return The {@link Translation} that this {@link TranslationEditJPanel}
174     * deals with.
175     */
176     public Translation getTranslation() {
177     return trans;
178     }
179    
180 alfonx 298 /**
181     * TODO Is never called?!
182     */
183     public void dispose() {
184 alfonx 711 for (TranslationJTextField f : langTextFields.values()) {
185 alfonx 298 f.dispose();
186     }
187 alfonx 711 langTextFields.clear();
188 alfonx 298 }
189    
190 alfonx 292 public void addTranslationChangeListener(final ActionListener al) {
191     final ActionListener actionListener = new ActionListener() {
192 alfonx 244
193 alfonx 292 @Override
194     public void actionPerformed(ActionEvent e) {
195     al.actionPerformed(new ActionEvent(TranslationEditJPanel.this,
196     new Random().nextInt(), ""));
197     }
198 alfonx 244
199 alfonx 292 };
200     actionListeners.put(al, actionListener);
201     getTranslation().addTranslationChangeListener(actionListener);
202 alfonx 244 }
203    
204 alfonx 292 public void removeTranslationChangeListener(ActionListener al) {
205     if (actionListeners.get(al) != null) {
206     ActionListener actionListener = actionListeners.get(al);
207     getTranslation().removeTranslationChangeListener(actionListener);
208     actionListeners.remove(actionListener);
209 alfonx 244 }
210     }
211    
212 alfonx 711
213 alfonx 244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26