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, focussing (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.Container; |
34 |
import java.awt.Dimension; |
35 |
import java.awt.event.ActionEvent; |
36 |
import java.awt.event.ActionListener; |
37 |
import java.awt.event.KeyEvent; |
38 |
import java.awt.event.KeyListener; |
39 |
import java.beans.PropertyChangeListener; |
40 |
import java.util.HashSet; |
41 |
import java.util.List; |
42 |
import java.util.Set; |
43 |
|
44 |
import javax.swing.BorderFactory; |
45 |
import javax.swing.JLabel; |
46 |
import javax.swing.JPanel; |
47 |
import javax.swing.JTextField; |
48 |
import javax.swing.SpringLayout; |
49 |
import javax.swing.SwingConstants; |
50 |
|
51 |
import org.apache.log4j.Logger; |
52 |
|
53 |
import schmitzm.swing.SpringUtilities; |
54 |
import skrueger.i8n.Translation; |
55 |
|
56 |
/** |
57 |
* A {@link JPanel} that asks the user for the translations of a String in |
58 |
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
59 |
* The class does not implement any backup/clong strategies. The |
60 |
* {@link Translation} object is manipulated directly.<br/> |
61 |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
62 |
* transparent Apply/Cancel logic. |
63 |
* |
64 |
* @author Stefan Alfons Krüger |
65 |
*/ |
66 |
public class TranslationEditJPanel extends JPanel { |
67 |
static final protected Logger LOGGER = Logger |
68 |
.getLogger(TranslationEditJPanel.class); |
69 |
|
70 |
private final List<String> languages; |
71 |
private JPanel translationGrid; |
72 |
private Translation trans; |
73 |
|
74 |
/** |
75 |
* Remembers all {@link JTextField} that have been created. |
76 |
*/ |
77 |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
78 |
|
79 |
/** |
80 |
* Creates a {@link JPanel} that asks the user for the translation of a |
81 |
* String in several languages |
82 |
*/ |
83 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
84 |
this(null, trans, languages_); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Creates a {@link JPanel} that asks the user for the translation of a |
89 |
* String in several languages and additionally puts a {@link JLabel} with a |
90 |
* question at the {@link JPanel}'s first row. |
91 |
*/ |
92 |
public TranslationEditJPanel(String question, Translation trans, |
93 |
List<String> languages_) { |
94 |
super(new BorderLayout()); |
95 |
|
96 |
if (trans == null) |
97 |
trans = new Translation(); |
98 |
|
99 |
this.trans = trans; |
100 |
this.languages = languages_; |
101 |
|
102 |
add(getTranslationGrid(), BorderLayout.CENTER); |
103 |
|
104 |
if (question != null) { |
105 |
JLabel questionLable = new JLabel(question); |
106 |
questionLable |
107 |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
108 |
add(questionLable, BorderLayout.NORTH); |
109 |
} |
110 |
} |
111 |
|
112 |
private JPanel getTranslationGrid() { |
113 |
if (translationGrid == null) { |
114 |
translationGrid = new JPanel(new SpringLayout()); |
115 |
|
116 |
for (String langId : languages) { |
117 |
|
118 |
// language code : entry field for translation |
119 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
120 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
121 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
122 |
|
123 |
TranslationJTextField langTextField = new TranslationJTextField( |
124 |
trans, langId); |
125 |
// Setting a size |
126 |
langTextField.setPreferredSize(new Dimension(360, 22)); |
127 |
langDesc.setLabelFor(langTextField); |
128 |
translationGrid.add(langDesc); |
129 |
translationGrid.add(langTextField); |
130 |
|
131 |
langTextFields .add(langTextField); |
132 |
} |
133 |
|
134 |
// Lay out the panel. |
135 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
136 |
2, // cols |
137 |
6, 6, // initX, initY |
138 |
6, 6); // xPad, yPad |
139 |
|
140 |
} |
141 |
return translationGrid; |
142 |
} |
143 |
|
144 |
/** |
145 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
146 |
* deals with. |
147 |
*/ |
148 |
public Translation getTranslation() { |
149 |
return trans; |
150 |
} |
151 |
|
152 |
public void addActionListener(final ActionListener actionListener) { |
153 |
for (final JTextField langTextField : langTextFields){ |
154 |
langTextField.addKeyListener( new KeyListener(){ |
155 |
|
156 |
@Override |
157 |
public void keyPressed(KeyEvent e) { |
158 |
} |
159 |
|
160 |
@Override |
161 |
public void keyReleased(KeyEvent e) { |
162 |
} |
163 |
|
164 |
@Override |
165 |
public void keyTyped(KeyEvent e) { |
166 |
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
167 |
} |
168 |
|
169 |
}); |
170 |
} |
171 |
} |
172 |
|
173 |
public void removeActionListener(ActionListener actionListener) { |
174 |
for (JTextField langTextField : langTextFields){ |
175 |
langTextField.removeActionListener(actionListener); |
176 |
} |
177 |
} |
178 |
|
179 |
} |