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; |
61 |
* |
* |
62 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
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 |
66 |
.getLogger(TranslationEditJPanel.class); |
.getLogger(TranslationEditJPanel.class); |
67 |
|
|
74 |
*/ |
*/ |
75 |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
private Set<JTextField> langTextFields = new HashSet<JTextField>(); |
76 |
|
|
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 |
81 |
* String in several languages |
* String in several languages |
127 |
langDesc.setLabelFor(langTextField); |
langDesc.setLabelFor(langTextField); |
128 |
translationGrid.add(langDesc); |
translationGrid.add(langDesc); |
129 |
translationGrid.add(langTextField); |
translationGrid.add(langTextField); |
130 |
|
|
131 |
langTextFields .add(langTextField); |
langTextFields.add(langTextField); |
132 |
} |
} |
133 |
|
|
134 |
// Lay out the panel. |
// Lay out the panel. |
149 |
return trans; |
return trans; |
150 |
} |
} |
151 |
|
|
152 |
public void addActionListener(final ActionListener actionListener) { |
public void addTranslationChangeListener(final ActionListener al) { |
153 |
for (final JTextField langTextField : langTextFields){ |
final ActionListener actionListener = new ActionListener() { |
154 |
langTextField.addKeyListener( new KeyListener(){ |
|
155 |
|
@Override |
156 |
@Override |
public void actionPerformed(ActionEvent e) { |
157 |
public void keyPressed(KeyEvent e) { |
al.actionPerformed(new ActionEvent(TranslationEditJPanel.this, |
158 |
} |
new Random().nextInt(), "")); |
159 |
|
} |
160 |
@Override |
|
161 |
public void keyReleased(KeyEvent e) { |
}; |
162 |
} |
actionListeners.put(al, actionListener); |
163 |
|
getTranslation().addTranslationChangeListener(actionListener); |
|
@Override |
|
|
public void keyTyped(KeyEvent e) { |
|
|
actionListener.actionPerformed(new ActionEvent(TranslationEditJPanel.this, 0, "")); |
|
|
} |
|
|
|
|
|
}); |
|
|
} |
|
164 |
} |
} |
165 |
|
|
166 |
public void removeActionListener(ActionListener actionListener) { |
public void removeTranslationChangeListener(ActionListener al) { |
167 |
for (JTextField langTextField : langTextFields){ |
if (actionListeners.get(al) != null) { |
168 |
langTextField.removeActionListener(actionListener); |
ActionListener actionListener = actionListeners.get(al); |
169 |
|
getTranslation().removeTranslationChangeListener(actionListener); |
170 |
|
actionListeners.remove(actionListener); |
171 |
} |
} |
172 |
} |
} |
173 |
|
|