From ada034a2dfb832771111c8c62c35dd4ce8d52b38 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 11 Jun 2010 12:26:58 +0000 Subject: [PATCH] Better handling for result files (e.g. automatic remove when project activated) and ui-candy git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16140 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../icons/chart_bar_blackAndWhite.png | Bin 0 -> 1307 bytes org.simantics.sysdyn.ui/icons/tick.png | Bin 0 -> 537 bytes org.simantics.sysdyn.ui/plugin.xml | 26 +++-- .../org/simantics/sysdyn/ui/Activator.java | 19 ++++ .../sysdyn/ui/browser/SysdynBrowser.java | 14 +++ .../contributions/ExperimentImager.java | 6 ++ .../ExperimentLabelDecorator.java | 43 ++++++++ .../contributions/SimulationResult.java | 29 +++-- .../SimulationResultDecorator.java | 10 +- .../contributions/SimulationResultImager.java | 7 +- .../sysdyn/ui/handlers/UnlinkNodeHandler.java | 3 + .../ui/project/SysdynProjectLifeCycle.java | 100 +++++++++++++++++- .../sysdyn/manager/SysdynExperiment.java | 19 +++- sysdyn_ontologies/sysdyn.graph | 4 +- 14 files changed, 251 insertions(+), 29 deletions(-) create mode 100644 org.simantics.sysdyn.ui/icons/chart_bar_blackAndWhite.png create mode 100644 org.simantics.sysdyn.ui/icons/tick.png create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentLabelDecorator.java diff --git a/org.simantics.sysdyn.ui/icons/chart_bar_blackAndWhite.png b/org.simantics.sysdyn.ui/icons/chart_bar_blackAndWhite.png new file mode 100644 index 0000000000000000000000000000000000000000..087f88c0f2e91ecb8a9f2be0d1eeafcfd15c7251 GIT binary patch literal 1307 zcmd^8TTfbP6g_g$2}a99Ner5ZQb`4?rc%Wlg^CPPf)ox`af&!gwv|=H``^mAt&Xjg5`@`FXiqZnauBH#dbsVP|J&dwV;sU>x8`B$C6!Lybl=J3HIg z*Ec*o?DzXI?BwJm#Gjs?LgdiUP;PE6W~`{FXl-q6YiolrZ~@k6G(z^+*cjwN@WH_W zyu}nTF);#xASo${%jL4!Yzl>fdRSN(V(1{}@pz$nd3kwdWo1M}1YR+j%<%AV#OUbg z#Kc6n%3`sQo6Tmp%HeQu3z&2|9r(zTlatri*CQh%akr$T1h%K9rXoK1asZ1Q=EH}X znHeldp-^COMEGDbnMjh%&(B8>jYf-$ivtmkPN%~XfsZ=a0S-psLrh3KBD6sR-a-Rr zkxHeZS#SlUH8nM;H#Rn6%jxOqD8k0r*jSNB1bp~^etyp9^O0v{WK>mEB_t$(9X_O_ zq!bqyV<6FRS65f@@$ulo#u*GoW@aXST>}FHSO-?t)YOEA zrKKg%t*)*%G&Gczm4*Dlj%sUbL$kAD<-cG%#8a(WNBsA7ABOv%JXrg>NG2tp8LLcw z#`hMpaQz3zwAn>c^f#O|q5e9H{>ecTQKK%ezvoac4VSzBy8bl&)vIELEpChGe~^Wr zILoTOuE)M7&*p!sbnv`lnO{ua6ZBhe2?RH$Hj^e(8<0p1|$b zMy27d;;kx!d+|VBVhHs{AQG2a)rMyf zFQK~pm1x3+7!nu%-M`k}``c>^00{o_1pjWJUTfl8mg=3qGEl8H@}^@w`VUx0_$uy4 z2FhRqKX}xI*?Tv1DJd8z#F#0c%*~rM30HE1@2o5m~}ZyoWhqv>ql{V z1ZGE0lgcoK^lx+eqc*rAX1Ky;Xx3U%u#zG!m-;eD1Qsn@kf3|F9qz~|95=&g3(7!X zB}JAT>RU;a%vaNOGnJ%e1=K6eAh43c(QN8RQ6~GP%O}Jju$~Ld*%`mO1p @@ -401,7 +401,7 @@ + name="Show in charts"> - - + + + + + + + + @@ -603,6 +613,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.SimulationResultDecorator" preference="2.0"> + + getBrowseContexts() { return browseContexts; } + + @Override + protected void createControls(Composite parent) { + // Make sure the resource manager of this plug-in is initialized + // properly before using it in this browser. + Activator.initializeResourceManager(parent.getDisplay()); + + super.createControls(parent); + //IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager(); + //toolBar.add(new HomeAction()); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentImager.java index a4262231..0f607d5d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentImager.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentImager.java @@ -4,13 +4,19 @@ import org.eclipse.jface.resource.ImageDescriptor; import org.simantics.browsing.ui.swt.ImagerContributor; import org.simantics.db.ReadGraph; import org.simantics.db.exception.DatabaseException; +import org.simantics.project.IProject; +import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; +import org.simantics.ui.SimanticsUI; public class ExperimentImager extends ImagerContributor { @Override public ImageDescriptor getDescriptor(ReadGraph graph, ExperimentNode experiment) throws DatabaseException { + IProject project = SimanticsUI.getProject(); + IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + System.out.println(experimentManager.getActiveExperiment()); return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/time.png")); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentLabelDecorator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentLabelDecorator.java new file mode 100644 index 00000000..f0e0361c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ExperimentLabelDecorator.java @@ -0,0 +1,43 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.graph.contributor.labeler.LabelDecoratorContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.simulation.experiment.IExperiment; +import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.manager.SysdynExperiment; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; +import org.simantics.ui.SimanticsUI; + +public class ExperimentLabelDecorator extends LabelDecoratorContributor { + + @Override + public LabelDecorator getDecorator(ReadGraph graph, ExperimentNode experimentNode) throws DatabaseException { + IExperimentManager manager = + SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment exp = manager.getActiveExperiment(); + if(exp instanceof SysdynExperiment) { + SysdynExperiment experiment = (SysdynExperiment) exp; + if(experiment.getResource().equals(experimentNode.resource)) + return new LabelDecorator.Stub() { + @Override + public String decorateLabel(String label, String column, int itemIndex) { + return label + " [ACTIVE]"; + } + + @SuppressWarnings("unchecked") + @Override + public F decorateFont(F font, String column, int itemIndex) { + return (F) Activator.getResources().get(FontDescriptor.createFrom((Font) font).withStyle(SWT.BOLD)); + } + }; + } + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java index 7133bd17..cf5bfb1d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java @@ -1,13 +1,15 @@ package org.simantics.sysdyn.ui.browser.contributions; +import java.io.File; import java.util.ArrayList; import java.util.Collection; import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; -import org.simantics.db.Builtins; 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.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.AbstractNode; @@ -18,13 +20,26 @@ public class SimulationResult extends ViewpointContributor { @Override public Collection getContribution(ReadGraph graph, ExperimentNode experiment) throws DatabaseException { ArrayList result = new ArrayList(); - Builtins b = graph.getBuiltins(); SysdynResource sr = SysdynResource.getInstance(graph); - for(Resource r : graph.syncRequest(new ObjectsWithType(experiment.resource, sr.HasResult, sr.Result))) { - try { - result.add(graph.adapt(r, AbstractNode.class)); - } catch(DatabaseException e) { - e.printStackTrace(); + for(final Resource r : graph.syncRequest(new ObjectsWithType(experiment.resource, sr.HasResult, sr.Result))) { + String resultPath = (String)graph.getPossibleRelatedValue(r, sr.HasResultFile); + File file = new File(resultPath); + if(file.exists()) { + try { + result.add(graph.adapt(r, AbstractNode.class)); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } else { + graph.asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.deny(r, b.PartOf); + graph.deny(r, graph.getInverse(SysdynResource.getInstance(graph).HasResult)); + + } + }); } } return result; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java index 78333c56..7b2400f4 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java @@ -1,10 +1,14 @@ package org.simantics.sysdyn.ui.browser.contributions; +import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; import org.simantics.browsing.ui.content.LabelDecorator; import org.simantics.browsing.ui.graph.contributor.labeler.LabelDecoratorContributor; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.browser.nodes.SimulationResultNode; public class SimulationResultDecorator extends LabelDecoratorContributor{ @@ -14,9 +18,11 @@ public class SimulationResultDecorator extends LabelDecoratorContributor F decorateFont(F font, String column, int itemIndex) { + return (F) Activator.getResources().get(FontDescriptor.createFrom((Font) font).withStyle(SWT.BOLD)); } }; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultImager.java index 1aef4422..1bc94307 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultImager.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultImager.java @@ -10,8 +10,11 @@ import org.simantics.sysdyn.ui.browser.nodes.SimulationResultNode; public class SimulationResultImager extends ImagerContributor{ @Override - public ImageDescriptor getDescriptor(ReadGraph graph, SimulationResultNode input) throws DatabaseException { - return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_bar.png")); + public ImageDescriptor getDescriptor(ReadGraph graph, SimulationResultNode result) throws DatabaseException { + if(graph.hasStatement(result.resource, graph.getBuiltins().IsActive)) + return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_bar.png")); + else + return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_bar_blackAndWhite.png")); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java index 52666750..171007aa 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/UnlinkNodeHandler.java @@ -129,6 +129,9 @@ public class UnlinkNodeHandler extends AbstractHandler { if(path != null) { File file = new File(path); file.delete(); + File parent = file.getParentFile(); + if(parent.listFiles().length == 0) + parent.delete(); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProjectLifeCycle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProjectLifeCycle.java index 85c9d6e7..2136673d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProjectLifeCycle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProjectLifeCycle.java @@ -1,9 +1,13 @@ package org.simantics.sysdyn.ui.project; +import java.io.File; +import java.util.HashMap; import java.util.Timer; +import org.eclipse.core.runtime.Platform; import org.simantics.databoard.accessor.Accessor; import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -11,18 +15,24 @@ import org.simantics.db.VirtualGraph; import org.simantics.db.WriteGraph; import org.simantics.db.common.procedure.single.SingleSetListener; import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.exception.ValidationException; import org.simantics.db.layer0.adapter.ActivationManager; import org.simantics.db.layer0.adapter.RuntimeValuations; import org.simantics.db.layer0.adapter.TrendVariable; import org.simantics.db.layer0.adapter.ValuationVirtualGraph; import org.simantics.db.layer0.util.Simantics; import org.simantics.db.management.ISessionContext; +import org.simantics.db.request.Read; import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.project.IProject; import org.simantics.project.IProjectLifeCycle; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; @@ -35,18 +45,24 @@ public class SysdynProjectLifeCycle implements IProjectLifeCycle{ @Override public void onCreate(WriteGraph g, Resource project) { -// System.out.println("onCreate Apros Project: " + GraphUtils.getReadableName(g, project)); +// System.out.println("onCreate Sysdyn Project: " + GraphUtils.getReadableName(g, project)); } @Override public void onDestroy(WriteGraph g, Resource project) { -// System.out.println("onDestroy Apros Project: " + GraphUtils.getReadableName(g, project)); + try { + System.out.println("onDestroy Sysdyn Project: " + GraphUtils.getReadableName(g, project)); + } catch (ValidationException e) { + e.printStackTrace(); + } catch (ServiceException e) { + e.printStackTrace(); + } } @Override public void onActivated(RequestProcessor processor, final IProject project) { -// System.out.println("onActivated DEVS Project"); + System.out.println("onActivated Sysdyn Project"); ISessionContext context = SimanticsUI.getSessionContext(); if (context.getHint(INITIALIZED) == null) { @@ -135,6 +151,10 @@ public class SysdynProjectLifeCycle implements IProjectLifeCycle{ support.resume("experiments"); + + + + } catch (DatabaseException e) { e.printStackTrace(); @@ -142,7 +162,7 @@ public class SysdynProjectLifeCycle implements IProjectLifeCycle{ } - + cleanProjectFolder(project.get()); context.setHint(INITIALIZED, Boolean.TRUE); } @@ -151,6 +171,76 @@ public class SysdynProjectLifeCycle implements IProjectLifeCycle{ @Override public void onDeactivated(IProject project) { -// System.out.println("onDeactivated Apros Project: " + project.getName()); +// System.out.println("onDeactivated Sysdyn Project: " + project.getName()); + } + + public void cleanProjectFolder(final Resource projectResource) { + System.out.println("cleaning up project folder"); + ISessionContext context = SimanticsUI.getSessionContext(); + final Session session = context.getSession(); + String projectName = null; + final HashMap resultPaths = new HashMap(); + try { + projectName = session.syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource model : graph.getObjects(projectResource, b.ConsistsOf)) { + if(graph.isInstanceOf(model, sr.SysdynModel)){ + for(Resource experiment : graph.getObjects(model, b.ConsistsOf)) { + if(graph.isInstanceOf(experiment, b.Experiment)) { + for(Resource result : graph.getObjects(experiment, sr.HasResult)) { + String resultFile = (String)graph.getPossibleRelatedValue(result, sr.HasResultFile); + if(result != null) resultPaths.put(resultFile, result); + } + } + } + } + } + return graph.getPossibleRelatedValue(projectResource, b.HasName); + + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + if(projectName != null) { + File root = new File(Platform.getLocation().toOSString(), "www.simantics.org"); + if(!root.isDirectory()) return; + File projectRoot = new File(root, projectName); + if(!projectRoot.isDirectory()) return; + File[] files = projectRoot.listFiles(); + + for(File file : files) { + if(resultPaths.get(file.getAbsolutePath()) == null) { + file.delete(); + } else { + resultPaths.remove(file.getAbsolutePath()); + } + } + + if (!resultPaths.keySet().isEmpty()) { + session.asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Builtins b = graph.getBuiltins(); + for(String key : resultPaths.keySet()) { + Resource result = resultPaths.get(key); + graph.deny(result, b.PartOf); + graph.deny(result, graph.getInverse(SysdynResource.getInstance(graph).HasResult)); + } + } + }) ; + + + } + + + + } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java index a2a2e88f..a4ec1791 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -16,6 +16,7 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.LifecycleSupport; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.simulation.experiment.Experiment; import org.simantics.simulation.experiment.ExperimentState; @@ -36,6 +37,10 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { this.experiment = experiment; // TODO Auto-generated constructor stub } + + public Resource getResource() { + return this.experiment; + } @Override public void rewindTo(double time) { @@ -54,11 +59,15 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { @Override public void perform(WriteGraph graph) throws DatabaseException { - File root = new File(Platform.getLocation().toOSString(), "www.simantics.org"); - if(!root.isDirectory()) - root.mkdir(); - File file = new File( root, UUID.randomUUID().toString() + ".dbb"); Builtins b = graph.getBuiltins(); + Resource model = graph.getSingleObject(experiment, b.PartOf); + Resource project = graph.getSingleObject(model, b.PartOf); + String projectName = graph.getPossibleRelatedValue(project, b.HasName); + File root = new File(Platform.getLocation().toOSString(), "www.simantics.org"); + if(!root.isDirectory()) root.mkdir(); + File projectRoot = new File(root, projectName); + if(!projectRoot.isDirectory()) projectRoot.mkdir(); + File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb"); SysdynResource sr = SysdynResource.getInstance(graph); Resource res = GraphUtils.create2(graph, sr.Result, b.HasLabel, "Result x", @@ -67,9 +76,9 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { sr.HasResultFile, file.getAbsolutePath()); graph.claim(experiment, sr.HasResult, res); result.saveToFile(file); - } }); + session.getService(LifecycleSupport.class).save(); } catch (DatabaseException e) { e.printStackTrace(); } diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 326a0f44..9f37766a 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -166,13 +166,13 @@ IsTailOf