1 |
mojays |
2 |
package skrueger.swing; |
2 |
|
|
|
3 |
|
|
import java.awt.BorderLayout; |
4 |
alfonx |
221 |
import java.awt.Container; |
5 |
mojays |
2 |
import java.awt.Dimension; |
6 |
alfonx |
221 |
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 |
mojays |
2 |
import java.util.List; |
13 |
alfonx |
221 |
import java.util.Set; |
14 |
mojays |
2 |
|
15 |
|
|
import javax.swing.BorderFactory; |
16 |
|
|
import javax.swing.JLabel; |
17 |
|
|
import javax.swing.JPanel; |
18 |
alfonx |
221 |
import javax.swing.JTextField; |
19 |
mojays |
2 |
import javax.swing.SpringLayout; |
20 |
|
|
import javax.swing.SwingConstants; |
21 |
|
|
|
22 |
|
|
import org.apache.log4j.Logger; |
23 |
|
|
|
24 |
|
|
import schmitzm.swing.SpringUtilities; |
25 |
|
|
import skrueger.i8n.Translation; |
26 |
|
|
|
27 |
|
|
/** |
28 |
alfonx |
220 |
* 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 |
mojays |
2 |
* @author Stefan Alfons Krüger |
36 |
|
|
*/ |
37 |
alfonx |
221 |
public class TranslationEditJPanel extends JPanel { |
38 |
alfonx |
220 |
static final protected Logger LOGGER = Logger |
39 |
|
|
.getLogger(TranslationEditJPanel.class); |
40 |
|
|
|
41 |
alfonx |
38 |
private final List<String> languages; |
42 |
mojays |
2 |
private JPanel translationGrid; |
43 |
|
|
private Translation trans; |
44 |
|
|
|
45 |
alfonx |
220 |
/** |
46 |
alfonx |
221 |
* Remembers all {@link JTextField} that have been created. |
47 |
|
|
*/ |
48 |
|
|
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
49 |
|
|
|
50 |
|
|
/** |
51 |
alfonx |
220 |
* Creates a {@link JPanel} that asks the user for the translation of a |
52 |
|
|
* String in several languages |
53 |
|
|
*/ |
54 |
mojays |
2 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
55 |
|
|
this(null, trans, languages_); |
56 |
|
|
} |
57 |
|
|
|
58 |
alfonx |
220 |
/** |
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 |
mojays |
2 |
public TranslationEditJPanel(String question, Translation trans, |
64 |
|
|
List<String> languages_) { |
65 |
|
|
super(new BorderLayout()); |
66 |
|
|
|
67 |
alfonx |
220 |
if (trans == null) |
68 |
|
|
trans = new Translation(); |
69 |
|
|
|
70 |
mojays |
2 |
this.trans = trans; |
71 |
|
|
this.languages = languages_; |
72 |
alfonx |
220 |
|
73 |
mojays |
2 |
add(getTranslationGrid(), BorderLayout.CENTER); |
74 |
|
|
|
75 |
|
|
if (question != null) { |
76 |
|
|
JLabel questionLable = new JLabel(question); |
77 |
alfonx |
220 |
questionLable |
78 |
|
|
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
79 |
mojays |
2 |
add(questionLable, BorderLayout.NORTH); |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
private JPanel getTranslationGrid() { |
84 |
|
|
if (translationGrid == null) { |
85 |
|
|
translationGrid = new JPanel(new SpringLayout()); |
86 |
|
|
|
87 |
|
|
for (String langId : languages) { |
88 |
|
|
|
89 |
|
|
// language code : entry field for translation |
90 |
|
|
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
91 |
|
|
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
92 |
|
|
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
93 |
|
|
|
94 |
|
|
TranslationJTextField langTextField = new TranslationJTextField( |
95 |
|
|
trans, langId); |
96 |
alfonx |
220 |
// Setting a size |
97 |
|
|
langTextField.setPreferredSize(new Dimension(360, 22)); |
98 |
mojays |
2 |
langDesc.setLabelFor(langTextField); |
99 |
|
|
translationGrid.add(langDesc); |
100 |
|
|
translationGrid.add(langTextField); |
101 |
alfonx |
221 |
|
102 |
|
|
langTextFields .add(langTextField); |
103 |
mojays |
2 |
} |
104 |
|
|
|
105 |
|
|
// Lay out the panel. |
106 |
|
|
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
107 |
|
|
2, // cols |
108 |
|
|
6, 6, // initX, initY |
109 |
|
|
6, 6); // xPad, yPad |
110 |
|
|
|
111 |
|
|
} |
112 |
|
|
return translationGrid; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
alfonx |
220 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
117 |
|
|
* deals with. |
118 |
alfonx |
33 |
*/ |
119 |
mojays |
2 |
public Translation getTranslation() { |
120 |
|
|
return trans; |
121 |
|
|
} |
122 |
alfonx |
38 |
|
123 |
alfonx |
221 |
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 |
mojays |
2 |
} |