diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java index b87899f..a6966ec 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java @@ -4,7 +4,6 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.beans.PropertyChangeEvent; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -12,7 +11,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; -import java.util.Objects; import javax.swing.BoxLayout; import javax.swing.JComboBox; @@ -20,12 +18,8 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; -import javax.swing.text.Document; -import javax.swing.text.JTextComponent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -70,17 +64,17 @@ public abstract class ParamField extends JPanel { this.paramName = paramName; } - public static class StringField extends ParamField { + public static class StringField extends ParamField implements TextFieldListener { JTextField field; StringField(RequestView requestView, String paramName) { super(requestView, paramName); field = new JTextField(); - addChangeListener(field, e -> onUpdate()); + addChangeListener(this, field); this.add(field); } - private void onUpdate() { + public void update(JTextField field) { if ("".equals(field.getText())) { requestView.updateParam(paramName, null); } else { @@ -89,17 +83,17 @@ public abstract class ParamField extends JPanel { } } - public static class FloatField extends ParamField { + public static class FloatField extends ParamField implements TextFieldListener { JTextField field; FloatField(RequestView requestView, String paramName) { super(requestView, paramName); field = new JTextField(); - addChangeListener(field, e -> onUpdate()); + addChangeListener(this, field); this.add(field); } - private void onUpdate() { + public void update(JTextField field) { if ("".equals(field.getText())) { field.setBackground(Color.WHITE); requestView.updateParam(paramName, null); @@ -114,7 +108,7 @@ public abstract class ParamField extends JPanel { } } - public static class DateRangeField extends ParamField { + public static class DateRangeField extends ParamField implements TextFieldListener { JTextField fieldMin; JTextField fieldMax; @@ -122,27 +116,29 @@ public abstract class ParamField extends JPanel { super(requestView, paramName); this.add(new JLabel("min ")); fieldMin = new JTextField(); + fieldMin.setName(MIN_SUFFIX); fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT)); - addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX)); + addChangeListener(this, fieldMin); this.add(fieldMin); this.add(new JLabel("max ")); fieldMax = new JTextField(); + fieldMax.setName(MAX_SUFFIX); fieldMax.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT)); - addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX)); + addChangeListener(this, fieldMin); this.add(fieldMax); } - private void onUpdate(JTextField field, String suffix) { + public void update(JTextField field) { DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH); if ("".equals(field.getText())) { field.setBackground(Color.WHITE); - requestView.updateParam(paramName + suffix, null); + requestView.updateParam(paramName + field.getName(), null); } else if (field.getText().matches(DATE_REGEX)) { try { long date = df.parse(field.getText()).getTime(); date = (Math.round((date / 86400000.0) + 2440587.5)); // to JD - requestView.updateParam(paramName + suffix, date); + requestView.updateParam(paramName + field.getName(), date); field.setBackground(Color.WHITE); } catch (ParseException e) { field.setBackground(Color.PINK); @@ -154,28 +150,31 @@ public abstract class ParamField extends JPanel { } } - public static class FloatRangeField extends ParamField { + public static class FloatRangeField extends ParamField implements TextFieldListener { JTextField fieldMin; JTextField fieldMax; FloatRangeField(RequestView requestView, String paramName) { super(requestView, paramName); fieldMin = new JTextField(); - addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX)); + fieldMin.setName(MIN_SUFFIX); + addChangeListener(this, fieldMin); this.add(fieldMin); fieldMax = new JTextField(); - addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX)); + fieldMax.setName(MAX_SUFFIX); + addChangeListener(this, fieldMax); this.add(fieldMax); } - private void onUpdate(JTextField field, String suffix) { + public void update(JTextField field) { if ("".equals(field.getText())) { field.setBackground(Color.WHITE); - requestView.updateParam(paramName + suffix, null); + requestView.updateParam(paramName + field.getName(), null); } else { try { - requestView.updateParam(paramName + suffix, Float.parseFloat(field.getText())); + requestView.updateParam(paramName + field.getName(), + Float.parseFloat(field.getText())); field.setBackground(Color.WHITE); } catch (NumberFormatException e) { field.setBackground(Color.PINK); @@ -184,7 +183,7 @@ public abstract class ParamField extends JPanel { } } - public static class TargetNameField extends ParamField { + public static class TargetNameField extends ParamField implements TextFieldListener { JComboBox comboBox; JTextField field; String lastContent; @@ -196,7 +195,7 @@ public abstract class ParamField extends JPanel { comboBox.setEditable(true); field = (JTextField) comboBox.getEditor().getEditorComponent(); - addChangeListener(field, e -> onUpdate()); + addChangeListener(this, field); this.add(comboBox); } @@ -219,15 +218,21 @@ public abstract class ParamField extends JPanel { return targetNames; } - private void onUpdate() { - String content = field.getText(); - if (content.length() >= 2 && !content.equals(lastContent)) { - lastContent = content; - int nbItems = comboBox.getItemCount(); + Runnable updateComboBox = new Runnable() { + @Override + public void run() { comboBox.removeAllItems(); - for (String s : getItems(content)) { + for (String s : getItems(lastContent)) { comboBox.addItem(s); } + } + }; + + public void update(JTextField field) { + String content = field.getText(); + if (content.length() >= 2 && !content.equals(lastContent)) { + lastContent = content; + SwingUtilities.invokeLater(updateComboBox); field.setText(content); requestView.updateParam(paramName, content); } @@ -303,50 +308,23 @@ public abstract class ParamField extends JPanel { } } - // public class EnumParamField extends ParamField { - // EnumParamField() { - // super(); - // } - // } - - public static void addChangeListener(JTextComponent text, ChangeListener changeListener) { - Objects.requireNonNull(text); - Objects.requireNonNull(changeListener); - DocumentListener dl = new DocumentListener() { - private int lastChange = 0, lastNotifiedChange = 0; + interface TextFieldListener { + void update(JTextField field); + } - @Override - public void insertUpdate(DocumentEvent e) { - changedUpdate(e); + static void addChangeListener(TextFieldListener changeListener, JTextField field) { + field.getDocument().addDocumentListener(new DocumentListener() { + public void removeUpdate(DocumentEvent de) { + changeListener.update(field); } - @Override - public void removeUpdate(DocumentEvent e) { - changedUpdate(e); + public void insertUpdate(DocumentEvent de) { + changeListener.update(field); } - @Override - public void changedUpdate(DocumentEvent e) { - lastChange++; - SwingUtilities.invokeLater(() -> { - if (lastNotifiedChange != lastChange) { - lastNotifiedChange = lastChange; - changeListener.stateChanged(new ChangeEvent(text)); - } - }); + public void changedUpdate(DocumentEvent de) { + changeListener.update(field); } - }; - text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> { - Document d1 = (Document) e.getOldValue(); - Document d2 = (Document) e.getNewValue(); - if (d1 != null) - d1.removeDocumentListener(dl); - if (d2 != null) - d2.addDocumentListener(dl); - dl.changedUpdate(null); }); - Document d = text.getDocument(); - if (d != null) - d.addDocumentListener(dl); } } \ No newline at end of file -- libgit2 0.21.2