]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
42c98c112f1ba2ea6cc9b924cd6e202dd7c1e73e
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.factories;\r
2 \r
3 import org.eclipse.jface.dialogs.IInputValidator;\r
4 import org.eclipse.jface.viewers.ISelection;\r
5 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
6 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.exception.DatabaseException;\r
10 import org.simantics.db.management.ISessionContext;\r
11 import org.simantics.db.request.Read;\r
12 import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
13 import org.simantics.ui.SimanticsUI;\r
14 import org.simantics.utils.ui.ISelectionUtils;\r
15 \r
16 public class VariableNameValidator implements IInputValidator, Widget {\r
17 \r
18         private Resource lastInput = null;\r
19         \r
20         public VariableNameValidator(WidgetSupport support) {\r
21                 support.register(this);\r
22         }\r
23         \r
24         /**\r
25          * Checks that the syntax of the given name is valid and there \r
26          * are no other components that have the same name in the configuration.\r
27          */\r
28         @Override\r
29         public String isValid(final String newText) {\r
30         if (!VariableNameUtils.isValid(newText))\r
31             return "Sorry but spaces and special characters are not allowed for names right now";\r
32         if(lastInput == null) return null;\r
33         String result = null;\r
34         try {\r
35                         result = SimanticsUI.getSession().syncRequest(new Read<String>(){\r
36 \r
37                                 @Override\r
38                                 public String perform(ReadGraph graph) throws DatabaseException {\r
39                                         if(VariableNameUtils.nameIsTaken(graph, lastInput, newText)) {\r
40                                                 return "Name is taken.";\r
41                                         }\r
42                                         return null;\r
43                                 }\r
44                                 \r
45                         }) ;\r
46                 } catch (DatabaseException e) {\r
47                         e.printStackTrace();\r
48                 }\r
49         return result;\r
50 \r
51         }\r
52 \r
53         @Override\r
54         public void setInput(ISessionContext context, Object input) {\r
55                 lastInput = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
56         }\r
57 \r
58 }\r