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.awt.event.KeyEvent; |
37 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
import java.awt.event.KeyListener; |
38 |
private static JDialog dialog; |
import java.util.HashSet; |
39 |
private static OkButton okButton; |
import java.util.List; |
40 |
|
import java.util.Set; |
41 |
private static CancelButton cancelButton; |
|
42 |
Logger log = Logger.getLogger(TranslationEditJPanel.class); |
import javax.swing.BorderFactory; |
43 |
private List<String> languages; |
import javax.swing.JLabel; |
44 |
private JPanel translationGrid; |
import javax.swing.JTextField; |
45 |
private Translation trans; |
import javax.swing.SpringLayout; |
46 |
|
import javax.swing.SwingConstants; |
47 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
|
48 |
this(null, trans, languages_); |
import org.apache.log4j.Logger; |
49 |
} |
|
50 |
|
import schmitzm.swing.JPanel; |
51 |
public TranslationEditJPanel(String question, Translation trans, |
import schmitzm.swing.SpringUtilities; |
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 Set<JTextField> langTextFields = new HashSet<JTextField>(); |
76 |
for (String langId : languages) { |
|
77 |
|
/** |
78 |
// language code : entry field for translation |
* Creates a {@link JPanel} that asks the user for the translation of a |
79 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
* String in several languages |
80 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
*/ |
81 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
82 |
|
this(null, trans, languages_); |
83 |
TranslationJTextField langTextField = new TranslationJTextField( |
} |
84 |
trans, langId); |
|
85 |
// Setting a size |
/** |
86 |
langTextField.setPreferredSize( new Dimension(360,22)); |
* Creates a {@link JPanel} that asks the user for the translation of a |
87 |
langDesc.setLabelFor(langTextField); |
* String in several languages and additionally puts a {@link JLabel} with a |
88 |
translationGrid.add(langDesc); |
* question at the {@link JPanel}'s first row. |
89 |
translationGrid.add(langTextField); |
*/ |
90 |
} |
public TranslationEditJPanel(String question, Translation trans, |
91 |
|
List<String> languages_) { |
92 |
// Lay out the panel. |
super(new BorderLayout()); |
93 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
|
94 |
2, // cols |
if (trans == null) |
95 |
6, 6, // initX, initY |
trans = new Translation(); |
96 |
6, 6); // xPad, yPad |
|
97 |
|
this.trans = trans; |
98 |
} |
this.languages = languages_; |
99 |
return translationGrid; |
|
100 |
} |
add(getTranslationGrid(), BorderLayout.CENTER); |
101 |
|
|
102 |
/** |
if (question != null) { |
103 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
JLabel questionLable = new JLabel(question); |
104 |
* So far this is working on the Translation object directly, Cancel not possible. |
questionLable |
105 |
* @param translationEditJPanel |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
106 |
* |
add(questionLable, BorderLayout.NORTH); |
107 |
* @deprecated Use {@link TranslationAskJDialog} |
} |
108 |
*/ |
} |
109 |
public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
|
110 |
dialog = new JDialog(parentFrame); |
private JPanel getTranslationGrid() { |
111 |
// backup( translationEditJPanels ); |
if (translationGrid == null) { |
112 |
showDialog(dialog, translationEditJPanels); |
translationGrid = new JPanel(new SpringLayout()); |
113 |
} |
|
114 |
|
for (String langId : languages) { |
115 |
|
|
116 |
/** |
// language code : entry field for translation |
117 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
118 |
* So far this is working on the Translation object directly, Cancel not possible. |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
119 |
* @param translationEditJPanel |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
120 |
|
|
121 |
* @deprecated User {@link TranslationAskJDialog} * |
TranslationJTextField langTextField = new TranslationJTextField( |
122 |
*/ |
trans, langId); |
123 |
public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
// Setting a size |
124 |
dialog = new JDialog(parentWindow); |
langTextField.setPreferredSize(new Dimension(360, 22)); |
125 |
// backup( translationEditJPanels ); |
langDesc.setLabelFor(langTextField); |
126 |
showDialog(dialog, translationEditJPanels); |
translationGrid.add(langDesc); |
127 |
} |
translationGrid.add(langTextField); |
128 |
|
|
129 |
/** |
langTextFields .add(langTextField); |
130 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
} |
131 |
* So far this is working on the Translation object directly, Cancel not possible. |
|
132 |
* @param translationEditJPanel |
// Lay out the panel. |
133 |
* |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
134 |
* * @deprecated User {@link TranslationAskJDialog} |
2, // cols |
135 |
*/ |
6, 6, // initX, initY |
136 |
public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) { |
6, 6); // xPad, yPad |
137 |
dialog = new JDialog(parentDialog); |
|
138 |
// backup( translationEditJPanels ); |
} |
139 |
showDialog(dialog, translationEditJPanels); |
return translationGrid; |
140 |
} |
} |
141 |
|
|
142 |
|
/** |
143 |
/** |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
144 |
* * @deprecated User {@link TranslationAskJDialog} |
* deals with. |
145 |
* @param d |
*/ |
146 |
* @param translationEditJPanels |
public Translation getTranslation() { |
147 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
return trans; |
148 |
*/ |
} |
149 |
public static void showDialog(JDialog d, JComponent... translationEditJPanels) { |
|
150 |
dialog = d; |
public void addActionListener(final ActionListener actionListener) { |
151 |
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
for (final JTextField langTextField : langTextFields){ |
152 |
SwingUtil.centerFrameOnScreen(dialog); |
langTextField.addKeyListener( new KeyListener(){ |
153 |
Box box = Box.createVerticalBox(); |
|
154 |
for (JComponent panel : translationEditJPanels) { |
@Override |
155 |
box.add(panel); |
public void keyPressed(KeyEvent e) { |
156 |
} |
} |
157 |
JPanel cp = new JPanel( new BorderLayout()); |
|
158 |
cp.add( box, BorderLayout.CENTER); |
@Override |
159 |
cp.add( getButtons(), BorderLayout.SOUTH ); |
public void keyReleased(KeyEvent e) { |
160 |
dialog.setContentPane(cp); |
} |
161 |
|
|
162 |
// dialog.getRootPane().setDefaultButton(okButton); |
@Override |
163 |
|
public void keyTyped(KeyEvent e) { |
164 |
dialog.setTitle("Please translate"); //i8n |
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
165 |
dialog.setModal(true); |
} |
166 |
dialog.pack(); |
|
167 |
dialog.setVisible(true); |
}); |
168 |
} |
} |
169 |
|
} |
170 |
/** |
|
171 |
* @deprecated Use TranslationAskJDialog |
public void removeActionListener(ActionListener actionListener) { |
172 |
* @return |
for (JTextField langTextField : langTextFields){ |
173 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
langTextField.removeActionListener(actionListener); |
174 |
*/ |
} |
175 |
private static JComponent getButtons() { |
} |
176 |
JPanel jPanel = new JPanel(); |
|
177 |
if (okButton == null) { |
} |
|
okButton = new OkButton(new AbstractAction("enter") { |
|
|
public void actionPerformed(ActionEvent evt) { |
|
|
firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null); |
|
|
dialog.dispose(); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
jPanel.add(okButton); |
|
|
|
|
|
if (cancelButton == null) { |
|
|
cancelButton = new CancelButton(new AbstractAction("") { |
|
|
public void actionPerformed(ActionEvent evt) { |
|
|
// restore(); |
|
|
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
|
|
dialog.dispose(); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
jPanel.add(okButton); |
|
|
|
|
|
return jPanel; |
|
|
} |
|
|
|
|
|
public Translation getTranslation() { |
|
|
return trans; |
|
|
} |
|
|
// |
|
|
// |
|
|
// public static void main(String[] args) { |
|
|
// ArrayList<String> lang = new ArrayList <String> (); |
|
|
// lang.add("de"); |
|
|
// lang.add("en"); |
|
|
// lang.add("fr"); |
|
|
// |
|
|
// Translation transe = new Translation(); |
|
|
// 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); |
|
|
// } |
|
|
} |
|