1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.Frame; |
6 |
import java.awt.Window; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.WindowAdapter; |
9 |
import java.awt.event.WindowEvent; |
10 |
import java.awt.event.WindowListener; |
11 |
import java.util.ArrayList; |
12 |
import java.util.List; |
13 |
|
14 |
import javax.swing.AbstractAction; |
15 |
import javax.swing.BorderFactory; |
16 |
import javax.swing.Box; |
17 |
import javax.swing.JComponent; |
18 |
import javax.swing.JDialog; |
19 |
import javax.swing.JLabel; |
20 |
import javax.swing.JPanel; |
21 |
import javax.swing.SpringLayout; |
22 |
import javax.swing.SwingConstants; |
23 |
|
24 |
import org.apache.log4j.Logger; |
25 |
|
26 |
import schmitzm.swing.SpringUtilities; |
27 |
import schmitzm.swing.SwingUtil; |
28 |
import skrueger.i8n.Translation; |
29 |
|
30 |
/** |
31 |
* A JPanel that asks the user for translation of several strings |
32 |
* @author Stefan Alfons Krüger |
33 |
* |
34 |
*/ |
35 |
public class TranslationEditJPanel extends JPanel { |
36 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
37 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
38 |
private static JDialog dialog; |
39 |
private static OkButton okButton; |
40 |
|
41 |
private static CancelButton cancelButton; |
42 |
Logger log = Logger.getLogger(TranslationEditJPanel.class); |
43 |
private List<String> languages; |
44 |
private JPanel translationGrid; |
45 |
private Translation trans; |
46 |
|
47 |
public TranslationEditJPanel(Translation trans, List<String> languages_) { |
48 |
this(null, trans, languages_); |
49 |
} |
50 |
|
51 |
public TranslationEditJPanel(String question, Translation trans, |
52 |
List<String> languages_) { |
53 |
super(new BorderLayout()); |
54 |
|
55 |
if (trans == null) trans = new Translation(); |
56 |
|
57 |
this.trans = trans; |
58 |
this.languages = languages_; |
59 |
|
60 |
|
61 |
|
62 |
add(getTranslationGrid(), BorderLayout.CENTER); |
63 |
|
64 |
if (question != null) { |
65 |
JLabel questionLable = new JLabel(question); |
66 |
questionLable.setBorder(BorderFactory.createEmptyBorder( |
67 |
6, 6, 6, 6)); |
68 |
add(questionLable, BorderLayout.NORTH); |
69 |
} |
70 |
} |
71 |
|
72 |
private JPanel getTranslationGrid() { |
73 |
if (translationGrid == null) { |
74 |
translationGrid = new JPanel(new SpringLayout()); |
75 |
|
76 |
for (String langId : languages) { |
77 |
|
78 |
// language code : entry field for translation |
79 |
JLabel langDesc = new JLabel(langId.toUpperCase() + " :"); // i8n |
80 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
81 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
82 |
|
83 |
TranslationJTextField langTextField = new TranslationJTextField( |
84 |
trans, langId); |
85 |
// Setting a size |
86 |
langTextField.setPreferredSize( new Dimension(360,22)); |
87 |
langDesc.setLabelFor(langTextField); |
88 |
translationGrid.add(langDesc); |
89 |
translationGrid.add(langTextField); |
90 |
} |
91 |
|
92 |
// Lay out the panel. |
93 |
SpringUtilities.makeCompactGrid(translationGrid, languages.size(), // rows, |
94 |
2, // cols |
95 |
6, 6, // initX, initY |
96 |
6, 6); // xPad, yPad |
97 |
|
98 |
} |
99 |
return translationGrid; |
100 |
} |
101 |
|
102 |
/** |
103 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
104 |
* So far this is working on the Translation object directly, Cancel not possible. |
105 |
* @param translationEditJPanel |
106 |
* |
107 |
* @deprecated Use {@link TranslationAskJDialog} |
108 |
*/ |
109 |
public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) { |
110 |
dialog = new JDialog(parentFrame); |
111 |
// backup( translationEditJPanels ); |
112 |
showDialog(dialog, translationEditJPanels); |
113 |
} |
114 |
|
115 |
|
116 |
/** |
117 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
118 |
* So far this is working on the Translation object directly, Cancel not possible. |
119 |
* @param translationEditJPanel |
120 |
|
121 |
* @deprecated User {@link TranslationAskJDialog} * |
122 |
*/ |
123 |
public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) { |
124 |
dialog = new JDialog(parentWindow); |
125 |
// backup( translationEditJPanels ); |
126 |
showDialog(dialog, translationEditJPanels); |
127 |
} |
128 |
|
129 |
/** |
130 |
* Merges a few {@link TranslationEditJPanel}s and shows them together.. |
131 |
* So far this is working on the Translation object directly, Cancel not possible. |
132 |
* @param translationEditJPanel |
133 |
* |
134 |
* * @deprecated User {@link TranslationAskJDialog} |
135 |
*/ |
136 |
public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) { |
137 |
dialog = new JDialog(parentDialog); |
138 |
// backup( translationEditJPanels ); |
139 |
showDialog(dialog, translationEditJPanels); |
140 |
} |
141 |
|
142 |
|
143 |
/** |
144 |
* * @deprecated User {@link TranslationAskJDialog} |
145 |
* @param d |
146 |
* @param translationEditJPanels |
147 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
148 |
*/ |
149 |
public static void showDialog(JDialog d, JComponent... translationEditJPanels) { |
150 |
dialog = d; |
151 |
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
152 |
SwingUtil.centerFrameOnScreen(dialog); |
153 |
Box box = Box.createVerticalBox(); |
154 |
for (JComponent panel : translationEditJPanels) { |
155 |
box.add(panel); |
156 |
} |
157 |
JPanel cp = new JPanel( new BorderLayout()); |
158 |
cp.add( box, BorderLayout.CENTER); |
159 |
cp.add( getButtons(), BorderLayout.SOUTH ); |
160 |
dialog.setContentPane(cp); |
161 |
|
162 |
// dialog.getRootPane().setDefaultButton(okButton); |
163 |
|
164 |
dialog.setTitle("Please translate"); //i8n |
165 |
dialog.setModal(true); |
166 |
dialog.pack(); |
167 |
dialog.setVisible(true); |
168 |
} |
169 |
|
170 |
/** |
171 |
* @deprecated Use TranslationAskJDialog |
172 |
* @return |
173 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
174 |
*/ |
175 |
private static JComponent getButtons() { |
176 |
JPanel jPanel = new JPanel(); |
177 |
if (okButton == null) { |
178 |
okButton = new OkButton(new AbstractAction("enter") { |
179 |
public void actionPerformed(ActionEvent evt) { |
180 |
firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null); |
181 |
dialog.dispose(); |
182 |
} |
183 |
} ); |
184 |
} |
185 |
jPanel.add(okButton); |
186 |
|
187 |
if (cancelButton == null) { |
188 |
cancelButton = new CancelButton(new AbstractAction("") { |
189 |
public void actionPerformed(ActionEvent evt) { |
190 |
// restore(); |
191 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
192 |
dialog.dispose(); |
193 |
} |
194 |
} ); |
195 |
} |
196 |
jPanel.add(okButton); |
197 |
|
198 |
return jPanel; |
199 |
} |
200 |
|
201 |
public Translation getTranslation() { |
202 |
return trans; |
203 |
} |
204 |
// |
205 |
// |
206 |
// public static void main(String[] args) { |
207 |
// ArrayList<String> lang = new ArrayList <String> (); |
208 |
// lang.add("de"); |
209 |
// lang.add("en"); |
210 |
// lang.add("fr"); |
211 |
// |
212 |
// Translation transe = new Translation(); |
213 |
// transe.put("de", "Terciopelo-Lanzenotter"); |
214 |
// TranslationEditJPanel p1 = new TranslationEditJPanel("Name von New Group", |
215 |
// transe, lang); |
216 |
// |
217 |
// |
218 |
// Translation transe2 = new Translation(); |
219 |
// transe2.put("de", "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart."); |
220 |
// TranslationEditJPanel p2 = new TranslationEditJPanel("Description of Animal:", |
221 |
// transe2, lang); |
222 |
// |
223 |
//// JFrame frame = new JFrame(); |
224 |
//// frame.setContentPane(p1); |
225 |
//// frame.pack(); |
226 |
//// frame.setVisible(true); |
227 |
// |
228 |
// ask(new Frame(),p1,p2); |
229 |
// } |
230 |
} |