Type specific graph explorer filter area 68/2268/2
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 2 Oct 2018 09:15:16 +0000 (12:15 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 2 Oct 2018 09:29:17 +0000 (09:29 +0000)
gitlab #140

Change-Id: I62c0323cabfcf448f037f7cf3cc4fd3f7d2b65d6

bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/FilterArea.java
bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java [new file with mode: 0644]

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);
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java
new file mode 100644 (file)
index 0000000..72ee0ff
--- /dev/null
@@ -0,0 +1,114 @@
+package org.simantics.browsing.ui.swt;
+
+import java.io.IOException;
+
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.simantics.browsing.ui.GraphExplorer;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.common.processors.FilterSelectionRequestQueryProcessor;
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.layer0.Layer0;
+
+/**
+ * Resource Type specific filters. 
+ * 
+ * Stores the filter string per type basis, and the loads the stored filter when object with the same type is encountered.
+ * 
+ * @author Marko Luukkainen <marko.luukkainen@semantum.fi>
+ *
+ */
+public class TypeContextFilterArea extends RootFilterArea implements Widget{
+       String preferencePrefix;
+       
+       String typeUri;
+
+       /**
+        * Creates new Filter Area.
+        * @param explorer
+        * @param queryProcessor
+        * @param support
+        * @param parent
+        * @param id unique id of the UI widget. The id is used to generate preference store keys to store filters.
+        * @param style
+        */
+       public TypeContextFilterArea(GraphExplorer explorer, FilterSelectionRequestQueryProcessor queryProcessor, WidgetSupport support,
+                       Composite parent, String id, int style) {
+               super(explorer, queryProcessor, parent, style);
+               this.preferencePrefix = id +"_TypeFilter_";
+               support.register(this);
+               
+       }
+       
+       @Override
+       public void setInput(ISessionContext context, Object input) {
+               final Resource resource = org.simantics.utils.ui.AdaptionUtils.adaptToSingle(input, Resource.class);
+               if (resource == null)
+                       return;
+               context.getSession().asyncRequest(new ReadRequest() {
+                       
+                       @Override
+                       public void run(ReadGraph graph) throws DatabaseException {
+                               setInput(graph, resource);
+                       }
+               });
+       }
+       
+       public void setInput(ReadGraph graph, Resource resource) throws DatabaseException{
+               Resource type = getType(graph, resource);
+               final String typeUri = type == null ? null : graph.getPossibleURI(type);
+               if (typeUri != null) {
+                       Display.getDefault().asyncExec(new Runnable() {
+                               @Override
+                               public void run() {
+                                       load(typeUri);
+                               }
+                       });
+               }
+       }
+
+       protected Resource getType(ReadGraph graph, Resource resource) throws DatabaseException {
+               Layer0 L0 = Layer0.getInstance(graph);
+               Resource type = graph.getPossibleType(resource, L0.Entity);
+               return type;
+       }
+       
+
+       @Override
+       protected synchronized void applyFilter(NodeContext context, String filter, boolean updateUI) {
+               super.applyFilter(context, filter,updateUI);
+               if (typeUri != null)
+                       store(typeUri, filter);
+       }
+       
+       private synchronized void store(String typeUri, String filter) {
+               ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+               if (filter != null && filter.length() > 0)
+                       store.setValue(preferencePrefix+typeUri, filter);
+               else
+                       store.setValue(preferencePrefix+typeUri,"");
+               try {
+                       store.save();
+               } catch (IOException e) {
+                       
+               }
+       }
+       
+       private synchronized void load(String typeUri) {
+               this.typeUri = typeUri;
+               ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+               String filter = store.getString(preferencePrefix+typeUri);
+               if (filter != null && filter.length() > 0) {
+                       setFilter(filter);
+               }
+       }
+
+}