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