X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FTypeContextFilterArea.java;fp=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FTypeContextFilterArea.java;h=72ee0fffbbbefac02d4467b3c55275d8dbbd86a9;hp=0000000000000000000000000000000000000000;hb=ad09d0837dcd03c6925ca1154d70c930aa622c9c;hpb=aa5e7bb5d56b050cfbd2e4a5ab2cc4fdfe40850b 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 index 000000000..72ee0fffb --- /dev/null +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java @@ -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 + * + */ +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); + } + } + +}