From: jsimomaa Date: Tue, 15 Jul 2014 12:06:11 +0000 (+0000) Subject: refs #5093 X-Git-Tag: v1.29.0~240 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=007e96e92fdcf9a56747ddfc6cf6609c2d040f3e;p=simantics%2Fsysdyn.git refs #5093 Fixing undo problems in Sysdyn by adding graph.markUndoPoint() calls in write transactions git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29891 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExternalFilesTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExternalFilesTab.java index f2a98f76..fb32aa51 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExternalFilesTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExternalFilesTab.java @@ -12,6 +12,7 @@ package org.simantics.sysdyn.ui.properties; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.eclipse.jface.dialogs.MessageDialog; @@ -23,6 +24,8 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.ui.IWorkbenchSite; @@ -35,17 +38,18 @@ import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -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.util.Layer0Utils; +import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.db.management.ISessionContext; import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.handlers.RemoveNodeHandler; import org.simantics.sysdyn.ui.handlers.exports.ExportExternalFunctionFilesHandler; import org.simantics.sysdyn.ui.handlers.imports.ImportExternalFunctionFilesHandler; -import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.ArrayMap; import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.threads.SWTThread; /** * Tab for properties of a SysdynModelicaFunction containing all external files added to that function @@ -81,8 +85,17 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid Resource.class)); externalFilesExplorer.setContextMenuId("#ExternalFunctionFileBrowser"); + + externalFilesExplorer.addListenerToControl(SWT.Selection, new Listener() { + + @Override + public void handleEvent(Event event) { + updateButtons(externalFilesExplorer); + } + }); + externalFilesExplorer.finish(); - + GridDataFactory.fillDefaults().grab(true, true).applyTo( externalFilesExplorer); @@ -100,34 +113,26 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid importButton.setText("Import"); importButton.addSelectionListener(new SelectionListenerImpl(context) { + private Pair importedFiles; + + @Override + public void beforeApply() { + Shell shell = importButton.getWidget().getShell(); + importedFiles = ImportExternalFunctionFilesHandler.importFiles(shell, "Import files", ImportExternalFunctionFilesHandler.C_EXTENSIONS); + } + @Override - public void apply(WriteGraph graph, final Resource input) - throws DatabaseException { - - importButton.getWidget().getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - Shell shell = importButton.getWidget().getShell(); - - final Pair importedFiles = ImportExternalFunctionFilesHandler.importFiles(shell, "Import files", ImportExternalFunctionFilesHandler.C_EXTENSIONS); - - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - ImportExternalFunctionFilesHandler.addFilesToFunction(graph, input, importedFiles); - } - }); - } - }); - - + public void apply(WriteGraph graph, final Resource input) throws DatabaseException { + graph.markUndoPoint(); + ImportExternalFunctionFilesHandler.addFilesToFunction(graph, input, importedFiles); + Layer0Utils.addCommentMetadata(graph, "Imported External File(s) " + Arrays.toString(importedFiles.second) + " to " + NameUtils.getSafeName(graph, input)); + updateButtons(externalFilesExplorer); } }); exportButton = new Button(buttonRow, support, SWT.NONE); exportButton.setText("Export"); + exportButton.getControl().setEnabled(false); exportButton.addSelectionListener(new SelectionListenerImpl(context) { @Override @@ -145,39 +150,46 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid ExportExternalFunctionFilesHandler.exportFiles(shell, resources); } }); - - } }); removeButton = new Button(buttonRow, support, SWT.NONE); removeButton.setText("Remove"); + removeButton.getControl().setEnabled(false); removeButton.addSelectionListener(new SelectionListenerImpl(context) { + private int delete; + private List resourceList; + + @Override + public void beforeApply() { + Shell shell = removeButton.getWidget().getShell(); + + resourceList = getSelectedResources(externalFilesExplorer); + Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]); + if(resources.length > 0) { + MessageDialog dialog = new MessageDialog(shell, resources.length > 1 ? "Remove selected items" : "Remove selected item" , null, "Are you sure?", 0, + new String[] { "OK", "Cancel" }, 0); + dialog.create(); + delete = dialog.open(); + } + } + @Override - public void apply(WriteGraph graph, final Resource input) - throws DatabaseException { - - removeButton.getWidget().getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - Shell shell = removeButton.getWidget().getShell(); - - List resourceList = getSelectedResources(externalFilesExplorer); - Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]); - if(resources.length > 0) { - MessageDialog dialog = new MessageDialog(shell, resources.length > 1 ? "Remove selected items" : "Remove selected item" , null, "Are you sure?", 0, - new String[] { "OK", "Cancel" }, 0); - dialog.create(); - if (dialog.open() == 0) { - RemoveNodeHandler.deleteItem(resources); - } - } - } - }); - - + public void apply(WriteGraph graph, final Resource input) throws DatabaseException { + if (delete == 0) { + Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]); + StringBuilder sb = new StringBuilder(); + sb.append("Removed External File(s) "); + for (Resource resource : resources) { + RemoverUtil.remove(graph, resource); + sb.append(NameUtils.getSafeName(graph, resource) + " "); + } + sb.append("from " + NameUtils.getSafeName(graph, input)); + Layer0Utils.addCommentMetadata(graph, sb.toString()); + + updateButtons(externalFilesExplorer); + } } }); } @@ -185,6 +197,7 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid @Override public void setInput(ISessionContext context, Object input) { externalFilesExplorer.setInput(context, input); + updateButtons(externalFilesExplorer); } /** @@ -194,7 +207,6 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid */ private List getSelectedResources(GraphExplorerComposite explorer) { List result = new ArrayList(); - ISelection selection = ((ISelectionProvider) explorer .getAdapter(ISelectionProvider.class)).getSelection(); if (selection == null) @@ -208,5 +220,30 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid } return result; } - + + protected void updateButtons(final GraphExplorerComposite explorer) { + if (SWTThread.getThreadAccess().currentThreadAccess()) { + runnable.run(); + } else { + SWTThread.getThreadAccess().asyncExec(runnable); + } + } + + Runnable runnable = new Runnable() { + + @Override + public void run() { + ISelection selection = ((ISelectionProvider) externalFilesExplorer.getAdapter(ISelectionProvider.class)).getSelection(); + + IStructuredSelection iss = (IStructuredSelection) selection; + @SuppressWarnings("unchecked") + List selections = iss.toList(); + + boolean enabled = false; + if (!selections.isEmpty()) + enabled = true; + removeButton.getControl().setEnabled(enabled); + exportButton.getControl().setEnabled(enabled); + } + }; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java index c0944c03..c4564eaf 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java @@ -17,7 +17,6 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.ui.IWorkbenchSite; -import org.lobobrowser.util.ID; import org.simantics.browsing.ui.swt.widgets.Label; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; @@ -25,17 +24,16 @@ import org.simantics.browsing.ui.swt.widgets.TrackedText; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.common.request.IsParent; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.db.request.Read; import org.simantics.jfreechart.chart.properties.AdjustableTab; import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.properties.widgets.FunctionLabelFactory; import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField; import org.simantics.sysdyn.ui.properties.widgets.factories.FunctionNameInputValidator; import org.simantics.sysdyn.ui.properties.widgets.functions.FunctionCodeWidget; +import org.simantics.sysdyn.utils.SelectionUtils; public class FunctionTab extends AdjustableTab { @@ -100,8 +98,7 @@ public class FunctionTab extends AdjustableTab { @Override public Boolean perform(ReadGraph graph) throws DatabaseException { - boolean editable = new IsParent(SysdynResource.getInstance(graph).Built$in_Functions, r).perform(graph); - return !editable; + return SelectionUtils.canEdit(graph, r); } }); information.setEditable(editable); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index 11ef3069..ab129cfb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -44,6 +44,7 @@ import org.simantics.sysdyn.JFreeChartResource; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder; import org.simantics.sysdyn.ui.trend.SensitivityChartAxisAndVariablesTab; +import org.simantics.sysdyn.utils.SelectionUtils; import org.simantics.ui.selection.AnyVariable; import org.simantics.ui.selection.WorkbenchSelectionElement; import org.simantics.utils.ui.AdaptionUtils; @@ -454,11 +455,13 @@ public class ResourceSelectionProcessor implements SelectionProcessor