]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
879dc8f688034a519af1649af1fdd8d233931fda
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.properties.widgets.factories;\r
13 \r
14 import org.eclipse.jface.dialogs.IInputValidator;\r
15 import org.eclipse.jface.viewers.ISelection;\r
16 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
17 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.management.ISessionContext;\r
20 import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
21 import org.simantics.utils.ui.ISelectionUtils;\r
22 \r
23 public class VariableNameValidator implements IInputValidator, Widget {\r
24 \r
25         private Resource lastInput = null;\r
26         \r
27         public VariableNameValidator(WidgetSupport support) {\r
28                 support.register(this);\r
29         }\r
30         \r
31         /**\r
32          * Checks that the syntax of the given name is valid and there \r
33          * are no other components that have the same name in the configuration.\r
34          */\r
35         @Override\r
36         public String isValid(final String newText) {\r
37         if (!VariableNameUtils.isValid(lastInput, newText, true))\r
38             return "Not valid";\r
39         else\r
40                 return null;\r
41         /*\r
42         if(lastInput == null) return null;\r
43         String result = null;\r
44         try {\r
45                         result = SimanticsUI.getSession().syncRequest(new Read<String>(){\r
46 \r
47                                 @Override\r
48                                 public String perform(ReadGraph graph) throws DatabaseException {\r
49                                         if(VariableNameUtils.nameIsTaken(graph, lastInput, newText)) {\r
50                                                 return "Name is taken.";\r
51                                         }\r
52                                         return null;\r
53                                 }\r
54                                 \r
55                         }) ;\r
56                 } catch (DatabaseException e) {\r
57                         e.printStackTrace();\r
58                 }\r
59         return result;\r
60         */\r
61 \r
62         }\r
63 \r
64         @Override\r
65         public void setInput(ISessionContext context, Object input) {\r
66                 lastInput = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
67         }\r
68 \r
69 }\r