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
import org.simantics.db.exception.DatabaseException;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.userComponent.ComponentTypeCommands;
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;
import org.simantics.scl.runtime.function.Function4;
import org.simantics.utils.ui.ErrorLogger;
this.componentType = componentType;
this.form = form;
}
this.componentType = componentType;
this.form = form;
}
public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
Pattern namePattern) {
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 =
int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
final Text text = new Text(table, SWT.NONE | extraStyle);
org.eclipse.swt.widgets.Listener listener =
if (e.type == SWT.Modify) {
// validate current name
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);
if (error != null) {
text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
form.setMessage(error, IMessageProvider.ERROR);
final String newValue = text.getText();
text.dispose();
final String newValue = text.getText();
text.dispose();
- String error = validatePropertyName(propertyInfo, newValue, namePattern);
+ String error = nameValidator.apply(propertyInfo, newValue);
if (error != null)
return;
if (error != null)
return;
+ String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
+ if (error != null)
+ return;
+
if (writer != null) {
Simantics.getSession().async(new WriteRequest() {
@Override
if (writer != null) {
Simantics.getSession().async(new WriteRequest() {
@Override
if (sel.isEmpty())
return Collections.emptyList();
if (sel.isEmpty())
return Collections.emptyList();
- Variable property = WorkbenchSelectionUtils.getPossibleVariable(sel);
+ Variable property = WorkbenchSelectionUtils.getPossibleVariableFromSelection(processor, sel);
if(property != null)
return Collections.singletonList(property);
if(property != null)
return Collections.singletonList(property);
@Override
public boolean accept(ReadGraph graph, Object input) throws DatabaseException {
@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);
}
if(res == null) return false;
return graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement);
}
@Override
public void contribute(ReadGraph graph, Object selection, Collection<ComparableTabContributor> result)
throws DatabaseException {
@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"));
if(res != null) {
if(graph.isInstanceOf(res, DiagramResource.getInstance(graph).SVGElement)) {
result.add(make(res, 100.0, "SVG"));
}
protected Object extractResource(ReadGraph graph, Object object) throws DatabaseException {
}
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;
@Override
protected boolean preAcceptSelection(Object[] selection) {
return selection.length == 1;
@Override
protected IAction[] getActions(ReadGraph g, Object[] selection) throws DatabaseException {
if(selection.length == 1) {
@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 {
if(r == null)
return NO_ACTIONS;
try {
}
public static String getPossibleJSON(Object selection) throws DatabaseException {
}
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 {
}
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 {
}
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 {
}
public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {
return wse.getContent(contentType);
}
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);
}
public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
return getPossibleJSON(Simantics.getSession(), wse);
}
return getPossibleElement(input, new AnyVariable(processor));
}
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;
public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {
Object single = getPossibleSingleElement(input);
if(single == null) 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);
+ }
+
* clazz or adaptable to it through {@link IAdaptable}.</li>
* </ul>
*
* 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
*/
* @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);
}
public static <T> T getSinglePossibleKey(Object object, Key key, Class<T> clazz) {
return getSinglePossibleKey(object, key, clazz, true);
}