--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.simantics.db.Resource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+\r
+public class ExportWizardFunction extends Wizard implements IImportWizard {\r
+ \r
+ private WizardFunctionsExportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ public Resource selection;\r
+\r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ExportWizardFunction() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ExportWizardFunction(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath;\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Export");\r
+ this.currentSelection = currentSelection;\r
+ selection = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ }\r
+ \r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardFunctionsExportPage(\r
+ "wizardFunctionsExportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects(selection);\r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.simantics.db.Resource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+public class ExportWizardModel extends Wizard implements IImportWizard {\r
+ \r
+ private WizardModelsExportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ public Resource selection;\r
+\r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ExportWizardModel() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ExportWizardModel(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath;\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Export");\r
+ this.currentSelection = currentSelection;\r
+ selection = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ }\r
+ \r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardModelsExportPage(\r
+ "wizardModelsExportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects(selection);\r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.simantics.db.Resource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+\r
+public class ExportWizardModule extends Wizard implements IImportWizard {\r
+ \r
+ private WizardModulesExportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ public Resource selection;\r
+\r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ExportWizardModule() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ExportWizardModule(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath;\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Export");\r
+ this.currentSelection = currentSelection;\r
+ selection = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ }\r
+ \r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardModulesExportPage(\r
+ "wizardModulesExportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects();\r
+ \r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.simantics.db.Resource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+\r
+public class ImportWizardFunction extends Wizard implements IImportWizard {\r
+ \r
+ private WizardFunctionsImportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ public Resource selection;\r
+\r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardFunction() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardFunction(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath;\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Import");\r
+ this.currentSelection = currentSelection;\r
+ selection = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ }\r
+ \r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardFunctionsImportPage(\r
+ "wizardFunctionsImportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects();\r
+ \r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+\r
+public class ImportWizardMdl extends Wizard implements IImportWizard {\r
+ \r
+ private WizardMdlImportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardMdl() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardMdl(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath;\r
+ }\r
+\r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardMdlImportPage(\r
+ "wizardMdlImportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Import");\r
+ this.currentSelection = currentSelection;\r
+ }\r
+ \r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects();\r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+\r
+\r
+public class ImportWizardModel extends Wizard implements IImportWizard {\r
+ \r
+ private WizardModelsImportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardModel() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardModel(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath; \r
+ }\r
+\r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardModelsImportPage(\r
+ "wizardModelsImportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Import");\r
+ this.currentSelection = currentSelection;\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects();\r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.ui.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.simantics.db.Resource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+\r
+public class ImportWizardModule extends Wizard implements IImportWizard {\r
+ \r
+ private WizardModulesImportPage mainPage;\r
+ private IStructuredSelection currentSelection = null;\r
+ private String initialPath = null;\r
+ public Resource selection;\r
+\r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardModule() {\r
+ this(null);\r
+ }\r
+ \r
+ /**\r
+ * Constructor for ExternalProjectImportWizard.\r
+ */\r
+ public ImportWizardModule(String initialPath)\r
+ {\r
+ super();\r
+ this.initialPath = initialPath;\r
+ }\r
+ \r
+ public void init(IWorkbench workbench, IStructuredSelection currentSelection) {\r
+ setWindowTitle("Import");\r
+ this.currentSelection = currentSelection;\r
+ selection = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ }\r
+ \r
+ public void addPages() {\r
+ super.addPages();\r
+ mainPage = new WizardModulesImportPage(\r
+ "wizardModulesImportPage", initialPath, currentSelection); //$NON-NLS-1$\r
+ addPage(mainPage);\r
+ }\r
+\r
+ @Override\r
+ public boolean performFinish() {\r
+ return mainPage.createProjects();\r
+ \r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class WizardFunctionsExportPage extends WizardPage {\r
+ \r
+ // dialog store id constants\r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+\r
+ private IStructuredSelection currentSelection;\r
+\r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardFunctionsExportPage() {\r
+ this("wizardFunctionsExportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardFunctionsExportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardFunctionsExportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ this.currentSelection = currentSelection;\r
+ setPageComplete(false);\r
+ setTitle("Export Function Library");\r
+ setDescription("Choose the location where the selected Function Library is exportted. The exportted Function Library is selected from the Model Browser.");\r
+ }\r
+ \r
+ public void createControl(Composite parent) {\r
+ \r
+ initializeDialogUnits(parent);\r
+\r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+\r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+\r
+ createProjectsRoot(workArea);\r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+ \r
+ \r
+ // project specification group\r
+ \r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select export destination for Function Library:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+\r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+ \r
+ // project location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+\r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+ \r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ \r
+ final Resource functionLibrary2 = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ String name = null;\r
+ try {\r
+ name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ if (!graph.hasStatement(functionLibrary2, Layer0.getInstance(graph).PartOf))\r
+ return null;\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary2, l0.HasName, Bindings.STRING ));\r
+ return name;\r
+ \r
+ }\r
+ \r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ \r
+ final Shell shell = filePathField.getShell();\r
+ \r
+ FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
+ \r
+ String[] ext = {"*.tg"};\r
+ dialog.setFilterExtensions(ext);\r
+ \r
+ dialog.setFileName(name);\r
+ \r
+ dialog.setText("Export Function Library");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ previouslyBrowsedFile = selectedFile;\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ setPageComplete(true);\r
+ } \r
+\r
+ }\r
+ \r
+ public boolean createProjects(Resource selection) {\r
+ \r
+ final String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+ final Resource modulesymbol = selection;\r
+ if(modulesymbol == null) return false;\r
+ \r
+ final Resource functionLibrary = selection;\r
+ \r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));\r
+ ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
+ roots.add(Pair.make(functionLibrary, name));\r
+ TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));\r
+\r
+ try {\r
+ Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+\r
+ \r
+ }\r
+ });\r
+ \r
+ return true;\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CCombo;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.MessageBox;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class WizardFunctionsImportPage extends WizardPage{\r
+ \r
+ // dialog store id constants\r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+ \r
+ private Shell shell;\r
+ \r
+ private IStructuredSelection currentSelection;\r
+ \r
+ private Resource current;\r
+ \r
+ private Resource selectedModel;\r
+ \r
+ private String currentModelName;\r
+ \r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardFunctionsImportPage() {\r
+ this("wizardFunctionsImportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardFunctionsImportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardFunctionsImportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ setPageComplete(false);\r
+ this.currentSelection = currentSelection;\r
+ setTitle("Import Function Library to Model");\r
+ setDescription("Select Function Library from the local file system to be imported to the selected Model.");\r
+ }\r
+ \r
+ public void createControl(Composite parent) {\r
+ \r
+ initializeDialogUnits(parent);\r
+ \r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+ \r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+ \r
+ createProjectsRoot(workArea);\r
+ \r
+ createDropDown(workArea);\r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+ \r
+ //set label for field\r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select Function Library source:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+ \r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+\r
+ // module location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+\r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+\r
+ filePathField.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ previouslyBrowsedFile = filePathField.getText(); \r
+ }\r
+ });\r
+ if (previouslyBrowsedFile != null){\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ validatePage();\r
+ }\r
+ \r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ private void createDropDown(Composite workArea) {\r
+ \r
+ final ArrayList<Pair<Resource, String>> resources = new ArrayList<Pair<Resource, String>>();\r
+ \r
+ new Label(workArea, SWT.NONE).setText("Import target:"); \r
+ \r
+ //Get resources for the DropDown-selection\r
+ try{\r
+ SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ Resource model;\r
+ current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ //If there is no selection\r
+ if(current == null){\r
+ model = SimanticsUI.getProject().get();\r
+ }\r
+ else{\r
+ model = graph.getPossibleObject(current, l0.PartOf);\r
+ } \r
+ currentModelName = NameUtils.getSafeName(graph, current);\r
+ \r
+ //Check if selection is not in top-level folder\r
+ String modelName = NameUtils.getSafeName(graph, model);\r
+ while (!modelName.equalsIgnoreCase("Development Project")){\r
+ Resource previousResource = graph.getPossibleObject(model, l0.PartOf);\r
+ String previousName = NameUtils.getSafeName(graph, previousResource);\r
+ if (previousName.equalsIgnoreCase("Development Project")){\r
+ currentModelName = modelName;\r
+ }\r
+ //If selection is under Built-in Functions directory\r
+ if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){\r
+ model = SimanticsUI.getProject().get();\r
+ current = null;\r
+ break;\r
+ }\r
+ current = model;\r
+ model = graph.getPossibleObject(model, l0.PartOf);\r
+ modelName = NameUtils.getSafeName(graph, model);\r
+ } \r
+ for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ resources.add(Pair.make(r, name));\r
+ }\r
+ \r
+ };\r
+ });\r
+ }\r
+ catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ //Create dropdown-bar\r
+ final CCombo drop = new CCombo(workArea, SWT.BORDER);\r
+ {\r
+ drop.setEditable(false);\r
+ drop.setText("");\r
+ drop.setToolTipText("Selects the Model where to import the Module.");\r
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); \r
+ \r
+ //Fill the Dropdown-list\r
+ for(int a=0; a<resources.size(); a++){\r
+ drop.add(resources.get(a).second);\r
+ }\r
+ int selectionNumber = 0;\r
+ for(int a=0; a<resources.size(); a++){\r
+ if (currentModelName.equals(resources.get(a).second)){\r
+ selectionNumber = a;\r
+ }\r
+ }\r
+ //Show selected model as default\r
+ drop.select(selectionNumber);\r
+ selectedModel = current;\r
+ if (selectedModel == null){\r
+ selectedModel = resources.get(0).first;\r
+ }\r
+ \r
+ drop.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ \r
+ for(int a=0; a<resources.size(); a++){\r
+ if (drop.getText().equals(resources.get(a).second))\r
+ selectedModel = resources.get(a).first;\r
+ } \r
+ validatePage(); \r
+ }\r
+ });\r
+ }\r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ \r
+ shell = filePathField.getShell();\r
+ \r
+ FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
+ \r
+ String[] ext = {"*.tg"};\r
+ dialog.setFilterExtensions(ext);\r
+ \r
+ dialog.setText("Import Function Library");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ filePathField.setText(selectedFile);\r
+ validatePage();\r
+ } \r
+ }\r
+ \r
+ //Create project after finish is pressed.\r
+ public boolean createProjects() {\r
+ \r
+ String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+ \r
+ TransferableGraph1 tg = null;\r
+ try {\r
+ tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ return false;\r
+ } catch (IOException e) {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Function Library");\r
+ mb.open();\r
+ return false;\r
+ } \r
+ if(tg == null) return false;\r
+\r
+ \r
+ try {\r
+ Boolean hasSharedOntologies;\r
+ hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ graph.getResource("http://SharedOntologies");\r
+ } catch (ResourceNotFoundException e) {\r
+ return false;\r
+ } \r
+ return true;\r
+ }\r
+ });\r
+\r
+ if(!hasSharedOntologies) {\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ GraphUtils.create2(graph, l0.Library, \r
+ l0.HasName, "SharedOntologies",\r
+ l0.PartOf, graph.getResource("http:/"));\r
+ }\r
+ });\r
+\r
+ }\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ return false;\r
+ }\r
+\r
+ \r
+ SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(selectedModel);\r
+ try {\r
+ DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ final Resource root = ia.getRoot();\r
+ \r
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) {\r
+ Resource library = graph.getResource("http://SharedOntologies");\r
+ if(!graph.hasStatement(library, l0.ConsistsOf, root)) {\r
+ graph.claim(library, l0.ConsistsOf, root);\r
+ }\r
+\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Resource model = selectedModel;\r
+ while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology))\r
+ model = graph.getSingleObject(model, l0.PartOf);\r
+ if(graph.isInstanceOf(model, sr.SysdynModel)) {\r
+ graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root);\r
+ }\r
+\r
+ } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) {\r
+ Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf);\r
+ String type = "...";\r
+ if(instanceOf != null)\r
+ type = NameUtils.getSafeName(graph, instanceOf);\r
+ else {\r
+ Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits);\r
+ if(inheritedFrom != null)\r
+ type = NameUtils.getSafeName(graph, inheritedFrom);\r
+ }\r
+ final String ft = type; \r
+ graph.deny(root, l0.PartOf);\r
+ \r
+ shell.getDisplay().asyncExec(new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Function Library (" + ft +")");\r
+ mb.open(); \r
+ }\r
+ });\r
+ } else {\r
+ FunctionUtils.updateFunctionFileForLibrary(graph, selectedModel);\r
+ }\r
+\r
+ }\r
+ });\r
+ return true;\r
+ }\r
+ \r
+ private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {\r
+ \r
+ public SysdynFunctionLibraryImportAdvisor(Resource library) {\r
+ super(library);\r
+ }\r
+ \r
+ @Override\r
+ public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
+ if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
+ try {\r
+ library = graph.getResource("http://SharedOntologies");\r
+ } catch (ResourceNotFoundException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
+ Layer0 l0 = graph.getService(Layer0.class);\r
+ this.root = graph.newResource();\r
+ graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);\r
+ String name = root.name;\r
+ String newName = nameMappings.get(name);\r
+ graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
+ return this.root;\r
+\r
+ }\r
+ \r
+ }\r
+ void validatePage() {\r
+ \r
+ if (previouslyBrowsedFile.isEmpty()){\r
+ setPageComplete(false);\r
+ return;\r
+ }\r
+ \r
+ setPageComplete(true);\r
+ \r
+ }\r
+ \r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.mdlImport.MdlParser;\r
+import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class WizardMdlImportPage extends WizardPage{\r
+ \r
+ // dialog store id constants\r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+ \r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardMdlImportPage() {\r
+ this("wizardMdlImportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardMdlImportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardMdlImportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ setPageComplete(false);\r
+ setTitle("Import Vensim model");\r
+ setDescription("Select Vensim model -file (.mdl) from the local file system.");\r
+ }\r
+\r
+ public void createControl(Composite parent) {\r
+ \r
+ initializeDialogUnits(parent);\r
+\r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+\r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+\r
+ createProjectsRoot(workArea); \r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+\r
+ // set label for field\r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select Vensim model source:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+\r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+\r
+ // model location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+ \r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+ filePathField.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ previouslyBrowsedFile = filePathField.getText(); \r
+ }\r
+ });\r
+ if (previouslyBrowsedFile != null){\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ validatePage();\r
+ }\r
+ \r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ \r
+ final Shell shell = filePathField.getShell();\r
+\r
+ FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
+ String[] ext = {"*.mdl"};\r
+ dialog.setFilterExtensions(ext);\r
+ dialog.setText("Import Vensim model (.mdl)");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ filePathField.setText(selectedFile);\r
+ validatePage();\r
+ }\r
+\r
+ }\r
+ \r
+ //Create project after finish is pressed.\r
+ public boolean createProjects() {\r
+ \r
+ final Resource project = SimanticsUI.getProject().get();\r
+ if(project == null) return false;\r
+\r
+ String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+ \r
+ File file = new File(selected);\r
+ \r
+ final Model model = MdlParser.parse(file);\r
+ \r
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ model.write(graph, project);\r
+ }\r
+ });\r
+ \r
+ return true;\r
+ \r
+ }\r
+ void validatePage(){\r
+ \r
+ if (previouslyBrowsedFile.isEmpty()){\r
+ setPageComplete(false);\r
+ return;\r
+ }\r
+ \r
+ setPageComplete(true);\r
+ }\r
+}\r
+
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CCombo;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class WizardModelsExportPage extends WizardPage {\r
+ \r
+ // dialog store id constants\r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+ \r
+ private IStructuredSelection currentSelection;\r
+ \r
+ private Resource current;\r
+ \r
+ private Resource selectedModel;\r
+ \r
+ private String currentModelName;\r
+ \r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardModelsExportPage() {\r
+ this("wizardModelsExportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardModelsExportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardModelsExportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ this.currentSelection = currentSelection;\r
+ setPageComplete(false);\r
+ setTitle("Export Model");\r
+ setDescription("Choose the location where the selected Model is exportted. The exportted Model is selected from the dropdown-menu.");\r
+ }\r
+ \r
+ public void createControl(Composite parent) {\r
+ \r
+ initializeDialogUnits(parent);\r
+\r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+\r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+ \r
+ createDropDown (workArea);\r
+ createProjectsRoot(workArea);\r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+ \r
+ // set label for field\r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select export destination for Model:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+\r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+ \r
+ // model location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+\r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+ \r
+ filePathField.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ previouslyBrowsedFile = filePathField.getText(); \r
+ }\r
+ });\r
+ if (previouslyBrowsedFile != null){\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ validatePage();\r
+ }\r
+\r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ private void createDropDown(Composite workArea) {\r
+ \r
+ final ArrayList<Pair<Resource, String>> resources = new ArrayList<Pair<Resource, String>>();\r
+ \r
+ new Label(workArea, SWT.NONE).setText("Select Model to export:"); \r
+ \r
+ //Get resources for the DropDown-selection\r
+ try{\r
+ SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ Resource model;\r
+ current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ //If there is no selection\r
+ if(current == null){\r
+ model = SimanticsUI.getProject().get();\r
+ }\r
+ else{\r
+ model = graph.getPossibleObject(current, l0.PartOf);\r
+ }\r
+ currentModelName = NameUtils.getSafeName(graph, current);\r
+ \r
+ //Check if selection is not in top-level folder\r
+ String modelName = NameUtils.getSafeName(graph, model);\r
+ while (!modelName.equalsIgnoreCase("Development Project")){\r
+ Resource previousResource = graph.getPossibleObject(model, l0.PartOf);\r
+ String previousName = NameUtils.getSafeName(graph, previousResource);\r
+ if (previousName.equalsIgnoreCase("Development Project")){\r
+ currentModelName = modelName;\r
+ }\r
+ //If selection is under Built-in Functions directory\r
+ if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){\r
+ model = SimanticsUI.getProject().get();\r
+ current = null;\r
+ break;\r
+ }\r
+ current = model;\r
+ model = graph.getPossibleObject(model, l0.PartOf);\r
+ modelName = NameUtils.getSafeName(graph, model);\r
+ }\r
+ for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ resources.add(Pair.make(r, name));\r
+ }\r
+ \r
+ };\r
+ });\r
+ }\r
+ catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ //Create dropdown-bar\r
+ final CCombo drop = new CCombo(workArea, SWT.BORDER);\r
+ {\r
+ drop.setEditable(false);\r
+ drop.setText("");\r
+ drop.setToolTipText("Selects the Model where to import the Module.");\r
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); \r
+ \r
+ //Fill the Dropdown-list\r
+ for(int a=0; a<resources.size(); a++){\r
+ drop.add(resources.get(a).second);\r
+ }\r
+ int selectionNumber = 0;\r
+ for(int a=0; a<resources.size(); a++){\r
+ if (currentModelName.equals(resources.get(a).second)){\r
+ selectionNumber = a;\r
+ }\r
+ }\r
+ //Show selected model as default\r
+ drop.select(selectionNumber);\r
+ selectedModel = current;\r
+ if (selectedModel == null){\r
+ selectedModel = resources.get(0).first;\r
+ }\r
+ \r
+ drop.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ \r
+ for(int a=0; a<resources.size(); a++){\r
+ if (drop.getText().equals(resources.get(a).second))\r
+ selectedModel = resources.get(a).first;\r
+ } \r
+ validatePage(); \r
+ }\r
+ });\r
+ }\r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ final Shell shell = filePathField.getShell();\r
+ \r
+ FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
+ \r
+ String[] ext = {"*.tg"};\r
+ dialog.setFilterExtensions(ext);\r
+ \r
+ dialog.setText("Export Model");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ filePathField.setText(selectedFile);\r
+ validatePage();\r
+ } \r
+\r
+ }\r
+ \r
+ public boolean createProjects(Resource selection) {\r
+ \r
+ final String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+ \r
+ // FIXME: Model browser doesn't change its selection even if the selected object is removed,\r
+ // so you can try to export a removed model \r
+ \r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ String name = graph.syncRequest(new PossibleRelatedValue<String>(selectedModel, l0.HasName, Bindings.STRING ));\r
+ ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
+ roots.add(Pair.make(selectedModel, name));\r
+ TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, selectedModel));\r
+\r
+ try {\r
+ Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ });\r
+\r
+ return true;\r
+ }\r
+ \r
+ void validatePage() {\r
+ \r
+ if (previouslyBrowsedFile.isEmpty()){\r
+ setPageComplete(false);\r
+ return;\r
+ }\r
+ \r
+ setPageComplete(true);\r
+ \r
+ }\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.HashSet;\r
+import java.util.UUID;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.MessageBox;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.OldTransferableGraph1;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class WizardModelsImportPage extends WizardPage{\r
+ \r
+ // dialog store id constants\r
+ \r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+\r
+ private Shell shell;\r
+ \r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardModelsImportPage() {\r
+ this("wizardModelsImportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardModelsImportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardModelsImportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ //this.initialPath = initialPath;\r
+ //this.currentSelection = currentSelection;\r
+ setPageComplete(false);\r
+ setTitle("Import Model");\r
+ setDescription("Select Model file from the local file system.");\r
+ }\r
+\r
+ \r
+ public void createControl(Composite parent) {\r
+\r
+ initializeDialogUnits(parent);\r
+\r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+\r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+\r
+ createProjectsRoot(workArea);\r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+\r
+ // set label for field\r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select Model source:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+\r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+\r
+ // model location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+ \r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+ filePathField.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ previouslyBrowsedFile = filePathField.getText(); \r
+ }\r
+ });\r
+ if (previouslyBrowsedFile != null){\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ validatePage();\r
+ }\r
+ \r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ }); \r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ \r
+ shell = filePathField.getShell();\r
+ \r
+ FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
+ \r
+ String[] ext = {"*.tg"};\r
+ dialog.setFilterExtensions(ext);\r
+ \r
+ dialog.setText("Import Model");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ filePathField.setText(selectedFile);\r
+ validatePage();\r
+ }\r
+ }\r
+\r
+ //Create project after finish is pressed.\r
+ public boolean createProjects() {\r
+ \r
+ Resource project = SimanticsUI.getProject().get();\r
+ if(project == null) return false;\r
+ \r
+ String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+ \r
+ TransferableGraph1 tg = null;\r
+ try {\r
+ tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) { \r
+ try {\r
+ OldTransferableGraph1 otg = (OldTransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(OldTransferableGraph1.class));\r
+ tg = new TransferableGraph1(otg.resourceCount, otg.identities, otg.statements, otg.values);\r
+ } catch (RuntimeBindingConstructionException e1) {\r
+ e1.printStackTrace();\r
+ } catch (IOException e1) {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Model");\r
+ mb.open();\r
+ return false;\r
+ }\r
+ }\r
+ if(tg == null) return false;\r
+ \r
+ try {\r
+ \r
+ DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project);\r
+ DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia);\r
+ \r
+ // Check that imported resource was actually a model \r
+ //and fix changes made to old ontology versions\r
+ final Resource root = ia.getRoot();\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ \r
+ if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModel)) {\r
+ Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
+ String type = "...";\r
+ if(instanceOf != null)\r
+ type = NameUtils.getSafeName(graph, instanceOf);\r
+ else {\r
+ Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
+ if(inheritedFrom != null)\r
+ type = NameUtils.getSafeName(graph, inheritedFrom);\r
+ }\r
+ final String ft = type; \r
+ graph.deny(root, Layer0.getInstance(graph).PartOf);\r
+ \r
+ shell.getDisplay().asyncExec(new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: System Dynamics Model (" + ft +")");\r
+ mb.open(); \r
+ }\r
+ });\r
+\r
+ } else {\r
+ updateOldConfigurationToBaseRealization(graph, root);\r
+ addDefaultOntologyLinks(graph, root);\r
+ addURIsToDiagrams(graph, root);\r
+ addSpreadSheetBook(graph, root);\r
+ }\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ return true; \r
+ }\r
+ \r
+ /**\r
+ * In old versions base realization was separate. Newer versions use configuration as base realization. \r
+ * @param graph WriteGraph\r
+ * @param model Imported model\r
+ */\r
+ private static void updateOldConfigurationToBaseRealization(WriteGraph graph, Resource model) {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ try {\r
+ Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
+ if(configuration != null && !graph.hasStatement(configuration, l0.IsBaseRealizationOf, model))\r
+ graph.claim(configuration, l0.IsBaseRealizationOf, model);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+ /**\r
+ * Links should be exported and imported automatically. If it has failed, the \r
+ * default ontology links sysdyn and layer0 are added.\r
+ * \r
+ * @param graph WriteGraph\r
+ * @param model Imported model\r
+ */\r
+ \r
+ private static void addDefaultOntologyLinks(WriteGraph graph, Resource model) {\r
+ try {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ // The links should be exported and imported automatically\r
+ Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.0");\r
+ Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.0");\r
+ if(!graph.hasStatement(model, l0.IsLinkedTo, sysdyn))\r
+ graph.claim(model, l0.IsLinkedTo, sysdyn);\r
+ if(!graph.hasStatement(model, l0.IsLinkedTo, layer0))\r
+ graph.claim(model, l0.IsLinkedTo, layer0);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ private static void addURIsToDiagrams(WriteGraph graph, Resource model) {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SimulationResource simu = SimulationResource.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+ try {\r
+ HashSet<Resource> configurations = new HashSet<Resource>();\r
+ \r
+ Resource configuration = graph.getPossibleObject(model, simu.HasConfiguration);\r
+ if(configuration != null) \r
+ configurations.add(configuration);\r
+ \r
+ for(Resource r : graph.getObjects(model, l0.ConsistsOf)) {\r
+ if(graph.isInheritedFrom(r, sr.Module)) {\r
+ Resource moduleConfiguration = graph.getPossibleObject(r, sr2.IsDefinedBy);\r
+ if(moduleConfiguration != null)\r
+ configurations.add(moduleConfiguration);\r
+ }\r
+ }\r
+ \r
+ for(Resource conf : configurations) {\r
+ Resource configurationDiagram = graph.getPossibleObject(conf, mr.CompositeToDiagram);\r
+ if(configurationDiagram != null && !graph.hasStatement(configurationDiagram, l0.PartOf)) {\r
+ GraphUtils.create2(graph, l0.Library, \r
+ l0.HasName, "__CONTAINER__",\r
+ l0.PartOf, conf,\r
+ l0.ConsistsOf, configurationDiagram);\r
+ }\r
+ }\r
+ \r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Add a missing spreadsheet book to the model\r
+ * \r
+ * @param graph\r
+ * @param model\r
+ */\r
+ private static void addSpreadSheetBook(WriteGraph graph, Resource model) {\r
+ try {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SpreadsheetResource ssr = SpreadsheetResource.getInstance(graph);\r
+ SimulationResource simu = SimulationResource.getInstance(graph);\r
+ Resource conf = graph.getPossibleObject(model, simu.HasConfiguration);\r
+ if(conf != null && graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, ssr.Book)).isEmpty()) {\r
+ Resource book = graph.newResource();\r
+ graph.claim(book, l0.InstanceOf, null, ssr.Book);\r
+ graph.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING);\r
+ graph.claim(conf, l0.ConsistsOf, l0.PartOf, book);\r
+\r
+ createSheet(graph, book, "Sheet1", new String[] { }, new int[] { 50 });\r
+ }\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Create a sheet (Copied from SysdynProject)\r
+ * \r
+ * @param graph\r
+ * @param book\r
+ * @param name\r
+ * @param colNames\r
+ * @param colWidths\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ private static Resource createSheet(WriteGraph graph, Resource book, String name, String[] colNames, int[] colWidths) throws DatabaseException {\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
+\r
+ Resource result = graph.newResource();\r
+ graph.claim(result, L0.InstanceOf, null, sr.Spreadsheet);\r
+\r
+ if(name == null) {\r
+ name = NameUtils.findFreshEscapedName(graph, "Sheet", book, sr.HasSheet);\r
+ }\r
+ graph.claimLiteral(result, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
+ graph.claim(book, L0.ConsistsOf, L0.PartOf, result);\r
+\r
+ {\r
+ Resource newCell = graph.newResource();\r
+ graph.claim(newCell, L0.InstanceOf, null, sr.Cell);\r
+ graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Dimensions", Bindings.STRING);\r
+ graph.addLiteral(newCell, sr.FitColumns, sr.FitColumnsOf, L0.Boolean, false, Bindings.BOOLEAN);\r
+ graph.addLiteral(newCell, sr.FitRows, sr.FitRowsOf, L0.Boolean, false, Bindings.BOOLEAN);\r
+ graph.addLiteral(newCell, sr.ColumnCount, sr.ColumnCountOf, L0.Integer, 128, Bindings.INTEGER);\r
+ graph.addLiteral(newCell, sr.RowCount, sr.RowCountOf, L0.Integer, 256, Bindings.INTEGER);\r
+ graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell);\r
+ }\r
+\r
+ {\r
+ Resource newCell = graph.newResource();\r
+ graph.claim(newCell, L0.InstanceOf, null, sr.Cell);\r
+ graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Headers", Bindings.STRING);\r
+ graph.addLiteral(newCell, sr.ColumnLabels, sr.ColumnLabelsOf, L0.StringArray, colNames, Bindings.STRING_ARRAY);\r
+ graph.addLiteral(newCell, sr.ColumnWidths, sr.ColumnWidthsOf, L0.IntegerArray, colWidths, Bindings.INT_ARRAY);\r
+ graph.claim(result, L0.ConsistsOf, L0.PartOf, newCell);\r
+ }\r
+\r
+ {\r
+ \r
+ double[] doubles = new double[10*2];\r
+ for(int i=0;i<10*2;i++) doubles[i] = i;\r
+ \r
+ Resource newCell = graph.newResource();\r
+ graph.claim(newCell, L0.InstanceOf, null, sr.DoubleArrayCell);\r
+ graph.addLiteral(newCell, sr.DoubleArrayCell_HasWidth, sr.DoubleArrayCell_HasWidth_Inverse, L0.Integer, 10, Bindings.INTEGER);\r
+ graph.addLiteral(newCell, sr.HasLocation, sr.HasLocation_Inverse, L0.String, "B2", Bindings.STRING);\r
+ graph.addLiteral(newCell, sr.DoubleArrayCell_HasDoubleArray, sr.DoubleArrayCell_HasDoubleArray_Inverse, L0.DoubleArray, doubles, Bindings.DOUBLE_ARRAY);\r
+ graph.claim(result, L0.HasChildVariables, L0.HasChildVariables_Inverse, newCell);\r
+ \r
+ }\r
+ \r
+ return result;\r
+\r
+ }\r
+ \r
+ void validatePage(){\r
+ \r
+ if (previouslyBrowsedFile.isEmpty()){\r
+ setPageComplete(false);\r
+ return;\r
+ }\r
+ \r
+ setPageComplete(true);\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CCombo;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class WizardModulesExportPage extends WizardPage {\r
+ \r
+ // dialog store id constants\r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+ \r
+ private IStructuredSelection currentSelection;\r
+ \r
+ private Resource current;\r
+ \r
+ private Resource selectedModel;\r
+ \r
+ private Resource selectedModule;\r
+ \r
+ private String currentModelName;\r
+ \r
+ private CCombo drop;\r
+ \r
+ private CCombo drop2;\r
+ \r
+ private ArrayList<Pair<Resource, String>> resources = new ArrayList<Pair<Resource, String>>();\r
+ \r
+ private ArrayList<Pair<Resource, String>> modules = new ArrayList<Pair<Resource, String>>();\r
+\r
+ \r
+ \r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardModulesExportPage() {\r
+ this("wizardModulesExportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardModulesExportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardModulesExportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ this.currentSelection = currentSelection;\r
+ setPageComplete(false);\r
+ setTitle("Export Module");\r
+ setDescription("Choose the Module and the export location.");\r
+ }\r
+ \r
+ public void createControl(Composite parent) {\r
+ \r
+ initializeDialogUnits(parent);\r
+\r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+\r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+\r
+ createDropDownSelections(workArea);\r
+ createProjectsRoot(workArea);\r
+ \r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+ \r
+ // set label for field\r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select export destination for Module:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+\r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+ \r
+ // module location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+\r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+ \r
+ filePathField.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ previouslyBrowsedFile = filePathField.getText(); \r
+ }\r
+ });\r
+ if (previouslyBrowsedFile != null){\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ validatePage();\r
+ }\r
+ \r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ private void createDropDownSelections(final Composite workArea) {\r
+ \r
+ new Label(workArea, SWT.NONE).setText("Select the Model which contains the Module:"); \r
+ \r
+ //Find available models\r
+ findModels();\r
+ \r
+ //Create Model dropdown-bar\r
+ drop = new CCombo(workArea, SWT.BORDER);\r
+ \r
+ drop.setEditable(false);\r
+ drop.setText("");\r
+ drop.setToolTipText("Selects the Model where to export the Module.");\r
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); \r
+ \r
+ //Fill the Model Dropdown-list\r
+ for(int a=0; a<resources.size(); a++){\r
+ drop.add(resources.get(a).second);\r
+ }\r
+ int selectionNumber = 0;\r
+ for(int a=0; a<resources.size(); a++){\r
+ if (currentModelName.equals(resources.get(a).second)){\r
+ selectionNumber = a;\r
+ }\r
+ }\r
+ //Show selected model as default\r
+ drop.select(selectionNumber);\r
+ selectedModel = current;\r
+ if (selectedModel == null){\r
+ selectedModel = resources.get(0).first;\r
+ }\r
+ \r
+ //Create Module dropdown-bar\r
+ new Label(workArea, SWT.NONE).setText("Select the exportted Module:"); \r
+ drop2 = new CCombo(workArea, SWT.BORDER);\r
+ drop2.setEditable(false);\r
+ drop2.setText("");\r
+ drop2.setToolTipText("Selects the Module to be exportted.");\r
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop2); \r
+ \r
+ //Update module list according to model list\r
+ updateModules();\r
+ \r
+ //Listeners for drop downs\r
+ drop.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ \r
+ for(Pair<Resource, String> resource : resources){\r
+ if (drop.getText().equals(resource.second))\r
+ selectedModel = resource.first;\r
+ } \r
+ \r
+ updateModules();\r
+ validatePage();\r
+ }\r
+ });\r
+ \r
+ drop2.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ \r
+ for(int a=0; a<modules.size(); a++){\r
+ if (drop2.getText().equals(modules.get(a).second))\r
+ selectedModule = modules.get(a).first;\r
+ } \r
+ validatePage(); \r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ \r
+ final Shell shell = filePathField.getShell();\r
+ \r
+ FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
+ \r
+ String[] ext = {"*.tg"};\r
+ dialog.setFilterExtensions(ext);\r
+ \r
+ dialog.setText("Export Module");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ filePathField.setText(selectedFile);\r
+ validatePage();\r
+ } \r
+\r
+ }\r
+ \r
+ private void findModels(){\r
+ \r
+ //Get resources for the DropDown-selection\r
+ try{\r
+ SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ Resource model;\r
+ current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ //If there is no selection\r
+ if(current == null){\r
+ model = SimanticsUI.getProject().get();\r
+ }\r
+ else{\r
+ model = graph.getPossibleObject(current, l0.PartOf);\r
+ } \r
+ currentModelName = NameUtils.getSafeName(graph, current);\r
+ \r
+ //Check if selection is not in top-level folder\r
+ String modelName = NameUtils.getSafeName(graph, model);\r
+ while (!modelName.equalsIgnoreCase("Development Project")){\r
+ Resource previousResource = graph.getPossibleObject(model, l0.PartOf);\r
+ String previousName = NameUtils.getSafeName(graph, previousResource);\r
+ if (previousName.equalsIgnoreCase("Development Project")){\r
+ currentModelName = modelName;\r
+ }\r
+ //If selection is under Built-in Functions directory\r
+ if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){\r
+ model = SimanticsUI.getProject().get();\r
+ current = null;\r
+ break;\r
+ }\r
+ current = model;\r
+ model = graph.getPossibleObject(model, l0.PartOf);\r
+ modelName = NameUtils.getSafeName(graph, model);\r
+ }\r
+ for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ resources.add(Pair.make(r, name));\r
+ }\r
+ \r
+ };\r
+ });\r
+ }\r
+ catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+ \r
+ private void findModules(){\r
+ \r
+ try{\r
+ SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+ //modules.removeAll(null);\r
+ modules = new ArrayList<Pair<Resource, String>>();\r
+ for (Resource r : graph.syncRequest(new ObjectsWithType(selectedModel, l0.ConsistsOf, sr2.ComponentType))){\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ modules.add(Pair.make(r, name));\r
+ }\r
+ \r
+ };\r
+ });\r
+ }\r
+ catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ private void updateModules(){\r
+ \r
+ //Get resources for the DropDown-selection\r
+ findModules(); \r
+ \r
+ //Clear the modules dropdown-list\r
+ if (drop2 != null){\r
+ drop2.removeAll();\r
+ \r
+ \r
+ //Fill the Modules dropdown-list\r
+ for(Pair<Resource, String> module : modules){\r
+ drop2.add(module.second);\r
+ }\r
+ int selectionNumber = 0;\r
+ for(int a=0; a<modules.size(); a++){\r
+ if (currentModelName.equals(modules.get(a).second)){\r
+ selectionNumber = a;\r
+ }\r
+ }\r
+ //Show selected module as default\r
+ drop2.select(selectionNumber);\r
+ selectedModel = current;\r
+ if (selectedModel == null){\r
+ selectedModel = modules.get(0).first;\r
+ }\r
+ }\r
+ \r
+ }\r
+ \r
+ public boolean createProjects() {\r
+ \r
+ final String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+\r
+ String name = null;\r
+ try {\r
+ name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ \r
+ Resource component = selectedModule;\r
+ // Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType);\r
+ if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))\r
+ return null;\r
+ \r
+ \r
+ Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
+ if (configuration == null)\r
+ return null;\r
+ \r
+ ArrayList<String> dependencies = null;\r
+ for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {\r
+ if(dependencies == null)\r
+ dependencies = new ArrayList<String>();\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ String instanceOf = NameUtils.getSafeName(graph, graph.getSingleObject(r, l0.InstanceOf));\r
+ dependencies.add(name + " : " + instanceOf);\r
+ }\r
+ if(dependencies != null && !dependencies.isEmpty())\r
+ throw new ContainsDependenciesException(dependencies);\r
+ \r
+ String name = graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING);\r
+ return name;\r
+ \r
+ }\r
+ \r
+ });\r
+ } \r
+ catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+// if(name == null) return false;\r
+ \r
+ \r
+ // Activator.getDefault().getPreferenceStore().setValue(ImportModuleHandler.IMPORTMODULETPATH, (new File(selected)).getParent());\r
+ \r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ \r
+ final Resource component = selectedModule;\r
+ //final Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType);\r
+ if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))\r
+ return;\r
+ String name = graph.syncRequest(new PossibleRelatedValue<String>(component, l0.HasName, Bindings.STRING ));\r
+ final ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
+ roots.add(Pair.make(component, name));\r
+\r
+ graph.asyncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ final Resource modulesymbol = graph.getPossibleObject(component, mr.ComponentTypeToSymbol);\r
+ Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
+ if (!graph.hasStatement(configuration, l0.PartOf, component)&& \r
+ !graph.hasStatement(modulesymbol, l0.PartOf, component)) {\r
+ // Make sure that configuration and symbol are included.\r
+ // In old versions, they were attached to model, not to module.\r
+ Resource previousPartof = graph.getSingleObject(configuration, l0.PartOf);\r
+\r
+ graph.deny(configuration, l0.PartOf);\r
+ graph.deny(modulesymbol, l0.PartOf);\r
+ graph.claim(configuration, l0.PartOf, l0.ConsistsOf, component);\r
+ graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, component);\r
+\r
+ export(graph, selected, roots, component);\r
+\r
+ graph.deny(configuration, l0.PartOf);\r
+ graph.deny(modulesymbol, l0.PartOf);\r
+ graph.claim(configuration, l0.PartOf, l0.ConsistsOf, previousPartof);\r
+ graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, previousPartof);\r
+ } else {\r
+ // Normal export\r
+ export(graph, selected, roots, component);\r
+ }\r
+ }\r
+ });\r
+\r
+ }\r
+ });\r
+\r
+ return true;\r
+ }\r
+ \r
+ private void export(WriteGraph graph, String path, ArrayList<Pair<Resource, String>> roots, Resource component) {\r
+ \r
+ // FIXME: Enumeration replacement handling like this is not suitable.\r
+ try {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
+\r
+ Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);\r
+ ArrayList<Pair<Resource, Resource>> replacements = new ArrayList<Pair<Resource, Resource>>();\r
+\r
+ for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) {\r
+ if(graph.hasStatement(enumeration, sr.ReplacedEnumeration_Inverse)) {\r
+ for(Resource replacement : graph.getObjects(enumeration, sr.ReplacedEnumeration_Inverse)) {\r
+ replacements.add(new Pair<Resource, Resource>(enumeration, replacement));\r
+ }\r
+ }\r
+ }\r
+ \r
+ for(Pair<Resource,Resource> replacement : replacements)\r
+ graph.deny(replacement.first, sr.ReplacedEnumeration_Inverse, replacement.second);\r
+ \r
+ TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component));\r
+ Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+ \r
+ for(Pair<Resource,Resource> replacement : replacements)\r
+ graph.claim(replacement.first, sr.ReplacedEnumeration_Inverse, replacement.second);\r
+\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ class ContainsDependenciesException extends DatabaseException {\r
+ private static final long serialVersionUID = -1533706136673146020L;\r
+ \r
+ private Collection<String> dependencies;\r
+ \r
+ ContainsDependenciesException(Collection<String> dependencies) {\r
+ this.dependencies = dependencies;\r
+ }\r
+ \r
+ public Collection<String> getDependencies() {\r
+ return this.dependencies;\r
+ }\r
+ \r
+ }\r
+ \r
+ void validatePage() {\r
+ \r
+ if (previouslyBrowsedFile.isEmpty()){\r
+ setPageComplete(false);\r
+ return;\r
+ }\r
+ \r
+ setPageComplete(true);\r
+ \r
+ }\r
+ \r
+\r
+\r
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.sysdyn.ui.wizards;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.PixelConverter;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CCombo;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.MessageBox;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.graph.db.MissingDependencyException;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+\r
+public class WizardModulesImportPage extends WizardPage{\r
+ \r
+ public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";\r
+ \r
+ // dialog store id constants\r
+ \r
+ private Text filePathField;\r
+ \r
+ // Keep track of the archive that we browsed to last time\r
+ // the wizard was invoked.\r
+ private static String previouslyBrowsedFile = "";\r
+\r
+ private Button browseDirectoriesButton;\r
+\r
+ private Shell shell;\r
+ \r
+ private IStructuredSelection currentSelection;\r
+ \r
+ private Resource current;\r
+ \r
+ private Resource selectedModel;\r
+ \r
+ private String currentModelName;\r
+ \r
+ /**\r
+ * Creates a new project creation wizard page.\r
+ * \r
+ */\r
+ public WizardModulesImportPage() {\r
+ this("wizardModulesImportPage", null, null); //$NON-NLS-1$\r
+ }\r
+\r
+ /**\r
+ * Create a new instance of the receiver.\r
+ * \r
+ * @param pageName\r
+ */\r
+ public WizardModulesImportPage(String pageName) {\r
+ this(pageName,null, null);\r
+ }\r
+ \r
+ /**\r
+ * More (many more) parameters.\r
+ * \r
+ * @param pageName\r
+ * @param initialPath\r
+ * @param currentSelection\r
+ * @since 3.5\r
+ */\r
+ public WizardModulesImportPage(String pageName,String initialPath,\r
+ IStructuredSelection currentSelection) {\r
+ super(pageName);\r
+ setPageComplete(false);\r
+ this.currentSelection = currentSelection;\r
+ setTitle("Import Module to Model");\r
+ setDescription("Select Module-file from the local file system.");\r
+ }\r
+ \r
+ public void createControl(Composite parent) {\r
+ \r
+ initializeDialogUnits(parent);\r
+\r
+ Composite workArea = new Composite(parent, SWT.NONE);\r
+ setControl(workArea);\r
+\r
+ workArea.setLayout(new GridLayout());\r
+ workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
+ | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
+\r
+ createProjectsRoot(workArea);\r
+ \r
+ createDropDown(workArea);\r
+ }\r
+ \r
+ private void createProjectsRoot(Composite workArea) {\r
+ \r
+ // set label for field\r
+ Label title = new Label(workArea, SWT.NONE);\r
+ title.setText("Select Module source:");\r
+ \r
+ Composite projectGroup = new Composite(workArea, SWT.NONE);\r
+ GridLayout layout = new GridLayout();\r
+ layout.numColumns = 2;\r
+ layout.makeColumnsEqualWidth = false;\r
+ layout.marginWidth = 0;\r
+ \r
+ projectGroup.setLayout(layout);\r
+ projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
+ \r
+ // module location entry field\r
+ this.filePathField = new Text(projectGroup, SWT.BORDER);\r
+\r
+ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
+ directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
+ filePathField.setLayoutData(directoryPathData);\r
+ \r
+ filePathField.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ previouslyBrowsedFile = filePathField.getText(); \r
+ }\r
+ });\r
+ if (previouslyBrowsedFile != null){\r
+ filePathField.setText(previouslyBrowsedFile);\r
+ validatePage();\r
+ }\r
+ \r
+ // browse button\r
+ browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
+ browseDirectoriesButton.setText("Browse");\r
+ setButtonLayoutData(browseDirectoriesButton);\r
+ \r
+ browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
+ * elected(org.eclipse.swt.events.SelectionEvent)\r
+ */\r
+ public void widgetSelected(SelectionEvent e) {\r
+ handleLocationDirectoryButtonPressed();\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ private void createDropDown(Composite workArea) {\r
+ \r
+ final ArrayList<Pair<Resource, String>> resources = new ArrayList<Pair<Resource, String>>();\r
+ \r
+ new Label(workArea, SWT.NONE).setText("Import target:"); \r
+ \r
+ //Get resources for the DropDown-selection\r
+ try{\r
+ SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ Resource model;\r
+ current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class);\r
+ //If there is no selection\r
+ if(current == null){\r
+ model = SimanticsUI.getProject().get();\r
+ }\r
+ else{\r
+ model = graph.getPossibleObject(current, l0.PartOf);\r
+ } \r
+ currentModelName = NameUtils.getSafeName(graph, current);\r
+ \r
+ //Check if selection is not in top-level folder\r
+ String modelName = NameUtils.getSafeName(graph, model);\r
+ while (!modelName.equalsIgnoreCase("Development Project")){\r
+ Resource previousResource = graph.getPossibleObject(model, l0.PartOf);\r
+ String previousName = NameUtils.getSafeName(graph, previousResource);\r
+ if (previousName.equalsIgnoreCase("Development Project")){\r
+ currentModelName = modelName;\r
+ }\r
+ //If selection is under Built-in Functions directory\r
+ if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){\r
+ model = SimanticsUI.getProject().get();\r
+ current = null;\r
+ break;\r
+ }\r
+ current = model;\r
+ model = graph.getPossibleObject(model, l0.PartOf);\r
+ modelName = NameUtils.getSafeName(graph, model);\r
+ }\r
+ for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){\r
+ String name = NameUtils.getSafeName(graph, r);\r
+ resources.add(Pair.make(r, name));\r
+ }\r
+ \r
+ };\r
+ });\r
+ }\r
+ catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ //Create dropdown-bar\r
+ final CCombo drop = new CCombo(workArea, SWT.BORDER);\r
+ {\r
+ drop.setEditable(false);\r
+ drop.setText("");\r
+ drop.setToolTipText("Selects the Model where to import the Module.");\r
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); \r
+ \r
+ //Fill the Dropdown-list\r
+ for(int a=0; a<resources.size(); a++){\r
+ drop.add(resources.get(a).second);\r
+ }\r
+ int selectionNumber = 0;\r
+ for(int a=0; a<resources.size(); a++){\r
+ if (currentModelName.equals(resources.get(a).second)){\r
+ selectionNumber = a;\r
+ }\r
+ }\r
+ //Show selected model as default\r
+ drop.select(selectionNumber);\r
+ selectedModel = current;\r
+ if (selectedModel == null){\r
+ selectedModel = resources.get(0).first;\r
+ }\r
+ \r
+ drop.addModifyListener(new ModifyListener(){\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ \r
+ for(int a=0; a<resources.size(); a++){\r
+ if (drop.getText().equals(resources.get(a).second))\r
+ selectedModel = resources.get(a).first;\r
+ } \r
+ validatePage(); \r
+ }\r
+ });\r
+ }\r
+ }\r
+ \r
+ //Set filePathField active\r
+ public void setVisible(boolean visible) {\r
+ super.setVisible(visible);\r
+ this.filePathField.setFocus();\r
+ }\r
+ \r
+ //Open dialog for choosing the file\r
+ protected void handleLocationDirectoryButtonPressed() {\r
+ \r
+ shell = filePathField.getShell();\r
+ \r
+ FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
+ \r
+ String[] ext = {"*.tg"};\r
+ dialog.setFilterExtensions(ext);\r
+ \r
+ dialog.setText("Import Module");\r
+\r
+ String dirName = filePathField.getText().trim();\r
+ \r
+ File path = new File(dirName);\r
+ if (path.exists()) {\r
+ dialog.setFilterPath(new Path(dirName).toOSString()); \r
+ }\r
+ \r
+ String selectedFile = dialog.open();\r
+ if (selectedFile != null) {\r
+ filePathField.setText(selectedFile);\r
+ validatePage();\r
+ } \r
+ }\r
+ \r
+ //Create project after finish is pressed.\r
+ public boolean createProjects() {\r
+ \r
+ String selected = previouslyBrowsedFile;\r
+ if(selected == null) return false;\r
+ \r
+ TransferableGraph1 tg = null;\r
+ try {\r
+ tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: Module Type");\r
+ mb.open();\r
+ return false;\r
+ }\r
+ if(tg == null) return false;\r
+\r
+ \r
+ DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(selectedModel);\r
+ try {\r
+ DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);\r
+ } catch (MissingDependencyException e) {\r
+ e.printStackTrace();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ final Resource root = ia.getRoot();\r
+ \r
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {\r
+ Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);\r
+ String type = "...";\r
+ if(instanceOf != null)\r
+ type = NameUtils.getSafeName(graph, instanceOf);\r
+ else {\r
+ Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);\r
+ if(inheritedFrom != null)\r
+ type = NameUtils.getSafeName(graph, inheritedFrom);\r
+ }\r
+ final String ft = type; \r
+ graph.deny(root, Layer0.getInstance(graph).PartOf);\r
+ \r
+ shell.getDisplay().asyncExec(new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR);\r
+ mb.setText("Error");\r
+ mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")");\r
+ mb.open(); \r
+ }\r
+ });\r
+ }\r
+ }\r
+ });\r
+ return true;\r
+ }\r
+ \r
+ void validatePage() {\r
+ \r
+ if (previouslyBrowsedFile.isEmpty()){\r
+ setPageComplete(false);\r
+ return;\r
+ }\r
+ \r
+ setPageComplete(true);\r
+ \r
+ }\r
+ \r
+}
\ No newline at end of file