JAL-2071 improvement to reduce webservice requests to Free Text Search (FTS) services...
authortcofoegbu <tcnofoegbu@dundee.ac.uk>
Tue, 26 Apr 2016 09:21:56 +0000 (10:21 +0100)
committertcofoegbu <tcnofoegbu@dundee.ac.uk>
Tue, 26 Apr 2016 09:21:56 +0000 (10:21 +0100)
1  2 
src/jalview/fts/core/GFTSPanel.java

@@@ -35,6 -32,6 +35,8 @@@ import java.awt.CardLayout
  import java.awt.Dimension;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
++import java.awt.event.FocusEvent;
++import java.awt.event.FocusListener;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseAdapter;
@@@ -57,6 -49,6 +59,7 @@@ import javax.swing.JScrollPane
  import javax.swing.JTabbedPane;
  import javax.swing.JTable;
  import javax.swing.JTextField;
++import javax.swing.Timer;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  import javax.swing.event.DocumentEvent;
@@@ -354,24 -342,24 +357,29 @@@ public abstract class GFTSPanel extend
        }
      });
  
--    txt_search.getDocument().addDocumentListener(new DocumentListener()
++    final DeferredTextInputListener listener = new DeferredTextInputListener(
++            500,
++            new ActionListener()
++            {
++              @Override
++              public void actionPerformed(ActionEvent e)
++              {
++                searchAction();
++              }
++            }, false);
++    txt_search.getDocument().addDocumentListener(listener);
++    txt_search.addFocusListener(new FocusListener()
      {
        @Override
--      public void insertUpdate(DocumentEvent e)
++      public void focusGained(FocusEvent e)
        {
-         searchAction();
-       }
-       @Override
-       public void removeUpdate(DocumentEvent e)
-       {
-         searchAction();
 -        txt_search_ActionPerformed();
++        listener.start();
        }
  
        @Override
-       public void changedUpdate(DocumentEvent e)
 -      public void removeUpdate(DocumentEvent e)
++      public void focusLost(FocusEvent e)
        {
-         searchAction();
 -        txt_search_ActionPerformed();
 -      }
 -
 -      @Override
 -      public void changedUpdate(DocumentEvent e)
 -      {
 -        txt_search_ActionPerformed();
++        listener.stop();
        }
      });
  
      mainFrame.setVisible(true);
      mainFrame.setContentPane(this);
      mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 -    Desktop.addInternalFrame(mainFrame, frameTitle, 800, 400);
 +    Desktop.addInternalFrame(mainFrame, getFTSFrameTitle(), 800, 400);
 +  }
 +
++  public class DeferredTextInputListener implements DocumentListener
++  {
++    private final Timer swingTimer;
++
++    public DeferredTextInputListener(int timeOut, ActionListener listener,
++            boolean repeats)
++    {
++      swingTimer = new Timer(timeOut, listener);
++      swingTimer.setRepeats(repeats);
++    }
++
++    public void start()
++    {
++      swingTimer.start();
++    }
++
++    public void stop()
++    {
++      swingTimer.stop();
++    }
++
++    @Override
++    public void insertUpdate(DocumentEvent e)
++    {
++      swingTimer.restart();
++    }
++
++    @Override
++    public void removeUpdate(DocumentEvent e)
++    {
++      swingTimer.restart();
++    }
++
++    @Override
++    public void changedUpdate(DocumentEvent e)
++    {
++      swingTimer.restart();
++    }
++
+   }
    public boolean wantedFieldsUpdated()
    {
      if (previousWantedFields == null)