25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.swing; |
package skrueger.swing; |
31 |
|
|
33 |
import java.awt.Dimension; |
import java.awt.Dimension; |
34 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
35 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
36 |
import java.util.HashSet; |
import java.util.HashMap; |
37 |
import java.util.List; |
import java.util.List; |
38 |
import java.util.Random; |
import java.util.Random; |
|
import java.util.Set; |
|
39 |
import java.util.WeakHashMap; |
import java.util.WeakHashMap; |
40 |
|
|
41 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
48 |
|
|
49 |
import schmitzm.swing.JPanel; |
import schmitzm.swing.JPanel; |
50 |
import schmitzm.swing.SpringUtilities; |
import schmitzm.swing.SpringUtilities; |
51 |
|
import schmitzm.swing.SwingUtil; |
52 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
53 |
|
|
54 |
/** |
/** |
59 |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
* {@link TranslationEditJPanel}s. {@link TranslationAskJDialog} implements a |
60 |
* transparent Apply/Cancel logic. |
* transparent Apply/Cancel logic. |
61 |
* |
* |
62 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
63 |
*/ |
*/ |
64 |
public class TranslationEditJPanel extends JPanel { |
public class TranslationEditJPanel extends JPanel { |
65 |
static final protected Logger LOGGER = Logger |
static final protected Logger LOGGER = Logger |
72 |
/** |
/** |
73 |
* Remembers all {@link JTextField} that have been created. |
* Remembers all {@link JTextField} that have been created. |
74 |
*/ |
*/ |
75 |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
private HashMap<String, TranslationJTextField> langTextFields = new HashMap<String, TranslationJTextField>(); |
|
|
|
76 |
private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>(); |
private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>(); |
77 |
|
|
78 |
|
private final String question; |
79 |
|
|
80 |
/** |
/** |
81 |
* Creates a {@link JPanel} that asks the user for the translation of a |
* Creates a {@link JPanel} that asks the user for the translation of a |
82 |
* String in several languages |
* String in several languages |
93 |
public TranslationEditJPanel(String question, Translation trans, |
public TranslationEditJPanel(String question, Translation trans, |
94 |
List<String> languages_) { |
List<String> languages_) { |
95 |
super(new BorderLayout()); |
super(new BorderLayout()); |
96 |
|
this.question = question; |
97 |
|
|
98 |
|
SwingUtil.setMinimumWidth(this, 400); |
99 |
|
|
100 |
if (trans == null) |
if (trans == null) |
101 |
trans = new Translation(); |
trans = new Translation(); |
103 |
this.trans = trans; |
this.trans = trans; |
104 |
this.languages = languages_; |
this.languages = languages_; |
105 |
|
|
106 |
|
initGUI(); |
107 |
|
} |
108 |
|
|
109 |
|
private void initGUI() { |
110 |
add(getTranslationGrid(), BorderLayout.CENTER); |
add(getTranslationGrid(), BorderLayout.CENTER); |
111 |
|
|
112 |
if (question != null) { |
if (question != null) { |
117 |
} |
} |
118 |
} |
} |
119 |
|
|
120 |
|
/** |
121 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
122 |
|
* String in several languages and additionally puts a {@link JLabel} with a |
123 |
|
* question at the {@link JPanel}'s first row. |
124 |
|
* |
125 |
|
* <br/> |
126 |
|
* This constructor also sets a TitledBorder with the given title. |
127 |
|
*/ |
128 |
|
public TranslationEditJPanel(Translation title, List<String> languages2, |
129 |
|
String borderTitle) { |
130 |
|
this(title, languages2); |
131 |
|
|
132 |
|
setBorder(BorderFactory.createTitledBorder(borderTitle)); |
133 |
|
} |
134 |
|
|
135 |
private JPanel getTranslationGrid() { |
private JPanel getTranslationGrid() { |
136 |
if (translationGrid == null) { |
if (translationGrid == null) { |
137 |
translationGrid = new JPanel(new SpringLayout()); |
translationGrid = new JPanel(new SpringLayout()); |
143 |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
langDesc.setHorizontalAlignment(SwingConstants.RIGHT); |
144 |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
langDesc.setVerticalAlignment(SwingConstants.NORTH); |
145 |
|
|
146 |
TranslationJTextField langTextField = new TranslationJTextField( |
TranslationJTextField langTextField = langTextFields |
147 |
trans, langId); |
.get(langId); |
148 |
// Setting a size |
|
149 |
langTextField.setPreferredSize(new Dimension(360, 22)); |
if (langTextField == null) { |
150 |
langDesc.setLabelFor(langTextField); |
langTextField = new TranslationJTextField(trans, langId); |
151 |
|
// Setting a size |
152 |
|
langTextField.setPreferredSize(new Dimension(360, 22)); |
153 |
|
langDesc.setLabelFor(langTextField); |
154 |
|
} |
155 |
|
|
156 |
translationGrid.add(langDesc); |
translationGrid.add(langDesc); |
157 |
translationGrid.add(langTextField); |
translationGrid.add(langTextField); |
158 |
|
|
159 |
langTextFields.add(langTextField); |
langTextFields.put(langId, langTextField); |
160 |
} |
} |
161 |
|
|
162 |
// Lay out the panel. |
// Lay out the panel. |
177 |
return trans; |
return trans; |
178 |
} |
} |
179 |
|
|
180 |
|
/** |
181 |
|
* TODO Is never called?! |
182 |
|
*/ |
183 |
|
public void dispose() { |
184 |
|
for (TranslationJTextField f : langTextFields.values()) { |
185 |
|
f.dispose(); |
186 |
|
} |
187 |
|
langTextFields.clear(); |
188 |
|
} |
189 |
|
|
190 |
public void addTranslationChangeListener(final ActionListener al) { |
public void addTranslationChangeListener(final ActionListener al) { |
191 |
final ActionListener actionListener = new ActionListener() { |
final ActionListener actionListener = new ActionListener() { |
192 |
|
|
209 |
} |
} |
210 |
} |
} |
211 |
|
|
212 |
|
|
213 |
} |
} |