1 package org.simantics.browsing.ui.swt;
3 import java.io.IOException;
5 import org.eclipse.core.runtime.preferences.InstanceScope;
6 import org.eclipse.swt.widgets.Composite;
7 import org.eclipse.swt.widgets.Display;
8 import org.eclipse.ui.preferences.ScopedPreferenceStore;
9 import org.simantics.browsing.ui.GraphExplorer;
10 import org.simantics.browsing.ui.NodeContext;
11 import org.simantics.browsing.ui.common.processors.FilterSelectionRequestQueryProcessor;
12 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
13 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.management.ISessionContext;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.utils.ui.SWTUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Resource Type specific filters.
27 * Stores the filter string per type basis, and the loads the stored filter when object with the same type is encountered.
29 * @author Marko Luukkainen <marko.luukkainen@semantum.fi>
32 public class TypeContextFilterArea extends RootFilterArea implements Widget {
34 private static final Logger LOGGER = LoggerFactory.getLogger(TypeContextFilterArea.class);
36 String preferencePrefix;
41 * Creates new Filter Area.
43 * @param queryProcessor
46 * @param id unique id of the UI widget. The id is used to generate preference store keys to store filters.
49 public TypeContextFilterArea(GraphExplorer explorer, FilterSelectionRequestQueryProcessor queryProcessor, WidgetSupport support,
50 Composite parent, String id, int style) {
51 super(explorer, queryProcessor, parent, style);
52 this.preferencePrefix = id +"_TypeFilter_"; //$NON-NLS-1$
53 support.register(this);
58 public void setInput(ISessionContext context, Object input) {
59 final Resource resource = org.simantics.utils.ui.AdaptionUtils.adaptToSingle(input, Resource.class);
62 context.getSession().asyncRequest(new ReadRequest() {
64 public void run(ReadGraph graph) throws DatabaseException {
65 setInput(graph, resource);
70 public void setInput(ReadGraph graph, Resource resource) throws DatabaseException{
71 Resource type = getType(graph, resource);
72 String typeUri = type == null ? null : graph.getPossibleURI(type);
76 public void setTypeURI(String typeUri) {
78 SWTUtils.asyncExec(Display.getDefault(), () -> load(typeUri));
81 protected Resource getType(ReadGraph graph, Resource resource) throws DatabaseException {
82 return graph.getPossibleType(resource, Layer0.getInstance(graph).Entity);
86 protected synchronized void applyFilter(NodeContext context, String filter, boolean updateUI) {
87 super.applyFilter(context, filter,updateUI);
89 store(typeUri, filter);
92 private synchronized void store(String typeUri, String filter) {
93 ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
94 if (filter != null && filter.length() > 0)
95 store.setValue(preferencePrefix+typeUri, filter);
97 store.setValue(preferencePrefix+typeUri, ""); //$NON-NLS-1$
100 } catch (IOException e) {
101 LOGGER.error("Failed to save filter preference '{}' for type-contextual filter area with type URI {}", //$NON-NLS-1$
106 private synchronized void load(String typeUri) {
107 this.typeUri = typeUri;
108 ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
109 String filter = store.getString(preferencePrefix+typeUri);
110 if (filter != null && filter.length() > 0) {