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