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; |
6 |
|
import java.awt.event.ActionEvent; |
7 |
|
import java.awt.event.ActionListener; |
8 |
|
import java.awt.event.KeyEvent; |
9 |
|
import java.awt.event.KeyListener; |
10 |
|
import java.beans.PropertyChangeListener; |
11 |
|
import java.util.HashSet; |
12 |
import java.util.List; |
import java.util.List; |
13 |
|
import java.util.Set; |
14 |
|
|
15 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
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 |
|
|
25 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
26 |
|
|
27 |
/** |
/** |
28 |
* A {@link 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 |
static final protected Logger LOGGER = Logger.getLogger(TranslationEditJPanel.class); |
static final protected Logger LOGGER = Logger |
39 |
|
.getLogger(TranslationEditJPanel.class); |
40 |
|
|
41 |
private final List<String> languages; |
private final 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. |
112 |
return translationGrid; |
return translationGrid; |
113 |
} |
} |
114 |
|
|
|
// /** |
|
|
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
|
|
// * So far this is working on the Translation object directly, Cancel not possible. |
|
|
// * @param translationEditJPanel |
|
|
// * |
|
|
// * @deprecated Use {@link TranslationAskJDialog} |
|
|
// */ |
|
|
// public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
|
|
// dialog = new JDialog(parentFrame); |
|
|
// // backup( translationEditJPanels ); |
|
|
// showDialog(dialog, translationEditJPanels); |
|
|
// } |
|
|
// |
|
|
// |
|
|
// /** |
|
|
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
|
|
// * So far this is working on the Translation object directly, Cancel not possible. |
|
|
// * @param translationEditJPanel |
|
|
// |
|
|
// * @deprecated User {@link TranslationAskJDialog} * |
|
|
// */ |
|
|
// public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
|
|
// dialog = new JDialog(parentWindow); |
|
|
// // backup( translationEditJPanels ); |
|
|
// showDialog(dialog, translationEditJPanels); |
|
|
// } |
|
|
// |
|
|
// /** |
|
|
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
|
|
// * So far this is working on the Translation object directly, Cancel not possible. |
|
|
// * @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); |
|
|
// } |
|
|
// 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); |
|
|
// } |
|
|
// |
|
|
// /** |
|
|
// * @deprecated Use TranslationAskJDialog |
|
|
// * @return |
|
|
// * @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(); |
|
|
// } |
|
|
// } ); |
|
|
// } |
|
|
// jPanel.add(okButton); |
|
|
// |
|
|
// return jPanel; |
|
|
// } |
|
|
|
|
115 |
/** |
/** |
116 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} deals with. |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
117 |
|
* deals with. |
118 |
*/ |
*/ |
119 |
public Translation getTranslation() { |
public Translation getTranslation() { |
120 |
return trans; |
return trans; |
121 |
} |
} |
122 |
|
|
123 |
|
public void addActionListener(final ActionListener actionListener) { |
124 |
|
for (final JTextField langTextField : langTextFields){ |
125 |
|
langTextField.addKeyListener( new KeyListener(){ |
126 |
|
|
127 |
|
@Override |
128 |
|
public void keyPressed(KeyEvent e) { |
129 |
|
} |
130 |
|
|
131 |
|
@Override |
132 |
|
public void keyReleased(KeyEvent e) { |
133 |
|
} |
134 |
|
|
135 |
|
@Override |
136 |
|
public void keyTyped(KeyEvent e) { |
137 |
|
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
138 |
|
} |
139 |
|
|
140 |
|
}); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
public void removeActionListener(ActionListener actionListener) { |
145 |
|
for (JTextField langTextField : langTextFields){ |
146 |
|
langTextField.removeActionListener(actionListener); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
} |
} |