1 |
package skrueger.swing; |
package skrueger.swing; |
2 |
|
|
3 |
import java.awt.BorderLayout; |
import java.awt.BorderLayout; |
4 |
|
import java.awt.Container; |
5 |
import java.awt.Dimension; |
import java.awt.Dimension; |
|
import java.awt.Frame; |
|
|
import java.awt.Window; |
|
6 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.WindowAdapter; |
import java.awt.event.ActionListener; |
8 |
import java.awt.event.WindowEvent; |
import java.awt.event.KeyEvent; |
9 |
import java.awt.event.WindowListener; |
import java.awt.event.KeyListener; |
10 |
import java.util.ArrayList; |
import java.beans.PropertyChangeListener; |
11 |
|
import java.util.HashSet; |
12 |
import java.util.List; |
import java.util.List; |
13 |
|
import java.util.Set; |
14 |
|
|
|
import javax.swing.AbstractAction; |
|
15 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
|
import javax.swing.Box; |
|
|
import javax.swing.JComponent; |
|
|
import javax.swing.JDialog; |
|
16 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
17 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
18 |
|
import javax.swing.JTextField; |
19 |
import javax.swing.SpringLayout; |
import javax.swing.SpringLayout; |
20 |
import javax.swing.SwingConstants; |
import javax.swing.SwingConstants; |
21 |
|
|
22 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
23 |
|
|
24 |
import schmitzm.swing.SpringUtilities; |
import schmitzm.swing.SpringUtilities; |
|
import schmitzm.swing.SwingUtil; |
|
25 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
26 |
|
|
27 |
/** |
/** |
28 |
* A JPanel that asks the user for translation of several strings |
* A {@link JPanel} that asks the user for the translations of a String in |
29 |
|
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
30 |
|
* The class does not implement any backup/clong strategies. The |
31 |
|
* {@link Translation} object is manipulated directly.<br/> |
32 |
|
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
33 |
|
* transparent Apply/Cancel logic. |
34 |
|
* |
35 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
|
* |
|
36 |
*/ |
*/ |
37 |
public class TranslationEditJPanel extends JPanel { |
public class TranslationEditJPanel extends JPanel { |
38 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
static final protected Logger LOGGER = Logger |
39 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
.getLogger(TranslationEditJPanel.class); |
40 |
private static JDialog dialog; |
|
41 |
private static OkButton okButton; |
private final List<String> languages; |
|
|
|
|
private static CancelButton cancelButton; |
|
|
Logger log = Logger.getLogger(TranslationEditJPanel.class); |
|
|
private List<String> languages; |
|
42 |
private JPanel translationGrid; |
private JPanel translationGrid; |
43 |
private Translation trans; |
private Translation trans; |
44 |
|
|
45 |
|
/** |
46 |
|
* Remembers all {@link JTextField} that have been created. |
47 |
|
*/ |
48 |
|
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
49 |
|
|
50 |
|
/** |
51 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
52 |
|
* String in several languages |
53 |
|
*/ |
54 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
55 |
this(null, trans, languages_); |
this(null, trans, languages_); |
56 |
} |
} |
57 |
|
|
58 |
|
/** |
59 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
60 |
|
* String in several languages and additionally puts a {@link JLabel} with a |
61 |
|
* question at the {@link JPanel}'s first row. |
62 |
|
*/ |
63 |
public TranslationEditJPanel(String question, Translation trans, |
public TranslationEditJPanel(String question, Translation trans, |
64 |
List<String> languages_) { |
List<String> languages_) { |
65 |
super(new BorderLayout()); |
super(new BorderLayout()); |
66 |
|
|
67 |
if (trans == null) trans = new Translation(); |
if (trans == null) |
68 |
|
trans = new Translation(); |
69 |
|
|
70 |
this.trans = trans; |
this.trans = trans; |
71 |
this.languages = languages_; |
this.languages = languages_; |
|
|
|
|
|
|
72 |
|
|
73 |
add(getTranslationGrid(), BorderLayout.CENTER); |
add(getTranslationGrid(), BorderLayout.CENTER); |
74 |
|
|
75 |
if (question != null) { |
if (question != null) { |
76 |
JLabel questionLable = new JLabel(question); |
JLabel questionLable = new JLabel(question); |
77 |
questionLable.setBorder(BorderFactory.createEmptyBorder( |
questionLable |
78 |
6, 6, 6, 6)); |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
79 |
add(questionLable, BorderLayout.NORTH); |
add(questionLable, BorderLayout.NORTH); |
80 |
} |
} |
81 |
} |
} |
93 |
|
|
94 |
TranslationJTextField langTextField = new TranslationJTextField( |
TranslationJTextField langTextField = new TranslationJTextField( |
95 |
trans, langId); |
trans, langId); |
96 |
// Setting a size |
// Setting a size |
97 |
langTextField.setPreferredSize( new Dimension(360,22)); |
langTextField.setPreferredSize(new Dimension(360, 22)); |
98 |
langDesc.setLabelFor(langTextField); |
langDesc.setLabelFor(langTextField); |
99 |
translationGrid.add(langDesc); |
translationGrid.add(langDesc); |
100 |
translationGrid.add(langTextField); |
translationGrid.add(langTextField); |
101 |
|
|
102 |
|
langTextFields .add(langTextField); |
103 |
} |
} |
104 |
|
|
105 |
// Lay out the panel. |
// Lay out the panel. |
113 |
} |
} |
114 |
|
|
115 |
/** |
/** |
116 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
117 |
* So far this is working on the Translation object directly, Cancel not possible. |
* deals with. |
|
* @param translationEditJPanel |
|
|
* |
|
|
* @deprecated Use {@link TranslationAskJDialog} |
|
118 |
*/ |
*/ |
119 |
public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
public Translation getTranslation() { |
120 |
dialog = new JDialog(parentFrame); |
return trans; |
|
// backup( translationEditJPanels ); |
|
|
showDialog(dialog, translationEditJPanels); |
|
121 |
} |
} |
122 |
|
|
123 |
|
public void addActionListener(final ActionListener actionListener) { |
124 |
/** |
for (final JTextField langTextField : langTextFields){ |
125 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
langTextField.addKeyListener( new KeyListener(){ |
126 |
* So far this is working on the Translation object directly, Cancel not possible. |
|
127 |
* @param translationEditJPanel |
@Override |
128 |
|
public void keyPressed(KeyEvent e) { |
129 |
* @deprecated User {@link TranslationAskJDialog} * |
} |
130 |
*/ |
|
131 |
public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
@Override |
132 |
dialog = new JDialog(parentWindow); |
public void keyReleased(KeyEvent e) { |
133 |
// backup( translationEditJPanels ); |
} |
134 |
showDialog(dialog, translationEditJPanels); |
|
135 |
} |
@Override |
136 |
|
public void keyTyped(KeyEvent e) { |
137 |
/** |
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
138 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
} |
139 |
* So far this is working on the Translation object directly, Cancel not possible. |
|
140 |
* @param translationEditJPanel |
}); |
|
* |
|
|
* * @deprecated User {@link TranslationAskJDialog} |
|
|
*/ |
|
|
public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) { |
|
|
dialog = new JDialog(parentDialog); |
|
|
// backup( translationEditJPanels ); |
|
|
showDialog(dialog, translationEditJPanels); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* * @deprecated User {@link TranslationAskJDialog} |
|
|
* @param d |
|
|
* @param translationEditJPanels |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
|
|
*/ |
|
|
public static void showDialog(JDialog d, JComponent... translationEditJPanels) { |
|
|
dialog = d; |
|
|
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
|
|
SwingUtil.centerFrameOnScreen(dialog); |
|
|
Box box = Box.createVerticalBox(); |
|
|
for (JComponent panel : translationEditJPanels) { |
|
|
box.add(panel); |
|
141 |
} |
} |
|
JPanel cp = new JPanel( new BorderLayout()); |
|
|
cp.add( box, BorderLayout.CENTER); |
|
|
cp.add( getButtons(), BorderLayout.SOUTH ); |
|
|
dialog.setContentPane(cp); |
|
|
|
|
|
// dialog.getRootPane().setDefaultButton(okButton); |
|
|
|
|
|
dialog.setTitle("Please translate"); //i8n |
|
|
dialog.setModal(true); |
|
|
dialog.pack(); |
|
|
dialog.setVisible(true); |
|
142 |
} |
} |
143 |
|
|
144 |
/** |
public void removeActionListener(ActionListener actionListener) { |
145 |
* @deprecated Use TranslationAskJDialog |
for (JTextField langTextField : langTextFields){ |
146 |
* @return |
langTextField.removeActionListener(actionListener); |
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
|
|
*/ |
|
|
private static JComponent getButtons() { |
|
|
JPanel jPanel = new JPanel(); |
|
|
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(); |
|
|
} |
|
|
} ); |
|
147 |
} |
} |
|
jPanel.add(okButton); |
|
|
|
|
|
return jPanel; |
|
148 |
} |
} |
149 |
|
|
|
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); |
|
|
// } |
|
150 |
} |
} |