andrej@1571: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1881: 
andrej@1881: from __future__ import absolute_import
Laurent@814: import wx
andrej@1872: from plcopen.structures import TestIdentifier, IEC_KEYWORDS
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                          Edit Step Name Dialog
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
andrej@1736: 
Laurent@814: class SFCStepNameDialog(wx.TextEntryDialog):
Laurent@814: 
andrej@2423:     def __init__(self, parent, message, caption=_("Please enter text"), defaultValue="",
andrej@1767:                  style=wx.OK | wx.CANCEL | wx.CENTRE, pos=wx.DefaultPosition):
Laurent@814:         wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
andrej@1730: 
Laurent@814:         self.PouNames = []
Laurent@814:         self.Variables = []
Laurent@814:         self.StepNames = []
andrej@1730: 
edouard@3303:         self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId())
andrej@1730: 
Laurent@814:     def OnOK(self, event):
Laurent@814:         message = None
Laurent@814:         step_name = self.GetSizer().GetItem(1).GetWindow().GetValue()
Laurent@814:         if step_name == "":
Laurent@814:             message = _("You must type a name!")
Laurent@814:         elif not TestIdentifier(step_name):
Laurent@814:             message = _("\"%s\" is not a valid identifier!") % step_name
Laurent@814:         elif step_name.upper() in IEC_KEYWORDS:
Laurent@814:             message = _("\"%s\" is a keyword. It can't be used!") % step_name
Laurent@814:         elif step_name.upper() in self.PouNames:
Laurent@814:             message = _("A POU named \"%s\" already exists!") % step_name
Laurent@814:         elif step_name.upper() in self.Variables:
Laurent@814:             message = _("A variable with \"%s\" as name already exists in this pou!") % step_name
Laurent@814:         elif step_name.upper() in self.StepNames:
Laurent@814:             message = _("\"%s\" step already exists!") % step_name
Laurent@814:         if message is not None:
andrej@1745:             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR)
Laurent@814:             dialog.ShowModal()
Laurent@814:             dialog.Destroy()
Laurent@814:         else:
Laurent@814:             self.EndModal(wx.ID_OK)
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def SetPouNames(self, pou_names):
Laurent@814:         self.PouNames = [pou_name.upper() for pou_name in pou_names]
Laurent@814: 
Laurent@814:     def SetVariables(self, variables):
Laurent@1347:         self.Variables = [var.Name.upper() for var in variables]
Laurent@814: 
Laurent@814:     def SetStepNames(self, step_names):
Laurent@814:         self.StepNames = [step_name.upper() for step_name in step_names]