]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerDialog.java
Use RuntimeEnvironmentRequest2 where applicable
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerDialog.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.Shell;
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 import org.simantics.utils.datastructures.UnaryFunction;
35
36
37 /*
38  * A dialog, which contains a single graph explorer composite
39  *
40  * Override getBrowseContexts() to specify the graph explorer contents.
41  * 
42  */
43
44 public abstract class GraphExplorerDialog implements UnaryFunction<SimanticsDialog, Shell> {
45
46         abstract public Set<String> getBrowseContexts();
47
48         Column[] getColumns() {
49                 return new Column[] { new Column(ColumnKeys.SINGLE, Align.RIGHT, 180, "single", true, 1) };
50         }
51
52         protected SessionContextInputSource getInputSource() {
53                 return GraphInputSources.projectSource();
54         }
55         
56         private final String title;
57         private final IWorkbenchSite site;
58         private final DataContainer<Object[]> result = new DataContainer<Object[]>();
59         
60         public GraphExplorerDialog(IWorkbenchSite site, String title) {
61                 this.site = site;
62                 this.title = title;
63         }
64         
65         class Dialog extends SimanticsDialog {
66
67                 private GraphExplorerComposite outputExplorer;
68                 
69                 public Dialog(Shell shell, String title) {
70                         super(shell, title);
71                 }
72                 
73                 @Override
74                 protected boolean isResizable() {
75                         return true;
76                 }
77                 
78                 @Override
79                 protected SessionContextInputSource getInputSource() {
80                         return GraphExplorerDialog.this.getInputSource();
81                 }
82
83                 @Override
84                 protected void createControls(Composite body, ISessionContext context, WidgetSupport support) {
85                         
86                 outputExplorer = new GraphExplorerComposite(ArrayMap.keys("displaySelectors", "displayFilter").values(false, false), site, body, support, SWT.BORDER);          
87                 outputExplorer.setBrowseContexts(getBrowseContexts());
88                 outputExplorer.setColumnsVisible(false);
89                 outputExplorer.setColumns(getColumns());
90                 outputExplorer.finish();
91                 
92                 outputExplorer.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_GREEN));
93                 
94                 GridDataFactory.fillDefaults().grab(true, true).span(2, 1).minSize(300, 400).applyTo(outputExplorer);
95                         
96                 }
97                 
98                 @Override
99                 protected void buttonPressed(int buttonId) {
100                         
101                         if(buttonId == Window.OK) {
102
103                                 Tree tree = (Tree)outputExplorer.getExplorerControl();
104                                 
105                                 TreeItem[] selectedItems = tree.getSelection();
106                                 Object[] res = new Object[selectedItems.length];
107                                 for(int i=0;i<selectedItems.length;i++) {
108                                         res[i] = selectedItems[i].getData();
109                                 }
110                                 result.set(res);
111                                 
112                         }
113                         
114                         super.buttonPressed(buttonId);
115                         
116                 }
117                 
118                 @Override
119                 protected Object getSelection() {
120                         return result.get();
121                 }
122                 
123         }
124         
125         @Override
126         public SimanticsDialog call(Shell shell) {
127                 return new Dialog(shell, title);
128         }
129         
130         
131         public Object[] getSelection() {
132                 return result.get(); 
133         }
134         
135 }