]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTExplorer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTExplorer.java
1 package org.simantics.views.swt.client.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.eclipse.jface.viewers.IPostSelectionProvider;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.ISelectionChangedListener;
12 import org.eclipse.jface.viewers.ISelectionProvider;
13 import org.eclipse.jface.viewers.SelectionChangedEvent;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.Listener;
18 import org.eclipse.ui.IWorkbenchSite;
19 import org.simantics.browsing.ui.Column;
20 import org.simantics.browsing.ui.StatePersistor;
21 import org.simantics.browsing.ui.swt.PassThruInputSource;
22 import org.simantics.browsing.ui.swt.widgets.ModelBrowser;
23 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
24 import org.simantics.scl.runtime.function.Function1;
25 import org.simantics.views.ViewUtils.ColumnBean;
26 import org.simantics.views.ViewUtils.LayoutBean;
27 import org.simantics.views.swt.client.base.SWTViewUtils;
28 import org.simantics.views.swt.client.base.SingleSWTViewNode;
29
30 public class SWTExplorer extends SingleSWTViewNode<ModelBrowser> {
31         
32         private static final long serialVersionUID = 7932335224632082902L;
33         
34         public LayoutBean layout;
35         public String browseContext;
36         public String contextMenuId;
37         public String uiContext;
38         public Function1<Object, Boolean> selectionListener;
39         public Object input;
40         public List<ColumnBean> columns;
41         public List<ColumnBean> editingColumns;
42         public Boolean columnsVisible;
43         public Boolean displayFilter;
44         public Boolean publishSelection;
45         public Boolean useNodeBrowseContexts;
46         public Boolean useNodeActionContexts;
47         public StatePersistor persistor;
48         
49         public ISelection lastSelection;
50         
51         @Override
52         public void createControls(Composite parent) {
53
54                 if(browseContext == null) return;
55                 
56                 final IWorkbenchSite site = getSite();
57
58                 Map<String, Object> args = new HashMap<String, Object>();
59                 args.put("displaySelectors", Boolean.FALSE);
60                 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
61                 if(useNodeBrowseContexts != null && useNodeBrowseContexts) {
62                         args.put("useNodeBrowseContexts", true);
63                 }
64                 if(useNodeActionContexts != null && useNodeActionContexts) {
65                         args.put("useNodeActionContexts", true);
66                 }
67
68                 control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
69                 control.setContextMenuId(contextMenuId);
70                 if (uiContext != null) {
71                         control.setUiContexts(Collections.singleton(uiContext));
72                 }
73                 
74                 control.setStatePersistor(persistor);
75                 
76                 control.finish();
77
78                 setProperties();
79                 
80                 control.setInputSource(PassThruInputSource.INSTANCE);
81
82                 ISelectionProvider sp = (ISelectionProvider)control.getExplorer().getAdapter(ISelectionProvider.class);
83                 sp.addSelectionChangedListener(new ISelectionChangedListener() {
84                         
85                         @Override
86                         public void selectionChanged(SelectionChangedEvent event) {
87                                 if (site != null) {
88                                         ISelectionProvider sp = site.getSelectionProvider();
89                                         if (sp != null) {
90                                                 sp.setSelection(event.getSelection());
91                                         }
92                                 }
93                         }
94                         
95                 });
96                 
97                 IPostSelectionProvider psp = (IPostSelectionProvider)control.getExplorer().getAdapter(IPostSelectionProvider.class);
98                 psp.addPostSelectionChangedListener(new ISelectionChangedListener() {
99                         
100                         @Override
101                         public void selectionChanged(SelectionChangedEvent event) {
102                                 
103 //                              ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
104                                 if (site != null) {
105                                         ISelectionProvider sp = site.getSelectionProvider();
106                                         if (sp != null) {
107                                                 sp.setSelection(event.getSelection());
108                                         }
109                                 }
110                                 
111                         }
112                         
113                 });
114                 
115                 control.addListenerToControl(SWT.Selection, new Listener() {
116
117                         @Override
118                         public void handleEvent(Event event) {
119                                 
120                                 if(selectionListener != null)
121                                         selectionListener.apply(event);
122                                 
123                                 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
124                                 // [Tuukka@2012-04-08] Disabled this because it was causing
125                                 // horrible selection feedback effects in the Model Browser
126                                 // view that is using it. It causes the browser to react to
127                                 // external selections which were initially published as its own
128                                 // with a delay.
129                                 //System.out.println("selection: " + selection);
130 //                              if(publishSelection != null && publishSelection) {
131 //                                      if (site != null) {
132 //                                              ISelectionProvider sp = site.getSelectionProvider();
133 //                                              if (sp != null) {
134 //                                                      sp.setSelection(selection);
135 //                                              }
136 //                                      }
137 //                              }
138                                 
139                                 propertyCallback.apply("selection", selection);
140                                 
141                                 lastSelection = selection;
142                                 
143                         }
144                         
145                 });
146                         
147         }
148
149         public void synchronizeColumnsVisible(Boolean columnsVisible) {
150                 if(columnsVisible != null) control.setColumnsVisible(columnsVisible);
151         }
152         
153         public void synchronizeDisplayFilter(Boolean displayFilter) {
154 //              if(displayFilter != null) control.setColumnsVisible(displayFilter);
155         }
156
157         public void synchronizePublishSelection(Boolean publishSelection) {
158         }
159
160         public void synchronizeInput(Object input) {
161                 if(input != null) control.setInput(input, false);
162         }
163         
164         public void synchronizeBrowseContext(String browseContext) {
165         }
166         
167         public void synchronizeContextMenuId(String contextMenuId) {
168         }
169
170         public void synchronizeSelectionListener(Function1<Object, Boolean> selectionListener) {
171         }
172
173         public void synchronizePersistor(StatePersistor persistor) {
174         }
175
176         public void synchronizeColumns(ArrayList<ColumnBean> columns) {
177                 if(columns != null) {
178                         Column[] cols = new Column[columns.size()];
179                         for(int i=0;i<columns.size();i++) cols[i] = SWTViewUtils.toColumn(columns.get(i));
180                         control.setColumns(cols);
181                 }
182         }
183
184         public void synchronizeEditingColumns(ArrayList<ColumnBean> columns) {
185                 if(columns != null) {
186                         String[] ecs = new String[columns.size()];
187                         for(int i=0;i<columns.size();i++) ecs[i] = columns.get(i).key;
188                         control.setEditingColumn(ecs);
189                 }
190         }
191
192         final public void synchronizeLayout(LayoutBean layout) {
193         }
194         
195 }