1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.util.List; |
6 |
|
7 |
import javax.swing.BorderFactory; |
8 |
import javax.swing.JLabel; |
9 |
import javax.swing.JPanel; |
10 |
import javax.swing.SpringLayout; |
11 |
import javax.swing.SwingConstants; |
12 |
|
13 |
import org.apache.log4j.Logger; |
14 |
|
15 |
import schmitzm.swing.SpringUtilities; |
16 |
import skrueger.i8n.Translation; |
17 |
|
18 |
/** |
19 |
* A {@link JPanel} that asks the user for the translations of a String in |
20 |
* several languages. Use {@link TranslationAskJDialog} to display.<br/> |
21 |
* The class does not implement any backup/clong strategies. The |
22 |
* {@link Translation} object is manipulated directly.<br/> |
23 |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
24 |
* transparent Apply/Cancel logic. |
25 |
* |
26 |
* @author Stefan Alfons Krüger |
27 |
*/ |
28 |
public class TranslationEditJPanel extends JPanel { |
29 |
static final protected Logger LOGGER = Logger |
30 |
.getLogger(TranslationEditJPanel.class); |
31 |
|
32 |
private final List<String> languages; |
33 |
private JPanel translationGrid; |
34 |
private Translation trans; |
35 |
|
36 |
/** |
37 |
* Creates a {@link JPanel} that asks the user for the translation of a |
38 |
* String in several languages |
39 |
*/ |
40 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
41 |
this(null, trans, languages_); |
42 |
} |
43 |
|
44 |
/** |
45 |
* Creates a {@link JPanel} that asks the user for the translation of a |
46 |
* String in several languages and additionally puts a {@link JLabel} with a |
47 |
* question at the {@link JPanel}'s first row. |
48 |
*/ |
49 |
public TranslationEditJPanel(String question, Translation trans, |
50 |
List<String> languages_) { |
51 |
super(new BorderLayout()); |
52 |
|
53 |
if (trans == null) |
54 |
trans = new Translation(); |
55 |
|
56 |
this.trans = trans; |
57 |
this.languages = languages_; |
58 |
|
59 |
add(getTranslationGrid(), BorderLayout.CENTER); |
60 |
|
61 |
if (question != null) { |
62 |
JLabel questionLable = new JLabel(question); |
63 |
questionLable |
64 |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
65 |
add(questionLable, BorderLayout.NORTH); |
66 |
} |
67 |
} |
68 |
|
69 |
private JPanel getTranslationGrid() { |
70 |
if (translationGrid == null) { |
71 |
translationGrid = new JPanel(new SpringLayout()); |
72 |
|
73 |
for (String langId : languages) { |
74 |
|
75 |
// language code : entry field for translation |
76 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
77 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
78 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
79 |
|
80 |
TranslationJTextField langTextField = new TranslationJTextField( |
81 |
trans, langId); |
82 |
// Setting a size |
83 |
langTextField.setPreferredSize(new Dimension(360, 22)); |
84 |
langDesc.setLabelFor(langTextField); |
85 |
translationGrid.add(langDesc); |
86 |
translationGrid.add(langTextField); |
87 |
} |
88 |
|
89 |
// Lay out the panel. |
90 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
91 |
2, // cols |
92 |
6, 6, // initX, initY |
93 |
6, 6); // xPad, yPad |
94 |
|
95 |
} |
96 |
return translationGrid; |
97 |
} |
98 |
|
99 |
/** |
100 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} |
101 |
* deals with. |
102 |
*/ |
103 |
public Translation getTranslation() { |
104 |
return trans; |
105 |
} |
106 |
|
107 |
} |