From: Tuukka Lehtonen Date: Tue, 18 Apr 2017 08:56:01 +0000 (+0300) Subject: Added alternatives taking RequestProcessor to WorkbenchSelectionUtils. X-Git-Tag: v1.29.0~108 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=e6c0bd94;p=simantics%2Fplatform.git Added alternatives taking RequestProcessor to WorkbenchSelectionUtils. Fixed several places which were found to use a method from WorkbenchSelectionUtils that could potentially try to initiate a new read transaction through org.simantics.db.Session to pass the current ReadGraph over to WorkbenchSelectionUtils instead. This fixes the very same problem from OpenWithMenuContribution that was introduced in 6d3ad788 change I45cfa3940df8e1f4abe016b215843dc083227b2b. Also, ComponentTypeViewerData.editValue now prevents committing new entered value if the value does not pass input validation. refs #7077 refs #7142 Change-Id: I542bd24e24ebc46c7da790c847ffafc5b8d18053 --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java index a3e29debd..7fd6340cf 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java @@ -42,6 +42,7 @@ import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.modeling.userComponent.ComponentTypeCommands; +import org.simantics.scl.runtime.function.Function2; import org.simantics.scl.runtime.function.Function4; import org.simantics.utils.ui.ErrorLogger; @@ -86,9 +87,15 @@ public class ComponentTypeViewerData { this.componentType = componentType; this.form = form; } - + public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, Pattern namePattern) { + editName(table, editor, propertyInfo, selectedItem, column, + (pInfo, name) -> validatePropertyName(pInfo, name, namePattern)); + } + + public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, + Function2 nameValidator) { int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0; final Text text = new Text(table, SWT.NONE | extraStyle); org.eclipse.swt.widgets.Listener listener = @@ -102,7 +109,7 @@ public class ComponentTypeViewerData { if (e.type == SWT.Modify) { // validate current name - String error = validatePropertyName(propertyInfo, text.getText(), namePattern); + String error = nameValidator.apply(propertyInfo, text.getText()); if (error != null) { text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED)); form.setMessage(error, IMessageProvider.ERROR); @@ -126,7 +133,7 @@ public class ComponentTypeViewerData { final String newValue = text.getText(); text.dispose(); - String error = validatePropertyName(propertyInfo, newValue, namePattern); + String error = nameValidator.apply(propertyInfo, newValue); if (error != null) return; @@ -302,6 +309,10 @@ public class ComponentTypeViewerData { } text.dispose(); + String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue); + if (error != null) + return; + if (writer != null) { Simantics.getSession().async(new WriteRequest() { @Override diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/PopulateElementMonitorDropParticipant.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/PopulateElementMonitorDropParticipant.java index aeecc818f..1c3514793 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/PopulateElementMonitorDropParticipant.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/PopulateElementMonitorDropParticipant.java @@ -147,7 +147,7 @@ public class PopulateElementMonitorDropParticipant extends PopulateElementDropPa if (sel.isEmpty()) return Collections.emptyList(); - Variable property = WorkbenchSelectionUtils.getPossibleVariable(sel); + Variable property = WorkbenchSelectionUtils.getPossibleVariableFromSelection(processor, sel); if(property != null) return Collections.singletonList(property); diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGElementComposite.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGElementComposite.java index 59a894113..44c1409fd 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGElementComposite.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGElementComposite.java @@ -46,7 +46,7 @@ public class SVGElementComposite extends ConfigurationComposite { @Override public boolean accept(ReadGraph graph, Object input) throws DatabaseException { - Resource res = WorkbenchSelectionUtils.getPossibleResource(input); + Resource res = WorkbenchSelectionUtils.getPossibleResourceFromSelection(graph, input); if(res == null) return false; return graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement); } @@ -54,7 +54,7 @@ public class SVGElementComposite extends ConfigurationComposite { @Override public void contribute(ReadGraph graph, Object selection, Collection result) throws DatabaseException { - Resource res = WorkbenchSelectionUtils.getPossibleResource(selection); + Resource res = WorkbenchSelectionUtils.getPossibleResourceFromSelection(graph, selection); if(res != null) { if(graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement)) { result.add(make(res, 100.0, "SVG")); diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java index 4112d9051..948fe4e82 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java @@ -58,12 +58,10 @@ public class OpenWithMenuContribution extends DynamicMenuContribution { } protected Object extractResource(ReadGraph graph, Object object) throws DatabaseException { - Resource resource = WorkbenchSelectionUtils.getPossibleResource(object); - //Resource resource = ResourceAdaptionUtils.adaptToResource(graph, object); - if(resource != null) return resource; - else return object; + Resource resource = WorkbenchSelectionUtils.getPossibleResourceFromSelection(graph, object); + return resource != null ? resource : object; } - + @Override protected boolean preAcceptSelection(Object[] selection) { return selection.length == 1; diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java index a49073a4d..c4d8a72ce 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java @@ -51,7 +51,7 @@ public abstract class OperationsMenuContribution extends DynamicMenuContribution @Override protected IAction[] getActions(ReadGraph g, Object[] selection) throws DatabaseException { if(selection.length == 1) { - final Resource r = WorkbenchSelectionUtils.getPossibleResource(selection[0]); + final Resource r = WorkbenchSelectionUtils.getPossibleResourceFromSelection(g, selection[0]); if(r == null) return NO_ACTIONS; try { diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java b/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java index 765a37c2a..ecdda45c2 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java @@ -33,24 +33,28 @@ public class WorkbenchSelectionUtils { } public static String getPossibleJSON(Object selection) throws DatabaseException { - if(selection instanceof WorkbenchSelectionElement) return getPossibleJSON((WorkbenchSelectionElement)selection); - WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class); - if(element == null) return null; - return getPossibleJSON(element); + WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection); + return element != null ? getPossibleJSON(element) : null; } public static Resource getPossibleResource(Object selection) throws DatabaseException { - if(selection instanceof WorkbenchSelectionElement) return getPossibleResource((WorkbenchSelectionElement)selection); - WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class); - if(element == null) return null; - return getPossibleResource(element); + WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection); + return element != null ? getPossibleResource(element) : null; } public static Variable getPossibleVariable(Object selection) throws DatabaseException { - if(selection instanceof WorkbenchSelectionElement) return getPossibleVariable((WorkbenchSelectionElement)selection); - WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class); - if(element == null) return null; - return getPossibleVariable(element); + WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection); + return element != null ? getPossibleVariable(element) : null; + } + + public static Resource getPossibleResourceFromSelection(RequestProcessor processor, Object selection) throws DatabaseException { + WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection); + return element != null ? getPossibleResource(processor, element) : null; + } + + public static Variable getPossibleVariableFromSelection(RequestProcessor processor, Object selection) throws DatabaseException { + WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection); + return element != null ? getPossibleVariable(processor, element) : null; } public static Variable getPossibleVariable(Object[] selection) throws DatabaseException { @@ -66,20 +70,6 @@ public class WorkbenchSelectionUtils { return wse.getContent(contentType); } -// public static T getPossibleExplorerInput(Object selection) throws DatabaseException { -// if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerInput((WorkbenchSelectionElement)selection); -// WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class); -// if(element == null) return null; -// return getPossibleExplorerInput(element); -// } - -// public static T getPossibleExplorerColumn(Object selection) throws DatabaseException { -// if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerColumn((WorkbenchSelectionElement)selection); -// WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class); -// if(element == null) return null; -// return getPossibleExplorerColumn(element); -// } - public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException { return getPossibleJSON(Simantics.getSession(), wse); } @@ -137,11 +127,6 @@ public class WorkbenchSelectionUtils { return getPossibleElement(input, new AnyVariable(processor)); } -// @SuppressWarnings("unchecked") -// public static T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException { -// return ((T)getPossibleElement(input, new ExplorerInputContentType())); -// } - public static T getPossibleElement(Object input, WorkbenchSelectionContentType contentType) { Object single = getPossibleSingleElement(input); if(single == null) return null; @@ -174,6 +159,16 @@ public class WorkbenchSelectionUtils { } return null; } - - + + private static WorkbenchSelectionElement getPossibleWorkbenchSelectionElement(Object selection) { + return getPossibleObject(selection, WorkbenchSelectionElement.class); + } + + @SuppressWarnings("unchecked") + private static T getPossibleObject(Object selection, Class clazz) { + return clazz.isInstance(selection) + ? (T) selection + : ISelectionUtils.filterSingleSelection(selection, clazz); + } + } diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ISelectionUtils.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ISelectionUtils.java index c04ad572b..d3c9f5dd1 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ISelectionUtils.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ISelectionUtils.java @@ -239,15 +239,12 @@ public class ISelectionUtils { * clazz or adaptable to it through {@link IAdaptable}. * * - * - * * @param selection * @param key * @param clazz desired class of the objects to look for in the selection * @return a single objects matching the search criteria. If there are no or * several matches, null is returned */ - @SuppressWarnings("unchecked") public static T getSinglePossibleKey(Object object, Key key, Class clazz) { return getSinglePossibleKey(object, key, clazz, true); }