]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerDialog.java
3d877212e4850031d84810d4eaf563db8710c29d
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import java.util.Set;\r
15 \r
16 import org.eclipse.jface.layout.GridDataFactory;\r
17 import org.eclipse.jface.window.Window;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.widgets.Composite;\r
20 import org.eclipse.swt.widgets.Shell;\r
21 import org.eclipse.swt.widgets.Tree;\r
22 import org.eclipse.swt.widgets.TreeItem;\r
23 import org.eclipse.ui.IWorkbenchSite;\r
24 import org.simantics.browsing.ui.Column;\r
25 import org.simantics.browsing.ui.Column.Align;\r
26 import org.simantics.browsing.ui.common.ColumnKeys;\r
27 import org.simantics.browsing.ui.graph.impl.GraphInputSources;\r
28 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;\r
29 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
30 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
31 import org.simantics.db.management.ISessionContext;\r
32 import org.simantics.utils.DataContainer;\r
33 import org.simantics.utils.datastructures.ArrayMap;\r
34 import org.simantics.utils.datastructures.UnaryFunction;\r
35 \r
36 \r
37 /*\r
38  * A dialog, which contains a single graph explorer composite\r
39  *\r
40  * Override getBrowseContexts() to specify the graph explorer contents.\r
41  * \r
42  */\r
43 \r
44 public abstract class GraphExplorerDialog implements UnaryFunction<SimanticsDialog, Shell> {\r
45 \r
46         abstract public Set<String> getBrowseContexts();\r
47 \r
48         Column[] getColumns() {\r
49                 return new Column[] { new Column(ColumnKeys.SINGLE, Align.RIGHT, 180, "single", true, 1) };\r
50         }\r
51 \r
52         protected SessionContextInputSource getInputSource() {\r
53                 return GraphInputSources.projectSource();\r
54         }\r
55         \r
56         private final String title;\r
57         private final IWorkbenchSite site;\r
58         private final DataContainer<Object[]> result = new DataContainer<Object[]>();\r
59         \r
60         public GraphExplorerDialog(IWorkbenchSite site, String title) {\r
61                 this.site = site;\r
62                 this.title = title;\r
63         }\r
64         \r
65         class Dialog extends SimanticsDialog {\r
66 \r
67                 private GraphExplorerComposite outputExplorer;\r
68                 \r
69                 public Dialog(Shell shell, String title) {\r
70                         super(shell, title);\r
71                 }\r
72                 \r
73                 @Override\r
74                 protected boolean isResizable() {\r
75                         return true;\r
76                 }\r
77                 \r
78                 @Override\r
79                 protected SessionContextInputSource getInputSource() {\r
80                         return GraphExplorerDialog.this.getInputSource();\r
81                 }\r
82 \r
83                 @Override\r
84                 protected void createControls(Composite body, ISessionContext context, WidgetSupport support) {\r
85                         \r
86                 outputExplorer = new GraphExplorerComposite(ArrayMap.keys("displaySelectors", "displayFilter").values(false, false), site, body, support, SWT.BORDER);          \r
87                 outputExplorer.setBrowseContexts(getBrowseContexts());\r
88                 outputExplorer.setColumnsVisible(false);\r
89                 outputExplorer.setColumns(getColumns());\r
90                 outputExplorer.finish();\r
91                 \r
92                 outputExplorer.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_GREEN));\r
93                 \r
94                 GridDataFactory.fillDefaults().grab(true, true).span(2, 1).minSize(300, 400).applyTo(outputExplorer);\r
95                         \r
96                 }\r
97                 \r
98                 @Override\r
99                 protected void buttonPressed(int buttonId) {\r
100                         \r
101                         if(buttonId == Window.OK) {\r
102 \r
103                                 Tree tree = (Tree)outputExplorer.getExplorerControl();\r
104                                 \r
105                                 TreeItem[] selectedItems = tree.getSelection();\r
106                                 Object[] res = new Object[selectedItems.length];\r
107                                 for(int i=0;i<selectedItems.length;i++) {\r
108                                         res[i] = selectedItems[i].getData();\r
109                                 }\r
110                                 result.set(res);\r
111                                 \r
112                         }\r
113                         \r
114                         super.buttonPressed(buttonId);\r
115                         \r
116                 }\r
117                 \r
118                 @Override\r
119                 protected Object getSelection() {\r
120                         return result.get();\r
121                 }\r
122                 \r
123         }\r
124         \r
125         @Override\r
126         public SimanticsDialog call(Shell shell) {\r
127                 return new Dialog(shell, title);\r
128         }\r
129         \r
130         \r
131         public Object[] getSelection() {\r
132                 return result.get(); \r
133         }\r
134         \r
135 }\r