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; |
|
import java.awt.event.KeyEvent; |
|
|
import java.awt.event.KeyListener; |
|
36 |
import java.util.HashSet; |
import java.util.HashSet; |
37 |
import java.util.List; |
import java.util.List; |
38 |
|
import java.util.Random; |
39 |
import java.util.Set; |
import java.util.Set; |
40 |
|
import java.util.WeakHashMap; |
41 |
|
|
42 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
43 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
49 |
|
|
50 |
import schmitzm.swing.JPanel; |
import schmitzm.swing.JPanel; |
51 |
import schmitzm.swing.SpringUtilities; |
import schmitzm.swing.SpringUtilities; |
52 |
|
import schmitzm.swing.SwingUtil; |
53 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
54 |
|
|
55 |
/** |
/** |
62 |
* |
* |
63 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
64 |
*/ |
*/ |
65 |
public class TranslationEditJPanel extends JPanel { |
public class TranslationEditJPanel extends JPanel { |
66 |
static final protected Logger LOGGER = Logger |
static final protected Logger LOGGER = Logger |
67 |
.getLogger(TranslationEditJPanel.class); |
.getLogger(TranslationEditJPanel.class); |
68 |
|
|
73 |
/** |
/** |
74 |
* Remembers all {@link JTextField} that have been created. |
* Remembers all {@link JTextField} that have been created. |
75 |
*/ |
*/ |
76 |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
private Set<TranslationJTextField> langTextFields = new HashSet<TranslationJTextField>(); |
77 |
|
private WeakHashMap<ActionListener, ActionListener> actionListeners = new WeakHashMap<ActionListener, ActionListener>(); |
78 |
|
|
79 |
/** |
/** |
80 |
* 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 |
92 |
public TranslationEditJPanel(String question, Translation trans, |
public TranslationEditJPanel(String question, Translation trans, |
93 |
List<String> languages_) { |
List<String> languages_) { |
94 |
super(new BorderLayout()); |
super(new BorderLayout()); |
95 |
|
|
96 |
|
SwingUtil.setMinimumWidth(this, 400); |
97 |
|
|
98 |
if (trans == null) |
if (trans == null) |
99 |
trans = new Translation(); |
trans = new Translation(); |
109 |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); |
110 |
add(questionLable, BorderLayout.NORTH); |
add(questionLable, BorderLayout.NORTH); |
111 |
} |
} |
112 |
|
|
113 |
|
/** |
114 |
|
* Add listeners |
115 |
|
*/ |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* Creates a {@link JPanel} that asks the user for the translation of a |
120 |
|
* String in several languages and additionally puts a {@link JLabel} with a |
121 |
|
* question at the {@link JPanel}'s first row. |
122 |
|
* |
123 |
|
* <br/> |
124 |
|
* This constructor also sets a TitledBorder with the given title. |
125 |
|
*/ |
126 |
|
public TranslationEditJPanel(Translation title, List<String> languages2, |
127 |
|
String borderTitle) { |
128 |
|
this(title, languages2); |
129 |
|
|
130 |
|
setBorder(BorderFactory.createTitledBorder(borderTitle)); |
131 |
} |
} |
132 |
|
|
133 |
private JPanel getTranslationGrid() { |
private JPanel getTranslationGrid() { |
148 |
langDesc.setLabelFor(langTextField); |
langDesc.setLabelFor(langTextField); |
149 |
translationGrid.add(langDesc); |
translationGrid.add(langDesc); |
150 |
translationGrid.add(langTextField); |
translationGrid.add(langTextField); |
151 |
|
|
152 |
langTextFields .add(langTextField); |
langTextFields.add(langTextField); |
153 |
} |
} |
154 |
|
|
155 |
// Lay out the panel. |
// Lay out the panel. |
170 |
return trans; |
return trans; |
171 |
} |
} |
172 |
|
|
173 |
public void addActionListener(final ActionListener actionListener) { |
/** |
174 |
for (final JTextField langTextField : langTextFields){ |
* TODO Is never called?! |
175 |
langTextField.addKeyListener( new KeyListener(){ |
*/ |
176 |
|
public void dispose() { |
177 |
@Override |
for (TranslationJTextField f: langTextFields) { |
178 |
public void keyPressed(KeyEvent e) { |
f.dispose(); |
|
} |
|
|
|
|
|
@Override |
|
|
public void keyReleased(KeyEvent e) { |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void keyTyped(KeyEvent e) { |
|
|
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
|
|
} |
|
|
|
|
|
}); |
|
179 |
} |
} |
180 |
} |
} |
181 |
|
|
182 |
public void removeActionListener(ActionListener actionListener) { |
public void addTranslationChangeListener(final ActionListener al) { |
183 |
for (JTextField langTextField : langTextFields){ |
final ActionListener actionListener = new ActionListener() { |
184 |
langTextField.removeActionListener(actionListener); |
|
185 |
|
@Override |
186 |
|
public void actionPerformed(ActionEvent e) { |
187 |
|
al.actionPerformed(new ActionEvent(TranslationEditJPanel.this, |
188 |
|
new Random().nextInt(), "")); |
189 |
|
} |
190 |
|
|
191 |
|
}; |
192 |
|
actionListeners.put(al, actionListener); |
193 |
|
getTranslation().addTranslationChangeListener(actionListener); |
194 |
|
} |
195 |
|
|
196 |
|
public void removeTranslationChangeListener(ActionListener al) { |
197 |
|
if (actionListeners.get(al) != null) { |
198 |
|
ActionListener actionListener = actionListeners.get(al); |
199 |
|
getTranslation().removeTranslationChangeListener(actionListener); |
200 |
|
actionListeners.remove(actionListener); |
201 |
} |
} |
202 |
} |
} |
203 |
|
|