]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/contribution/NameInputValidator.java
14ff5f60d62e7a4f49cb238fcb2431dd0d38400f
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / contribution / NameInputValidator.java
1 package org.simantics.document.ui.contribution;
2
3 import java.util.Collection;
4
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;
15
16 public class NameInputValidator implements IInputValidator, Widget {
17         
18         private Resource selection;
19         
20         @Override
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) {
25                         try {
26                                 return Simantics.getSession().syncRequest(new Read<String>() {
27                                         @Override
28                                         public String perform(ReadGraph graph) throws DatabaseException {
29                                                 Layer0 l0 = Layer0.getInstance(graph);
30                                                 Resource lib = graph.getPossibleObject(selection, l0.PartOf);
31                                                 if (lib == null)
32                                                         return null;
33                                                 Collection<Resource> resources = graph.getObjects(lib, l0.ConsistsOf);
34                                                 for (Resource resource : resources) {
35                                                         if (resource.equals(selection))
36                                                                 continue;
37                                                         String n = graph.getPossibleRelatedValue(resource, l0.HasName);
38                                                         if (newText.equals(n))
39                                                                 return "Cannot have duplicate name " + newText;
40                                                 }
41                                                 return null;
42                                         }
43                                 });
44                         } catch (DatabaseException e) {
45                                 return e.getMessage();
46                         }
47                 }
48                 return null;
49         }
50         
51         @Override
52         public void setInput(ISessionContext context, Object input) {
53                 selection = AdaptionUtils.adaptToSingle(input, Resource.class);
54                 
55         }
56
57 }