1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.views.swt;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
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;
82 * @author Antti Villberg
83 * @deprecated in favor of org.simantics.views.swt.ModelledView
85 public abstract class SimanticsView extends ViewPart {
87 private final WidgetSupportImpl widgetSupport = createSupport();
89 protected IHintContext factoryHints = new HintContext();
91 protected LocalResourceManager resourceManager;
93 protected ISelectionListener workbenchSelectionListener;
95 protected Composite parent;
97 private Map<String, String> args;
99 private ISessionContextProvider contextProvider;
101 private ISessionContext sessionContext;
103 protected IMemento memento;
105 private IHintTracker sessionContextTracker = new SessionContextProjectTracker();
107 private SessionContextInputSource inputSource = GraphInputSources.projectSource();
109 private DisposeState disposeState = DisposeState.Alive;
111 protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
113 public void sessionContextChanged(SessionContextChangedEvent event) {
114 sessionContext = event.getNewValue();
115 sessionContextTracker.track(sessionContext);
119 protected Set<String> getBrowseContexts() {
120 return Collections.emptySet();
123 protected WidgetSupportImpl createSupport() {
124 return new WidgetSupportImpl();
127 abstract protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support);
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);
138 protected Transfer[] getAcceptedDataTypes() {
139 return new Transfer[] { LocalObjectTransfer.getTransfer(), FileTransfer.getInstance() };
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);
151 protected Object createDragSource(GraphExplorer explorer) {
152 ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
154 DragSourceListener listener = new LocalSelectionDragSourceListener(selectionProvider);
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));
166 * @return the set of <code>org.eclipse.ui.context</code> contexts to
167 * activate for this view site
169 protected Set<String> getUiContexts() {
170 return Collections.emptySet();
174 * The default hint tracker that will be active if
175 * {@link SimanticsViewBase#setSessionContextTracker(IHintTracker) is
178 public class SessionContextProjectTracker extends HintTracker {
179 public SessionContextProjectTracker() {
180 IHintListener activeProjectListener = new HintListenerAdapter() {
182 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
183 applySessionContext(getSessionContext());
186 addKeyHintListener(ProjectKeys.KEY_PROJECT, activeProjectListener);
190 protected void setSessionContextTracker(IHintTracker tracker) {
191 this.sessionContextTracker = tracker;
194 public void setInputSource(SessionContextInputSource source) {
195 this.inputSource = source;
198 protected final SessionContextInputSource getInputSource() {
202 protected Map<String, String> getViewArguments() {
206 protected DisposeState getDisposeState() {
210 public ISessionContext getSessionContext() {
211 return sessionContext;
214 public ISessionContextProvider getSessionContextProvider() {
215 return contextProvider;
219 public void createPartControl(Composite parent) {
220 this.parent = parent;
221 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()));
223 contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
225 parent.setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
227 setWorkbenchListeners();
228 activateUiContexts();
230 createControls(parent, getSite(), getSessionContext(), widgetSupport);
237 * Invoked when this viewpart is disposed. Unhooks the view from its
238 * ISessionContextProvider. Overriding is allowed but super.dispose() must
241 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
244 public void dispose() {
245 removeWorkbenchListeners();
246 disposeState = DisposeState.Disposing;
248 if (inputSource instanceof Disposable) {
249 ((Disposable) inputSource).dispose();
251 //System.out.println(this + ".GraphExplorerViewBase.dispose()");
252 if (contextProvider != null) {
253 contextProvider.removeContextChangedListener(contextChangeListener);
254 contextProvider = null;
256 sessionContextTracker.untrack();
257 resourceManager.dispose();
258 if (widgetSupport instanceof Disposable)
259 ((Disposable) widgetSupport).dispose();
260 resourceManager = null;
262 sessionContext = null;
266 disposeState = DisposeState.Disposed;
271 public void setFocus() {
275 public void init(IViewSite site) throws PartInitException {
277 this.args = ViewArgumentUtils.parseViewArguments(this);
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;
288 public void saveState(IMemento memento) {
289 if (this.memento != null) {
290 memento.putMemento(this.memento);
292 // if (explorer != null)
293 // explorer.saveState(memento);
296 protected void setWorkbenchListeners() {
297 if (workbenchSelectionListener == null) {
299 // ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
300 // getSite().setSelectionProvider(selectionProvider);
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);
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;
315 getSite().setSelectionProvider(null);
319 protected final void attachToSession() {
320 // Track active ISessionContext changes
321 //contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
322 contextProvider.addContextChangedListener(contextChangeListener);
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);
334 // /////////////////////////////////////////////////////////////////////////
335 // Override / implement these:
337 protected Column[] getColumns() {
342 * Override this method to add controls to the view part. This is invoked
343 * before attaching the view part to a database session.
347 protected void createControls(Composite parent) {
349 parent.setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
354 * Override this method and provide a proper context menu initializer if you
355 * want to have this base class initialize one for you.
357 * @return the initializer to be used by {@link #createControls(Composite)}
359 protected IContextMenuInitializer getContextMenuInitializer() {
360 String contextMenuId = getContextMenuId();
361 if(contextMenuId != null) {
362 return new ContextMenuInitializer(contextMenuId);
368 protected String getContextMenuId() {
372 protected int getStyle() {
380 protected GraphExplorer createExplorerControl(Composite parent) {
381 return GraphExplorerFactory.getInstance()
382 .selectionDataResolver(new DefaultSelectionDataResolver())
383 .create(parent, getStyle());
387 * Override to customize the addition of listeners a newly created
392 protected void addListeners(GraphExplorer explorer, IMenuManager menuManager) {
393 addSelectionInputListeners(explorer, menuManager);
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[]>() {
400 public String[] execute(Object... obj) {
401 return new String[] { getEditingColumn((NodeContext) obj[0]) };
404 // Default double click handling
405 explorer.addListener(new DefaultMouseListener(explorer));
408 protected String getEditingColumn(NodeContext context) {
409 return ColumnKeys.SINGLE;
412 // Needed for preventing unnecessary re-initialization of the explorer with the same input.
413 private Object currentInput;
415 protected boolean isImportantInput(Object previousInput, Object input) {
416 return !ObjectUtils.objectEquals(previousInput, input);
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.
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)
433 if(this.sessionContext == null && context == null)
436 this.sessionContext = context;
437 Object input = getInputSource().get(context);
438 //System.out.println(this + "(SimanticsView).applySessionContext(input=" + input + ")");
439 if (!isImportantInput(currentInput, input))
442 // System.out.println(this + ": initializeExplorer(" + explorer + ", " + context + ")");
443 // initializeExplorer(explorer, context);
444 // System.out.println(this + ": setRoot(" + input + ")");
445 // explorer.setRoot(input);
447 currentInput = input;
448 //System.out.println(this + "(SimanticsView).applySessionContext(new input=" + currentInput + ")");
450 widgetSupport.fireInput(context, input);
452 // Start tracking the session context.
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
459 sessionContextTracker.track(sessionContext);
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);
475 @SuppressWarnings("unchecked")
477 public <T> T getAdapter(Class<T> adapter) {
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();
493 return super.getAdapter(adapter);
497 abstract protected IPropertyPage getPropertyPage();
499 // protected IPropertyPage getPropertyPage() {
500 // return new StandardPropertyPage(getSite(), getBrowseContexts());