1 package org.simantics.document.ui.contribution;
3 import java.util.Collection;
5 import org.eclipse.jface.dialogs.IInputValidator;
6 import org.simantics.Simantics;
7 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.management.ISessionContext;
12 import org.simantics.db.request.Read;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.utils.ui.AdaptionUtils;
16 public class NameInputValidator implements IInputValidator, Widget {
18 private Resource selection;
21 public String isValid(final String newText) {
22 if (newText == null || newText.length() == 0)
23 return "Empty name is not allowed";
24 if (selection != null) {
26 return Simantics.getSession().syncRequest(new Read<String>() {
28 public String perform(ReadGraph graph) throws DatabaseException {
29 Layer0 l0 = Layer0.getInstance(graph);
30 Resource lib = graph.getPossibleObject(selection, l0.PartOf);
33 Collection<Resource> resources = graph.getObjects(lib, l0.ConsistsOf);
34 for (Resource resource : resources) {
35 if (resource.equals(selection))
37 String n = graph.getPossibleRelatedValue(resource, l0.HasName);
38 if (newText.equals(n))
39 return "Cannot have duplicate name " + newText;
44 } catch (DatabaseException e) {
45 return e.getMessage();
52 public void setInput(ISessionContext context, Object input) {
53 selection = AdaptionUtils.adaptToSingle(input, Resource.class);