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