]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerDialog2.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerDialog2.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.browsing.ui.swt;
13
14 import java.util.Set;
15
16 import org.eclipse.jface.layout.GridDataFactory;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Tree;
22 import org.eclipse.swt.widgets.TreeItem;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.simantics.browsing.ui.Column;
25 import org.simantics.browsing.ui.Column.Align;
26 import org.simantics.browsing.ui.common.ColumnKeys;
27 import org.simantics.browsing.ui.graph.impl.GraphInputSources;
28 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
29 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
30 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
31 import org.simantics.db.management.ISessionContext;
32 import org.simantics.utils.DataContainer;
33 import org.simantics.utils.datastructures.ArrayMap;
34
35 /*
36  * A dialog, which contains a single graph explorer composite
37  *
38  * Override getBrowseContexts() to specify the graph explorer contents.
39  * 
40  */
41
42 public abstract class GraphExplorerDialog2 extends SimanticsDialog {
43
44     abstract public Set<String> getBrowseContexts();
45
46     private final IWorkbenchSite site;
47     private final DataContainer<Object[]> result = new DataContainer<Object[]>();
48     
49     private GraphExplorerComposite outputExplorer;
50
51     public GraphExplorerDialog2(IWorkbenchSite site, String title) {
52         super(Display.getCurrent().getActiveShell(), title);
53         this.site = site;
54     }
55
56     Column[] getColumns() {
57         return new Column[] { new Column(ColumnKeys.SINGLE, Align.RIGHT, 180, "single", true, 1) };
58     }
59
60     @Override
61     protected SessionContextInputSource getInputSource() {
62         return GraphInputSources.projectSource();
63     }
64
65     @Override
66     protected boolean isResizable() {
67         return true;
68     }
69
70     @Override
71     protected void createControls(Composite body, ISessionContext context, WidgetSupport support) {
72
73         outputExplorer = new GraphExplorerComposite(
74                 ArrayMap.keys("displaySelectors", "displayFilter").values(false, false), site, body, support,
75                 SWT.BORDER);
76         outputExplorer.setBrowseContexts(getBrowseContexts());
77         outputExplorer.setColumnsVisible(false);
78         outputExplorer.setColumns(getColumns());
79         outputExplorer.finish();
80
81         outputExplorer.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_GREEN));
82
83         GridDataFactory.fillDefaults().grab(true, true).span(2, 1).minSize(300, 400).applyTo(outputExplorer);
84
85     }
86
87     @Override
88     protected void buttonPressed(int buttonId) {
89         if (buttonId == Window.OK) {
90
91             Tree tree = (Tree) outputExplorer.getExplorerControl();
92
93             TreeItem[] selectedItems = tree.getSelection();
94             Object[] res = new Object[selectedItems.length];
95             for (int i = 0; i < selectedItems.length; i++) {
96                 res[i] = selectedItems[i].getData();
97             }
98             result.set(res);
99         }
100         super.buttonPressed(buttonId);
101
102     }
103
104     public Object[] getSelection() {
105         return result.get();
106     }
107     
108     public GraphExplorerComposite getGraphExplorerComposite() {
109         return outputExplorer;
110     }
111
112 }