]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/SimanticsView.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / SimanticsView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.views.swt;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.resource.LocalResourceManager;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.dnd.DND;
26 import org.eclipse.swt.dnd.DragSource;
27 import org.eclipse.swt.dnd.DragSourceListener;
28 import org.eclipse.swt.dnd.FileTransfer;
29 import org.eclipse.swt.dnd.Transfer;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.ui.IMemento;
33 import org.eclipse.ui.ISelectionListener;
34 import org.eclipse.ui.IViewSite;
35 import org.eclipse.ui.IWorkbenchSite;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.contexts.IContextService;
38 import org.eclipse.ui.part.ViewPart;
39 import org.simantics.browsing.ui.BuiltinKeys;
40 import org.simantics.browsing.ui.Column;
41 import org.simantics.browsing.ui.GraphExplorer;
42 import org.simantics.browsing.ui.NodeContext;
43 import org.simantics.browsing.ui.common.ColumnKeys;
44 import org.simantics.browsing.ui.common.node.IDropTargetNode;
45 import org.simantics.browsing.ui.graph.impl.GraphInputSources;
46 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
47 import org.simantics.browsing.ui.swt.ContextMenuInitializer;
48 import org.simantics.browsing.ui.swt.DefaultKeyListener;
49 import org.simantics.browsing.ui.swt.DefaultMouseListener;
50 import org.simantics.browsing.ui.swt.DefaultSelectionDataResolver;
51 import org.simantics.browsing.ui.swt.GraphExplorerFactory;
52 import org.simantics.browsing.ui.swt.IContextMenuInitializer;
53 import org.simantics.browsing.ui.swt.ViewArgumentUtils;
54 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
55 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
56 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
57 import org.simantics.db.Disposable;
58 import org.simantics.db.management.ISessionContext;
59 import org.simantics.db.management.ISessionContextChangedListener;
60 import org.simantics.db.management.ISessionContextProvider;
61 import org.simantics.db.management.SessionContextChangedEvent;
62 import org.simantics.project.ProjectKeys;
63 import org.simantics.ui.SimanticsUI;
64 import org.simantics.ui.dnd.LocalObjectTransfer;
65 import org.simantics.ui.dnd.LocalSelectionDragSourceListener;
66 import org.simantics.ui.dnd.NoImageDragSourceEffect;
67 import org.simantics.ui.workbench.IPropertyPage;
68 import org.simantics.utils.ObjectUtils;
69 import org.simantics.utils.datastructures.Function;
70 import org.simantics.utils.datastructures.disposable.DisposeState;
71 import org.simantics.utils.datastructures.hints.HintContext;
72 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
73 import org.simantics.utils.datastructures.hints.HintTracker;
74 import org.simantics.utils.datastructures.hints.IHintContext;
75 import org.simantics.utils.datastructures.hints.IHintContext.Key;
76 import org.simantics.utils.datastructures.hints.IHintListener;
77 import org.simantics.utils.datastructures.hints.IHintObservable;
78 import org.simantics.utils.datastructures.hints.IHintTracker;
79 import org.simantics.utils.ui.LayoutUtils;
80
81 /**
82  * @author Antti Villberg
83  * @deprecated in favor of org.simantics.views.swt.ModelledView
84  */
85 public abstract class SimanticsView extends ViewPart {
86
87     private final WidgetSupportImpl          widgetSupport = createSupport();
88
89     protected IHintContext                   factoryHints = new HintContext();
90
91     protected LocalResourceManager           resourceManager;
92
93     protected ISelectionListener             workbenchSelectionListener;
94
95     protected Composite                      parent;
96
97     private Map<String, String>              args;
98
99     private ISessionContextProvider          contextProvider;
100
101     private ISessionContext                  sessionContext;
102
103     protected IMemento                       memento;
104
105     private IHintTracker                     sessionContextTracker = new SessionContextProjectTracker();
106
107     private SessionContextInputSource        inputSource           = GraphInputSources.projectSource();
108
109     private DisposeState                     disposeState          = DisposeState.Alive;
110
111     protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
112         @Override
113         public void sessionContextChanged(SessionContextChangedEvent event) {
114             sessionContext = event.getNewValue();
115             sessionContextTracker.track(sessionContext);
116         }
117     };
118
119     protected Set<String> getBrowseContexts() {
120         return Collections.emptySet();
121     }
122
123     protected WidgetSupportImpl createSupport() {
124         return new WidgetSupportImpl();
125     }
126     
127     abstract protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support);
128
129     protected void activateUiContexts() {
130         Collection<String> contexts = getUiContexts();
131         if (!contexts.isEmpty()) {
132             IContextService cs = (IContextService) getSite().getService(IContextService.class);
133             for (String context : contexts)
134                 cs.activateContext(context);
135         }
136     }
137
138     protected Transfer[] getAcceptedDataTypes() {
139         return new Transfer[] {  LocalObjectTransfer.getTransfer(), FileTransfer.getInstance() };
140     }
141
142     protected void handleDrop(Object data, NodeContext target) {
143         if (target != null) {
144             Object input = target.getConstant(BuiltinKeys.INPUT);
145             //System.out.println("DROPPED " + data + " ON " + target);
146             if (input instanceof IDropTargetNode)
147                 ((IDropTargetNode) input).drop(data);
148         }
149     }
150
151     protected Object createDragSource(GraphExplorer explorer) {
152         ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
153
154         DragSourceListener listener = new LocalSelectionDragSourceListener(selectionProvider);
155
156         Control control = explorer.getControl();
157         DragSource source = new DragSource(control, DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
158         source.setTransfer(new Transfer[] {LocalObjectTransfer.getTransfer()});
159         source.addDragListener(listener);
160         source.setDragSourceEffect(new NoImageDragSourceEffect(control));
161
162         return listener;
163     }
164
165     /**
166      * @return the set of <code>org.eclipse.ui.context</code> contexts to
167      *         activate for this view site
168      */
169     protected Set<String> getUiContexts() {
170         return Collections.emptySet();
171     }
172
173     /**
174      * The default hint tracker that will be active if
175      * {@link SimanticsViewBase#setSessionContextTracker(IHintTracker) is
176      * not called.
177      */
178     public class SessionContextProjectTracker extends HintTracker {
179         public SessionContextProjectTracker() {
180             IHintListener activeProjectListener = new HintListenerAdapter() {
181                 @Override
182                 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
183                     applySessionContext(getSessionContext());
184                 }
185             };
186             addKeyHintListener(ProjectKeys.KEY_PROJECT, activeProjectListener);
187         }
188     }
189
190     protected void setSessionContextTracker(IHintTracker tracker) {
191         this.sessionContextTracker = tracker;
192     }
193
194     public void setInputSource(SessionContextInputSource source) {
195         this.inputSource = source;
196     }
197
198     protected final SessionContextInputSource getInputSource() {
199         return inputSource;
200     }
201
202     protected Map<String, String> getViewArguments() {
203         return args;
204     }
205
206     protected DisposeState getDisposeState() {
207         return disposeState;
208     }
209
210     public ISessionContext getSessionContext() {
211         return sessionContext;
212     }
213
214     public ISessionContextProvider getSessionContextProvider() {
215         return contextProvider;
216     }
217
218     @Override
219     public void createPartControl(Composite parent) {
220         this.parent = parent;
221         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()));
222
223         contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
224
225         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
226
227         setWorkbenchListeners();
228         activateUiContexts();
229
230         createControls(parent, getSite(), getSessionContext(), widgetSupport);
231
232         attachToSession();
233
234     }
235
236     /**
237      * Invoked when this viewpart is disposed. Unhooks the view from its
238      * ISessionContextProvider. Overriding is allowed but super.dispose() must
239      * be called.
240      * 
241      * @see org.eclipse.ui.part.WorkbenchPart#dispose()
242      */
243     @Override
244     public void dispose() {
245         removeWorkbenchListeners();
246         disposeState = DisposeState.Disposing;
247         try {
248             if (inputSource instanceof Disposable) {
249                 ((Disposable) inputSource).dispose();
250             }
251             //System.out.println(this + ".GraphExplorerViewBase.dispose()");
252             if (contextProvider != null) {
253                 contextProvider.removeContextChangedListener(contextChangeListener);
254                 contextProvider = null;
255             }
256             sessionContextTracker.untrack();
257             resourceManager.dispose();
258             if (widgetSupport instanceof Disposable)
259                 ((Disposable) widgetSupport).dispose();
260             resourceManager = null;
261             args = null;
262             sessionContext = null;
263             parent = null;
264             super.dispose();
265         } finally {
266             disposeState = DisposeState.Disposed;
267         }
268     }
269
270     @Override
271     public void setFocus() {
272     }
273
274     @Override
275     public void init(IViewSite site) throws PartInitException {
276         super.init(site);
277         this.args = ViewArgumentUtils.parseViewArguments(this);
278     }
279
280     @Override
281     public void init(IViewSite site, IMemento memento) throws PartInitException {
282         super.init(site, memento);
283         this.args = ViewArgumentUtils.parseViewArguments(this);
284         this.memento = memento;
285     }
286
287     @Override
288     public void saveState(IMemento memento) {
289         if (this.memento != null) {
290             memento.putMemento(this.memento);
291         }
292 //        if (explorer != null)
293 //            explorer.saveState(memento);
294     }
295
296     protected void setWorkbenchListeners() {
297         if (workbenchSelectionListener == null) {
298
299 //            ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
300 //            getSite().setSelectionProvider(selectionProvider);
301
302             // Listen to the workbench selection also to propagate it to
303             // the explorer also.
304 //            workbenchSelectionListener = new DefaultExplorerSelectionListener(this, explorer);
305 //            getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(workbenchSelectionListener);
306         }
307     }
308
309     protected void removeWorkbenchListeners() {
310         // Remember to remove the installed workbench selection listener
311         if (workbenchSelectionListener != null) {
312             getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(workbenchSelectionListener);
313             workbenchSelectionListener = null;
314
315             getSite().setSelectionProvider(null);
316         }
317     }
318
319     protected final void attachToSession() {
320         // Track active ISessionContext changes
321         //contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
322         contextProvider.addContextChangedListener(contextChangeListener);
323
324         // Start tracking the current session context for input changes.
325         // This will/must cause applySessionContext to get called.
326         // Doing the applySessionContext initialization this way
327         // instead of directly calling it will also make sure that
328         // applySessionContext is only called once when first initialized,
329         // and not twice like with the direct invocation.
330         this.sessionContext = contextProvider.getSessionContext();
331         sessionContextTracker.track(sessionContext);
332     }
333
334     // /////////////////////////////////////////////////////////////////////////
335     // Override / implement these:
336
337     protected Column[] getColumns() {
338         return null;
339     }
340
341     /**
342      * Override this method to add controls to the view part. This is invoked
343      * before attaching the view part to a database session.
344      * 
345      * @param parent
346      */
347     protected void createControls(Composite parent) {
348
349         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
350
351     }
352
353     /**
354      * Override this method and provide a proper context menu initializer if you
355      * want to have this base class initialize one for you.
356      * 
357      * @return the initializer to be used by {@link #createControls(Composite)}
358      */
359     protected IContextMenuInitializer getContextMenuInitializer() {
360         String contextMenuId = getContextMenuId();
361         if(contextMenuId != null) {
362             return new ContextMenuInitializer(contextMenuId);
363         } else {
364             return null;
365         }
366     }
367
368     protected String getContextMenuId() {
369         return null;
370     }
371
372     protected int getStyle() {
373         return SWT.MULTI;
374     }
375
376     /**
377      * @param parent
378      * @return
379      */
380     protected GraphExplorer createExplorerControl(Composite parent) {
381         return GraphExplorerFactory.getInstance()
382         .selectionDataResolver(new DefaultSelectionDataResolver())
383         .create(parent, getStyle());
384     }
385
386     /**
387      * Override to customize the addition of listeners a newly created
388      * GraphExplorer.
389      * 
390      * @param explorer
391      */
392     protected void addListeners(GraphExplorer explorer, IMenuManager menuManager) {
393         addSelectionInputListeners(explorer, menuManager);
394     }
395
396     protected void addSelectionInputListeners(GraphExplorer explorer, IMenuManager menuManager) {
397         // Consider ENTER presses to simulate mouse left button double clicks
398         explorer.addListener(new DefaultKeyListener(contextProvider, explorer, new Function<String[]>() {
399             @Override
400             public String[] execute(Object... obj) {
401                 return new String[] { getEditingColumn((NodeContext) obj[0]) };
402             }
403         }));
404         // Default double click handling
405         explorer.addListener(new DefaultMouseListener(explorer));
406     }
407
408     protected String getEditingColumn(NodeContext context) {
409         return ColumnKeys.SINGLE;
410     }
411
412     // Needed for preventing unnecessary re-initialization of the explorer with the same input.
413     private Object currentInput;
414
415     protected boolean isImportantInput(Object previousInput, Object input) {
416         return !ObjectUtils.objectEquals(previousInput, input);
417     }
418
419     /**
420      * Invoke this to reinitialize the explorer and reset its input. The input
421      * will be resolved from the specified ISessionContext based on the
422      * {@link SessionContextInputSource} that is currently in use. If the input
423      * is identical to the previous input, nothing will be done.
424      * 
425      * @param context
426      */
427     protected final boolean applySessionContext(ISessionContext context) {
428         // If control is not alive anymore, do nothing.
429         //System.out.println(this + "(SimanticsView).applySessionContext(context=" + context + ")");
430         if (disposeState != DisposeState.Alive)
431             return false;
432         
433         if(this.sessionContext == null && context == null)
434             return false;
435
436         this.sessionContext = context;
437         Object input = getInputSource().get(context);
438         //System.out.println(this + "(SimanticsView).applySessionContext(input=" + input + ")");
439         if (!isImportantInput(currentInput, input))
440             return false;
441
442 //        System.out.println(this + ": initializeExplorer(" + explorer + ", " + context + ")");
443 //        initializeExplorer(explorer, context);
444 //        System.out.println(this + ": setRoot(" + input + ")");
445 //        explorer.setRoot(input);
446
447         currentInput = input;
448         //System.out.println(this + "(SimanticsView).applySessionContext(new input=" + currentInput + ")");
449
450         widgetSupport.fireInput(context, input);
451
452         // Start tracking the session context.
453         //
454         // If this is not the same session that is currently tracked, it will
455         // cause IHintListeners of the sessionContextTracker to fire.
456         // For this we need the above input equality (identity) checking.
457         // This is here just to make sure that we are tracking the correct
458         // session context.
459         sessionContextTracker.track(sessionContext);
460
461         return true;
462     }
463     
464     @SuppressWarnings("unchecked")
465     <T extends Control> void searchControl(Control control, Class<T> c, Set<T> result) {
466         if(c.isInstance(control))
467                 result.add((T)control);
468         if(control instanceof Composite) {
469                 for(Control child : ((Composite)control).getChildren()) {
470                         searchControl(child, c, result);
471                 }
472         }
473     }
474
475     @SuppressWarnings("unchecked")
476     @Override
477     public <T> T getAdapter(Class<T> adapter) {
478
479         if(ISessionContextProvider.class == adapter)
480             return (T) getSessionContextProvider();
481         else if(IPropertyPage.class == adapter)
482             return (T) getPropertyPage();
483         else if(GraphExplorer.class == adapter) {
484                 Set<GraphExplorerComposite> composites = new HashSet<>();
485                 searchControl(parent, GraphExplorerComposite.class, composites);
486                 if(composites.size() == 1) {
487                         GraphExplorerComposite gec = composites.iterator().next(); 
488                         return (T) gec.getExplorer();
489                 }
490                 return null;
491         }
492
493         return super.getAdapter(adapter);
494
495     }
496
497     abstract protected IPropertyPage getPropertyPage();
498     
499 //    protected IPropertyPage getPropertyPage() {
500 //        return new StandardPropertyPage(getSite(), getBrowseContexts());
501 //    }
502
503 }