Type specific graph explorer filter area
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / FilterArea.java
index 0e8ddd63d2b86c86c31459d8d38810bb45afd484..27c32abc03190dc6e43ad1bef26737d95ed5140a 100644 (file)
@@ -236,52 +236,70 @@ public class FilterArea extends Composite implements IFocusable, IFilterArea, IF
         explorer.select(null);
         //textChanged();
     }
-
+    
     protected void addTextModifyListener() {
         filterText.addModifyListener(new ModifyListener() {
 
-            Map<NodeContext, AtomicInteger> modCount = new HashMap<NodeContext, AtomicInteger>();
-
+            
             @Override
             public void modifyText(ModifyEvent e) {
-
-                final NodeContext context = getFilteredNode();
-                if (context == null)
-                    return;
-
                 final String filter = filterText.getText();
                 //System.out.println("Scheduling setFilter(" + context + ", " + filter + ")");
-
-                AtomicInteger i = modCount.get(context);
-                if (i == null)
-                    modCount.put(context, new AtomicInteger());
-                final AtomicInteger counter = modCount.get(context);
-                final int count = counter.incrementAndGet();
-
-                ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
-                    @Override
-                    public void run() {
-                        int newCount = counter.get();
-                        if (newCount != count)
-                            return;
-                        //System.out.println("schedule setFilter(" + context + ", " + filter + ")");
-                        modCount.remove(context);
-                        if (isDisposed())
-                            return;
-                        ThreadUtils.asyncExec(SWTThread.getThreadAccess(getDisplay()), new Runnable() {
-                            @Override
-                            public void run() {
-                                if (isDisposed())
-                                    return;
-                                //System.out.println("queryProcessor.setFilter(" + context + ", " + filter + ")");
-                                queryProcessor.setFilter(context, filter.isEmpty() ? null : filter);
-                            }
-                        });
-                    }
-                }, FILTER_DELAY, TimeUnit.MILLISECONDS);
+                setFilter(filter, false);
             }
         });
     }
+    
+    private Map<NodeContext, AtomicInteger> modCount = new HashMap<NodeContext, AtomicInteger>();
+
+    
+    public void setFilter(String filter) {
+       setFilter(filter, true);
+    }
+    
+    protected void setFilter(String filter, boolean updateUI) {
+       final NodeContext context = getFilteredNode();
+        if (context == null)
+            return;
+       
+        AtomicInteger i = modCount.get(context);
+         if (i == null)
+             modCount.put(context, new AtomicInteger());
+         final AtomicInteger counter = modCount.get(context);
+         final int count = counter.incrementAndGet();
+
+         ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
+             @Override
+             public void run() {
+                 int newCount = counter.get();
+                 if (newCount != count)
+                     return;
+                 //System.out.println("schedule setFilter(" + context + ", " + filter + ")");
+                 modCount.remove(context);
+                 if (isDisposed())
+                     return;
+                 ThreadUtils.asyncExec(SWTThread.getThreadAccess(getDisplay()), new Runnable() {
+                     @Override
+                     public void run() {
+                         if (isDisposed())
+                             return;
+                         applyFilter(context, filter, updateUI);
+                     }
+                 });
+             }
+         }, FILTER_DELAY, TimeUnit.MILLISECONDS);
+       
+    }
+    
+    protected void applyFilter(NodeContext context, String filter, boolean updateUI) {
+        if (updateUI) {
+                String current = filterText.getText();
+                if (!current.equals(filter))
+                        filterText.setText(filter);
+        }
+         //System.out.println("queryProcessor.setFilter(" + context + ", " + filter + ")");
+         queryProcessor.setFilter(context, filter.isEmpty() ? null : filter);
+    }
 
     protected void addExplorerSelectionListener() {
         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);