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.team.ui;
16 import org.eclipse.jface.layout.GridDataFactory;
17 import org.eclipse.jface.resource.LocalResourceManager;
18 import org.eclipse.jface.viewers.TreeViewer;
19 import org.eclipse.jface.viewers.ViewerCell;
20 import org.eclipse.jface.viewers.ViewerFilter;
21 import org.eclipse.jface.window.ToolTip;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.swt.widgets.Tree;
33 import org.eclipse.ui.IMemento;
34 import org.eclipse.ui.IViewSite;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.part.ViewPart;
37 import org.simantics.browsing.ui.swt.ViewArgumentUtils;
38 import org.simantics.db.management.ISessionContext;
39 import org.simantics.db.management.ISessionContextChangedListener;
40 import org.simantics.db.management.ISessionContextProvider;
41 import org.simantics.db.management.SessionContextChangedEvent;
42 import org.simantics.utils.datastructures.disposable.DisposeState;
45 * @author Kalle Kondelin
47 abstract class TreeView extends ViewPart {
48 protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
50 public void sessionContextChanged(SessionContextChangedEvent event) {
51 sessionContext = event.getNewValue();
54 protected LocalResourceManager resourceManager;
55 protected Composite parent;
56 protected TreeViewer treeViewer;
57 protected IMemento memento;
58 private Map<String, String> args;
59 private ISessionContextProvider contextProvider;
60 private ISessionContext sessionContext;
61 private DisposeState disposeState = DisposeState.Alive;
68 protected Map<String, String> getViewArguments() {
72 protected DisposeState getDisposeState() {
76 public ISessionContext getSessionContext() {
77 return sessionContext;
80 public ISessionContextProvider getSessionContextProvider() {
81 return contextProvider;
86 * Invoked when this viewpart is disposed. Unhooks the view from its
87 * ISessionContextProvider. Overriding is allowed but super.dispose() must
90 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
93 public void dispose() {
94 disposeState = DisposeState.Disposing;
96 if (contextProvider != null) {
97 contextProvider.removeContextChangedListener(contextChangeListener);
98 contextProvider = null;
100 resourceManager.dispose();
101 resourceManager = null;
103 sessionContext = null;
107 disposeState = DisposeState.Disposed;
112 public void setFocus() {
113 treeViewer.getTree().setFocus();
117 public void init(IViewSite site) throws PartInitException {
119 this.args = ViewArgumentUtils.parseViewArguments(this);
123 public void init(IViewSite site, IMemento memento) throws PartInitException {
124 super.init(site, memento);
125 this.args = ViewArgumentUtils.parseViewArguments(this);
126 this.memento = memento;
130 public void saveState(IMemento memento) {
131 if (this.memento != null) {
132 memento.putMemento(this.memento);
134 // if (explorer != null)
135 // explorer.saveState(memento);
138 protected static abstract class NameAndDescriptionToolTip extends ToolTip {
139 public NameAndDescriptionToolTip(Control control, int style) {
140 super(control, style, false);
143 protected abstract Object getModelElement(Event event);
146 * Adds logic to only show a tooltip if a meaningful item is under the
149 protected boolean shouldCreateToolTip(Event event) {
150 if (!super.shouldCreateToolTip(event))
152 Object tableElement = getModelElement(event);
153 return tableElement != null && tableElement instanceof DisplayElement;
156 protected Composite createToolTipContentArea(Event event,
158 Object modelElement = getModelElement(event);
160 Image iconImage = null;
161 String nameString = "no name";
163 if (modelElement instanceof DisplayElement) {
165 nameString = ((DisplayElement)modelElement).getIdText();
168 // Create the content area
169 Composite composite = new Composite(parent, SWT.NONE);
170 composite.setBackground(parent.getDisplay().getSystemColor(
171 SWT.COLOR_INFO_BACKGROUND));
172 composite.setLayout(new GridLayout(2, false));
174 // The title area with the icon (if there is one) and label.
175 Label title = createEntry(composite, iconImage, nameString);
176 // title.setFont(tooltipHeading);
177 GridDataFactory.createFrom((GridData)title.getLayoutData())
178 .hint(SWT.DEFAULT, SWT.DEFAULT)
179 // .minSize(MIN_TOOLTIP_WIDTH, 1)
182 // The description (if there is one)
183 // String descriptionString = "description";
184 // if (descriptionString != null)
185 // createEntry(composite, null, descriptionString);
187 // Other Content to add
188 addContent(composite, modelElement);
194 * Adds a line of information to <code>parent</code>. If
195 * <code>icon</code> is not <code>null</code>, an icon is placed on the
196 * left, and then a label with <code>text</code>.
199 * the composite to add the entry to
201 * the icon to place next to the text. <code>null</code> for
204 * the text to display
205 * @return the created label
207 protected Label createEntry(Composite parent, Image icon, String text) {
209 Label iconLabel = new Label(parent, SWT.NONE);
210 iconLabel.setImage(icon);
211 iconLabel.setBackground(parent.getDisplay().getSystemColor(
212 SWT.COLOR_INFO_BACKGROUND));
213 iconLabel.setData(new GridData());
216 Label textLabel = new Label(parent, SWT.WRAP);
219 GridDataFactory.generate(textLabel, 2, 1);
221 GridDataFactory.generate(textLabel, 1, 1);
224 textLabel.setText(text);
225 textLabel.setBackground(parent.getDisplay().getSystemColor(
226 SWT.COLOR_INFO_BACKGROUND));
231 * Adds a line of information to <code>parent</code>. If
232 * <code>icon</code> is not <code>null</code>, an icon is placed on the
233 * left, and then a label with <code>text</code>, which supports using
234 * anchor tags to creates links
237 * the composite to add the entry to
239 * the icon to place next to the text. <code>null</code> for
242 * the text to display
243 * @return the created link
245 protected Text createEntryWithText(Composite parent, Image icon, String text) {
247 Label iconLabel = new Label(parent, SWT.NONE);
248 iconLabel.setImage(icon);
249 iconLabel.setBackground(parent.getDisplay().getSystemColor(
250 SWT.COLOR_INFO_BACKGROUND));
251 iconLabel.setData(new GridData());
253 Text texts = new Text(parent, SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL);
255 GridDataFactory.generate(texts, 2, 1);
258 texts.setBackground(parent.getDisplay().getSystemColor(
259 SWT.COLOR_INFO_BACKGROUND));
263 protected void addContent(Composite destination, Object modelElement) {
267 protected static class ItemDetailToolTip extends NameAndDescriptionToolTip {
268 // private final boolean DEBUG = false;
269 private TreeViewer viewer;
271 ItemDetailToolTip(TreeViewer viewer, Tree tree, ViewerFilter filter) {
272 super(tree,NO_RECREATE);
274 this.viewer = viewer;
275 this.setHideOnMouseDown(false);
277 public Point getLocation(Point tipSize, Event event) {
278 // try to position the tooltip at the bottom of the cell
279 ViewerCell cell = viewer.getCell(new Point(event.x, event.y));
281 return tree.toDisplay(event.x,cell.getBounds().y+cell.getBounds().height);
282 return super.getLocation(tipSize, event);
284 protected Object getToolTipArea(Event event) {
285 // Ensure that the tooltip is hidden when the cell is left
286 return viewer.getCell(new Point(event.x, event.y));
288 protected void addContent(Composite destination, Object modelElement) {
289 final DisplayElement item = (DisplayElement)modelElement;
292 text = item.getValue();
293 createEntryWithText(destination, null, text.toString());
296 protected Object getModelElement(Event event) {
297 org.eclipse.swt.widgets.TreeItem treeItem = tree.getItem(new Point(event.x, event.y));
298 if (treeItem == null)
300 return treeItem.getData();