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 translation of several strings |
20 |
* @author Stefan Alfons Krüger |
21 |
* |
22 |
*/ |
23 |
public class TranslationEditJPanel extends JPanel { |
24 |
static final protected Logger LOGGER = Logger.getLogger(TranslationEditJPanel.class); |
25 |
|
26 |
private final List<String> languages; |
27 |
private JPanel translationGrid; |
28 |
private Translation trans; |
29 |
|
30 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
31 |
this(null, trans, languages_); |
32 |
} |
33 |
|
34 |
public TranslationEditJPanel(String question, Translation trans, |
35 |
List<String> languages_) { |
36 |
super(new BorderLayout()); |
37 |
|
38 |
if (trans == null) trans = new Translation(); |
39 |
|
40 |
this.trans = trans; |
41 |
this.languages = languages_; |
42 |
|
43 |
add(getTranslationGrid(), BorderLayout.CENTER); |
44 |
|
45 |
if (question != null) { |
46 |
JLabel questionLable = new JLabel(question); |
47 |
questionLable.setBorder(BorderFactory.createEmptyBorder( |
48 |
6, 6, 6, 6)); |
49 |
add(questionLable, BorderLayout.NORTH); |
50 |
} |
51 |
} |
52 |
|
53 |
private JPanel getTranslationGrid() { |
54 |
if (translationGrid == null) { |
55 |
translationGrid = new JPanel(new SpringLayout()); |
56 |
|
57 |
for (String langId : languages) { |
58 |
|
59 |
// language code : entry field for translation |
60 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
61 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
62 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
63 |
|
64 |
TranslationJTextField langTextField = new TranslationJTextField( |
65 |
trans, langId); |
66 |
// Setting a size |
67 |
langTextField.setPreferredSize( new Dimension(360,22)); |
68 |
langDesc.setLabelFor(langTextField); |
69 |
translationGrid.add(langDesc); |
70 |
translationGrid.add(langTextField); |
71 |
} |
72 |
|
73 |
// Lay out the panel. |
74 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
75 |
2, // cols |
76 |
6, 6, // initX, initY |
77 |
6, 6); // xPad, yPad |
78 |
|
79 |
} |
80 |
return translationGrid; |
81 |
} |
82 |
|
83 |
// /** |
84 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
85 |
// * So far this is working on the Translation object directly, Cancel not possible. |
86 |
// * @param translationEditJPanel |
87 |
// * |
88 |
// * @deprecated Use {@link TranslationAskJDialog} |
89 |
// */ |
90 |
// public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
91 |
// dialog = new JDialog(parentFrame); |
92 |
// // backup( translationEditJPanels ); |
93 |
// showDialog(dialog, translationEditJPanels); |
94 |
// } |
95 |
// |
96 |
// |
97 |
// /** |
98 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
99 |
// * So far this is working on the Translation object directly, Cancel not possible. |
100 |
// * @param translationEditJPanel |
101 |
// |
102 |
// * @deprecated User {@link TranslationAskJDialog} * |
103 |
// */ |
104 |
// public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
105 |
// dialog = new JDialog(parentWindow); |
106 |
// // backup( translationEditJPanels ); |
107 |
// showDialog(dialog, translationEditJPanels); |
108 |
// } |
109 |
// |
110 |
// /** |
111 |
// * Merges a few {@link TranslationEditJPanel}s and shows them together.. |
112 |
// * So far this is working on the Translation object directly, Cancel not possible. |
113 |
// * @param translationEditJPanel |
114 |
// * |
115 |
// * * @deprecated User {@link TranslationAskJDialog} |
116 |
// */ |
117 |
// public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) { |
118 |
// dialog = new JDialog(parentDialog); |
119 |
// // backup( translationEditJPanels ); |
120 |
// showDialog(dialog, translationEditJPanels); |
121 |
// } |
122 |
// |
123 |
// |
124 |
// /** |
125 |
// * * @deprecated User {@link TranslationAskJDialog} |
126 |
// * @param d |
127 |
// * @param translationEditJPanels |
128 |
// * @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
129 |
// */ |
130 |
// public static void showDialog(JDialog d, JComponent... translationEditJPanels) { |
131 |
// dialog = d; |
132 |
// dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
133 |
// SwingUtil.centerFrameOnScreen(dialog); |
134 |
// Box box = Box.createVerticalBox(); |
135 |
// for (JComponent panel : translationEditJPanels) { |
136 |
// box.add(panel); |
137 |
// } |
138 |
// JPanel cp = new JPanel( new BorderLayout()); |
139 |
// cp.add( box, BorderLayout.CENTER); |
140 |
// cp.add( getButtons(), BorderLayout.SOUTH ); |
141 |
// dialog.setContentPane(cp); |
142 |
// |
143 |
// // dialog.getRootPane().setDefaultButton(okButton); |
144 |
// |
145 |
// dialog.setTitle("Please translate"); //i8n |
146 |
// dialog.setModal(true); |
147 |
// dialog.pack(); |
148 |
// dialog.setVisible(true); |
149 |
// } |
150 |
// |
151 |
// /** |
152 |
// * @deprecated Use TranslationAskJDialog |
153 |
// * @return |
154 |
// * @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
155 |
// */ |
156 |
// private static JComponent getButtons() { |
157 |
// JPanel jPanel = new JPanel(); |
158 |
// if (okButton == null) { |
159 |
// okButton = new OkButton(new AbstractAction("enter") { |
160 |
// public void actionPerformed(ActionEvent evt) { |
161 |
// firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null); |
162 |
// dialog.dispose(); |
163 |
// } |
164 |
// } ); |
165 |
// } |
166 |
// jPanel.add(okButton); |
167 |
// |
168 |
// if (cancelButton == null) { |
169 |
// cancelButton = new CancelButton(new AbstractAction("") { |
170 |
// public void actionPerformed(ActionEvent evt) { |
171 |
// // restore(); |
172 |
// firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
173 |
// dialog.dispose(); |
174 |
// } |
175 |
// } ); |
176 |
// } |
177 |
// jPanel.add(okButton); |
178 |
// |
179 |
// return jPanel; |
180 |
// } |
181 |
|
182 |
/** |
183 |
* @return The {@link Translation} that this {@link TranslationEditJPanel} deals with. |
184 |
*/ |
185 |
public Translation getTranslation() { |
186 |
return trans; |
187 |
} |
188 |
|
189 |
} |