]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java
Added possibility to directly schedule filter setting type URI
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / TypeContextFilterArea.java
index 72ee0fffbbbefac02d4467b3c55275d8dbbd86a9..299b96c1c4ad87315ddc0397153008dc69459dcf 100644 (file)
@@ -17,6 +17,9 @@ import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.management.ISessionContext;
 import org.simantics.layer0.Layer0;
+import org.simantics.utils.ui.SWTUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Resource Type specific filters. 
@@ -26,9 +29,12 @@ import org.simantics.layer0.Layer0;
  * @author Marko Luukkainen <marko.luukkainen@semantum.fi>
  *
  */
-public class TypeContextFilterArea extends RootFilterArea implements Widget{
+public class TypeContextFilterArea extends RootFilterArea implements Widget {
+
+       private static final Logger LOGGER = LoggerFactory.getLogger(TypeContextFilterArea.class);
+
        String preferencePrefix;
-       
+
        String typeUri;
 
        /**
@@ -43,7 +49,7 @@ public class TypeContextFilterArea extends RootFilterArea implements Widget{
        public TypeContextFilterArea(GraphExplorer explorer, FilterSelectionRequestQueryProcessor queryProcessor, WidgetSupport support,
                        Composite parent, String id, int style) {
                super(explorer, queryProcessor, parent, style);
-               this.preferencePrefix = id +"_TypeFilter_";
+               this.preferencePrefix = id +"_TypeFilter_"; //$NON-NLS-1$
                support.register(this);
                
        }
@@ -54,7 +60,6 @@ public class TypeContextFilterArea extends RootFilterArea implements Widget{
                if (resource == null)
                        return;
                context.getSession().asyncRequest(new ReadRequest() {
-                       
                        @Override
                        public void run(ReadGraph graph) throws DatabaseException {
                                setInput(graph, resource);
@@ -64,23 +69,18 @@ public class TypeContextFilterArea extends RootFilterArea implements Widget{
        
        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);
-                               }
-                       });
-               }
+               String typeUri = type == null ? null : graph.getPossibleURI(type);
+               setTypeURI(typeUri);
+       }
+
+       public void setTypeURI(String typeUri) {
+               if (typeUri != null)
+                       SWTUtils.asyncExec(Display.getDefault(), () -> 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;
+               return graph.getPossibleType(resource, Layer0.getInstance(graph).Entity);
        }
-       
 
        @Override
        protected synchronized void applyFilter(NodeContext context, String filter, boolean updateUI) {
@@ -94,11 +94,12 @@ public class TypeContextFilterArea extends RootFilterArea implements Widget{
                if (filter != null && filter.length() > 0)
                        store.setValue(preferencePrefix+typeUri, filter);
                else
-                       store.setValue(preferencePrefix+typeUri,"");
+                       store.setValue(preferencePrefix+typeUri, ""); //$NON-NLS-1$
                try {
                        store.save();
                } catch (IOException e) {
-                       
+                       LOGGER.error("Failed to save filter preference '{}' for type-contextual filter area with type URI {}", //$NON-NLS-1$
+                                       filter, typeUri, e);
                }
        }
        
@@ -107,7 +108,7 @@ public class TypeContextFilterArea extends RootFilterArea implements Widget{
                ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
                String filter = store.getString(preferencePrefix+typeUri);
                if (filter != null && filter.length() > 0) {
-                       setFilter(filter);
+                       applyFilter(filter);
                }
        }