]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java
Type specific graph explorer filter area
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / TypeContextFilterArea.java
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);
+               }
+       }
+
+}