From cdc9b9a9d2d0dc94bb0aa1fcc473a5212bf02d07 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Sat, 8 Feb 2014 17:55:31 +0100 Subject: [PATCH] [cooja] plugins/VariableWatcher: Replace varName KeyListener by DocumentListener as only content changes change validation state of varName Modify foreground color of varName instead of background color because some LookAndFeels do not support colored backgrounds. --- .../cooja/plugins/VariableWatcher.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/plugins/VariableWatcher.java b/tools/cooja/java/org/contikios/cooja/plugins/VariableWatcher.java index e3905ff0b..98ad6394b 100644 --- a/tools/cooja/java/org/contikios/cooja/plugins/VariableWatcher.java +++ b/tools/cooja/java/org/contikios/cooja/plugins/VariableWatcher.java @@ -65,10 +65,14 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.DocumentFilter; +import javax.swing.text.JTextComponent; import javax.swing.text.PlainDocument; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Cooja; @@ -234,18 +238,26 @@ public class VariableWatcher extends VisPlugin implements MotePlugin { varNameCombo.addItem(aVarName); } - varNameCombo.addKeyListener(new KeyListener() { + /* Reset variable read feedbacks if variable name was changed */ + final JTextComponent tc = (JTextComponent) varNameCombo.getEditor().getEditorComponent(); + tc.getDocument().addDocumentListener(new DocumentListener() { + @Override - public void keyPressed(KeyEvent e) { + public void insertUpdate(DocumentEvent e) { writeButton.setEnabled(false); + ((JTextField) varNameCombo.getEditor().getEditorComponent()).setForeground(UIManager.getColor("TextField.foreground")); } + @Override - public void keyTyped(KeyEvent e) { + public void removeUpdate(DocumentEvent e) { writeButton.setEnabled(false); + ((JTextField) varNameCombo.getEditor().getEditorComponent()).setForeground(UIManager.getColor("TextField.foreground")); } + @Override - public void keyReleased(KeyEvent e) { + public void changedUpdate(DocumentEvent e) { writeButton.setEnabled(false); + ((JTextField) varNameCombo.getEditor().getEditorComponent()).setForeground(UIManager.getColor("TextField.foreground")); } }); @@ -498,7 +510,7 @@ public class VariableWatcher extends VisPlugin implements MotePlugin { varNameCombo.setBackground(Color.WHITE); writeButton.setEnabled(true); } catch (UnknownVariableException ex) { - varNameCombo.setBackground(Color.RED); + ((JTextField) varNameCombo.getEditor().getEditorComponent()).setForeground(Color.RED); writeButton.setEnabled(false); } } else if (varTypeCombo.getSelectedIndex() == INT_INDEX) { @@ -508,7 +520,7 @@ public class VariableWatcher extends VisPlugin implements MotePlugin { varNameCombo.setBackground(Color.WHITE); writeButton.setEnabled(true); } catch (UnknownVariableException ex) { - varNameCombo.setBackground(Color.RED); + ((JTextField) varNameCombo.getEditor().getEditorComponent()).setForeground(Color.RED); writeButton.setEnabled(false); } } else if (varTypeCombo.getSelectedIndex() == ARRAY_INDEX || @@ -529,7 +541,7 @@ public class VariableWatcher extends VisPlugin implements MotePlugin { varNameCombo.setBackground(Color.WHITE); writeButton.setEnabled(true); } catch (UnknownVariableException ex) { - varNameCombo.setBackground(Color.RED); + ((JTextField) varNameCombo.getEditor().getEditorComponent()).setForeground(Color.RED); writeButton.setEnabled(false); } }