1 |
package skrueger.swing; |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
import java.awt.BorderLayout; |
* |
4 |
import java.awt.Container; |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
import java.awt.Dimension; |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
import java.awt.event.ActionEvent; |
* and the Geotools library. |
7 |
import java.awt.event.ActionListener; |
* |
8 |
import java.awt.event.KeyEvent; |
* The SCHMITZM project is hosted at: |
9 |
import java.awt.event.KeyListener; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import java.beans.PropertyChangeListener; |
* |
11 |
import java.util.HashSet; |
* This program is free software; you can redistribute it and/or |
12 |
import java.util.List; |
* modify it under the terms of the GNU Lesser General Public License |
13 |
import java.util.Set; |
* as published by the Free Software Foundation; either version 3 |
14 |
|
* of the License, or (at your option) any later version. |
15 |
import javax.swing.BorderFactory; |
* |
16 |
import javax.swing.JLabel; |
* This program is distributed in the hope that it will be useful, |
17 |
import javax.swing.JPanel; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import javax.swing.JTextField; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import javax.swing.SpringLayout; |
* GNU General Public License for more details. |
20 |
import javax.swing.SwingConstants; |
* |
21 |
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
import org.apache.log4j.Logger; |
* 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 |
import schmitzm.swing.SpringUtilities; |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
import skrueger.i8n.Translation; |
* |
26 |
|
* Contributors: |
27 |
/** |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* A {@link JPanel} that asks the user for the translations of a String in |
* Stefan A. Krüger - additional utility classes |
29 |
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
******************************************************************************/ |
30 |
* The class does not implement any backup/clong strategies. The |
package skrueger.swing; |
31 |
* {@link Translation} object is manipulated directly.<br/> |
|
32 |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
import java.awt.BorderLayout; |
33 |
* transparent Apply/Cancel logic. |
import java.awt.Dimension; |
34 |
* |
import java.awt.event.ActionEvent; |
35 |
* @author Stefan Alfons Krüger |
import java.awt.event.ActionListener; |
36 |
*/ |
import java.util.HashSet; |
37 |
public class TranslationEditJPanel extends JPanel { |
import java.util.List; |
38 |
static final protected Logger LOGGER = Logger |
import java.util.Random; |
39 |
.getLogger(TranslationEditJPanel.class); |
import java.util.Set; |
40 |
|
import java.util.WeakHashMap; |
41 |
private final List<String> languages; |
|
42 |
private JPanel translationGrid; |
import javax.swing.BorderFactory; |
43 |
private Translation trans; |
import javax.swing.JLabel; |
44 |
|
import javax.swing.JTextField; |
45 |
/** |
import javax.swing.SpringLayout; |
46 |
* Remembers all {@link JTextField} that have been created. |
import javax.swing.SwingConstants; |
47 |
*/ |
|
48 |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
import org.apache.log4j.Logger; |
49 |
|
|
50 |
/** |
import schmitzm.swing.JPanel; |
51 |
* Creates a {@link JPanel} that asks the user for the translation of a |
import schmitzm.swing.SpringUtilities; |
52 |
* String in several languages |
import skrueger.i8n.Translation; |
53 |
*/ |
|
54 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
/** |
55 |
this(null, trans, languages_); |
* 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 |
* Creates a {@link JPanel} that asks the user for the translation of a |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
60 |
* String in several languages and additionally puts a {@link JLabel} with a |
* transparent Apply/Cancel logic. |
61 |
* question at the {@link JPanel}'s first row. |
* |
62 |
*/ |
* @author Stefan Alfons Krüger |
63 |
public TranslationEditJPanel(String question, Translation trans, |
*/ |
64 |
List<String> languages_) { |
public class TranslationEditJPanel extends JPanel { |
65 |
super(new BorderLayout()); |
static final protected Logger LOGGER = Logger |
66 |
|
.getLogger(TranslationEditJPanel.class); |
67 |
if (trans == null) |
|
68 |
trans = new Translation(); |
private final List<String> languages; |
69 |
|
private JPanel translationGrid; |
70 |
this.trans = trans; |
private Translation trans; |
71 |
this.languages = languages_; |
|
72 |
|
/** |
73 |
add(getTranslationGrid(), BorderLayout.CENTER); |
* Remembers all {@link JTextField} that have been created. |
74 |
|
*/ |
75 |
if (question != null) { |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
76 |
JLabel questionLable = new JLabel(question); |
|
77 |
questionLable |
private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>(); |
78 |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
|
79 |
add(questionLable, BorderLayout.NORTH); |
/** |
80 |
} |
* Creates a {@link JPanel} that asks the user for the translation of a |
81 |
} |
* String in several languages |
82 |
|
*/ |
83 |
private JPanel getTranslationGrid() { |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
84 |
if (translationGrid == null) { |
this(null, trans, languages_); |
85 |
translationGrid = new JPanel(new SpringLayout()); |
} |
86 |
|
|
87 |
for (String langId : languages) { |
/** |
88 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
89 |
// language code : entry field for translation |
* String in several languages and additionally puts a {@link JLabel} with a |
90 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
* question at the {@link JPanel}'s first row. |
91 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
*/ |
92 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
public TranslationEditJPanel(String question, Translation trans, |
93 |
|
List<String> languages_) { |
94 |
TranslationJTextField langTextField = new TranslationJTextField( |
super(new BorderLayout()); |
95 |
trans, langId); |
|
96 |
// Setting a size |
if (trans == null) |
97 |
langTextField.setPreferredSize(new Dimension(360, 22)); |
trans = new Translation(); |
98 |
langDesc.setLabelFor(langTextField); |
|
99 |
translationGrid.add(langDesc); |
this.trans = trans; |
100 |
translationGrid.add(langTextField); |
this.languages = languages_; |
101 |
|
|
102 |
langTextFields .add(langTextField); |
add(getTranslationGrid(), BorderLayout.CENTER); |
103 |
} |
|
104 |
|
if (question != null) { |
105 |
// Lay out the panel. |
JLabel questionLable = new JLabel(question); |
106 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
questionLable |
107 |
2, // cols |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
108 |
6, 6, // initX, initY |
add(questionLable, BorderLayout.NORTH); |
109 |
6, 6); // xPad, yPad |
} |
110 |
|
} |
111 |
} |
|
112 |
return translationGrid; |
private JPanel getTranslationGrid() { |
113 |
} |
if (translationGrid == null) { |
114 |
|
translationGrid = new JPanel(new SpringLayout()); |
115 |
/** |
|
116 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
for (String langId : languages) { |
117 |
* deals with. |
|
118 |
*/ |
// language code : entry field for translation |
119 |
public Translation getTranslation() { |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
120 |
return trans; |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
121 |
} |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
122 |
|
|
123 |
public void addActionListener(final ActionListener actionListener) { |
TranslationJTextField langTextField = new TranslationJTextField( |
124 |
for (final JTextField langTextField : langTextFields){ |
trans, langId); |
125 |
langTextField.addKeyListener( new KeyListener(){ |
// Setting a size |
126 |
|
langTextField.setPreferredSize(new Dimension(360, 22)); |
127 |
@Override |
langDesc.setLabelFor(langTextField); |
128 |
public void keyPressed(KeyEvent e) { |
translationGrid.add(langDesc); |
129 |
} |
translationGrid.add(langTextField); |
130 |
|
|
131 |
@Override |
langTextFields.add(langTextField); |
132 |
public void keyReleased(KeyEvent e) { |
} |
133 |
} |
|
134 |
|
// Lay out the panel. |
135 |
@Override |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
136 |
public void keyTyped(KeyEvent e) { |
2, // cols |
137 |
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
6, 6, // initX, initY |
138 |
} |
6, 6); // xPad, yPad |
139 |
|
|
140 |
}); |
} |
141 |
} |
return translationGrid; |
142 |
} |
} |
143 |
|
|
144 |
public void removeActionListener(ActionListener actionListener) { |
/** |
145 |
for (JTextField langTextField : langTextFields){ |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
146 |
langTextField.removeActionListener(actionListener); |
* deals with. |
147 |
} |
*/ |
148 |
} |
public Translation getTranslation() { |
149 |
|
return trans; |
150 |
} |
} |
151 |
|
|
152 |
|
public void addTranslationChangeListener(final ActionListener al) { |
153 |
|
final ActionListener actionListener = new ActionListener() { |
154 |
|
|
155 |
|
@Override |
156 |
|
public void actionPerformed(ActionEvent e) { |
157 |
|
al.actionPerformed(new ActionEvent(TranslationEditJPanel.this, |
158 |
|
new Random().nextInt(), "")); |
159 |
|
} |
160 |
|
|
161 |
|
}; |
162 |
|
actionListeners.put(al, actionListener); |
163 |
|
getTranslation().addTranslationChangeListener(actionListener); |
164 |
|
} |
165 |
|
|
166 |
|
public void removeTranslationChangeListener(ActionListener al) { |
167 |
|
if (actionListeners.get(al) != null) { |
168 |
|
ActionListener actionListener = actionListeners.get(al); |
169 |
|
getTranslation().removeTranslationChangeListener(actionListener); |
170 |
|
actionListeners.remove(actionListener); |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
} |