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 the translations of a String in |
* GNU General Public License for more details. |
20 |
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
* |
21 |
* The class does not implement any backup/clong strategies. The |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
* {@link Translation} object is manipulated directly.<br/> |
* along with this program; if not, write to the Free Software |
23 |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
* transparent Apply/Cancel logic. |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
* |
* |
26 |
* @author Stefan Alfons Krüger |
* Contributors: |
27 |
*/ |
* Martin O. J. Schmitz - initial API and implementation |
28 |
public class TranslationEditJPanel extends JPanel { |
* Stefan A. Krüger - additional utility classes |
29 |
static final protected Logger LOGGER = Logger |
******************************************************************************/ |
30 |
.getLogger(TranslationEditJPanel.class); |
package skrueger.swing; |
31 |
|
|
32 |
private final List<String> languages; |
import java.awt.BorderLayout; |
33 |
private JPanel translationGrid; |
import java.awt.Dimension; |
34 |
private Translation trans; |
import java.awt.event.ActionEvent; |
35 |
|
import java.awt.event.ActionListener; |
36 |
/** |
import java.util.HashSet; |
37 |
* Creates a {@link JPanel} that asks the user for the translation of a |
import java.util.List; |
38 |
* String in several languages |
import java.util.Random; |
39 |
*/ |
import java.util.Set; |
40 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
import java.util.WeakHashMap; |
41 |
this(null, trans, languages_); |
|
42 |
} |
import javax.swing.BorderFactory; |
43 |
|
import javax.swing.JLabel; |
44 |
/** |
import javax.swing.JTextField; |
45 |
* Creates a {@link JPanel} that asks the user for the translation of a |
import javax.swing.SpringLayout; |
46 |
* String in several languages and additionally puts a {@link JLabel} with a |
import javax.swing.SwingConstants; |
47 |
* question at the {@link JPanel}'s first row. |
|
48 |
*/ |
import org.apache.log4j.Logger; |
49 |
public TranslationEditJPanel(String question, Translation trans, |
|
50 |
List<String> languages_) { |
import schmitzm.swing.JPanel; |
51 |
super(new BorderLayout()); |
import schmitzm.swing.SpringUtilities; |
52 |
|
import schmitzm.swing.SwingUtil; |
53 |
if (trans == null) |
import skrueger.i8n.Translation; |
54 |
trans = new Translation(); |
|
55 |
|
/** |
56 |
this.trans = trans; |
* A {@link JPanel} that asks the user for the translations of a String in |
57 |
this.languages = languages_; |
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
58 |
|
* The class does not implement any backup/clong strategies. The |
59 |
add(getTranslationGrid(), BorderLayout.CENTER); |
* {@link Translation} object is manipulated directly.<br/> |
60 |
|
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
61 |
if (question != null) { |
* transparent Apply/Cancel logic. |
62 |
JLabel questionLable = new JLabel(question); |
* |
63 |
questionLable |
* @author Stefan Alfons Krüger |
64 |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
*/ |
65 |
add(questionLable, BorderLayout.NORTH); |
public class TranslationEditJPanel extends JPanel { |
66 |
} |
static final protected Logger LOGGER = Logger |
67 |
} |
.getLogger(TranslationEditJPanel.class); |
68 |
|
|
69 |
private JPanel getTranslationGrid() { |
private final List<String> languages; |
70 |
if (translationGrid == null) { |
private JPanel translationGrid; |
71 |
translationGrid = new JPanel(new SpringLayout()); |
private Translation trans; |
72 |
|
|
73 |
for (String langId : languages) { |
/** |
74 |
|
* Remembers all {@link JTextField} that have been created. |
75 |
// language code : entry field for translation |
*/ |
76 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
private Set<TranslationJTextField> langTextFields = new HashSet<TranslationJTextField>(); |
77 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>(); |
78 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
|
79 |
|
/** |
80 |
TranslationJTextField langTextField = new TranslationJTextField( |
* Creates a {@link JPanel} that asks the user for the translation of a |
81 |
trans, langId); |
* String in several languages |
82 |
// Setting a size |
*/ |
83 |
langTextField.setPreferredSize(new Dimension(360, 22)); |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
84 |
langDesc.setLabelFor(langTextField); |
this(null, trans, languages_); |
85 |
translationGrid.add(langDesc); |
} |
86 |
translationGrid.add(langTextField); |
|
87 |
} |
/** |
88 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
89 |
// Lay out the panel. |
* String in several languages and additionally puts a {@link JLabel} with a |
90 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
* question at the {@link JPanel}'s first row. |
91 |
2, // cols |
*/ |
92 |
6, 6, // initX, initY |
public TranslationEditJPanel(String question, Translation trans, |
93 |
6, 6); // xPad, yPad |
List<String> languages_) { |
94 |
|
super(new BorderLayout()); |
95 |
} |
|
96 |
return translationGrid; |
SwingUtil.setMinimumWidth(this, 400); |
97 |
} |
|
98 |
|
if (trans == null) |
99 |
/** |
trans = new Translation(); |
100 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
|
101 |
* deals with. |
this.trans = trans; |
102 |
*/ |
this.languages = languages_; |
103 |
public Translation getTranslation() { |
|
104 |
return trans; |
add(getTranslationGrid(), BorderLayout.CENTER); |
105 |
} |
|
106 |
|
if (question != null) { |
107 |
} |
JLabel questionLable = new JLabel(question); |
108 |
|
questionLable |
109 |
|
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
110 |
|
add(questionLable, BorderLayout.NORTH); |
111 |
|
} |
112 |
|
|
113 |
|
/** |
114 |
|
* Add listeners |
115 |
|
*/ |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
120 |
|
* String in several languages and additionally puts a {@link JLabel} with a |
121 |
|
* question at the {@link JPanel}'s first row. |
122 |
|
* |
123 |
|
* <br/> |
124 |
|
* This constructor also sets a TitledBorder with the given title. |
125 |
|
*/ |
126 |
|
public TranslationEditJPanel(Translation title, List<String> languages2, |
127 |
|
String borderTitle) { |
128 |
|
this(title, languages2); |
129 |
|
|
130 |
|
setBorder(BorderFactory.createTitledBorder(borderTitle)); |
131 |
|
} |
132 |
|
|
133 |
|
private JPanel getTranslationGrid() { |
134 |
|
if (translationGrid == null) { |
135 |
|
translationGrid = new JPanel(new SpringLayout()); |
136 |
|
|
137 |
|
for (String langId : languages) { |
138 |
|
|
139 |
|
// language code : entry field for translation |
140 |
|
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
141 |
|
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
142 |
|
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
143 |
|
|
144 |
|
TranslationJTextField langTextField = new TranslationJTextField( |
145 |
|
trans, langId); |
146 |
|
// Setting a size |
147 |
|
langTextField.setPreferredSize(new Dimension(360, 22)); |
148 |
|
langDesc.setLabelFor(langTextField); |
149 |
|
translationGrid.add(langDesc); |
150 |
|
translationGrid.add(langTextField); |
151 |
|
|
152 |
|
langTextFields.add(langTextField); |
153 |
|
} |
154 |
|
|
155 |
|
// Lay out the panel. |
156 |
|
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
157 |
|
2, // cols |
158 |
|
6, 6, // initX, initY |
159 |
|
6, 6); // xPad, yPad |
160 |
|
|
161 |
|
} |
162 |
|
return translationGrid; |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
|
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
167 |
|
* deals with. |
168 |
|
*/ |
169 |
|
public Translation getTranslation() { |
170 |
|
return trans; |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
|
* TODO Is never called?! |
175 |
|
*/ |
176 |
|
public void dispose() { |
177 |
|
for (TranslationJTextField f: langTextFields) { |
178 |
|
f.dispose(); |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
public void addTranslationChangeListener(final ActionListener al) { |
183 |
|
final ActionListener actionListener = new ActionListener() { |
184 |
|
|
185 |
|
@Override |
186 |
|
public void actionPerformed(ActionEvent e) { |
187 |
|
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 |
|
} |