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.JDialog; |
* The SCHMITZM project is hosted at: |
9 |
import javax.swing.JLabel; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import javax.swing.JPanel; |
* |
11 |
import javax.swing.SpringLayout; |
* This program is free software; you can redistribute it and/or |
12 |
import javax.swing.SwingConstants; |
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
* as published by the Free Software Foundation; either version 3 |
14 |
import org.apache.log4j.Logger; |
* of the License, or (at your option) any later version. |
15 |
|
* |
16 |
import schmitzm.swing.SpringUtilities; |
* This program is distributed in the hope that it will be useful, |
17 |
import skrueger.i8n.Translation; |
* 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 |
* A {@link JPanel} that asks the user for translation of several strings |
* |
21 |
* @author Stefan Alfons Krüger |
* 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 |
public class TranslationEditJPanel extends JPanel { |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
static final protected Logger LOGGER = Logger.getLogger(TranslationEditJPanel.class); |
* |
26 |
|
* Contributors: |
27 |
private final List<String> languages; |
* Martin O. J. Schmitz - initial API and implementation |
28 |
private JPanel translationGrid; |
* Stefan A. Krüger - additional utility classes |
29 |
private Translation trans; |
******************************************************************************/ |
30 |
|
package skrueger.swing; |
31 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
|
32 |
this(null, trans, languages_); |
import java.awt.BorderLayout; |
33 |
} |
import java.awt.Dimension; |
34 |
|
import java.awt.event.ActionEvent; |
35 |
public TranslationEditJPanel(String question, Translation trans, |
import java.awt.event.ActionListener; |
36 |
List<String> languages_) { |
import java.util.HashMap; |
37 |
super(new BorderLayout()); |
import java.util.List; |
38 |
|
import java.util.Random; |
39 |
if (trans == null) trans = new Translation(); |
import java.util.WeakHashMap; |
40 |
|
|
41 |
this.trans = trans; |
import javax.swing.BorderFactory; |
42 |
this.languages = languages_; |
import javax.swing.JLabel; |
43 |
|
import javax.swing.JTextField; |
44 |
add(getTranslationGrid(), BorderLayout.CENTER); |
import javax.swing.SpringLayout; |
45 |
|
import javax.swing.SwingConstants; |
46 |
if (question != null) { |
|
47 |
JLabel questionLable = new JLabel(question); |
import org.apache.log4j.Logger; |
48 |
questionLable.setBorder(BorderFactory.createEmptyBorder( |
|
49 |
6, 6, 6, 6)); |
import schmitzm.swing.JPanel; |
50 |
add(questionLable, BorderLayout.NORTH); |
import schmitzm.swing.SpringUtilities; |
51 |
} |
import schmitzm.swing.SwingUtil; |
52 |
} |
import skrueger.i8n.Translation; |
53 |
|
|
54 |
private JPanel getTranslationGrid() { |
/** |
55 |
if (translationGrid == null) { |
* A {@link JPanel} that asks the user for the translations of a String in |
56 |
translationGrid = new JPanel(new SpringLayout()); |
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
57 |
|
* The class does not implement any backup/clong strategies. The |
58 |
for (String langId : languages) { |
* {@link Translation} object is manipulated directly.<br/> |
59 |
|
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
60 |
// language code : entry field for translation |
* transparent Apply/Cancel logic. |
61 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
* |
62 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
* @author Stefan Alfons Krüger |
63 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
*/ |
64 |
|
public class TranslationEditJPanel extends JPanel { |
65 |
TranslationJTextField langTextField = new TranslationJTextField( |
static final protected Logger LOGGER = Logger |
66 |
trans, langId); |
.getLogger(TranslationEditJPanel.class); |
67 |
// Setting a size |
|
68 |
langTextField.setPreferredSize( new Dimension(360,22)); |
private final List<String> languages; |
69 |
langDesc.setLabelFor(langTextField); |
private JPanel translationGrid; |
70 |
translationGrid.add(langDesc); |
private Translation trans; |
71 |
translationGrid.add(langTextField); |
|
72 |
} |
/** |
73 |
|
* Remembers all {@link JTextField} that have been created. |
74 |
// Lay out the panel. |
*/ |
75 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
private HashMap<String, TranslationJTextField> langTextFields = new HashMap<String, TranslationJTextField>(); |
76 |
2, // cols |
private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>(); |
77 |
6, 6, // initX, initY |
|
78 |
6, 6); // xPad, yPad |
private final String question; |
79 |
|
|
80 |
} |
/** |
81 |
return translationGrid; |
* 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 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
this(null, trans, languages_); |
86 |
// * So far this is working on the Translation object directly, Cancel not possible. |
} |
87 |
// * @param translationEditJPanel |
|
88 |
// * |
/** |
89 |
// * @deprecated Use {@link TranslationAskJDialog} |
* 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 |
// public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
* question at the {@link JPanel}'s first row. |
92 |
// dialog = new JDialog(parentFrame); |
*/ |
93 |
// // backup( translationEditJPanels ); |
public TranslationEditJPanel(String question, Translation trans, |
94 |
// showDialog(dialog, translationEditJPanels); |
List<String> languages_) { |
95 |
// } |
super(new BorderLayout()); |
96 |
// |
this.question = question; |
97 |
// |
|
98 |
// /** |
SwingUtil.setMinimumWidth(this, 400); |
99 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
|
100 |
// * So far this is working on the Translation object directly, Cancel not possible. |
if (trans == null) |
101 |
// * @param translationEditJPanel |
trans = new Translation(); |
102 |
// |
|
103 |
// * @deprecated User {@link TranslationAskJDialog} * |
this.trans = trans; |
104 |
// */ |
this.languages = languages_; |
105 |
// public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
|
106 |
// dialog = new JDialog(parentWindow); |
initGUI(); |
107 |
// // backup( translationEditJPanels ); |
} |
108 |
// showDialog(dialog, translationEditJPanels); |
|
109 |
// } |
private void initGUI() { |
110 |
// |
add(getTranslationGrid(), BorderLayout.CENTER); |
111 |
// /** |
|
112 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
if (question != null) { |
113 |
// * So far this is working on the Translation object directly, Cancel not possible. |
JLabel questionLable = new JLabel(question); |
114 |
// * @param translationEditJPanel |
questionLable |
115 |
// * |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
116 |
// * * @deprecated User {@link TranslationAskJDialog} |
add(questionLable, BorderLayout.NORTH); |
117 |
// */ |
} |
118 |
// public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) { |
} |
119 |
// dialog = new JDialog(parentDialog); |
|
120 |
// // backup( translationEditJPanels ); |
/** |
121 |
// showDialog(dialog, translationEditJPanels); |
* 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 |
// * * @deprecated User {@link TranslationAskJDialog} |
* This constructor also sets a TitledBorder with the given title. |
127 |
// * @param d |
*/ |
128 |
// * @param translationEditJPanels |
public TranslationEditJPanel(Translation title, List<String> languages2, |
129 |
// * @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
String borderTitle) { |
130 |
// */ |
this(title, languages2); |
131 |
// public static void showDialog(JDialog d, JComponent... translationEditJPanels) { |
|
132 |
// dialog = d; |
setBorder(BorderFactory.createTitledBorder(borderTitle)); |
133 |
// dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
} |
134 |
// SwingUtil.centerFrameOnScreen(dialog); |
|
135 |
// Box box = Box.createVerticalBox(); |
private JPanel getTranslationGrid() { |
136 |
// for (JComponent panel : translationEditJPanels) { |
if (translationGrid == null) { |
137 |
// box.add(panel); |
translationGrid = new JPanel(new SpringLayout()); |
138 |
// } |
|
139 |
// JPanel cp = new JPanel( new BorderLayout()); |
for (String langId : languages) { |
140 |
// cp.add( box, BorderLayout.CENTER); |
|
141 |
// cp.add( getButtons(), BorderLayout.SOUTH ); |
// language code : entry field for translation |
142 |
// dialog.setContentPane(cp); |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
143 |
// |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
144 |
// // dialog.getRootPane().setDefaultButton(okButton); |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
145 |
// |
|
146 |
// dialog.setTitle("Please translate"); //i8n |
TranslationJTextField langTextField = langTextFields |
147 |
// dialog.setModal(true); |
.get(langId); |
148 |
// dialog.pack(); |
|
149 |
// dialog.setVisible(true); |
if (langTextField == null) { |
150 |
// } |
langTextField = new TranslationJTextField(trans, langId); |
151 |
// |
// Setting a size |
152 |
// /** |
langTextField.setPreferredSize(new Dimension(360, 22)); |
153 |
// * @deprecated Use TranslationAskJDialog |
langDesc.setLabelFor(langTextField); |
154 |
// * @return |
} |
155 |
// * @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
|
156 |
// */ |
translationGrid.add(langDesc); |
157 |
// private static JComponent getButtons() { |
translationGrid.add(langTextField); |
158 |
// JPanel jPanel = new JPanel(); |
|
159 |
// if (okButton == null) { |
langTextFields.put(langId, langTextField); |
160 |
// okButton = new OkButton(new AbstractAction("enter") { |
} |
161 |
// public void actionPerformed(ActionEvent evt) { |
|
162 |
// firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null); |
// Lay out the panel. |
163 |
// dialog.dispose(); |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
164 |
// } |
2, // cols |
165 |
// } ); |
6, 6, // initX, initY |
166 |
// } |
6, 6); // xPad, yPad |
167 |
// jPanel.add(okButton); |
|
168 |
// |
} |
169 |
// if (cancelButton == null) { |
return translationGrid; |
170 |
// cancelButton = new CancelButton(new AbstractAction("") { |
} |
171 |
// public void actionPerformed(ActionEvent evt) { |
|
172 |
// // restore(); |
/** |
173 |
// firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
174 |
// dialog.dispose(); |
* deals with. |
175 |
// } |
*/ |
176 |
// } ); |
public Translation getTranslation() { |
177 |
// } |
return trans; |
178 |
// jPanel.add(okButton); |
} |
179 |
// |
|
180 |
// return jPanel; |
/** |
181 |
// } |
* TODO Is never called?! |
182 |
|
*/ |
183 |
/** |
public void dispose() { |
184 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} deals with. |
for (TranslationJTextField f : langTextFields.values()) { |
185 |
*/ |
f.dispose(); |
186 |
public Translation getTranslation() { |
} |
187 |
return trans; |
langTextFields.clear(); |
188 |
} |
} |
189 |
|
|
190 |
} |
public void addTranslationChangeListener(final ActionListener al) { |
191 |
|
final ActionListener actionListener = new ActionListener() { |
192 |
|
|
193 |
|
@Override |
194 |
|
public void actionPerformed(ActionEvent e) { |
195 |
|
al.actionPerformed(new ActionEvent(TranslationEditJPanel.this, |
196 |
|
new Random().nextInt(), "")); |
197 |
|
} |
198 |
|
|
199 |
|
}; |
200 |
|
actionListeners.put(al, actionListener); |
201 |
|
getTranslation().addTranslationChangeListener(actionListener); |
202 |
|
} |
203 |
|
|
204 |
|
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 |
|
} |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
} |