1 package org.simantics.sysdyn.ui.wizards.models;
\r
5 import org.eclipse.core.runtime.IStatus;
\r
6 import org.eclipse.core.runtime.Platform;
\r
7 import org.eclipse.core.runtime.Status;
\r
8 import org.eclipse.jface.layout.PixelConverter;
\r
9 import org.eclipse.jface.viewers.IStructuredSelection;
\r
10 import org.eclipse.jface.wizard.WizardPage;
\r
11 import org.eclipse.swt.SWT;
\r
12 import org.eclipse.swt.events.ModifyEvent;
\r
13 import org.eclipse.swt.events.ModifyListener;
\r
14 import org.eclipse.swt.events.SelectionAdapter;
\r
15 import org.eclipse.swt.events.SelectionEvent;
\r
16 import org.eclipse.swt.layout.GridData;
\r
17 import org.eclipse.swt.layout.GridLayout;
\r
18 import org.eclipse.swt.widgets.Button;
\r
19 import org.eclipse.swt.widgets.Composite;
\r
20 import org.eclipse.swt.widgets.FileDialog;
\r
21 import org.eclipse.swt.widgets.Label;
\r
22 import org.eclipse.swt.widgets.Shell;
\r
23 import org.eclipse.swt.widgets.Text;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.exception.DatabaseException;
\r
26 import org.simantics.sysdyn.ui.Activator;
\r
27 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
\r
28 import org.simantics.ui.SimanticsUI;
\r
30 public class WizardModelsImportPage extends WizardPage{
\r
32 private Text filePathField;
\r
34 // Keep track of the archive that we browsed to last time
\r
35 // the wizard was invoked.
\r
36 private static String previouslyBrowsedFile = "";
\r
38 private Button browseDirectoriesButton;
\r
40 private Shell shell;
\r
43 * Creates a new project creation wizard page.
\r
46 public WizardModelsImportPage() {
\r
47 this("wizardModelsImportPage", null, null); //$NON-NLS-1$
\r
51 * Create a new instance of the receiver.
\r
55 public WizardModelsImportPage(String pageName) {
\r
56 this(pageName,null, null);
\r
60 * More (many more) parameters.
\r
63 * @param initialPath
\r
64 * @param currentSelection
\r
67 public WizardModelsImportPage(String pageName,String initialPath,
\r
68 IStructuredSelection currentSelection) {
\r
70 //this.initialPath = initialPath;
\r
71 //this.currentSelection = currentSelection;
\r
72 setPageComplete(false);
\r
73 setTitle("Import Model");
\r
74 setDescription("Choose the Model file, then press Finish.");
\r
78 public void createControl(Composite parent) {
\r
80 initializeDialogUnits(parent);
\r
82 Composite workArea = new Composite(parent, SWT.NONE);
\r
83 setControl(workArea);
\r
85 workArea.setLayout(new GridLayout());
\r
86 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
87 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
89 createProjectsRoot(workArea);
\r
92 private void createProjectsRoot(Composite workArea) {
\r
94 // set label for field
\r
95 Label title = new Label(workArea, SWT.NONE);
\r
96 title.setText("Select Model source:");
\r
98 Composite projectGroup = new Composite(workArea, SWT.NONE);
\r
99 GridLayout layout = new GridLayout();
\r
100 layout.numColumns = 2;
\r
101 layout.makeColumnsEqualWidth = false;
\r
102 layout.marginWidth = 0;
\r
104 projectGroup.setLayout(layout);
\r
105 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
107 // model location entry field
\r
108 this.filePathField = new Text(projectGroup, SWT.BORDER);
\r
110 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
\r
111 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
\r
112 filePathField.setLayoutData(directoryPathData);
\r
113 filePathField.addModifyListener(new ModifyListener(){
\r
115 public void modifyText(ModifyEvent e) {
\r
116 previouslyBrowsedFile = filePathField.getText();
\r
119 if (previouslyBrowsedFile != null){
\r
120 filePathField.setText(previouslyBrowsedFile);
\r
125 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
126 browseDirectoriesButton.setText("Browse");
\r
127 setButtonLayoutData(browseDirectoriesButton);
\r
129 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
133 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
134 * elected(org.eclipse.swt.events.SelectionEvent)
\r
136 public void widgetSelected(SelectionEvent e) {
\r
137 setErrorMessage(null);
\r
138 handleLocationDirectoryButtonPressed();
\r
144 //Set filePathField active
\r
145 public void setVisible(boolean visible) {
\r
146 super.setVisible(visible);
\r
147 this.filePathField.setFocus();
\r
151 //Open dialog for choosing the file
\r
152 protected void handleLocationDirectoryButtonPressed() {
\r
154 shell = filePathField.getShell();
\r
156 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
\r
158 String[] ext = {"*.tg"};
\r
159 dialog.setFilterExtensions(ext);
\r
161 dialog.setText("Import Model");
\r
163 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTMODELTPATH);
\r
164 if(path.isEmpty() || !(new File(path).exists()))
\r
165 path = Platform.getLocation().toOSString();
\r
166 dialog.setFilterPath(path);
\r
168 String selectedFile = dialog.open();
\r
169 if (selectedFile != null) {
\r
170 filePathField.setText(selectedFile);
\r
176 //Create project after finish is pressed.
\r
177 public boolean createProjects() {
\r
179 Resource project = SimanticsUI.getProject().get();
\r
180 if(project == null){
\r
181 setErrorMessage("Error when retrieving resource");
\r
185 final String selected = previouslyBrowsedFile;
\r
186 if(selected == null){
\r
187 setErrorMessage("No file selected");
\r
191 IStatus status = ImportUtilsUI.importModelFile(selected, null);
\r
194 TransferableGraph1 tg = null;
\r
196 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
\r
197 } catch (RuntimeBindingConstructionException e) {
\r
198 e.printStackTrace();
\r
199 } catch (IOException e) {
\r
201 OldTransferableGraph1 otg = (OldTransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(OldTransferableGraph1.class));
\r
202 tg = new TransferableGraph1(otg.resourceCount, otg.identities, otg.statements, otg.values);
\r
203 } catch (RuntimeBindingConstructionException e1) {
\r
204 e1.printStackTrace();
\r
205 } catch (IOException e1) {
\r
206 setErrorMessage("The imported file is not of type: System Dynamics Model");
\r
211 setErrorMessage("The imported file is not of type: System Dynamics Model");
\r
217 DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project);
\r
218 DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia);
\r
220 // Check that imported resource was actually a model
\r
221 //and fix changes made to old ontology versions
\r
222 final Resource root = ia.getRoot();
\r
223 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
226 public void perform(WriteGraph graph) throws DatabaseException {
\r
228 if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModel)) {
\r
229 Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);
\r
230 String type = "...";
\r
231 if(instanceOf != null)
\r
232 type = NameUtils.getSafeName(graph, instanceOf);
\r
234 Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);
\r
235 if(inheritedFrom != null)
\r
236 type = NameUtils.getSafeName(graph, inheritedFrom);
\r
238 graph.deny(root, Layer0.getInstance(graph).PartOf);
\r
241 updateOldConfigurationToBaseRealization(graph, root);
\r
242 addDefaultOntologyLinks(graph, root);
\r
243 addURIsToDiagrams(graph, root);
\r
244 addSpreadSheetBook(graph, root);
\r
248 } catch (DatabaseException e) {
\r
249 e.printStackTrace();
\r
250 } catch (Exception e) {
\r
251 e.printStackTrace();
\r
254 if (!error.isEmpty()){
\r
255 setErrorMessage("The imported file is not of type: System Dynamics Model (" + error +")");
\r
260 if(status == null || !status.equals(Status.OK_STATUS)) {
\r
261 setErrorMessage(status.getMessage());
\r
268 * In old versions base realization was separate. Newer versions use configuration as base realization.
\r
269 * @param graph WriteGraph
\r
270 * @param model Imported model
\r
273 private static void updateOldConfigurationToBaseRealization(WriteGraph graph, Resource model) {
\r
274 Layer0X L0X = Layer0X.getInstance(graph);
\r
276 Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);
\r
277 if(configuration != null && !graph.hasStatement(configuration, L0X.IsBaseRealizationOf, model))
\r
278 graph.claim(configuration, L0X.IsBaseRealizationOf, model);
\r
279 } catch (DatabaseException e) {
\r
280 e.printStackTrace();
\r
287 * Links should be exported and imported automatically. If it has failed, the
\r
288 * default ontology links sysdyn and layer0 are added.
\r
290 * @param graph WriteGraph
\r
291 * @param model Imported model
\r
294 private static void addDefaultOntologyLinks(WriteGraph graph, Resource model) {
\r
296 Layer0 l0 = Layer0.getInstance(graph);
\r
297 // The links should be exported and imported automatically
\r
298 Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.1");
\r
299 Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.1");
\r
300 if(!graph.hasStatement(model, l0.IsLinkedTo, sysdyn))
\r
301 graph.claim(model, l0.IsLinkedTo, sysdyn);
\r
302 if(!graph.hasStatement(model, l0.IsLinkedTo, layer0))
\r
303 graph.claim(model, l0.IsLinkedTo, layer0);
\r
304 } catch (DatabaseException e) {
\r
305 e.printStackTrace();
\r
309 private static void addURIsToDiagrams(WriteGraph graph, Resource model) {
\r
310 Layer0 l0 = Layer0.getInstance(graph);
\r
311 SimulationResource simu = SimulationResource.getInstance(graph);
\r
312 ModelingResources mr = ModelingResources.getInstance(graph);
\r
313 SysdynResource sr = SysdynResource.getInstance(graph);
\r
314 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
316 HashSet<Resource> configurations = new HashSet<Resource>();
\r
318 Resource configuration = graph.getPossibleObject(model, simu.HasConfiguration);
\r
319 if(configuration != null)
\r
320 configurations.add(configuration);
\r
322 for(Resource r : graph.getObjects(model, l0.ConsistsOf)) {
\r
323 if(graph.isInheritedFrom(r, sr.Module)) {
\r
324 Resource moduleConfiguration = graph.getPossibleObject(r, sr2.IsDefinedBy);
\r
325 if(moduleConfiguration != null)
\r
326 configurations.add(moduleConfiguration);
\r
330 for(Resource conf : configurations) {
\r
331 Resource configurationDiagram = graph.getPossibleObject(conf, mr.CompositeToDiagram);
\r
332 if(configurationDiagram != null && !graph.hasStatement(configurationDiagram, l0.PartOf)) {
\r
333 GraphUtils.create2(graph, l0.Library,
\r
334 l0.HasName, "__CONTAINER__",
\r
336 l0.ConsistsOf, configurationDiagram);
\r
340 } catch (DatabaseException e) {
\r
341 e.printStackTrace();
\r
347 * Add a missing spreadsheet book to the model
\r
353 private static void addSpreadSheetBook(WriteGraph graph, Resource model) {
\r
355 Layer0 l0 = Layer0.getInstance(graph);
\r
356 SpreadsheetResource ssr = SpreadsheetResource.getInstance(graph);
\r
357 SimulationResource simu = SimulationResource.getInstance(graph);
\r
358 Resource conf = graph.getPossibleObject(model, simu.HasConfiguration);
\r
359 if(conf != null && graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, ssr.Book)).isEmpty()) {
\r
360 Resource book = graph.newResource();
\r
361 graph.claim(book, l0.InstanceOf, null, ssr.Book);
\r
362 graph.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING);
\r
363 graph.claim(conf, l0.ConsistsOf, l0.PartOf, book);
\r
365 createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 });
\r
367 } catch (DatabaseException e) {
\r
368 e.printStackTrace();
\r
373 * Create a sheet (Copied from SysdynProject)
\r
381 * @throws DatabaseException
\r
384 private static Resource createSheet(WriteGraph graph, Resource book, String name, String[] colNames, int[] colWidths) throws DatabaseException {
\r
386 Layer0 L0 = Layer0.getInstance(graph);
\r
387 Layer0X L0X = Layer0X.getInstance(graph);
\r
388 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
\r
390 Resource result = graph.newResource();
\r
391 graph.claim(result, L0.InstanceOf, null, sr.Spreadsheet);
\r
394 name = NameUtils.findFreshEscapedName(graph, "Sheet", book, sr.HasSheet);
\r
396 graph.claimLiteral(result, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);
\r
397 graph.claim(book, L0.ConsistsOf, L0.PartOf, result);
\r
400 Resource newCell = graph.newResource();
\r
401 graph.claim(newCell, L0.InstanceOf, null, sr.Dimensions);
\r
402 graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Dimensions", Bindings.STRING);
\r
403 graph.addLiteral(newCell, sr.Dimensions_fitColumns, sr.Dimensions_fitColumns_Inverse, L0.Boolean, false, Bindings.BOOLEAN);
\r
404 graph.addLiteral(newCell, sr.Dimensions_fitRows, sr.Dimensions_fitRows_Inverse, L0.Boolean, false, Bindings.BOOLEAN);
\r
405 graph.addLiteral(newCell, sr.Dimensions_columnCount, sr.Dimensions_columnCount_Inverse, L0.Integer, 128, Bindings.INTEGER);
\r
406 graph.addLiteral(newCell, sr.Dimensions_rowCount, sr.Dimensions_rowCount_Inverse, L0.Integer, 256, Bindings.INTEGER);
\r
407 graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell);
\r
411 Resource newCell = graph.newResource();
\r
412 graph.claim(newCell, L0.InstanceOf, null, sr.Dimensions);
\r
413 graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Headers", Bindings.STRING);
\r
414 graph.addLiteral(newCell, sr.Headers_columnLabels, sr.Headers_columnLabels_Inverse, L0.StringArray, colNames, Bindings.STRING_ARRAY);
\r
415 graph.addLiteral(newCell, sr.Headers_columnWidths, sr.Headers_columnWidths_Inverse, L0.IntegerArray, colWidths, Bindings.INT_ARRAY);
\r
416 graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell);
\r
421 double[] doubles = new double[10*2];
\r
422 for(int i=0;i<10*2;i++) doubles[i] = i;
\r
424 Resource newCell = graph.newResource();
\r
425 graph.claim(newCell, L0.InstanceOf, null, sr.DoubleArrayCell);
\r
426 graph.addLiteral(newCell, sr.DoubleArrayCell_HasWidth, sr.DoubleArrayCell_HasWidth_Inverse, L0.Integer, 10, Bindings.INTEGER);
\r
427 graph.addLiteral(newCell, sr.HasLocation, sr.HasLocation_Inverse, L0.String, "B2", Bindings.STRING);
\r
428 graph.addLiteral(newCell, sr.DoubleArrayCell_HasDoubleArray, sr.DoubleArrayCell_HasDoubleArray_Inverse, L0.DoubleArray, doubles, Bindings.DOUBLE_ARRAY);
\r
429 graph.claim(result, L0X.HasChildVariables, L0X.HasChildVariables_Inverse, newCell);
\r
437 void validatePage(){
\r
439 if (previouslyBrowsedFile.isEmpty()){
\r
440 setPageComplete(false);
\r
443 setErrorMessage(null);
\r
444 setPageComplete(true);
\r