From ad09d0837dcd03c6925ca1154d70c930aa622c9c Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Tue, 2 Oct 2018 12:15:16 +0300 Subject: [PATCH] Type specific graph explorer filter area gitlab #140 Change-Id: I62c0323cabfcf448f037f7cf3cc4fd3f7d2b65d6 --- .../simantics/browsing/ui/swt/FilterArea.java | 90 ++++++++------ .../ui/swt/TypeContextFilterArea.java | 114 ++++++++++++++++++ 2 files changed, 168 insertions(+), 36 deletions(-) create mode 100644 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/FilterArea.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/FilterArea.java index 0e8ddd63d..27c32abc0 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/FilterArea.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/FilterArea.java @@ -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 modCount = new HashMap(); - + @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 modCount = new HashMap(); + + + 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 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); + } + } + +} -- 2.43.2