From d03cc201506762db02d9705baae6ab2ffd8af3af Mon Sep 17 00:00:00 2001 From: melander Date: Tue, 16 Aug 2011 06:22:44 +0000 Subject: [PATCH] git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21749 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../{ => functions}/ExportWizardFunction.java | 2 +- .../FunctionTreeFunctionLabeler.java | 20 + .../functions/FunctionTreeLibraries.java | 37 ++ .../functions/FunctionTreeModelLabeler.java | 17 + .../wizards/functions/FunctionTreeModels.java | 43 ++ .../FunctionTreeSharedFolderLabeler.java | 15 + .../FunctionTreeSharedLibraries.java | 35 ++ .../FunctionTreeSharedLibraryLabeler.java | 17 + .../functions/FunctionTreeSubLibraries.java | 36 ++ .../{ => functions}/ImportWizardFunction.java | 6 +- .../functions/SharedFunctionsFolderNode.java | 65 +++ .../WizardFunctionsExportPage.java | 215 ++++++++-- .../WizardFunctionsImportPage.java | 402 +++++++++--------- .../ui/wizards/{ => mdl}/ImportWizardMdl.java | 2 +- .../{ => mdl}/WizardMdlImportPage.java | 4 +- .../{ => models}/ExportWizardModel.java | 4 +- .../{ => models}/ImportWizardModel.java | 2 +- .../{ => models}/WizardModelsExportPage.java | 197 ++++----- .../{ => models}/WizardModelsImportPage.java | 71 ++-- .../{ => modules}/ExportWizardModule.java | 2 +- .../{ => modules}/ImportWizardModule.java | 2 +- .../modules/ModuleComponentTypeNode.java | 161 +++++++ .../ui/wizards/modules/ModuleTreeModels.java | 40 ++ .../ui/wizards/modules/ModuleTreeModules.java | 35 ++ .../modules/ModuleTreeModulesLabeler.java | 16 + .../WizardModulesExportPage.java | 332 ++++++--------- .../WizardModulesImportPage.java | 276 ++++++------ 27 files changed, 1297 insertions(+), 757 deletions(-) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => functions}/ExportWizardFunction.java (93%) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeFunctionLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeLibraries.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModelLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModels.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedFolderLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraries.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraryLabeler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSubLibraries.java rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => functions}/ImportWizardFunction.java (86%) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/SharedFunctionsFolderNode.java rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => functions}/WizardFunctionsExportPage.java (54%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => functions}/WizardFunctionsImportPage.java (52%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => mdl}/ImportWizardMdl.java (92%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => mdl}/WizardMdlImportPage.java (94%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => models}/ExportWizardModel.java (90%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => models}/ImportWizardModel.java (92%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => models}/WizardModelsExportPage.java (57%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => models}/WizardModelsImportPage.java (90%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => modules}/ExportWizardModule.java (93%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => modules}/ImportWizardModule.java (93%) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleComponentTypeNode.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModels.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModules.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModulesLabeler.java rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => modules}/WizardModulesExportPage.java (59%) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/{ => modules}/WizardModulesImportPage.java (50%) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/ExportWizardFunction.java similarity index 93% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardFunction.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/ExportWizardFunction.java index df62dc60..755282e1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/ExportWizardFunction.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.functions; import org.eclipse.jface.viewers.IStructuredSelection; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeFunctionLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeFunctionLabeler.java new file mode 100644 index 00000000..37d85e05 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeFunctionLabeler.java @@ -0,0 +1,20 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode; + +public class FunctionTreeFunctionLabeler extends LabelerContributor>{ + + @Override + public String getLabel(ReadGraph graph, FunctionLibraryNode input) + throws DatabaseException { + String name = NameUtils.getSafeName(graph, input.data); + return name; + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeLibraries.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeLibraries.java new file mode 100644 index 00000000..f2344247 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeLibraries.java @@ -0,0 +1,37 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode; +import org.simantics.sysdyn.ui.browser.nodes.ModelNode; + +public class FunctionTreeLibraries extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, ModelNode model) + throws DatabaseException { + ArrayList> result = new ArrayList>(); + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + for (Resource r : graph.syncRequest(new ObjectsWithType(model.data, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))){ + result.add(new FunctionLibraryNode(r)); + } + + return result; + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModelLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModelLabeler.java new file mode 100644 index 00000000..1a2003ab --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModelLabeler.java @@ -0,0 +1,17 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import org.simantics.browsing.ui.graph.impl.contributor.labeler.LabelerContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.ModelNode; + +public class FunctionTreeModelLabeler extends LabelerContributorImpl{ + + @Override + public String getLabel(ReadGraph graph, ModelNode input) + throws DatabaseException { + return NameUtils.getSafeName(graph, input.data); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModels.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModels.java new file mode 100644 index 00000000..623f1a30 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeModels.java @@ -0,0 +1,43 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.graph.impl.contributor.viewpoint.ViewpointContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.ModelNode; + +public class FunctionTreeModels extends ViewpointContributorImpl { + + @Override + public Collection getContribution(ReadGraph graph, Resource input) + throws DatabaseException { + + + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + ArrayList> result = new ArrayList>(); + for(Resource r : graph.syncRequest(new ObjectsWithType(input, l0.ConsistsOf, sr.SysdynModel))) { + result.add(new ModelNode(r)); + } + Resource sharedlibrary = graph.getPossibleResource("http://SharedOntologies"); + if (sharedlibrary != null) + result.add(new SharedFunctionsFolderNode(sharedlibrary)); + + return result; + } + + @Override + public String getViewpointId() { + return "Function Library Import"; + } + + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedFolderLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedFolderLabeler.java new file mode 100644 index 00000000..17f53686 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedFolderLabeler.java @@ -0,0 +1,15 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import org.simantics.browsing.ui.graph.impl.contributor.labeler.LabelerContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; + +public class FunctionTreeSharedFolderLabeler extends LabelerContributorImpl{ + + @Override + public String getLabel(ReadGraph graph, SharedFunctionsFolderNode input) + throws DatabaseException { + return "Shared Functions"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraries.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraries.java new file mode 100644 index 00000000..e9003a1a --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraries.java @@ -0,0 +1,35 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode; + +public class FunctionTreeSharedLibraries extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, SharedFunctionsFolderNode folder) + throws DatabaseException { + ArrayList result = new ArrayList(); + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + Resource sharedlibrary = graph.getPossibleResource("http://SharedOntologies"); + for (Resource r : graph.syncRequest(new ObjectsWithType(sharedlibrary, l0.ConsistsOf, sr.SharedFunctionOntology))){ + result.add(new SharedFunctionLibraryNode(r)); + } + return result; + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraryLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraryLabeler.java new file mode 100644 index 00000000..e054cf60 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSharedLibraryLabeler.java @@ -0,0 +1,17 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import org.simantics.browsing.ui.graph.impl.contributor.labeler.LabelerContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode; + +public class FunctionTreeSharedLibraryLabeler extends LabelerContributorImpl{ + + @Override + public String getLabel(ReadGraph graph, SharedFunctionLibraryNode input) + throws DatabaseException { + return NameUtils.getSafeName(graph, input.data); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSubLibraries.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSubLibraries.java new file mode 100644 index 00000000..c88421a3 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/FunctionTreeSubLibraries.java @@ -0,0 +1,36 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode; + +public class FunctionTreeSubLibraries extends ViewpointContributor> { + + @Override + public Collection getContribution(ReadGraph graph, FunctionLibraryNode library) + throws DatabaseException { + ArrayList> result = new ArrayList>(); + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + for (Resource r : graph.syncRequest(new ObjectsWithType(library.data, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))){ + result.add(new FunctionLibraryNode(r)); + } + + return result; + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/ImportWizardFunction.java similarity index 86% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardFunction.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/ImportWizardFunction.java index dd394a67..eb8b06f5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/ImportWizardFunction.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.functions; import org.eclipse.jface.viewers.IStructuredSelection; @@ -17,14 +17,14 @@ public class ImportWizardFunction extends Wizard implements IImportWizard { public Resource selection; /** - * Constructor for ExternalProjectImportWizard. + * Constructor for ImportWizardFunction. */ public ImportWizardFunction() { this(null); } /** - * Constructor for ExternalProjectImportWizard. + * Constructor for ImportWizardFunction. */ public ImportWizardFunction(String initialPath) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/SharedFunctionsFolderNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/SharedFunctionsFolderNode.java new file mode 100644 index 00000000..989c5cdb --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/SharedFunctionsFolderNode.java @@ -0,0 +1,65 @@ +package org.simantics.sysdyn.ui.wizards.functions; + +import org.simantics.browsing.ui.common.node.DeleteException; +import org.simantics.browsing.ui.common.node.IDropTargetNode; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.FunctionUtils; +import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; + +public class SharedFunctionsFolderNode extends FunctionLibraryNode implements IDropTargetNode { + + public SharedFunctionsFolderNode(Resource resource) { + super(resource); + } + + @Override + public void delete() throws DeleteException { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + + graph.deny(data, l0.PartOf); + graph.deny(data, l0.IsLinkedTo_Inverse); + + // TODO: remove file + } + }); + } + + @Override + public void drop(Object data) { + final Resource[] resources = ResourceAdaptionUtils.toResources(data); + final Resource library = this.data; + if(resources.length > 0) { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + for(Resource tobeMoved : resources) { + if(graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunction) || + graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunctionLibrary)) { + Resource oldLib = graph.getSingleObject(tobeMoved, l0.PartOf); + graph.deny(tobeMoved, l0.PartOf); + graph.claim(tobeMoved, l0.PartOf, library); + FunctionUtils.updateFunctionFileForLibrary(graph, oldLib); + FunctionUtils.updateFunctionFileForLibrary(graph, library); + } + } + + } + }); + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardFunctionsExportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsExportPage.java similarity index 54% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardFunctionsExportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsExportPage.java index 83096c64..af6c0b21 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardFunctionsExportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsExportPage.java @@ -1,16 +1,22 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.functions; import java.io.File; import java.io.IOException; import java.util.ArrayList; import org.eclipse.core.runtime.Path; +import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -19,6 +25,9 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Tree; +import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; @@ -26,13 +35,14 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.TransferableGraphRequest2; import org.simantics.db.request.Read; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.ArrayMap; import org.simantics.utils.datastructures.Pair; public class WizardFunctionsExportPage extends WizardPage { @@ -45,9 +55,13 @@ public class WizardFunctionsExportPage extends WizardPage { private static String previouslyBrowsedFile = ""; private Button browseDirectoriesButton; - - private IStructuredSelection currentSelection; - + + //private IStructuredSelection currentSelection; + + GraphExplorerComposite functionLibraryExplorer; + + private boolean selectionMade = false; + /** * Creates a new project creation wizard page. * @@ -76,10 +90,10 @@ public class WizardFunctionsExportPage extends WizardPage { public WizardFunctionsExportPage(String pageName,String initialPath, IStructuredSelection currentSelection) { super(pageName); - this.currentSelection = currentSelection; + //this.currentSelection = currentSelection; setPageComplete(false); setTitle("Export Function Library"); - setDescription("Choose the location where the selected Function Library is exportted. The exportted Function Library is selected from the Model Browser."); + setDescription("Choose the Function Library and the export location, then press Finish."); } public void createControl(Composite parent) { @@ -94,15 +108,15 @@ public class WizardFunctionsExportPage extends WizardPage { | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL)); createProjectsRoot(workArea); + createTree(workArea); + } private void createProjectsRoot(Composite workArea) { - - // project specification group - + // set label for field Label title = new Label(workArea, SWT.NONE); - title.setText("Select export destination for Function Library:"); + title.setText("Select the export location for Function Library:"); Composite projectGroup = new Composite(workArea, SWT.NONE); GridLayout layout = new GridLayout(); @@ -113,13 +127,24 @@ public class WizardFunctionsExportPage extends WizardPage { projectGroup.setLayout(layout); projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - // project location entry field + // function library location entry field this.filePathField = new Text(projectGroup, SWT.BORDER); GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false); directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25); filePathField.setLayoutData(directoryPathData); + filePathField.addModifyListener(new ModifyListener(){ + @Override + public void modifyText(ModifyEvent e) { + previouslyBrowsedFile = filePathField.getText(); + } + }); + if (previouslyBrowsedFile != null){ + filePathField.setText(previouslyBrowsedFile); + validatePage(); + } + // browse button browseDirectoriesButton = new Button(projectGroup, SWT.PUSH); browseDirectoriesButton.setText("Browse"); @@ -139,6 +164,60 @@ public class WizardFunctionsExportPage extends WizardPage { } + private void createTree(Composite workArea){ + + //set label for tree + Label title = new Label(workArea, SWT.NONE); + title.setText("Select Function Library to export:"); + + try { + Resource input = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) + throws DatabaseException { + Resource model = SimanticsUI.getProject().get(); + return model; + } + + }); + + functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE); + + functionLibraryExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/FunctionTree"); + + functionLibraryExplorer.finish(); + + functionLibraryExplorer.setInput(null, input); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + functionLibraryExplorer); + + ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + setMessage(null); + selectionMade = true; + validatePage(); + } + @Override + public void widgetDefaultSelected(SelectionEvent e) { + setMessage(null); + selectionMade = true; + validatePage(); + } + }); + + } catch (DatabaseException e) { + e.printStackTrace(); + } + + + } + //Set filePathField active public void setVisible(boolean visible) { super.setVisible(visible); @@ -149,26 +228,6 @@ public class WizardFunctionsExportPage extends WizardPage { //Open dialog for choosing the file protected void handleLocationDirectoryButtonPressed() { - final Resource functionLibrary2 = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class); - String name = null; - try { - name = SimanticsUI.getSession().syncRequest(new Read() { - - @Override - public String perform(ReadGraph graph) throws DatabaseException { - if (!graph.hasStatement(functionLibrary2, Layer0.getInstance(graph).PartOf)) - return null; - Layer0 l0 = Layer0.getInstance(graph); - String name = graph.syncRequest(new PossibleRelatedValue(functionLibrary2, l0.HasName, Bindings.STRING )); - return name; - - } - - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - final Shell shell = filePathField.getShell(); FileDialog dialog = new FileDialog(shell, SWT.SAVE); @@ -176,8 +235,6 @@ public class WizardFunctionsExportPage extends WizardPage { String[] ext = {"*.tg"}; dialog.setFilterExtensions(ext); - dialog.setFileName(name); - dialog.setText("Export Function Library"); String dirName = filePathField.getText().trim(); @@ -189,22 +246,59 @@ public class WizardFunctionsExportPage extends WizardPage { String selectedFile = dialog.open(); if (selectedFile != null) { - previouslyBrowsedFile = selectedFile; - filePathField.setText(previouslyBrowsedFile); - setPageComplete(true); + filePathField.setText(selectedFile); + validatePage(); } } + //Get selection from the tree + @SuppressWarnings("unchecked") + public static T getExplorerResource(GraphExplorerComposite explorer, + Class clazz) { + if(explorer == null) + return null; + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return null; + IStructuredSelection iss = (IStructuredSelection) selection; + AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement(); + if (inc == null) + return null; + final T resource = (T) inc.getAdapter(clazz); + + return resource; + } + public boolean createProjects(Resource selection) { final String selected = previouslyBrowsedFile; if(selected == null) return false; - final Resource modulesymbol = selection; - if(modulesymbol == null) return false; - - final Resource functionLibrary = selection; + + final Resource functionLibrary = getExplorerResource(functionLibraryExplorer, Resource.class); + if(functionLibrary == null) return false; + String name = null; + try { + name = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf)) + return null; + Layer0 l0 = Layer0.getInstance(graph); + String name = graph.syncRequest(new PossibleRelatedValue(functionLibrary, l0.HasName, Bindings.STRING )); + return name; + + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + if(name == null) return false; + SimanticsUI.getSession().asyncRequest(new ReadRequest() { @Override @@ -229,4 +323,43 @@ public class WizardFunctionsExportPage extends WizardPage { return true; } + + void validatePage() { + + if (previouslyBrowsedFile.isEmpty() || selectionMade == false){ + setPageComplete(false); + return; + } + + if (functionLibraryExplorer != null){ + final Resource selectedResource = getExplorerResource(functionLibraryExplorer, Resource.class); + + String root = null; + try { + root = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getPossibleObject(selectedResource, l0.PartOf); + String rootName = NameUtils.getSafeName(graph, model); + + return rootName; + } + + }); + if (root != null && root.equalsIgnoreCase("Development Project")){ + setPageComplete(false); + setMessage("Select Function Library folder under the Model or from the Shared Functions folder."); + return; + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + setPageComplete(true); + + } + } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardFunctionsImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java similarity index 52% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardFunctionsImportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java index a6adcf96..3a5d4e50 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardFunctionsImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java @@ -1,29 +1,32 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.functions; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import org.eclipse.core.runtime.Path; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Tree; +import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; @@ -31,8 +34,6 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.WriteOnlyGraph; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; @@ -47,29 +48,30 @@ import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.FunctionUtils; import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; -import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.datastructures.ArrayMap; public class WizardFunctionsImportPage extends WizardPage{ - + // dialog store id constants private Text filePathField; - + // Keep track of the archive that we browsed to last time // the wizard was invoked. private static String previouslyBrowsedFile = ""; private Button browseDirectoriesButton; - + private Shell shell; - - private IStructuredSelection currentSelection; - - private Resource current; - + + //private IStructuredSelection currentSelection; + private Resource selectedModel; + + GraphExplorerComposite functionLibraryExplorer; + + private String error = ""; - private String currentModelName; + private boolean selectionMade = false; /** * Creates a new project creation wizard page. @@ -87,7 +89,7 @@ public class WizardFunctionsImportPage extends WizardPage{ public WizardFunctionsImportPage(String pageName) { this(pageName,null, null); } - + /** * More (many more) parameters. * @@ -98,41 +100,43 @@ public class WizardFunctionsImportPage extends WizardPage{ */ public WizardFunctionsImportPage(String pageName,String initialPath, IStructuredSelection currentSelection) { - super(pageName); + super(pageName); setPageComplete(false); - this.currentSelection = currentSelection; - setTitle("Import Function Library to Model"); - setDescription("Select Function Library from the local file system to be imported to the selected Model."); + //this.currentSelection = currentSelection; + setTitle("Import Function Library"); + setDescription("Choose the Function Library file and the import location, then press Finish."); } - + public void createControl(Composite parent) { - + initializeDialogUnits(parent); - + Composite workArea = new Composite(parent, SWT.NONE); setControl(workArea); - + workArea.setLayout(new GridLayout()); workArea.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL)); - + createProjectsRoot(workArea); + + createTree(workArea); + - createDropDown(workArea); } - + private void createProjectsRoot(Composite workArea) { - + //set label for field Label title = new Label(workArea, SWT.NONE); title.setText("Select Function Library source:"); - + Composite projectGroup = new Composite(workArea, SWT.NONE); GridLayout layout = new GridLayout(); layout.numColumns = 2; layout.makeColumnsEqualWidth = false; layout.marginWidth = 0; - + projectGroup.setLayout(layout); projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); @@ -142,24 +146,24 @@ public class WizardFunctionsImportPage extends WizardPage{ GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false); directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25); filePathField.setLayoutData(directoryPathData); + filePathField.addModifyListener(new ModifyListener(){ - @Override - public void modifyText(ModifyEvent e) { - previouslyBrowsedFile = filePathField.getText(); - } + @Override + public void modifyText(ModifyEvent e) { + previouslyBrowsedFile = filePathField.getText(); + } }); if (previouslyBrowsedFile != null){ filePathField.setText(previouslyBrowsedFile); validatePage(); } - + // browse button browseDirectoriesButton = new Button(projectGroup, SWT.PUSH); browseDirectoriesButton.setText("Browse"); setButtonLayoutData(browseDirectoriesButton); - - + browseDirectoriesButton.addSelectionListener(new SelectionAdapter() { /* * (non-Javadoc) @@ -171,141 +175,127 @@ public class WizardFunctionsImportPage extends WizardPage{ handleLocationDirectoryButtonPressed(); } }); - + } - - private void createDropDown(Composite workArea) { - - final ArrayList> resources = new ArrayList>(); - - new Label(workArea, SWT.NONE).setText("Import target:"); - - //Get resources for the DropDown-selection - try{ - SimanticsUI.getSession().syncRequest(new ReadRequest() { + + private void createTree(Composite workArea){ + + //set label for tree + Label title = new Label(workArea, SWT.NONE); + title.setText("Select import location:"); + + try { + Resource input = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) + throws DatabaseException { + Resource model = SimanticsUI.getProject().get(); + return model; + } + + }); + + functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE); + + functionLibraryExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/FunctionTree"); + + functionLibraryExplorer.finish(); + + functionLibraryExplorer.setInput(null, input); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + functionLibraryExplorer); - @Override - public void run(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); + ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() { - Resource model; - current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class); - //If there is no selection - if(current == null){ - model = SimanticsUI.getProject().get(); - } - else{ - model = graph.getPossibleObject(current, l0.PartOf); - } - currentModelName = NameUtils.getSafeName(graph, current); - - //Check if selection is not in top-level folder - String modelName = NameUtils.getSafeName(graph, model); - while (!modelName.equalsIgnoreCase("Development Project")){ - Resource previousResource = graph.getPossibleObject(model, l0.PartOf); - String previousName = NameUtils.getSafeName(graph, previousResource); - if (previousName.equalsIgnoreCase("Development Project")){ - currentModelName = modelName; - } - //If selection is under Built-in Functions directory - if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){ - model = SimanticsUI.getProject().get(); - current = null; - break; - } - current = model; - model = graph.getPossibleObject(model, l0.PartOf); - modelName = NameUtils.getSafeName(graph, model); - } - for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){ - String name = NameUtils.getSafeName(graph, r); - resources.add(Pair.make(r, name)); - } - - }; - }); - } - catch (DatabaseException e) { + @Override + public void widgetSelected(SelectionEvent e) { + selectionMade = true; + validatePage(); + } + @Override + public void widgetDefaultSelected(SelectionEvent e) { + selectionMade = true; + validatePage(); + } + }); + + } catch (DatabaseException e) { e.printStackTrace(); - } - //Create dropdown-bar - final CCombo drop = new CCombo(workArea, SWT.BORDER); - { - drop.setEditable(false); - drop.setText(""); - drop.setToolTipText("Selects the Model where to import the Module."); - GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); - - //Fill the Dropdown-list - for(int a=0; a T getExplorerResource(GraphExplorerComposite explorer, + Class clazz) { + + if(explorer == null) + return null; + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return null; + IStructuredSelection iss = (IStructuredSelection) selection; + AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement(); + if (inc == null) + return null; + final T resource = (T) inc.getAdapter(clazz); + + return resource; + } + //Create project after finish is pressed. public boolean createProjects() { + selectedModel= getExplorerResource(functionLibraryExplorer, Resource.class); + if(selectedModel == null){ + setErrorMessage("Error when retrieving resource"); + return false; + } + String selected = previouslyBrowsedFile; - if(selected == null) return false; - + if(selected == null){ + setErrorMessage("No file selected"); + return false; + } + TransferableGraph1 tg = null; try { tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); @@ -313,15 +303,15 @@ public class WizardFunctionsImportPage extends WizardPage{ e.printStackTrace(); return false; } catch (IOException e) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Function Library"); - mb.open(); + setErrorMessage("The imported file is not of type: Function Library"); return false; } - if(tg == null) return false; + if(tg == null){ + setErrorMessage("The imported file is not of type: Function Library"); + return false; + } + - try { Boolean hasSharedOntologies; hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read() { @@ -355,73 +345,71 @@ public class WizardFunctionsImportPage extends WizardPage{ return false; } - + SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(selectedModel); try { DefaultPasteHandler.defaultExecute(tg, selectedModel, ia); } catch (Exception e) { e.printStackTrace(); } - + final Resource root = ia.getRoot(); - - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) { - Resource library = graph.getResource("http://SharedOntologies"); - if(!graph.hasStatement(library, l0.ConsistsOf, root)) { - graph.claim(library, l0.ConsistsOf, root); - } - - SysdynResource sr = SysdynResource.getInstance(graph); - Resource model = selectedModel; - while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology)) - model = graph.getSingleObject(model, l0.PartOf); - if(graph.isInstanceOf(model, sr.SysdynModel)) { - graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root); - } - - } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { - Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - graph.deny(root, l0.PartOf); - - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Function Library (" + ft +")"); - mb.open(); + + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + if(graph.isInstanceOf(root, SysdynResource.getInstance(graph).SharedFunctionOntology)) { + Resource library = graph.getResource("http://SharedOntologies"); + if(!graph.hasStatement(library, l0.ConsistsOf, root)) { + graph.claim(library, l0.ConsistsOf, root); } - }); - } else { - FunctionUtils.updateFunctionFileForLibrary(graph, selectedModel); - } - } - }); + SysdynResource sr = SysdynResource.getInstance(graph); + Resource model = selectedModel; + while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology)) + model = graph.getSingleObject(model, l0.PartOf); + if(graph.isInstanceOf(model, sr.SysdynModel)) { + graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, root); + } + + } else if(!graph.isInstanceOf(root, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { + Resource instanceOf = graph.getPossibleObject(root,l0.InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(root, l0.Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + graph.deny(root, l0.PartOf); + error = type; + } else { + FunctionUtils.updateFunctionFileForLibrary(graph, selectedModel); + } + + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + if (!error.isEmpty()){ + setErrorMessage("The imported file is not of type: Function Library (" + error +")"); + error = ""; + return false; + } return true; } - + private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor { - + public SysdynFunctionLibraryImportAdvisor(Resource library) { super(library); } - + @Override public void analyzeType(ReadGraph graph, Root root) throws DatabaseException { if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) { @@ -432,7 +420,7 @@ public class WizardFunctionsImportPage extends WizardPage{ } } } - + @Override public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException { Layer0 l0 = graph.getService(Layer0.class); @@ -444,17 +432,17 @@ public class WizardFunctionsImportPage extends WizardPage{ return this.root; } - + } void validatePage() { - - if (previouslyBrowsedFile.isEmpty()){ + + if (previouslyBrowsedFile.isEmpty() || selectionMade == false){ setPageComplete(false); return; } - + setErrorMessage(null); setPageComplete(true); - + } - + } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardMdl.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/ImportWizardMdl.java similarity index 92% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardMdl.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/ImportWizardMdl.java index 297c0926..91422841 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardMdl.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/ImportWizardMdl.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.mdl; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardMdlImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java similarity index 94% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardMdlImportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java index 7d390dd4..bf813ba6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardMdlImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/mdl/WizardMdlImportPage.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.mdl; import java.io.File; @@ -68,7 +68,7 @@ public class WizardMdlImportPage extends WizardPage{ super(pageName); setPageComplete(false); setTitle("Import Vensim model"); - setDescription("Select Vensim model -file (.mdl) from the local file system."); + setDescription("Choose the Vensim model file (.mdl), then press Finish."); } public void createControl(Composite parent) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardModel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/ExportWizardModel.java similarity index 90% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardModel.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/ExportWizardModel.java index 62310be4..d68f341a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardModel.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/ExportWizardModel.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.models; import org.eclipse.jface.viewers.IStructuredSelection; @@ -46,7 +46,7 @@ public class ExportWizardModel extends Wizard implements IImportWizard { @Override public boolean performFinish() { - return mainPage.createProjects(selection); + return mainPage.createProjects(); } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardModel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/ImportWizardModel.java similarity index 92% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardModel.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/ImportWizardModel.java index ff3841a2..e743d750 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardModel.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/ImportWizardModel.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.models; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModelsExportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsExportPage.java similarity index 57% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModelsExportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsExportPage.java index b7ec7074..36d147f9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModelsExportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsExportPage.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.models; import java.io.File; import java.io.IOException; @@ -7,14 +7,16 @@ import java.util.ArrayList; import org.eclipse.core.runtime.Path; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -23,23 +25,23 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Tree; +import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; -import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.TransferableGraphRequest2; import org.simantics.db.request.Read; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.ArrayMap; import org.simantics.utils.datastructures.Pair; public class WizardModelsExportPage extends WizardPage { @@ -54,13 +56,15 @@ public class WizardModelsExportPage extends WizardPage { private Button browseDirectoriesButton; - private IStructuredSelection currentSelection; - - private Resource current; + //private IStructuredSelection currentSelection; private Resource selectedModel; - private String currentModelName; + GraphExplorerComposite modelExplorer; + + private boolean selectionMade = false; + + /** * Creates a new project creation wizard page. @@ -90,10 +94,10 @@ public class WizardModelsExportPage extends WizardPage { public WizardModelsExportPage(String pageName,String initialPath, IStructuredSelection currentSelection) { super(pageName); - this.currentSelection = currentSelection; + //this.currentSelection = currentSelection; setPageComplete(false); setTitle("Export Model"); - setDescription("Choose the location where the selected Model is exportted. The exportted Model is selected from the dropdown-menu."); + setDescription("Choose the Model and the export location, then press Finish."); } public void createControl(Composite parent) { @@ -107,15 +111,15 @@ public class WizardModelsExportPage extends WizardPage { workArea.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL)); - createDropDown (workArea); createProjectsRoot(workArea); + createTree (workArea); } private void createProjectsRoot(Composite workArea) { // set label for field Label title = new Label(workArea, SWT.NONE); - title.setText("Select export destination for Model:"); + title.setText("Select the export location for Model:"); Composite projectGroup = new Composite(workArea, SWT.NONE); GridLayout layout = new GridLayout(); @@ -163,99 +167,57 @@ public class WizardModelsExportPage extends WizardPage { } - private void createDropDown(Composite workArea) { - - final ArrayList> resources = new ArrayList>(); - - new Label(workArea, SWT.NONE).setText("Select Model to export:"); - - //Get resources for the DropDown-selection - try{ - SimanticsUI.getSession().syncRequest(new ReadRequest() { + private void createTree(Composite workArea){ + + //set label for tree + Label title = new Label(workArea, SWT.NONE); + title.setText("Select Model to export:"); + + try { + Resource input = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) + throws DatabaseException { + Resource model = SimanticsUI.getProject().get(); + return model; + } + + }); + + modelExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE); + + modelExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/ImportModuleTree"); + + modelExplorer.finish(); + + modelExplorer.setInput(null, input); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + modelExplorer); - @Override - public void run(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource model; - current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class); - //If there is no selection - if(current == null){ - model = SimanticsUI.getProject().get(); - } - else{ - model = graph.getPossibleObject(current, l0.PartOf); - } - currentModelName = NameUtils.getSafeName(graph, current); - - //Check if selection is not in top-level folder - String modelName = NameUtils.getSafeName(graph, model); - while (!modelName.equalsIgnoreCase("Development Project")){ - Resource previousResource = graph.getPossibleObject(model, l0.PartOf); - String previousName = NameUtils.getSafeName(graph, previousResource); - if (previousName.equalsIgnoreCase("Development Project")){ - currentModelName = modelName; - } - //If selection is under Built-in Functions directory - if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){ - model = SimanticsUI.getProject().get(); - current = null; - break; - } - current = model; - model = graph.getPossibleObject(model, l0.PartOf); - modelName = NameUtils.getSafeName(graph, model); - } - for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){ - String name = NameUtils.getSafeName(graph, r); - resources.add(Pair.make(r, name)); - } + ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() { - }; - }); - } - catch (DatabaseException e) { + @Override + public void widgetSelected(SelectionEvent e) { + selectionMade = true; + validatePage(); + } + @Override + public void widgetDefaultSelected(SelectionEvent e) { + selectionMade = true; + validatePage(); + } + }); + + + } catch (DatabaseException e) { e.printStackTrace(); - } - //Create dropdown-bar - final CCombo drop = new CCombo(workArea, SWT.BORDER); - { - drop.setEditable(false); - drop.setText(""); - drop.setToolTipText("Selects the Model where to import the Module."); - GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); - - //Fill the Dropdown-list - for(int a=0; a T getExplorerResource(GraphExplorerComposite explorer, + Class clazz) { + + if(explorer == null) + return null; + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return null; + IStructuredSelection iss = (IStructuredSelection) selection; + AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement(); + if (inc == null) + return null; + final T resource = (T) inc.getAdapter(clazz); + + return resource; + } - public boolean createProjects(Resource selection) { + public boolean createProjects() { final String selected = previouslyBrowsedFile; if(selected == null) return false; + + selectedModel= getExplorerResource(modelExplorer, Resource.class); + if(selectedModel == null) + return false; // FIXME: Model browser doesn't change its selection even if the selected object is removed, // so you can try to export a removed model @@ -323,7 +308,7 @@ public class WizardModelsExportPage extends WizardPage { void validatePage() { - if (previouslyBrowsedFile.isEmpty()){ + if (previouslyBrowsedFile.isEmpty() || selectionMade == false){ setPageComplete(false); return; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModelsImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java similarity index 90% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModelsImportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java index e7c447f6..9e2ecc5c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModelsImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java @@ -1,11 +1,11 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.models; import java.io.File; import java.io.IOException; import java.util.HashSet; import java.util.UUID; -import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; import org.eclipse.jface.layout.PixelConverter; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; @@ -20,7 +20,6 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.simantics.databoard.Bindings; @@ -43,13 +42,13 @@ import org.simantics.simulation.ontology.SimulationResource; import org.simantics.spreadsheet.resource.SpreadsheetResource; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.sysdyn.ui.handlers.ImportModelHandler; import org.simantics.sysdyn.ui.utils.OldTransferableGraph1; import org.simantics.ui.SimanticsUI; public class WizardModelsImportPage extends WizardPage{ - // dialog store id constants - private Text filePathField; // Keep track of the archive that we browsed to last time @@ -60,6 +59,8 @@ public class WizardModelsImportPage extends WizardPage{ private Shell shell; + private String error = ""; + /** * Creates a new project creation wizard page. * @@ -92,7 +93,7 @@ public class WizardModelsImportPage extends WizardPage{ //this.currentSelection = currentSelection; setPageComplete(false); setTitle("Import Model"); - setDescription("Select Model file from the local file system."); + setDescription("Choose the Model file, then press Finish."); } @@ -134,7 +135,7 @@ public class WizardModelsImportPage extends WizardPage{ filePathField.addModifyListener(new ModifyListener(){ @Override public void modifyText(ModifyEvent e) { - previouslyBrowsedFile = filePathField.getText(); + previouslyBrowsedFile = filePathField.getText(); } }); if (previouslyBrowsedFile != null){ @@ -155,10 +156,12 @@ public class WizardModelsImportPage extends WizardPage{ * elected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent e) { + setErrorMessage(null); handleLocationDirectoryButtonPressed(); } }); } + //Set filePathField active public void setVisible(boolean visible) { @@ -179,28 +182,33 @@ public class WizardModelsImportPage extends WizardPage{ dialog.setText("Import Model"); - String dirName = filePathField.getText().trim(); - - File path = new File(dirName); - if (path.exists()) { - dialog.setFilterPath(new Path(dirName).toOSString()); - } + String path = Activator.getDefault().getPreferenceStore().getString(ImportModelHandler.IMPORTMODELTPATH); + if(path.isEmpty() || !(new File(path).exists())) + path = Platform.getLocation().toOSString(); + dialog.setFilterPath(path); String selectedFile = dialog.open(); if (selectedFile != null) { filePathField.setText(selectedFile); validatePage(); } + } //Create project after finish is pressed. public boolean createProjects() { Resource project = SimanticsUI.getProject().get(); - if(project == null) return false; + if(project == null){ + setErrorMessage("Error when retrieving resource"); + return false; + } String selected = previouslyBrowsedFile; - if(selected == null) return false; + if(selected == null){ + setErrorMessage("No file selected"); + return false; + } TransferableGraph1 tg = null; try { @@ -214,14 +222,14 @@ public class WizardModelsImportPage extends WizardPage{ } catch (RuntimeBindingConstructionException e1) { e1.printStackTrace(); } catch (IOException e1) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Model"); - mb.open(); + setErrorMessage("The imported file is not of type: System Dynamics Model"); return false; } } - if(tg == null) return false; + if(tg == null){ + setErrorMessage("The imported file is not of type: System Dynamics Model"); + return false; + } try { @@ -246,20 +254,8 @@ public class WizardModelsImportPage extends WizardPage{ if(inheritedFrom != null) type = NameUtils.getSafeName(graph, inheritedFrom); } - final String ft = type; graph.deny(root, Layer0.getInstance(graph).PartOf); - - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: System Dynamics Model (" + ft +")"); - mb.open(); - } - }); - + error = type; } else { updateOldConfigurationToBaseRealization(graph, root); addDefaultOntologyLinks(graph, root); @@ -273,7 +269,12 @@ public class WizardModelsImportPage extends WizardPage{ } catch (Exception e) { e.printStackTrace(); } - + + if (!error.isEmpty()){ + setErrorMessage("The imported file is not of type: System Dynamics Model (" + error +")"); + error = ""; + return false; + } return true; } @@ -449,7 +450,7 @@ public class WizardModelsImportPage extends WizardPage{ setPageComplete(false); return; } - + setErrorMessage(null); setPageComplete(true); } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardModule.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ExportWizardModule.java similarity index 93% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardModule.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ExportWizardModule.java index 1f8faed7..c3534403 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ExportWizardModule.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ExportWizardModule.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.modules; import org.eclipse.jface.viewers.IStructuredSelection; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardModule.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ImportWizardModule.java similarity index 93% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardModule.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ImportWizardModule.java index 1ea86855..b70d589a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/ImportWizardModule.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ImportWizardModule.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.modules; import org.eclipse.jface.viewers.IStructuredSelection; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleComponentTypeNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleComponentTypeNode.java new file mode 100644 index 00000000..7b501811 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleComponentTypeNode.java @@ -0,0 +1,161 @@ +package org.simantics.sysdyn.ui.wizards.modules; + +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.common.node.DeleteException; +import org.simantics.browsing.ui.common.node.IDeletableNode; +import org.simantics.browsing.ui.common.node.IModifiableNode; +import org.simantics.browsing.ui.content.Labeler.Modifier; +import org.simantics.browsing.ui.graph.impl.LabelModifier; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.layer0.adapter.PasteHandler; +import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.ui.SimanticsUI; + +public class ModuleComponentTypeNode extends AbstractNode implements IDeletableNode, IModifiableNode { + + + Listener configurationNameSynchronizer; + private boolean disposed = false; + private Resource configuration; + + public ModuleComponentTypeNode(Resource resource) { + super(resource); + + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + configuration = graph.getPossibleObject(data, sr2.IsDefinedBy); + } + }); + + // Not the best solution for name sync + configurationNameSynchronizer = new Listener() { + + @Override + public void execute(final String result) { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if(configuration != null) + graph.claimLiteral(configuration, Layer0.getInstance(graph).HasLabel, result); + } + }); + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return disposed; + } + }; + + SimanticsUI.getSession().asyncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + return graph.getRelatedValue(data, l0.HasName); + } + + }, configurationNameSynchronizer); + + } + + @Override + public Modifier getModifier(String columnId) { + Modifier modifier = null; + try { + modifier = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Modifier perform(ReadGraph graph) throws ManyObjectsForFunctionalRelationException, ServiceException { + ModelingResources mr = ModelingResources.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType); + + LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) { + @Override + public String isValid(String label) { + if (label.isEmpty()) + return "Empty name not allowed"; + return null; + } + }; + + + return modifier; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + return modifier; + } + + @Override + public void delete() throws DeleteException { + disposed = true; + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 st = StructuralResource2.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + + Resource type = graph.getPossibleObject(data, mr.SymbolToComponentType); + Resource model = graph.getSingleObject(type, l0.PartOf); + Resource modelConfiguration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration); + if (!graph.syncRequest(new ObjectsWithType(modelConfiguration, l0.ConsistsOf, type)).isEmpty()) { + System.out.println("The module is used at the model configuration"); + return; + } + Collection moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType)); + for(Resource r : moduleTypes) { + Resource configuration = graph.getSingleObject(r, st.IsDefinedBy); + if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) { + System.out.println("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName)); + return; + } + } + graph.deny(model, l0.ConsistsOf, type); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("rawtypes") + @Override + public Object getAdapter(Class adapter) { + if(PasteHandler.class == adapter && configuration != null) + return new DefaultPasteHandler(configuration); + return super.getAdapter(adapter); + } +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModels.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModels.java new file mode 100644 index 00000000..272a2af5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModels.java @@ -0,0 +1,40 @@ +package org.simantics.sysdyn.ui.wizards.modules; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.graph.impl.contributor.viewpoint.ViewpointContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.ModelNode; + +public class ModuleTreeModels extends ViewpointContributorImpl { + + @Override + public Collection getContribution(ReadGraph graph, Resource input) + throws DatabaseException { + + + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + ArrayList> result = new ArrayList>(); + for(Resource r : graph.syncRequest(new ObjectsWithType(input, l0.ConsistsOf, sr.SysdynModel))) { + result.add(new ModelNode(r)); + } + + return result; + } + + @Override + public String getViewpointId() { + return "Module Import"; + } + + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModules.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModules.java new file mode 100644 index 00000000..76f76f85 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModules.java @@ -0,0 +1,35 @@ +package org.simantics.sysdyn.ui.wizards.modules; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.ui.browser.nodes.ModelNode; + +public class ModuleTreeModules extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, ModelNode model) + throws DatabaseException { + ArrayList result = new ArrayList(); + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + for (Resource r : graph.syncRequest(new ObjectsWithType(model.data, l0.ConsistsOf, sr2.ComponentType))){ + result.add(new ModuleComponentTypeNode(r)); + } + + return result; + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModulesLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModulesLabeler.java new file mode 100644 index 00000000..0aa030a9 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/ModuleTreeModulesLabeler.java @@ -0,0 +1,16 @@ +package org.simantics.sysdyn.ui.wizards.modules; + +import org.simantics.browsing.ui.graph.impl.contributor.labeler.LabelerContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; + +public class ModuleTreeModulesLabeler extends LabelerContributorImpl{ + + @Override + public String getLabel(ReadGraph graph, ModuleComponentTypeNode input) + throws DatabaseException { + return NameUtils.getSafeName(graph, input.data); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModulesExportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesExportPage.java similarity index 59% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModulesExportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesExportPage.java index bcc80dc6..af0a1996 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModulesExportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesExportPage.java @@ -1,4 +1,4 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.modules; import java.io.File; import java.io.IOException; @@ -8,14 +8,16 @@ import java.util.Collection; import org.eclipse.core.runtime.Path; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -24,6 +26,9 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Tree; +import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; @@ -44,7 +49,7 @@ import org.simantics.modeling.ModelingResources; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.ArrayMap; import org.simantics.utils.datastructures.Pair; public class WizardModulesExportPage extends WizardPage { @@ -58,25 +63,11 @@ public class WizardModulesExportPage extends WizardPage { private Button browseDirectoriesButton; - private IStructuredSelection currentSelection; - - private Resource current; - - private Resource selectedModel; - private Resource selectedModule; - private String currentModelName; - - private CCombo drop; - - private CCombo drop2; - - private ArrayList> resources = new ArrayList>(); - - private ArrayList> modules = new ArrayList>(); + GraphExplorerComposite modelExplorer; - + private boolean selectionMade = false; /** * Creates a new project creation wizard page. @@ -106,10 +97,10 @@ public class WizardModulesExportPage extends WizardPage { public WizardModulesExportPage(String pageName,String initialPath, IStructuredSelection currentSelection) { super(pageName); - this.currentSelection = currentSelection; + //this.currentSelection = currentSelection; setPageComplete(false); setTitle("Export Module"); - setDescription("Choose the Module and the export location."); + setDescription("Choose the Module and the export location, then press Finish."); } public void createControl(Composite parent) { @@ -123,16 +114,15 @@ public class WizardModulesExportPage extends WizardPage { workArea.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL)); - createDropDownSelections(workArea); createProjectsRoot(workArea); - + createTree(workArea); } private void createProjectsRoot(Composite workArea) { // set label for field Label title = new Label(workArea, SWT.NONE); - title.setText("Select export destination for Module:"); + title.setText("Select the export location for Module:"); Composite projectGroup = new Composite(workArea, SWT.NONE); GridLayout layout = new GridLayout(); @@ -179,78 +169,59 @@ public class WizardModulesExportPage extends WizardPage { }); } - - private void createDropDownSelections(final Composite workArea) { - - new Label(workArea, SWT.NONE).setText("Select the Model which contains the Module:"); - - //Find available models - findModels(); - - //Create Model dropdown-bar - drop = new CCombo(workArea, SWT.BORDER); - - drop.setEditable(false); - drop.setText(""); - drop.setToolTipText("Selects the Model where to export the Module."); - GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); - - //Fill the Model Dropdown-list - for(int a=0; a resource : resources){ - if (drop.getText().equals(resource.second)) - selectedModel = resource.first; - } - - updateModules(); - validatePage(); - } - }); - - drop2.addModifyListener(new ModifyListener(){ - @Override - public void modifyText(ModifyEvent e) { - - for(int a=0; a() { + + @Override + public Resource perform(ReadGraph graph) + throws DatabaseException { + Resource model = SimanticsUI.getProject().get(); + return model; + } + + }); + + modelExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE); + + modelExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/ExportModuleTree"); + + modelExplorer.finish(); + + modelExplorer.setInput(null, input); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + modelExplorer); + + ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + setMessage(null); + selectionMade = true; + validatePage(); + } + @Override + public void widgetDefaultSelected(SelectionEvent e) { + setMessage(null); + selectionMade = true; + validatePage(); + } + }); + + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } //Set filePathField active public void setVisible(boolean visible) { @@ -258,7 +229,6 @@ public class WizardModulesExportPage extends WizardPage { this.filePathField.setFocus(); } - //Open dialog for choosing the file protected void handleLocationDirectoryButtonPressed() { @@ -286,137 +256,50 @@ public class WizardModulesExportPage extends WizardPage { } - private void findModels(){ - - //Get resources for the DropDown-selection - try{ - SimanticsUI.getSession().syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource model; - current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class); - //If there is no selection - if(current == null){ - model = SimanticsUI.getProject().get(); - } - else{ - model = graph.getPossibleObject(current, l0.PartOf); - } - currentModelName = NameUtils.getSafeName(graph, current); - - //Check if selection is not in top-level folder - String modelName = NameUtils.getSafeName(graph, model); - while (!modelName.equalsIgnoreCase("Development Project")){ - Resource previousResource = graph.getPossibleObject(model, l0.PartOf); - String previousName = NameUtils.getSafeName(graph, previousResource); - if (previousName.equalsIgnoreCase("Development Project")){ - currentModelName = modelName; - } - //If selection is under Built-in Functions directory - if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){ - model = SimanticsUI.getProject().get(); - current = null; - break; - } - current = model; - model = graph.getPossibleObject(model, l0.PartOf); - modelName = NameUtils.getSafeName(graph, model); - } - for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){ - String name = NameUtils.getSafeName(graph, r); - resources.add(Pair.make(r, name)); - } - - }; - }); - } - catch (DatabaseException e) { - e.printStackTrace(); - } - - } - - - private void findModules(){ - - try{ - SimanticsUI.getSession().syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - StructuralResource2 sr2 = StructuralResource2.getInstance(graph); - //modules.removeAll(null); - modules = new ArrayList>(); - for (Resource r : graph.syncRequest(new ObjectsWithType(selectedModel, l0.ConsistsOf, sr2.ComponentType))){ - String name = NameUtils.getSafeName(graph, r); - modules.add(Pair.make(r, name)); - } - - }; - }); - } - catch (DatabaseException e) { - e.printStackTrace(); - } - } - - private void updateModules(){ - - //Get resources for the DropDown-selection - findModules(); - - //Clear the modules dropdown-list - if (drop2 != null){ - drop2.removeAll(); - - - //Fill the Modules dropdown-list - for(Pair module : modules){ - drop2.add(module.second); - } - int selectionNumber = 0; - for(int a=0; a T getExplorerResource(GraphExplorerComposite explorer, + Class clazz) { + + if(explorer == null) + return null; + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return null; + IStructuredSelection iss = (IStructuredSelection) selection; + AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement(); + if (inc == null) + return null; + final T resource = (T) inc.getAdapter(clazz); + + return resource; } + //Create export file when finish is pressed public boolean createProjects() { final String selected = previouslyBrowsedFile; if(selected == null) return false; + selectedModule= getExplorerResource(modelExplorer, Resource.class); + if(selectedModule == null) + return false; + String name = null; try { name = SimanticsUI.getSession().syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { - ModelingResources mr = ModelingResources.getInstance(graph); StructuralResource2 sr2 = StructuralResource2.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); Resource component = selectedModule; - // Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType); if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf)) return null; - Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy); if (configuration == null) return null; @@ -442,17 +325,13 @@ public class WizardModulesExportPage extends WizardPage { catch (DatabaseException e1) { e1.printStackTrace(); } -// if(name == null) return false; - - - // Activator.getDefault().getPreferenceStore().setValue(ImportModuleHandler.IMPORTMODULETPATH, (new File(selected)).getParent()); + if(name == null) return false; SimanticsUI.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); - ModelingResources mr = ModelingResources.getInstance(graph); final Resource component = selectedModule; //final Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType); @@ -555,11 +434,38 @@ public class WizardModulesExportPage extends WizardPage { void validatePage() { - if (previouslyBrowsedFile.isEmpty()){ + if (previouslyBrowsedFile.isEmpty() || selectionMade == false){ setPageComplete(false); return; } + if (modelExplorer != null){ + final Resource selectedResource = getExplorerResource(modelExplorer, Resource.class); + + String root = null; + try { + root = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getPossibleObject(selectedResource, l0.PartOf); + String rootName = NameUtils.getSafeName(graph, model); + + return rootName; + } + + }); + if (root != null && root.equalsIgnoreCase("Development Project")){ + setPageComplete(false); + setMessage("Select Module under the Model."); + return; + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + setPageComplete(true); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModulesImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java similarity index 50% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModulesImportPage.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java index 3ad33a18..0950f7da 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/WizardModulesImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java @@ -1,49 +1,50 @@ -package org.simantics.sysdyn.ui.wizards; +package org.simantics.sysdyn.ui.wizards.modules; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import org.eclipse.core.runtime.Path; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Tree; +import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.db.request.Read; import org.simantics.graph.db.MissingDependencyException; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; -import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.datastructures.ArrayMap; public class WizardModulesImportPage extends WizardPage{ @@ -62,13 +63,15 @@ public class WizardModulesImportPage extends WizardPage{ private Shell shell; - private IStructuredSelection currentSelection; - - private Resource current; + //private IStructuredSelection currentSelection; private Resource selectedModel; - private String currentModelName; + GraphExplorerComposite modelExplorer; + + private boolean selectionMade = false; + + private String error = ""; /** * Creates a new project creation wizard page. @@ -99,9 +102,9 @@ public class WizardModulesImportPage extends WizardPage{ IStructuredSelection currentSelection) { super(pageName); setPageComplete(false); - this.currentSelection = currentSelection; - setTitle("Import Module to Model"); - setDescription("Select Module-file from the local file system."); + //this.currentSelection = currentSelection; + setTitle("Import Module"); + setDescription("Choose the Module file and the import location, then press Finish."); } public void createControl(Composite parent) { @@ -117,7 +120,7 @@ public class WizardModulesImportPage extends WizardPage{ createProjectsRoot(workArea); - createDropDown(workArea); + createTree(workArea); } private void createProjectsRoot(Composite workArea) { @@ -171,100 +174,58 @@ public class WizardModulesImportPage extends WizardPage{ }); } - - private void createDropDown(Composite workArea) { - - final ArrayList> resources = new ArrayList>(); - - new Label(workArea, SWT.NONE).setText("Import target:"); - - //Get resources for the DropDown-selection - try{ - SimanticsUI.getSession().syncRequest(new ReadRequest() { + + private void createTree(Composite workArea){ + + //set label for tree + Label title = new Label(workArea, SWT.NONE); + title.setText("Select import location:"); + + try { + Resource input = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) + throws DatabaseException { + Resource model = SimanticsUI.getProject().get(); + return model; + } + + }); + + modelExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE); + + modelExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/ImportModuleTree"); + + modelExplorer.finish(); + + modelExplorer.setInput(null, input); - @Override - public void run(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource model; - current = (Resource)AdaptionUtils.adaptToSingle(currentSelection, org.simantics.db.Resource.class); - //If there is no selection - if(current == null){ - model = SimanticsUI.getProject().get(); - } - else{ - model = graph.getPossibleObject(current, l0.PartOf); - } - currentModelName = NameUtils.getSafeName(graph, current); - - //Check if selection is not in top-level folder - String modelName = NameUtils.getSafeName(graph, model); - while (!modelName.equalsIgnoreCase("Development Project")){ - Resource previousResource = graph.getPossibleObject(model, l0.PartOf); - String previousName = NameUtils.getSafeName(graph, previousResource); - if (previousName.equalsIgnoreCase("Development Project")){ - currentModelName = modelName; - } - //If selection is under Built-in Functions directory - if (modelName.equalsIgnoreCase("Built-in Functions") || currentModelName.equalsIgnoreCase("Built-in Functions")){ - model = SimanticsUI.getProject().get(); - current = null; - break; - } - current = model; - model = graph.getPossibleObject(model, l0.PartOf); - modelName = NameUtils.getSafeName(graph, model); - } - for (Resource r : graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SysdynModel))){ - String name = NameUtils.getSafeName(graph, r); - resources.add(Pair.make(r, name)); - } + GridDataFactory.fillDefaults().grab(true, true).applyTo( + modelExplorer); + + ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() { - }; - }); - } - catch (DatabaseException e) { + @Override + public void widgetSelected(SelectionEvent e) { + selectionMade = true; + validatePage(); + } + @Override + public void widgetDefaultSelected(SelectionEvent e) { + selectionMade = true; + validatePage(); + } + }); + + + } catch (DatabaseException e) { e.printStackTrace(); - } - //Create dropdown-bar - final CCombo drop = new CCombo(workArea, SWT.BORDER); - { - drop.setEditable(false); - drop.setText(""); - drop.setToolTipText("Selects the Model where to import the Module."); - GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(drop); - - //Fill the Dropdown-list - for(int a=0; a T getExplorerResource(GraphExplorerComposite explorer, + Class clazz) { + + if(explorer == null) + return null; + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return null; + IStructuredSelection iss = (IStructuredSelection) selection; + AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement(); + if (inc == null) + return null; + final T resource = (T) inc.getAdapter(clazz); + + return resource; + } + //Create project after finish is pressed. public boolean createProjects() { String selected = previouslyBrowsedFile; - if(selected == null) return false; + if(selected == null){ + setErrorMessage("Error when retrieving resource"); + return false; + } + + selectedModel= getExplorerResource(modelExplorer, Resource.class); + if(selectedModel == null){ + setErrorMessage("No file selected"); + return false; + } TransferableGraph1 tg = null; try { @@ -310,13 +300,13 @@ public class WizardModulesImportPage extends WizardPage{ } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); } catch (IOException e) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Module Type"); - mb.open(); + setErrorMessage("The imported file is not of type: Module Type"); + return false; + } + if(tg == null){ + setErrorMessage("The imported file is not of type: Module Type"); return false; } - if(tg == null) return false; DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(selectedModel); @@ -330,46 +320,46 @@ public class WizardModulesImportPage extends WizardPage{ final Resource root = ia.getRoot(); - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) { - Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - graph.deny(root, Layer0.getInstance(graph).PartOf); - - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: System Dynamics Module (" + ft +")"); - mb.open(); + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) { + Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); } - }); + graph.deny(root, Layer0.getInstance(graph).PartOf); + error = type; + } } - } - }); - return true; + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + if (!error.isEmpty()){ + setErrorMessage("The imported file is not of type: Module Type (" + error +")"); + error = ""; + return false; + } + return true; } void validatePage() { - if (previouslyBrowsedFile.isEmpty()){ + if (previouslyBrowsedFile.isEmpty() || selectionMade == false){ setPageComplete(false); return; } + setErrorMessage(null); setPageComplete(true); } -- 2.47.1