Fix bug when opening and closing confnode extra viewers
authorlaurent
Fri, 15 Jun 2012 18:08:26 +0200
changeset 774 78b5fa92dd1c
parent 773 3f3f1b8fcb07
child 775 172be32a2482
Fix bug when opening and closing confnode extra viewers
ConfigTreeNode.py
ProjectController.py
canfestival/canfestival.py
--- a/ConfigTreeNode.py	Thu Jun 14 18:50:28 2012 +0200
+++ b/ConfigTreeNode.py	Fri Jun 15 18:08:26 2012 +0200
@@ -406,25 +406,30 @@
         return res
 
     def _OpenView(self, name=None):
-        if self.EditorType is not None and self._View is None:
-            app_frame = self.GetCTRoot().AppFrame
-            
-            self._View = self.EditorType(app_frame.TabsOpened, self, app_frame)
-            
-            app_frame.EditProjectElement(self._View, self.CTNName())
-            
+        if self.EditorType is not None:
+            if self._View is None:
+                app_frame = self.GetCTRoot().AppFrame
+                
+                self._View = self.EditorType(app_frame.TabsOpened, self, app_frame)
+                
+                app_frame.EditProjectElement(self._View, self.CTNName())
+                
             return self._View
         return None
 
+    def _CloseView(self, view):
+        app_frame = self.GetCTRoot().AppFrame
+        if app_frame is not None:
+            app_frame.DeletePage(view)
+
     def OnCloseEditor(self, view):
         if self._View == view:
             self._View = None
 
     def OnCTNClose(self):
         if self._View is not None:
-            app_frame = self.GetCTRoot().AppFrame
-            if app_frame is not None:
-                app_frame.DeletePage(self._View)
+            self._CloseView(self.View)
+            self._View = None
         return True
 
     def _doRemoveChild(self, CTNInstance):
