]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added alternatives taking RequestProcessor to WorkbenchSelectionUtils. 15/415/4
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 18 Apr 2017 08:56:01 +0000 (11:56 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 18 Apr 2017 09:06:44 +0000 (12:06 +0300)
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

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/PopulateElementMonitorDropParticipant.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGElementComposite.java
bundles/org.simantics.ui/src/org/simantics/ui/contribution/OpenWithMenuContribution.java
bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java
bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ISelectionUtils.java

index a3e29debd21348990b8286a31727799428e837a2..7fd6340cfbc859770bf889ed36fd5e2074f28367 100644 (file)
@@ -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<ComponentTypeViewerPropertyInfo, String, String> 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
index aeecc818f25e4ab4997ec6078926c2b5f84350e9..1c35147934c37ecfc7ab5b1f0e49523561b8bcbf 100644 (file)
@@ -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);
 
index 59a894113138d409fe228fa373cf887755157e9b..44c1409fdfe21f02a6ae4a5f4b1fd38d451543d6 100644 (file)
@@ -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<ComparableTabContributor> 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"));
index 4112d905134e5b9319a86e8db55a1f14a7112f6c..948fe4e8256287c4c271a5f63d51d77fc686236f 100644 (file)
@@ -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;
index a49073a4d7a7ddbe0ffd07e7340f8f34edd88bb7..c4d8a72ce961a6b1c155b854cbfa68d1106f96a8 100644 (file)
@@ -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 {
index 765a37c2a8e60c8c4f009bf513a659044a4a8863..ecdda45c24ba654de669a56bf052a9d8255d4e1c 100644 (file)
@@ -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> 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> 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> T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException {
-//             return ((T)getPossibleElement(input, new ExplorerInputContentType()));
-//     }
-
        public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> 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> T getPossibleObject(Object selection, Class<T> clazz) {
+        return clazz.isInstance(selection)
+                ? (T) selection
+                : ISelectionUtils.filterSingleSelection(selection, clazz);
+    }
+
 }
index c04ad572bad1ebd48ed130e3c8916cf64fa2311b..d3c9f5dd1bc14802bbe83371e1afa84138a2e87a 100644 (file)
@@ -239,15 +239,12 @@ public class ISelectionUtils<T> {
      * clazz or adaptable to it through {@link IAdaptable}.</li>
      * </ul>
      * 
-     * 
-     * 
      * @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, <code>null</code> is returned
      */
-    @SuppressWarnings("unchecked")
     public static <T> T getSinglePossibleKey(Object object, Key key, Class<T> clazz) {
         return getSinglePossibleKey(object, key, clazz, true);
     }