C:/mesDocs/QT_confDbVis_canvas/CreateLinkType.py

Go to the documentation of this file.
00001 import string
00002 
00003 from wxPython.wx import *
00004 
00005 from cdbVisCore import *
00006 from objectclasses import *
00007 import helper
00008 
00009 ###################################################
00010 # CreateLinkTypeWindow class                    ###
00011 ###################################################
00012 
00013 # GUI CONSTANTS
00014 LINKTYPEID = 102
00015 LINKTYPENAME = 103
00016 COLORBUTTON = 104
00017 LINKTYPE_COMBO = 105
00018 ID_ADD = 106
00019 ID_REMOVE = 107
00020 CHECK_COMP = 108
00021 COMP_LINKTYPES = 109
00022 
00023 ##
00024 #  The user creates or modifies a link type;
00025 #       of either simple link type or composite link type.
00026 #       A Composite link type is a link type that consists
00027 #       of several other simple link types.
00028 #       
00029 class CreateLinkTypeWindow(wxDialog):
00030 
00031         ##
00032         #  Constructor.
00033         # 
00034         #                   Parameters:
00035         #                   @parent          - parent window to this window (usually MainWindow)
00036         #                   @id              - id of this window (set by its parent)
00037         #                   @selectedsystem  - the subsystem to create this link type in
00038         #                   @creationmode    - if True, then the link type is being created,
00039         #                                      if False the link type is being modified (already exists)
00040         #                   @linktypeobj     - if we're in modifying a link type, then we have to send a
00041         #                                      link type object with all the variables set to be able to 
00042         #                                      show the attributes to the user so he/she can modify them
00043         #                   @linktypes       - a list of names of link types available in the ConfDB; to
00044         #                                      check that we don't get name conflicts and also for adding
00045         #                                      simple link types to a composite link type.
00046         #               
00047         def __init__(self,parent,id,selectedsystem,creationmode=True,linktypeobj=None,linktypes=[]):
00048 
00049                 self.__creationmode = creationmode
00050                 self.main = parent
00051                 self.__linktypes = linktypes
00052                                                                                                                                                                        
00053                 if creationmode == True:
00054                         title = "Create A Link Type"
00055                 else:
00056                         title = "Modify A Link Type"
00057                         selectedsystem = linktypeobj.GetSystem()
00058                         self.linktypeobj = linktypeobj
00059                         self.renameobj = None # We need a rename object in case we rename the link type
00060                         
00061                         try:
00062                                 self.__linktypes.remove(linktypeobj.GetName()) # We remove this link type name from the list
00063                                                                                 # of link types; we can not add this link
00064                                                                                 # type as a part of a composite link type
00065                                                                                 # for itself
00066                         except ValueError,err:
00067                                 pass
00068 
00069                 height = 200
00070 
00071                 wxDialog.__init__(self,parent,id,title,wxDefaultPosition,wxSize(260,height))
00072 
00073                 #Panel
00074                 self.__panel = wxPanel(self,-1,wxDLG_PNT(self,wxPoint(5,5)))
00075 
00076 
00077                 if not self.__creationmode: # Modify
00078                         id = linktypeobj.GetID()
00079                         self.__linktypeidcaption = wxStaticText(self.__panel,-1,"Link Type ID:")
00080                         self.__linktypeidtxtbox = wxTextCtrl(self.__panel,LINKTYPEID,str(id),wxDefaultPosition,wxSize(50,-1))
00081                         self.__linktypeidtxtbox.Enable(False)
00082                         name = linktypeobj.GetName()
00083                         style = wxCB_READONLY
00084                 else: # Create
00085                         name = ""
00086                         my_links = []
00087                         style = 0
00088 
00089                 self.__linktypecaption = wxStaticText(self.__panel,-1,"Link Type Name(20):")
00090                 self.__linktypetxtbox = wxTextCtrl(self.__panel,LINKTYPENAME,name,wxDefaultPosition,wxSize(120,-1))
00091 
00092                 # Checkbox - composite? If yes, enable combo box
00093                 self.__compositelinktype = wxCheckBox(self.__panel,CHECK_COMP,"Composite linktype?")
00094                 EVT_CHECKBOX(self.__panel,CHECK_COMP,self.CheckboxClicked)
00095 
00096                 self.__linktypescaption = wxStaticText(self.__panel,-1,"Created linktypes:")
00097                 self.__linktypescmbbox = wxComboBox(self.__panel,LINKTYPE_COMBO,"",choices=self.__linktypes,size=wxSize(120,-1))
00098                 self.__addbutton = wxButton(self.__panel,ID_ADD,"&Add",size=wxSize(60,-1))
00099                 EVT_BUTTON(self.__panel,ID_ADD,self.OnAdd)
00100                 self.__rembutton = wxButton(self.__panel,ID_REMOVE,"&Remove",size=wxSize(60,-1))
00101                 EVT_BUTTON(self.__panel,ID_REMOVE,self.OnRemove)
00102                 self.__complinktypes = wxTextCtrl(self.__panel,COMP_LINKTYPES,"",wxDefaultPosition,wxSize(220,50),style=wxTE_MULTILINE | wxTE_READONLY)
00103 
00104                 self.CheckboxClicked(None) #set composite link type to disabled as default
00105 
00106                 #OK and Cancel buttons
00107                 self.__okbutton = wxButton(self.__panel,wxID_OK,"&Ok")
00108                 self.__cancelbutton = wxButton(self.__panel,wxID_CANCEL,"&Cancel")
00109 
00110                 EVT_BUTTON(self,wxID_OK,self.OkButtonClicked)
00111 
00112                 #Layout
00113                 self.__layout = wxBoxSizer(wxVERTICAL)
00114                 buttonlayout = wxBoxSizer(wxHORIZONTAL)
00115                 buttonlayout.Add(self.__okbutton)
00116                 buttonlayout.Add(self.__cancelbutton)
00117 
00118                 namelayout = wxBoxSizer(wxVERTICAL)
00119                 namelayout.Add(self.__linktypecaption)
00120                 namelayout.Add(self.__linktypetxtbox)
00121                 linklayout = wxBoxSizer(wxHORIZONTAL)
00122                 linklayout.Add(namelayout)
00123                 if not creationmode:
00124                         idlayout = wxBoxSizer(wxVERTICAL)
00125                         idlayout.Add(self.__linktypeidcaption)
00126                         idlayout.Add(self.__linktypeidtxtbox)
00127                         linklayout.AddSpacer((10,-1))
00128                         linklayout.Add(idlayout)
00129                 self.__layout.Add(linklayout)
00130 
00131                 linktypeslayout = wxBoxSizer(wxHORIZONTAL)
00132                 linktypeslayout.Add(self.__linktypescmbbox)
00133                 linktypeslayout.Add(self.__addbutton)
00134                 linktypeslayout.Add(self.__rembutton)
00135 
00136                 self.__layout.Add(self.__compositelinktype)
00137                 self.__layout.AddSpacer((-1,5))
00138                 self.__layout.Add(self.__linktypescaption)
00139                 self.__layout.Add(linktypeslayout)
00140                 self.__layout.Add(self.__complinktypes)
00141 
00142                 self.__layout.AddSpacer((-1,5))
00143                 self.__layout.Add(buttonlayout)
00144 
00145                 self.SetSizer(self.__layout)
00146                 self.SetAutoLayout(1)
00147                 self.__layout.Fit(self.__panel)
00148 
00149         ##
00150         #  Enable/Disable the controls for composite link type.
00151         #               
00152         def CheckboxClicked(self,event=None):
00153                 
00154                 if self.__compositelinktype.GetValue() == False:
00155                         self.__linktypescaption.Enable(False)
00156                         self.__linktypescmbbox.Enable(False)
00157                         self.__addbutton.Enable(False)
00158                         self.__rembutton.Enable(False)
00159                         self.__complinktypes.Enable(False)
00160                 else:
00161                         self.__linktypescaption.Enable(True)
00162                         self.__linktypescmbbox.Enable(True)
00163                         self.__addbutton.Enable(True)
00164                         self.__rembutton.Enable(True)
00165 
00166         ##
00167         #  Add a link type (selected in the combo box)
00168         #               to the collection of link types for the current 
00169         #               composite link type.
00170         #               
00171         def OnAdd(self,event):
00172                 
00173                 val = self.__linktypescmbbox.GetValue()
00174                 if string.find(self.__complinktypes.GetValue(),val,0) != -1:
00175                         self.main.ShowError("The linktype is already added to the composite linktype.",ERR_ERROR)
00176                 else:
00177                         self.__complinktypes.SetValue(self.__complinktypes.GetValue() + val + ",")
00178 
00179         ##
00180         #  Remove a link type (selected in the combo box)
00181         #               from the collection of link types for the current
00182         #               composite link type
00183         #               
00184         def OnRemove(self,event):
00185                 
00186                 val = self.__linktypescmbbox.GetValue()
00187                 if string.find(self.__complinktypes.GetValue(),val,0) == -1:
00188                         self.main.ShowError("This linktype is not a part of the composite linktype, and cannot be removed.",ERR_ERROR)
00189                 else:
00190                         myvals = self.__complinktypes.GetValue()
00191                         myvals = myvals.replace(val + ",","")
00192                         self.__complinktypes.SetValue(myvals)
00193 
00194         ##
00195         #  Excessive validation and create/modify the
00196         #               link type object.
00197         # 
00198         #               !return - True if successful, False if not.
00199         #               
00200         def SaveObject(self):
00201                 
00202                 if self.__creationmode:
00203                         create = True
00204                 else:
00205                         create = False
00206 
00207                 try:
00208                         if self.__creationmode: # Create
00209                                 if self.__linktypetxtbox.GetValue() in self.__linktypes:
00210                                         raise ValidationError, "Cannot create linktype as the name you chose is already in use."
00211 
00212                         else: # Modify
00213                                 if self.__linktypetxtbox.GetValue() != self.linktypeobj.GetName():
00214                                         ans = wxMessageBox("You have renamed the link type, was that your intention? Remember that you can not undo renaming, \nbut you can rename the object as many times as you want.","Rename?",wxYES_NO | wxICON_QUESTION)
00215                                         if ans == wxNO:
00216                                                 self.__linktypetxtbox.SetValue(self.linktypeobj.GetName())
00217                                                 return False
00218                                         
00219                                         if self.__linktypetxtbox.GetValue() in self.__linktypes:
00220                                                 raise ValidationError,"The linktype cannot be created as the name you have given is already in use."
00221                                         # Create the rename object since we renamed the link type
00222                                         self.renameobj = LinkType(self.main.GetActiveSystem(),self.__linktypetxtbox.GetValue(),True)
00223                                         self.renameobj.SetOldName(self.linktypeobj.GetName())
00224                                         self.renameobj.SetSaveStatus(RENAME)
00225 
00226                         # Create the standard link type object (create or modify)
00227                         self.lktypeobj = LinkType(self.main.GetActiveSystem(),self.__linktypetxtbox.GetValue(),create)
00228                         self.lktypeobj.SetName(self.__linktypetxtbox.GetValue())
00229                         
00230                         # Create a composite link type (also if we change a simple link type to a composite link type)
00231                         if self.__compositelinktype.GetValue() == True:
00232                                 if self.__complinktypes.GetValue() == "":
00233                                         raise ValidationError,"You have set this type as composite linktype, but you haven't added any linktypes to this linktype."
00234                                 else:
00235                                         self.lktypeobj.SetCompositeLinks(self.__complinktypes.GetValue())
00236                                 
00237 
00238                         if not self.__creationmode and self.__linktypeidtxtbox != "":
00239                                         self.lktypeobj.SetID(int(self.__linktypeidtxtbox.GetValue())) # Set the ID, if already stored in ConfDB
00240                                         
00241                         return True                                                                                                                                                               
00242                 except ValidationError,err:
00243                         self.main.ShowError(str(err),ERR_ERROR,True)
00244                         return False
00245                 except ValueError,err:
00246                         self.main.ShowError("The link ID is not a legal integer value.",ERR_ERROR,True)
00247                         return False
00248 
00249         ##
00250         #  Simple validation before trying to create the
00251         #               link type object.
00252         # 
00253         #               !return - False if unsuccessful, no return if successful; window is closed
00254         #                 
00255         def OkButtonClicked(self,event):
00256                 
00257                 if self.__linktypetxtbox.GetValue() == "":
00258                         self.main.ShowError("The link type has to be given a name.",ERR_ERROR)
00259                         return False
00260 
00261                 success = self.SaveObject()
00262                 if not success:
00263                         return False
00264 
00265                 event.Skip()
00266 
00267         ##
00268         #  Whether we create or modify a link type
00269         #               
00270         #               !return - True if we create a link type, False if we modify
00271         #               
00272         def IsCreationMode(self):
00273                 
00274                 return self.__creationmode
00275 
00276         ##
00277         #  Return link type object (and rename object).
00278         # 
00279         #               !return - The created link type object if we Create,
00280         #               The modified link type object if we modify (and a rename
00281         #               object if we renamed)
00282         #               
00283         def GetLinkTypeObj(self):
00284                 
00285                 if self.__creationmode:
00286                         return self.lktypeobj
00287                 else:
00288                         return self.lktypeobj,self.renameobj
00289 
00290 

Generated on Fri Aug 31 11:11:12 2007 for CDBVis by  doxygen 1.5.3