--- a/ProjectController.py	Thu Jun 14 18:50:28 2012 +0200
+++ b/ProjectController.py	Fri Jun 15 18:08:26 2012 +0200
@@ -805,6 +805,7 @@
         """
         if self.AppFrame is not None:
             self.AppFrame.ClearErrors()
+        self._CloseView(self._IECCodeView)
         
         buildpath = self._getBuildPath()
 
@@ -924,49 +925,62 @@
             #print from_location, to_location, start_row, start_col, start, end
             if self.AppFrame is not None:
                 self.AppFrame.ShowError(infos, start, end)
-
+    
+    _IECCodeView = None
     def _showIECcode(self):
         self._OpenView("IEC code")
 
+    _IECRawCodeView = None
     def _editIECrawcode(self):
         self._OpenView("IEC raw code")
 
     def _OpenView(self, name=None):
         if name == "IEC code":
-            plc_file = self._getIECcodepath()
-        
-            IEC_code_viewer = TextViewer(self.AppFrame.TabsOpened, "", None, None, instancepath=name)
-            #IEC_code_viewer.Enable(False)
-            IEC_code_viewer.SetTextSyntax("ALL")
-            IEC_code_viewer.SetKeywords(IEC_KEYWORDS)
-            try:
-                text = file(plc_file).read()
-            except:
-                text = '(* No IEC code have been generated at that time ! *)'
-            IEC_code_viewer.SetText(text = text)
-            IEC_code_viewer.SetIcon(self.AppFrame.GenerateBitmap("ST"))
+            if self._IEC_code_viewer is None:
+                plc_file = self._getIECcodepath()
+            
+                self._IECCodeView = TextViewer(self.AppFrame.TabsOpened, "", None, None, instancepath=name)
+                #self._IECCodeViewr.Enable(False)
+                self._IECCodeView.SetTextSyntax("ALL")
+                self._IECCodeView.SetKeywords(IEC_KEYWORDS)
+                try:
+                    text = file(plc_file).read()
+                except:
+                    text = '(* No IEC code have been generated at that time ! *)'
+                self._IECCodeView.SetText(text = text)
+                self._IECCodeView.SetIcon(self.AppFrame.GenerateBitmap("ST"))
+                    
+                self.AppFrame.EditProjectElement(self._IECCodeView, name)
                 
-            self.AppFrame.EditProjectElement(IEC_code_viewer, name)
-            
-            return IEC_code_viewer
+            return self._IECCodeView
         
         elif name == "IEC raw code":
-            controler = MiniTextControler(self._getIECrawcodepath())
-            IEC_raw_code_viewer = TextViewer(self.AppFrame.TabsOpened, "", None, controler, instancepath=name)
-            #IEC_raw_code_viewer.Enable(False)
-            IEC_raw_code_viewer.SetTextSyntax("ALL")
-            IEC_raw_code_viewer.SetKeywords(IEC_KEYWORDS)
-            IEC_raw_code_viewer.RefreshView()
-            IEC_raw_code_viewer.SetIcon(self.AppFrame.GenerateBitmap("ST"))
+            if self.IEC_raw_code_viewer is None:
+                controler = MiniTextControler(self._getIECrawcodepath())
                 
-            self.AppFrame.EditProjectElement(IEC_raw_code_viewer, name)
-
-            return IEC_raw_code_viewer
+                self.IEC_raw_code_viewer = TextViewer(self.AppFrame.TabsOpened, "", None, controler, instancepath=name)
+                #self.IEC_raw_code_viewer.Enable(False)
+                self.IEC_raw_code_viewer.SetTextSyntax("ALL")
+                self.IEC_raw_code_viewer.SetKeywords(IEC_KEYWORDS)
+                self.IEC_raw_code_viewer.RefreshView()
+                self.IEC_raw_code_viewer.SetIcon(self.AppFrame.GenerateBitmap("ST"))
+                    
+                self.AppFrame.EditProjectElement(self.IEC_raw_code_viewer, name)
+
+            return self.IEC_raw_code_viewer
         
         else:
             return ConfigTreeNode._OpenView(self, name)
 
+    def OnCloseEditor(self, view):
+        ConfigTreeNode.OnCloseEditor(self, view)
+        if self._IECCodeView == view:
+            self._IECCodeView = None
+        if self._IECRawCodeView == view:
+            self._IECRawCodeView = None
+
     def _Clean(self):
+        self._CloseView(self._IECCodeView)
         if os.path.isdir(os.path.join(self._getBuildPath())):
             self.logger.write(_("Cleaning the build directory\n"))
             shutil.rmtree(os.path.join(self._getBuildPath()))
--- a/canfestival/canfestival.py	Thu Jun 14 18:50:28 2012 +0200
+++ b/canfestival/canfestival.py	Fri Jun 15 18:08:26 2012 +0200
@@ -112,6 +112,7 @@
         ConfigTreeNode._OpenView(self)
         if self._View is not None:
             self._View.SetBusId(self.GetCurrentLocation())
+        return self._View
 
     ConfNodeMethods = [
         {"bitmap" : "NetworkEdit",
@@ -120,10 +121,6 @@
          "method" : "_OpenView"},
     ]
 
-    def OnCTNClose(self):
-        if self._View:
-            self._View.Close()
-
     def CTNTestModified(self):
         return self.ChangesToSave or self.OneFileHasChanged()
         
@@ -201,6 +198,8 @@
     def GetBufferState(self):
         return self.GetCurrentBufferState()
 
+    ConfNodeMethods = []
+
 class _NodeListCTN(NodeList):
     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
@@ -238,37 +237,37 @@
         
         return result
     
-    def _OpenView(self):
-        ConfigTreeNode._OpenView(self)
-        if self._View is not None:
-            self._View.SetBusId(self.GetCurrentLocation())
-    
-    _GeneratedView = None
-    def _ShowMasterGenerated(self):
-        if self._GeneratedView is None:
-            buildpath = self._getBuildPath()
-            # Eventually create build dir
-            if not os.path.exists(buildpath):
-                self.GetCTRoot().logger.write_error(_("Error: No PLC built\n"))
-                return
+    _GeneratedMasterView = None
+    def _ShowGeneratedMaster(self):
+        self._OpenView("Generated master")
+        
+    def _OpenView(self, name=None):
+        if name == "Generated master":
+            if self._GeneratedMasterView is None:
+                buildpath = self._getBuildPath()
+                # Eventually create build dir
+                if not os.path.exists(buildpath):
+                    self.GetCTRoot().logger.write_error(_("Error: No PLC built\n"))
+                    return
+                
+                masterpath = os.path.join(buildpath, "MasterGenerated.od")
+                if not os.path.exists(masterpath):
+                    self.GetCTRoot().logger.write_error(_("Error: No Master generated\n"))
+                    return
+                
+                app_frame = self.GetCTRoot().AppFrame
+                
+                manager = MiniNodeManager(self, masterpath, self.CTNFullName() + ".generated_master")
+                self._GeneratedMasterView = SlaveEditor(app_frame.TabsOpened, manager, app_frame, False)
+                
+                app_frame.EditProjectElement(self._GeneratedMasterView, name)
             
-            masterpath = os.path.join(buildpath, "MasterGenerated.od")
-            if not os.path.exists(masterpath):
-                self.GetCTRoot().logger.write_error(_("Error: No Master generated\n"))
-                return
-            
-            app_frame = self.GetCTRoot().AppFrame
-            
-            manager = MiniNodeManager(self, masterpath, self.CTNFullName() + ".generated_master")
-            self._GeneratedView = SlaveEditor(app_frame.TabsOpened, manager, app_frame, False)
-            
-            app_frame.EditProjectElement(self._GeneratedView, "MasterGenerated")
-    
-    def _CloseGenerateView(self):
-        if self._GeneratedView is not None:
-            app_frame = self.GetCTRoot().AppFrame
-            if app_frame is not None:
-                app_frame.DeletePage(self._GeneratedView)
+            return self._GeneratedMasterView
+        else:
+            ConfigTreeNode._OpenView(self)
+            if self._View is not None:
+                self._View.SetBusId(self.GetCurrentLocation())
+            return self._View
     
     ConfNodeMethods = [
         {"bitmap" : "NetworkEdit",
@@ -278,17 +277,17 @@
         {"bitmap" : "ShowMaster",
          "name" : _("Show Master"), 
          "tooltip" : _("Show Master generated by config_utils"),
-         "method" : "_ShowMasterGenerated"}
+         "method" : "_ShowGeneratedMaster"}
     ]
     
     def OnCloseEditor(self, view):
         ConfigTreeNode.OnCloseEditor(self, view)
-        if self._GeneratedView == view:
-            self._GeneratedView = None
-
+        if self._GeneratedMasterView == view:
+            self._GeneratedMasterView = None
+    
     def OnCTNClose(self):
         ConfigTreeNode.OnCTNClose(self)
-        self._CloseGenerateView()
+        self._CloseView(self._GeneratedMasterView)
         return True
 
     def CTNTestModified(self):
@@ -311,7 +310,7 @@
             }, ...]
         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
         """
-        self._CloseGenerateView()
+        self._CloseView(self._GeneratedMasterView)
         current_location = self.GetCurrentLocation()
         # define a unique name for the generated C file
         prefix = "_".join(map(str, current_location))