]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/editor/SpreadsheetEditor2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / editor / SpreadsheetEditor2.java
1 package org.simantics.spreadsheet.ui.editor;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.TreeMap;
6
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.ui.contexts.IContextService;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.UnaryRead;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.request.VariableName;
17 import org.simantics.db.layer0.request.VariableRead;
18 import org.simantics.db.layer0.request.VariableRepresents;
19 import org.simantics.db.layer0.request.VariableURI;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.request.Read;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.selectionview.StandardPropertyPage;
24 import org.simantics.spreadsheet.graph.GraphUI;
25 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
26 import org.simantics.spreadsheet.resource.SpreadsheetResource;
27 import org.simantics.spreadsheet.ui.Spreadsheet;
28 import org.simantics.spreadsheet.util.SpreadsheetUtils;
29 import org.simantics.ui.workbench.IPropertyPage;
30 import org.simantics.ui.workbench.ResourceEditorPart2;
31 import org.simantics.utils.ui.LayoutUtils;
32 import org.simantics.utils.ui.jface.ActiveSelectionProvider;
33
34 public class SpreadsheetEditor2 extends ResourceEditorPart2 {
35
36     public static final boolean EXCEL = false;
37     public static final String EDITOR_ID = "org.simantics.spreadsheet.ui.editor2";
38
39     private Spreadsheet spreadsheet;
40     private final ActiveSelectionProvider selectionProvider = new ActiveSelectionProvider();
41     private ExcelLink excelLink;
42     private GraphUI ui;
43
44     public Spreadsheet getSpreadsheet() {
45         return spreadsheet;
46     }
47
48     @Override
49     public void createPartControl(Composite parent) {
50
51         // Create UI
52         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1));
53         parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
54
55         ui = new GraphUI(getSession());
56
57         try {
58                 
59             Variable bookVariable = getSession().syncRequest(new Read<Variable>() {
60
61                 @Override
62                 public Variable perform(ReadGraph graph) throws DatabaseException {
63                         
64                         Resource book = getResourceInput2().getResource();
65                         Variable withContext = SpreadsheetUtils.getBookVariable(graph, book); 
66                         return withContext; 
67
68                 }
69
70             });
71             
72 //            getSession().syncRequest(new VariableRead<Variable>(bookVariable) {
73 //
74 //                @Override
75 //                public Variable perform(ReadGraph graph) throws DatabaseException {
76 //                    SpreadsheetGraphUtils.fullSynchronization(graph, variable);
77 //                    return variable;
78 //                }
79 //
80 //            });
81             
82             Variable sheetVariable = getSession().syncRequest(new VariableRead<Variable>(bookVariable) {
83
84                 @Override
85                 public Variable perform(ReadGraph graph) throws DatabaseException {
86                         
87                         SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);
88                         Collection<Variable> children = variable.getChildren(graph);
89                         if(children.isEmpty()) return null;
90                         
91                         TreeMap<String,Variable> names = new TreeMap<String,Variable>();
92                         for(Variable child : children) {
93                                 Resource type = child.getPossibleType(graph, SR.Spreadsheet);
94                                 if(type != null)
95                                         names.put(child.getName(graph), child);
96                         }
97                         
98                         return names.firstEntry().getValue();
99
100                 }
101
102             });
103             
104             String uri = getSession().syncRequest(new VariableURI(sheetVariable));
105             
106             Resource book = getSession().syncRequest(new VariableRepresents(bookVariable));
107             Resource sheet = getSession().syncRequest(new VariableRepresents(sheetVariable));
108             String sheetName = getSession().syncRequest(new VariableName(sheetVariable));
109             String bookName = getSession().syncRequest(new VariableName(bookVariable));
110
111             spreadsheet = new Spreadsheet(parent, SWT.NONE, ui, selectionProvider);
112                 spreadsheet.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
113         
114                 excelLink = new ExcelLink(getSession(), sheet, spreadsheet.getClientModel(), book, bookName, sheetName, uri);
115
116                 if (GraphUI.DEBUG)
117                     System.out.println("Opening SpreadsheetEditor2 with uri: " + uri);
118             ui.load(sheetVariable, spreadsheet.getClientInterface());
119             
120         } catch (DatabaseException e) {
121                 
122             e.printStackTrace();
123                 return;
124                 
125         }
126
127         spreadsheet.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128         spreadsheet.defaultInitializeUI();
129
130         getSite().setSelectionProvider(selectionProvider);
131
132         // Start tracking editor input validity. 
133         activateValidation();
134         
135         IContextService contexts = (IContextService)getSite().getService(IContextService.class);
136         contexts.activateContext("org.simantics.spreadsheet.ui.context");
137
138     }
139
140     @Override
141     public void setFocus() {
142         if (spreadsheet != null)
143             spreadsheet.setFocus();
144     }
145
146     @Override
147     public Object getAdapter(Class adapter) {
148         if (adapter == IPropertyPage.class)
149             return new StandardPropertyPage(getSite(), Collections.singleton(SpreadsheetResource.URIs.BrowseContext));
150         if (adapter == GraphUI.class)
151             return ui;
152         if (adapter == Spreadsheet.class)
153             return getSpreadsheet();
154         return null;
155     }
156
157     @Override
158     public void dispose() {
159
160         if(EXCEL)
161             excelLink.dispose();
162         
163 //        backend.dispose();
164
165 //        tableModel.dispose();
166         ui.dispose();
167         super.dispose();
168
169     }
170
171 }