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 translation of several strings |
* GNU General Public License for more details. |
20 |
* @author Stefan Alfons Krüger |
* |
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 |
public class TranslationEditJPanel extends JPanel { |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
static final protected Logger LOGGER = Logger.getLogger(TranslationEditJPanel.class); |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
* |
26 |
private final List<String> languages; |
* Contributors: |
27 |
private JPanel translationGrid; |
* Martin O. J. Schmitz - initial API and implementation |
28 |
private Translation trans; |
* Stefan A. Krüger - additional utility classes |
29 |
|
******************************************************************************/ |
30 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
package skrueger.swing; |
31 |
this(null, trans, languages_); |
|
32 |
} |
import java.awt.BorderLayout; |
33 |
|
import java.awt.Dimension; |
34 |
public TranslationEditJPanel(String question, Translation trans, |
import java.awt.event.ActionEvent; |
35 |
List<String> languages_) { |
import java.awt.event.ActionListener; |
36 |
super(new BorderLayout()); |
import java.awt.event.KeyEvent; |
37 |
|
import java.awt.event.KeyListener; |
38 |
if (trans == null) trans = new Translation(); |
import java.util.HashSet; |
39 |
|
import java.util.List; |
40 |
this.trans = trans; |
import java.util.Set; |
41 |
this.languages = languages_; |
|
42 |
|
import javax.swing.BorderFactory; |
43 |
add(getTranslationGrid(), BorderLayout.CENTER); |
import javax.swing.JLabel; |
44 |
|
import javax.swing.JTextField; |
45 |
if (question != null) { |
import javax.swing.SpringLayout; |
46 |
JLabel questionLable = new JLabel(question); |
import javax.swing.SwingConstants; |
47 |
questionLable.setBorder(BorderFactory.createEmptyBorder( |
|
48 |
6, 6, 6, 6)); |
import org.apache.log4j.Logger; |
49 |
add(questionLable, BorderLayout.NORTH); |
|
50 |
} |
import schmitzm.swing.JPanel; |
51 |
} |
import schmitzm.swing.SpringUtilities; |
52 |
|
import skrueger.i8n.Translation; |
53 |
private JPanel getTranslationGrid() { |
|
54 |
if (translationGrid == null) { |
/** |
55 |
translationGrid = new JPanel(new SpringLayout()); |
* A {@link JPanel} that asks the user for the translations of a String in |
56 |
|
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
57 |
for (String langId : languages) { |
* The class does not implement any backup/clong strategies. The |
58 |
|
* {@link Translation} object is manipulated directly.<br/> |
59 |
// language code : entry field for translation |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
60 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
* transparent Apply/Cancel logic. |
61 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
* |
62 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
* @author Stefan Alfons Krüger |
63 |
|
*/ |
64 |
TranslationJTextField langTextField = new TranslationJTextField( |
public class TranslationEditJPanel extends JPanel { |
65 |
trans, langId); |
static final protected Logger LOGGER = Logger |
66 |
// Setting a size |
.getLogger(TranslationEditJPanel.class); |
67 |
langTextField.setPreferredSize( new Dimension(360,22)); |
|
68 |
langDesc.setLabelFor(langTextField); |
private final List<String> languages; |
69 |
translationGrid.add(langDesc); |
private JPanel translationGrid; |
70 |
translationGrid.add(langTextField); |
private Translation trans; |
71 |
} |
|
72 |
|
/** |
73 |
// Lay out the panel. |
* Remembers all {@link JTextField} that have been created. |
74 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
*/ |
75 |
2, // cols |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
76 |
6, 6, // initX, initY |
|
77 |
6, 6); // xPad, yPad |
/** |
78 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
79 |
} |
* String in several languages |
80 |
return translationGrid; |
*/ |
81 |
} |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
82 |
|
this(null, trans, languages_); |
83 |
// /** |
} |
84 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
|
85 |
// * So far this is working on the Translation object directly, Cancel not possible. |
/** |
86 |
// * @param translationEditJPanel |
* Creates a {@link JPanel} that asks the user for the translation of a |
87 |
// * |
* String in several languages and additionally puts a {@link JLabel} with a |
88 |
// * @deprecated Use {@link TranslationAskJDialog} |
* question at the {@link JPanel}'s first row. |
89 |
// */ |
*/ |
90 |
// public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
public TranslationEditJPanel(String question, Translation trans, |
91 |
// dialog = new JDialog(parentFrame); |
List<String> languages_) { |
92 |
// // backup( translationEditJPanels ); |
super(new BorderLayout()); |
93 |
// showDialog(dialog, translationEditJPanels); |
|
94 |
// } |
if (trans == null) |
95 |
// |
trans = new Translation(); |
96 |
// |
|
97 |
// /** |
this.trans = trans; |
98 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
this.languages = languages_; |
99 |
// * So far this is working on the Translation object directly, Cancel not possible. |
|
100 |
// * @param translationEditJPanel |
add(getTranslationGrid(), BorderLayout.CENTER); |
101 |
// |
|
102 |
// * @deprecated User {@link TranslationAskJDialog} * |
if (question != null) { |
103 |
// */ |
JLabel questionLable = new JLabel(question); |
104 |
// public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
questionLable |
105 |
// dialog = new JDialog(parentWindow); |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
106 |
// // backup( translationEditJPanels ); |
add(questionLable, BorderLayout.NORTH); |
107 |
// showDialog(dialog, translationEditJPanels); |
} |
108 |
// } |
} |
109 |
// |
|
110 |
// /** |
private JPanel getTranslationGrid() { |
111 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
if (translationGrid == null) { |
112 |
// * So far this is working on the Translation object directly, Cancel not possible. |
translationGrid = new JPanel(new SpringLayout()); |
113 |
// * @param translationEditJPanel |
|
114 |
// * |
for (String langId : languages) { |
115 |
// * * @deprecated User {@link TranslationAskJDialog} |
|
116 |
// */ |
// language code : entry field for translation |
117 |
// public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) { |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
118 |
// dialog = new JDialog(parentDialog); |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
119 |
// // backup( translationEditJPanels ); |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
120 |
// showDialog(dialog, translationEditJPanels); |
|
121 |
// } |
TranslationJTextField langTextField = new TranslationJTextField( |
122 |
// |
trans, langId); |
123 |
// |
// Setting a size |
124 |
// /** |
langTextField.setPreferredSize(new Dimension(360, 22)); |
125 |
// * * @deprecated User {@link TranslationAskJDialog} |
langDesc.setLabelFor(langTextField); |
126 |
// * @param d |
translationGrid.add(langDesc); |
127 |
// * @param translationEditJPanels |
translationGrid.add(langTextField); |
128 |
// * @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
|
129 |
// */ |
langTextFields .add(langTextField); |
130 |
// public static void showDialog(JDialog d, JComponent... translationEditJPanels) { |
} |
131 |
// dialog = d; |
|
132 |
// dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
// Lay out the panel. |
133 |
// SwingUtil.centerFrameOnScreen(dialog); |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
134 |
// Box box = Box.createVerticalBox(); |
2, // cols |
135 |
// for (JComponent panel : translationEditJPanels) { |
6, 6, // initX, initY |
136 |
// box.add(panel); |
6, 6); // xPad, yPad |
137 |
// } |
|
138 |
// JPanel cp = new JPanel( new BorderLayout()); |
} |
139 |
// cp.add( box, BorderLayout.CENTER); |
return translationGrid; |
140 |
// cp.add( getButtons(), BorderLayout.SOUTH ); |
} |
141 |
// dialog.setContentPane(cp); |
|
142 |
// |
/** |
143 |
// // dialog.getRootPane().setDefaultButton(okButton); |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
144 |
// |
* deals with. |
145 |
// dialog.setTitle("Please translate"); //i8n |
*/ |
146 |
// dialog.setModal(true); |
public Translation getTranslation() { |
147 |
// dialog.pack(); |
return trans; |
148 |
// dialog.setVisible(true); |
} |
149 |
// } |
|
150 |
// |
public void addActionListener(final ActionListener actionListener) { |
151 |
// /** |
for (final JTextField langTextField : langTextFields){ |
152 |
// * @deprecated Use TranslationAskJDialog |
langTextField.addKeyListener( new KeyListener(){ |
153 |
// * @return |
|
154 |
// * @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
@Override |
155 |
// */ |
public void keyPressed(KeyEvent e) { |
156 |
// private static JComponent getButtons() { |
} |
157 |
// JPanel jPanel = new JPanel(); |
|
158 |
// if (okButton == null) { |
@Override |
159 |
// okButton = new OkButton(new AbstractAction("enter") { |
public void keyReleased(KeyEvent e) { |
160 |
// public void actionPerformed(ActionEvent evt) { |
} |
161 |
// firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null); |
|
162 |
// dialog.dispose(); |
@Override |
163 |
// } |
public void keyTyped(KeyEvent e) { |
164 |
// } ); |
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
165 |
// } |
} |
166 |
// jPanel.add(okButton); |
|
167 |
// |
}); |
168 |
// if (cancelButton == null) { |
} |
169 |
// cancelButton = new CancelButton(new AbstractAction("") { |
} |
170 |
// public void actionPerformed(ActionEvent evt) { |
|
171 |
// // restore(); |
public void removeActionListener(ActionListener actionListener) { |
172 |
// firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
for (JTextField langTextField : langTextFields){ |
173 |
// dialog.dispose(); |
langTextField.removeActionListener(actionListener); |
174 |
// } |
} |
175 |
// } ); |
} |
176 |
// } |
|
177 |
// jPanel.add(okButton); |
} |
|
// |
|
|
// return jPanel; |
|
|
// } |
|
|
|
|
|
/** |
|
|
* @return The {@link Translation} that this {@link TranslationEditJPanel} deals with. |
|
|
*/ |
|
|
public Translation getTranslation() { |
|
|
return trans; |
|
|
} |
|
|
|
|
|
} |
|