]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / TypeContextFilterArea.java
1 package org.simantics.browsing.ui.swt;
2
3 import java.io.IOException;
4
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;
23
24 /**
25  * Resource Type specific filters. 
26  * 
27  * Stores the filter string per type basis, and the loads the stored filter when object with the same type is encountered.
28  * 
29  * @author Marko Luukkainen <marko.luukkainen@semantum.fi>
30  *
31  */
32 public class TypeContextFilterArea extends RootFilterArea implements Widget {
33
34         private static final Logger LOGGER = LoggerFactory.getLogger(TypeContextFilterArea.class);
35
36         String preferencePrefix;
37
38         String typeUri;
39
40         /**
41          * Creates new Filter Area.
42          * @param explorer
43          * @param queryProcessor
44          * @param support
45          * @param parent
46          * @param id unique id of the UI widget. The id is used to generate preference store keys to store filters.
47          * @param style
48          */
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);
54                 
55         }
56         
57         @Override
58         public void setInput(ISessionContext context, Object input) {
59                 final Resource resource = org.simantics.utils.ui.AdaptionUtils.adaptToSingle(input, Resource.class);
60                 if (resource == null)
61                         return;
62                 context.getSession().asyncRequest(new ReadRequest() {
63                         @Override
64                         public void run(ReadGraph graph) throws DatabaseException {
65                                 setInput(graph, resource);
66                         }
67                 });
68         }
69         
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);
73                 setTypeURI(typeUri);
74         }
75
76         public void setTypeURI(String typeUri) {
77                 if (typeUri != null)
78                         SWTUtils.asyncExec(Display.getDefault(), () -> load(typeUri));
79         }
80
81         protected Resource getType(ReadGraph graph, Resource resource) throws DatabaseException {
82                 return graph.getPossibleType(resource, Layer0.getInstance(graph).Entity);
83         }
84
85         @Override
86         protected synchronized void applyFilter(NodeContext context, String filter, boolean updateUI) {
87                 super.applyFilter(context, filter,updateUI);
88                 if (typeUri != null)
89                         store(typeUri, filter);
90         }
91         
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);
96                 else
97                         store.setValue(preferencePrefix+typeUri, ""); //$NON-NLS-1$
98                 try {
99                         store.save();
100                 } catch (IOException e) {
101                         LOGGER.error("Failed to save filter preference '{}' for type-contextual filter area with type URI {}", //$NON-NLS-1$
102                                         filter, typeUri, e);
103                 }
104         }
105         
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) {
111                         applyFilter(filter);
112                 }
113         }
114
115 }