]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TypeContextFilterArea.java
Assertion template for L0.SCLValue
[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
21 /**
22  * Resource Type specific filters. 
23  * 
24  * Stores the filter string per type basis, and the loads the stored filter when object with the same type is encountered.
25  * 
26  * @author Marko Luukkainen <marko.luukkainen@semantum.fi>
27  *
28  */
29 public class TypeContextFilterArea extends RootFilterArea implements Widget{
30         String preferencePrefix;
31         
32         String typeUri;
33
34         /**
35          * Creates new Filter Area.
36          * @param explorer
37          * @param queryProcessor
38          * @param support
39          * @param parent
40          * @param id unique id of the UI widget. The id is used to generate preference store keys to store filters.
41          * @param style
42          */
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);
48                 
49         }
50         
51         @Override
52         public void setInput(ISessionContext context, Object input) {
53                 final Resource resource = org.simantics.utils.ui.AdaptionUtils.adaptToSingle(input, Resource.class);
54                 if (resource == null)
55                         return;
56                 context.getSession().asyncRequest(new ReadRequest() {
57                         
58                         @Override
59                         public void run(ReadGraph graph) throws DatabaseException {
60                                 setInput(graph, resource);
61                         }
62                 });
63         }
64         
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() {
70                                 @Override
71                                 public void run() {
72                                         load(typeUri);
73                                 }
74                         });
75                 }
76         }
77
78         protected Resource getType(ReadGraph graph, Resource resource) throws DatabaseException {
79                 Layer0 L0 = Layer0.getInstance(graph);
80                 Resource type = graph.getPossibleType(resource, L0.Entity);
81                 return type;
82         }
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,"");
98                 try {
99                         store.save();
100                 } catch (IOException e) {
101                         
102                 }
103         }
104         
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) {
110                         setFilter(filter);
111                 }
112         }
113
114 }