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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 315 - (show annotations)
Wed Aug 26 11:03:27 2009 UTC (15 years, 6 months ago) by mojays
Original Path: branches/1.0-gt2-2.6/src/skrueger/swing/TranslationEditJPanel.java
File size: 6355 byte(s)
Branch created from SCHMITZM 1.0 (rev 313) to switch to gt2-2.6.x.
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * 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 * Stefan A. Krüger - additional utility classes
29 ******************************************************************************/
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 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Random;
39 import java.util.Set;
40 import java.util.WeakHashMap;
41
42 import javax.swing.BorderFactory;
43 import javax.swing.JLabel;
44 import javax.swing.JTextField;
45 import javax.swing.SpringLayout;
46 import javax.swing.SwingConstants;
47
48 import org.apache.log4j.Logger;
49
50 import schmitzm.swing.JPanel;
51 import schmitzm.swing.SpringUtilities;
52 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 * @author Stefan Alfons Krüger
63 */
64 public class TranslationEditJPanel extends JPanel {
65 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 private Set<TranslationJTextField> langTextFields = new HashSet<TranslationJTextField>();
76 private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>();
77
78 /**
79 * Creates a {@link JPanel} that asks the user for the translation of a
80 * String in several languages
81 */
82 public TranslationEditJPanel(Translation trans, List<String> languages_) {
83 this(null, trans, languages_);
84 }
85
86 /**
87 * Creates a {@link JPanel} that asks the user for the translation of a
88 * String in several languages and additionally puts a {@link JLabel} with a
89 * question at the {@link JPanel}'s first row.
90 */
91 public TranslationEditJPanel(String question, Translation trans,
92 List<String> languages_) {
93 super(new BorderLayout());
94
95 if (trans == null)
96 trans = new Translation();
97
98 this.trans = trans;
99 this.languages = languages_;
100
101 add(getTranslationGrid(), BorderLayout.CENTER);
102
103 if (question != null) {
104 JLabel questionLable = new JLabel(question);
105 questionLable
106 .setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
107 add(questionLable, BorderLayout.NORTH);
108 }
109
110 /**
111 * Add listeners
112 */
113 }
114
115 /**
116 * Creates a {@link JPanel} that asks the user for the translation of a
117 * String in several languages and additionally puts a {@link JLabel} with a
118 * question at the {@link JPanel}'s first row.
119 *
120 * <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 this(title, languages2);
126
127 setBorder(BorderFactory.createTitledBorder(borderTitle));
128 }
129
130 private JPanel getTranslationGrid() {
131 if (translationGrid == null) {
132 translationGrid = new JPanel(new SpringLayout());
133
134 for (String langId : languages) {
135
136 // language code : entry field for translation
137 JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n
138 langDesc.setHorizontalAlignment(SwingConstants.RIGHT);
139 langDesc.setVerticalAlignment(SwingConstants.NORTH);
140
141 TranslationJTextField langTextField = new TranslationJTextField(
142 trans, langId);
143 // Setting a size
144 langTextField.setPreferredSize(new Dimension(360, 22));
145 langDesc.setLabelFor(langTextField);
146 translationGrid.add(langDesc);
147 translationGrid.add(langTextField);
148
149 langTextFields.add(langTextField);
150 }
151
152 // Lay out the panel.
153 SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows,
154 2, // cols
155 6, 6, // initX, initY
156 6, 6); // xPad, yPad
157
158 }
159 return translationGrid;
160 }
161
162 /**
163 * @return The {@link Translation} that this {@link TranslationEditJPanel}
164 * deals with.
165 */
166 public Translation getTranslation() {
167 return trans;
168 }
169
170 /**
171 * TODO Is never called?!
172 */
173 public void dispose() {
174 for (TranslationJTextField f: langTextFields) {
175 f.dispose();
176 }
177 }
178
179 public void addTranslationChangeListener(final ActionListener al) {
180 final ActionListener actionListener = new ActionListener() {
181
182 @Override
183 public void actionPerformed(ActionEvent e) {
184 al.actionPerformed(new ActionEvent(TranslationEditJPanel.this,
185 new Random().nextInt(), ""));
186 }
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 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26