Adding support for checking location validity when filled by user
authorlaurent
Mon, 09 Jan 2012 18:16:52 +0100
changeset 623 e747685e4241
parent 622 05394e275a1c
child 624 efedc9d06a59
Adding support for checking location validity when filled by user
controls/VariablePanel.py
--- a/controls/VariablePanel.py	Mon Jan 09 11:08:08 2012 +0100
+++ b/controls/VariablePanel.py	Mon Jan 09 18:16:52 2012 +0100
@@ -24,6 +24,7 @@
 
 import os
 import wx, wx.grid
+import re
 
 from types import TupleType, StringType, UnicodeType
 
@@ -85,6 +86,8 @@
                        "External": lambda x: {"Constant": "Constant"}.get(x, "")
                       }
 
+LOCATION_MODEL = re.compile("((?:%[IQM](?:\*|(?:[XBWLD]?[0-9](?:\.[0-9])*)))?)$")
+
 class VariableTable(CustomTable):
     
     """
@@ -637,28 +640,17 @@
         row, col = event.GetRow(), event.GetCol()
         colname = self.Table.GetColLabelValue(col, False)
         value = self.Table.GetValue(row, col)
-
+        message = None
+        
         if colname == "Name" and value != "":
             if not TestIdentifier(value):
-                message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%value, _("Error"), wx.OK|wx.ICON_ERROR)
-                message.ShowModal()
-                message.Destroy()
-                event.Veto()
+                message = _("\"%s\" is not a valid identifier!") % value
             elif value.upper() in IEC_KEYWORDS:
-                message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%value, _("Error"), wx.OK|wx.ICON_ERROR)
-                message.ShowModal()
-                message.Destroy()
-                event.Veto()
+                message = _("\"%s\" is a keyword. It can't be used!") % value
             elif value.upper() in self.PouNames:
-                message = wx.MessageDialog(self, _("A POU named \"%s\" already exists!")%value, _("Error"), wx.OK|wx.ICON_ERROR)
-                message.ShowModal()
-                message.Destroy()
-                event.Veto()
+                message = _("A POU named \"%s\" already exists!") % value
             elif value.upper() in [var["Name"].upper() for var in self.Values if var != self.Table.data[row]]:
-                message = wx.MessageDialog(self, _("A variable with \"%s\" as name already exists in this pou!")%value, _("Error"), wx.OK|wx.ICON_ERROR)
-                message.ShowModal()
-                message.Destroy()
-                event.Veto()
+                message = _("A variable with \"%s\" as name already exists in this pou!") % value
             else:
                 self.SaveValues(False)
                 old_value = self.Table.GetOldValue()
@@ -668,10 +660,19 @@
                 self.ParentWindow.RefreshView(variablepanel = False)
                 self.ParentWindow._Refresh(TITLE, FILEMENU, EDITMENU, INSTANCESTREE, LIBRARYTREE)
                 event.Skip()
+        elif colname == "Location" and LOCATION_MODEL.match(value) is None:
+            message =  _("\"%s\" is not a valid location!") % value
         else:
             self.SaveValues()
             if colname == "Class":
                 self.ParentWindow.RefreshView(variablepanel = False)
+            
+        if message is not None:
+            dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
+            dialog.ShowModal()
+            dialog.Destroy()
+            event.Veto()
+        else:
             event.Skip()
     
     def OnVariablesGridEditorShown(self, event):