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;
22 * Resource Type specific filters.
24 * Stores the filter string per type basis, and the loads the stored filter when object with the same type is encountered.
26 * @author Marko Luukkainen <marko.luukkainen@semantum.fi>
29 public class TypeContextFilterArea extends RootFilterArea implements Widget{
30 String preferencePrefix;
35 * Creates new Filter Area.
37 * @param queryProcessor
40 * @param id unique id of the UI widget. The id is used to generate preference store keys to store filters.
43 public TypeContextFilterArea(GraphExplorer explorer, FilterSelectionRequestQueryProcessor queryProcessor, WidgetSupport support,
44 Composite parent, String id, int style) {
45 super(explorer, queryProcessor, parent, style);
46 this.preferencePrefix = id +"_TypeFilter_";
47 support.register(this);
52 public void setInput(ISessionContext context, Object input) {
53 final Resource resource = org.simantics.utils.ui.AdaptionUtils.adaptToSingle(input, Resource.class);
56 context.getSession().asyncRequest(new ReadRequest() {
59 public void run(ReadGraph graph) throws DatabaseException {
60 setInput(graph, resource);
65 public void setInput(ReadGraph graph, Resource resource) throws DatabaseException{
66 Resource type = getType(graph, resource);
67 final String typeUri = type == null ? null : graph.getPossibleURI(type);
68 if (typeUri != null) {
69 Display.getDefault().asyncExec(new Runnable() {
78 protected Resource getType(ReadGraph graph, Resource resource) throws DatabaseException {
79 Layer0 L0 = Layer0.getInstance(graph);
80 Resource type = graph.getPossibleType(resource, L0.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,"");
100 } catch (IOException e) {
105 private synchronized void load(String typeUri) {
106 this.typeUri = typeUri;
107 ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
108 String filter = store.getString(preferencePrefix+typeUri);
109 if (filter != null && filter.length() > 0) {