]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.team.ui/src/org/simantics/team/ui/TreeView.java
Removed contact application support prints
[simantics/platform.git] / bundles / org.simantics.team.ui / src / org / simantics / team / ui / TreeView.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.team.ui;
13
14 import java.util.Map;
15
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;
43
44 /**
45  * @author Kalle Kondelin
46  */
47 abstract class TreeView extends ViewPart {
48     protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
49         @Override
50         public void sessionContextChanged(SessionContextChangedEvent event) {
51             sessionContext = event.getNewValue();
52         }
53     };
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;
62
63     public TreeView() {
64     }
65
66     
67
68     protected Map<String, String> getViewArguments() {
69         return args;
70     }
71
72     protected DisposeState getDisposeState() {
73         return disposeState;
74     }
75
76     public ISessionContext getSessionContext() {
77         return sessionContext;
78     }
79
80     public ISessionContextProvider getSessionContextProvider() {
81         return contextProvider;
82     }
83
84
85     /**
86      * Invoked when this viewpart is disposed. Unhooks the view from its
87      * ISessionContextProvider. Overriding is allowed but super.dispose() must
88      * be called.
89      * 
90      * @see org.eclipse.ui.part.WorkbenchPart#dispose()
91      */
92     @Override
93     public void dispose() {
94         disposeState = DisposeState.Disposing;
95         try {
96             if (contextProvider != null) {
97                 contextProvider.removeContextChangedListener(contextChangeListener);
98                 contextProvider = null;
99             }
100             resourceManager.dispose();
101             resourceManager = null;
102             args = null;
103             sessionContext = null;
104             parent = null;
105             super.dispose();
106         } finally {
107            disposeState = DisposeState.Disposed;
108         }
109     }
110
111     @Override
112     public void setFocus() {
113         treeViewer.getTree().setFocus();
114     }
115
116     @Override
117     public void init(IViewSite site) throws PartInitException {
118         super.init(site);
119         this.args = ViewArgumentUtils.parseViewArguments(this);
120     }
121
122     @Override
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;
127     }
128
129     @Override
130     public void saveState(IMemento memento) {
131         if (this.memento != null) {
132             memento.putMemento(this.memento);
133         }
134 //        if (explorer != null)
135 //            explorer.saveState(memento);
136     }
137     
138     protected static abstract class NameAndDescriptionToolTip extends ToolTip {
139         public NameAndDescriptionToolTip(Control control, int style) {
140             super(control, style, false);
141         }
142
143         protected abstract Object getModelElement(Event event);
144
145         /**
146          * Adds logic to only show a tooltip if a meaningful item is under the
147          * cursor.
148          */
149         protected boolean shouldCreateToolTip(Event event) {
150             if (!super.shouldCreateToolTip(event))
151                 return false;
152             Object tableElement = getModelElement(event); 
153             return tableElement != null && tableElement instanceof DisplayElement;
154         }
155
156         protected Composite createToolTipContentArea(Event event,
157                 Composite parent) {
158             Object modelElement = getModelElement(event);
159
160             Image iconImage = null;
161             String nameString = "no name";
162
163             if (modelElement instanceof DisplayElement) {
164                 iconImage = null;
165                 nameString = ((DisplayElement)modelElement).getIdText();
166             }
167
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));
173
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)
180                 .applyTo(title);
181
182             // The description (if there is one)
183 //            String descriptionString = "description";
184 //            if (descriptionString != null)
185 //                createEntry(composite, null, descriptionString);
186
187             // Other Content to add
188             addContent(composite, modelElement);
189
190             return composite;
191         }
192
193         /**
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>.
197          * 
198          * @param parent
199          *            the composite to add the entry to
200          * @param icon
201          *            the icon to place next to the text. <code>null</code> for
202          *            none.
203          * @param text
204          *            the text to display
205          * @return the created label
206          */
207         protected Label createEntry(Composite parent, Image icon, String text) {
208             if (icon != null) {
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());
214             }
215
216             Label textLabel = new Label(parent, SWT.WRAP);
217             
218             if(icon == null) {
219                 GridDataFactory.generate(textLabel, 2, 1);
220             } else {
221                 GridDataFactory.generate(textLabel, 1, 1);
222             }
223             
224             textLabel.setText(text);
225             textLabel.setBackground(parent.getDisplay().getSystemColor(
226                     SWT.COLOR_INFO_BACKGROUND));
227             return textLabel;
228         }
229
230         /**
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
235          * 
236          * @param parent
237          *            the composite to add the entry to
238          * @param icon
239          *            the icon to place next to the text. <code>null</code> for
240          *            none.
241          * @param text
242          *            the text to display
243          * @return the created link
244          */
245         protected Text createEntryWithText(Composite parent, Image icon, String text) {
246             if (icon != null) {
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());
252             }
253             Text texts = new Text(parent, SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL);
254             if(icon == null) {
255                 GridDataFactory.generate(texts, 2, 1);
256             }
257             texts.setText(text);
258             texts.setBackground(parent.getDisplay().getSystemColor(
259                     SWT.COLOR_INFO_BACKGROUND));
260             return texts;
261         }
262
263         protected void addContent(Composite destination, Object modelElement) {
264         }
265     }
266
267     protected static class ItemDetailToolTip extends NameAndDescriptionToolTip {
268 //        private final boolean DEBUG = false;
269         private TreeViewer viewer;
270         private Tree tree;
271         ItemDetailToolTip(TreeViewer viewer, Tree tree, ViewerFilter filter) {
272             super(tree,NO_RECREATE);
273             this.tree = tree;
274             this.viewer = viewer;
275             this.setHideOnMouseDown(false);
276         }
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));
280             if( cell != null )
281                 return tree.toDisplay(event.x,cell.getBounds().y+cell.getBounds().height);
282             return super.getLocation(tipSize, event);
283         }
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));
287         }
288         protected void addContent(Composite destination, Object modelElement) {
289             final DisplayElement item = (DisplayElement)modelElement;
290             String text = null;
291             if (null != item) {
292                 text = item.getValue();
293                 createEntryWithText(destination, null, text.toString());
294             }
295         }
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)
299                 return null;
300             return treeItem.getData();
301         }
302     }
303 }