[commit] r1159 - trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter

GMESRC Repository Notifications gme-commit at list.isis.vanderbilt.edu
Wed Feb 9 14:19:42 CST 2011


Author: ksmyth
Date: Wed Feb  9 14:19:42 2011
New Revision: 1159

Log:
Cleanup: remove some static vars, remove message boxes, etc

Modified:
   trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/GMEConsole.cs
   trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MgaGateway.cs
   trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MyInterpreter.cs
   trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/Registrar.cs

Modified: trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/GMEConsole.cs
==============================================================================
--- trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/GMEConsole.cs	Wed Feb  9 12:53:29 2011	(r1158)
+++ trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/GMEConsole.cs	Wed Feb  9 14:19:42 2011	(r1159)
@@ -11,17 +11,25 @@
     /// Automatically redirects console messages to the GME console output, if GME is available.
     /// Otherwise prints the output to System console.
     /// </summary>
-    public static class GMEConsole
+    public class GMEConsole
     {
 
         /// <summary>
         /// The GME application variable
         /// </summary>
-        static public IGMEOLEApp gme = null;
-        private static GMETextWriter error = new GMETextWriter(msgtype_enum.MSG_ERROR);
-        private static GMETextWriter warning = new GMETextWriter(msgtype_enum.MSG_WARNING);
-        private static GMETextWriter info = new GMETextWriter(msgtype_enum.MSG_INFO);
-        private static GMETextWriter normal = new GMETextWriter(msgtype_enum.MSG_NORMAL);
+        public IGMEOLEApp gme = null;
+        private GMETextWriter error;
+        private GMETextWriter warning;
+        private GMETextWriter info;
+        private GMETextWriter normal;
+
+        public GMEConsole()
+        {
+            error = new GMETextWriter(msgtype_enum.MSG_ERROR, this);
+            warning = new GMETextWriter(msgtype_enum.MSG_WARNING, this);
+            info = new GMETextWriter(msgtype_enum.MSG_INFO, this);
+            normal = new GMETextWriter(msgtype_enum.MSG_NORMAL, this);
+        }
 
         /// <summary>
         /// Handles error messages
@@ -30,10 +38,9 @@
         /// If console is initialized, the message appears in GME console, if not, then in standard error.
         /// If DEBUG is defined, it also appears in VS output window.
         /// </summary>
-        public static TextWriter Error
+        public TextWriter Error
         {
-
-            get { return error; }            
+            get { return error; }
         }
 
         /// <summary>
@@ -41,9 +48,8 @@
         /// The message to be written. GME Console does not handle special characters and trims white-spaces.
         /// Example: GMEConsole.Out.Write("RootFolder name : {0}.", rf.Name);
         /// </summary>
-        public static TextWriter Out
+        public TextWriter Out
         {
-
             get { return normal; }
         }
 
@@ -53,20 +59,18 @@
         /// The message to be written. GME Console does not handle special characters and trims white-spaces.
         /// Example: GMEConsole.Warning.Write("RootFolder name is not changed : {0}.", rf.Name);
         /// </summary>
-        public static TextWriter Warning
+        public TextWriter Warning
         {
-
             get { return warning; }
         }
 
 
-
         /// <summary>
         /// Proints info messages.
         /// The message to be written. GME Console does not handle special characters and trims white-spaces.
         /// Example: GMEConsole.Info.Write("RootFolder name is changed : {0}.", rf.Name);
         /// </summary>
-        public static TextWriter Info
+        public TextWriter Info
         {
             get { return info; }
         }
@@ -74,7 +78,7 @@
         /// <summary>
         /// Clear the console
         /// </summary>
-        public static void Clear()
+        public void Clear()
         {
             if (gme != null)
                 gme.ConsoleClear();
@@ -82,22 +86,44 @@
                 System.Console.Clear();
         }
 
+
+        public GMEConsole CreateFromProject(MGALib.MgaProject project)
+        {
+            GMEConsole console = new GMEConsole();
+            try
+            {
+                // Initializing console               
+                console.gme = (IGMEOLEApp)project.GetClientByName("GME.Application").OLEServer;
+            }
+            catch (System.Runtime.InteropServices.COMException ex)
+            {
+                // if GME is not present, the interpreter is called from standalone test application
+                if (ex.ErrorCode != -2023423888) // HResult 0x87650070: "Search by name failed"
+                {
+                    throw;
+                }
+                console.gme = null;
+            }
+            return console;
+        }
     }
 
 
     public class GMETextWriter : System.IO.TextWriter
     {
         private msgtype_enum type;
+        private GMEConsole console;
 
-        public GMETextWriter(msgtype_enum type)
+        public GMETextWriter(msgtype_enum type, GMEConsole console)
         {
             this.type = type;
+            this.console = console;
         }
 
         override public Encoding Encoding
         {
-            get {return Encoding.ASCII;} 
-                
+            get { return Encoding.ASCII; }
+
         }
 
         override public void WriteLine(string str)
@@ -107,7 +133,7 @@
 
         override public void Write(string str)
         {
-            if (GMEConsole.gme == null)
+            if (console.gme == null)
             {
                 switch (type)
                 {
@@ -122,15 +148,15 @@
                         break;
                     case msgtype_enum.MSG_ERROR:
                         Console.Error.Write(str);
-                        #if(DEBUG)
-                            System.Diagnostics.Debug.Write(str);
-                        #endif
+#if(DEBUG)
+                        System.Diagnostics.Debug.Write(str);
+#endif
                         break;
                 }
             }
             else
             {
-                GMEConsole.gme.ConsoleMessage(str, type);
+                console.gme.ConsoleMessage(str, type);
             }
         }
     }

Modified: trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MgaGateway.cs
==============================================================================
--- trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MgaGateway.cs	Wed Feb  9 12:53:29 2011	(r1158)
+++ trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MgaGateway.cs	Wed Feb  9 14:19:42 2011	(r1159)
@@ -9,34 +9,60 @@
 
 namespace GME.CSharp
 {
-    static class MgaGateway
+    class MgaGateway
     {
-        public static IMgaProject project = null;
-        public static IMgaTerritory territory = null;
-		// TODO: Add your static variables here. 
-        // Never forget to initialize static variables in each invocation of the component.
-        // Static variables preserves their values across the invocations from the same GME process.
+        public MgaGateway(IMgaProject project)
+        {
+            this.project = project;
+        }
 
+        public IMgaProject project = null;
+        public IMgaTerritory territory = null;
 
-        // TODO: Add your generic MGA access functions here
-#region TRANSACTION HANDLING
-        public static void BeginTransaction(transactiontype_enum mode = transactiontype_enum.TRANSACTION_GENERAL)
+        #region TRANSACTION HANDLING
+        public void BeginTransaction(transactiontype_enum mode = transactiontype_enum.TRANSACTION_GENERAL)
         {
             project.BeginTransaction(territory, mode);
         }
 
-        public static void CommitTransaction()
+        public void CommitTransaction()
         {
-            project.CommitTransaction();
+            if ((project.ProjectStatus & 8) != 0)
+            {
+                project.CommitTransaction();
+            }
         }
 
-        public static void AbortTransaction()
+        public void AbortTransaction()
         {
-            project.AbortTransaction();
+            if ((project.ProjectStatus & 8) != 0)
+            {
+                project.AbortTransaction();
+            }
         }
-#endregion
-#region UTILITIES
-	public static IMgaMetaBase GetMetaByName(string name)
+
+        public delegate void voidDelegate();
+        public void PerformInTransaction(voidDelegate d, transactiontype_enum mode = transactiontype_enum.TRANSACTION_GENERAL)
+        {
+            BeginTransaction(mode);
+            try
+            {
+                d();
+                CommitTransaction();
+            }
+            catch (Exception e)
+            {
+                try
+                {
+                    AbortTransaction();
+                }
+                catch { }
+                throw e;
+            }
+        }
+        #endregion
+        #region UTILITIES
+        public IMgaMetaBase GetMetaByName(string name)
         {
             try
             {
@@ -48,9 +74,9 @@
                 return project.RootMeta.RootFolder.get_DefinedFolderByName(name, false) as MgaMetaFolder;
             }
 #pragma warning restore 0168
-        }
+        }
 
-#endregion
+        #endregion
 
 
     }

Modified: trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MyInterpreter.cs
==============================================================================
--- trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MyInterpreter.cs	Wed Feb  9 12:53:29 2011	(r1158)
+++ trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/MyInterpreter.cs	Wed Feb  9 14:19:42 2011	(r1159)
@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Runtime.InteropServices;
@@ -35,8 +35,8 @@
             GME_BGCONTEXT_START = 18,	// Using the context menu by right clicking the background of the GME modeling window
             GME_ICON_START = 32,		// Not used by GME
             GME_SILENT_MODE = 128 		// Not used by GME, available to testers not using GME
-        } 
-    
+        }
+
         /// <summary>
         /// This function is called for each interpreter invocation before Main.
         /// Don't perform MGA operations here unless you open a tansaction.
@@ -48,7 +48,7 @@
         {
             // TODO: Add your initialization code here...            
         }
-        
+
         /// <summary>
         /// The main entry point of the interpreter. A transaction is already open,
         /// GMEConsole is avaliable. A general try-catch block catches all the exceptions
@@ -76,6 +76,9 @@
 
         #region IMgaComponentEx Members
 
+        MgaGateway MgaGateway { get; set; }
+        GMEConsole GMEConsole { get; set; }
+
         public void InvokeEx(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, int param)
         {
             if (!enabled)
@@ -85,37 +88,14 @@
 
             try
             {
-                MgaGateway.territory = null;
-                try
-                {
-                    // Initializing console               
-                    GMEConsole.gme = (IGMEOLEApp)project.GetClientByName("GME.Application").OLEServer;                    
-                }
-                catch (COMException ex)
-                {
-                    // if GME is not present, the interpreter is called from standalone test application
-                    if (ex.ErrorCode != -2023423888) // HResult 0x87650070: "Search by name failed"
-                    {
-                        throw;
-                    }
-                    GMEConsole.gme = null;
-                }
-                MgaGateway.project = project;
+                GMEConsole = GMEConsole.CreateFromProject(project);
+                MgaGateway = new MgaGateway(project);
                 project.CreateTerritoryWithoutSink(out MgaGateway.territory);
 
-                MgaGateway.BeginTransaction(); // Remove this line for custom transactions, change if read-only interpretation
-                Main(project, currentobj, selectedobjs, Convert(param));
-                MgaGateway.CommitTransaction(); // Remove this line for custom transactions
-            }
-            catch (Exception ex)
-            {                
-                GMEConsole.Error.WriteLine(ex.Message);
-                // Remove this line for custom transactions. 
-                // Be careful: if a transaction is left open, GME is in inconsistent state
-                if (MgaGateway.territory != null)
+                MgaGateway.PerformInTransaction(delegate
                 {
-                    MgaGateway.AbortTransaction();
-                }
+                    Main(project, currentobj, selectedobjs, Convert(param));
+                });
             }
             finally
             {
@@ -123,12 +103,11 @@
                 {
                     MgaGateway.territory.Destroy();
                 }
-                MgaGateway.territory = null;
-                MgaGateway.project = null;
+                MgaGateway = null;
                 project = null;
                 currentobj = null;
                 selectedobjs = null;
-                GMEConsole.gme = null;
+                GMEConsole = null;
                 GC.Collect();
                 GC.WaitForPendingFinalizers();
             }
@@ -139,7 +118,7 @@
             switch (param)
             {
                 case (int)ComponentStartMode.GME_BGCONTEXT_START:
-                    return ComponentStartMode.GME_BGCONTEXT_START;                
+                    return ComponentStartMode.GME_BGCONTEXT_START;
                 case (int)ComponentStartMode.GME_BROWSER_START:
                     return ComponentStartMode.GME_BROWSER_START;
 
@@ -160,7 +139,7 @@
                 case (int)ComponentStartMode.GME_SILENT_MODE:
                     return ComponentStartMode.GME_SILENT_MODE;
             }
-            
+
             return ComponentStartMode.GME_SILENT_MODE;
         }
 
@@ -177,7 +156,7 @@
                 return ComponentConfig.progID;
             }
         }
-        
+
         public componenttype_enum ComponentType
         {
             get { return ComponentConfig.componentType; }
@@ -209,7 +188,7 @@
                 interactiveMode = value;
             }
         }
-        #endregion       
+        #endregion
 
         #region Custom Parameters
         SortedDictionary<string, object> componentParameters = null;
@@ -226,11 +205,11 @@
                 return GetType().FullName;
 
             object value;
-            if(componentParameters!= null && componentParameters.TryGetValue(Name, out value))
+            if (componentParameters != null && componentParameters.TryGetValue(Name, out value))
             {
                 return value;
             }
-                        
+
             return null;
         }
 

Modified: trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/Registrar.cs
==============================================================================
--- trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/Registrar.cs	Wed Feb  9 12:53:29 2011	(r1158)
+++ trunk/SDK/DotNet/CSharpComponentWizard/Templates/CSharpInterpreter/Registrar.cs	Wed Feb  9 14:19:42 2011	(r1159)
@@ -13,77 +13,52 @@
     [ComVisible(false)]
     public class RegistrationException : ApplicationException
     {
-        public RegistrationException(string message):base(message){}
+        public RegistrationException(string message) : base(message) { }
     }
 
     [ComVisible(false)]
-    public class Registrar
+    public static class Registrar
     {
-       
-        public Registrar()
-        {
-        }
-
-
         public static void RegisterComponentsInGMERegistry()
         {
-			if (ComponentConfig.iconPath == null)
-            {               
-                ComponentConfig.iconPath =  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + '\\' + ComponentConfig.iconName;                                
-            }
-			
-			try
+            if (ComponentConfig.iconPath == null)
             {
-                MgaRegistrar registrar = new MgaRegistrar();
-                if ((int)GMEInterfaceVersion_enum.GMEInterfaceVersion_Current != (int)((IGMEVersionInfo)registrar).version)
-                {
-                    throw new RegistrationException("GMEInterfaceVersion mismatch: this assembly is using " +
-                        (int)GMEInterfaceVersion_enum.GMEInterfaceVersion_Current +
-                        " but the GME interface version is " + (int)((IGMEVersionInfo)registrar).version +
-                        "\n\nPlease install a compatible GME version or update the interop dlls.");
-                }
-             
-                registrar.RegisterComponent(ComponentConfig.progID, ComponentConfig.componentType, ComponentConfig.componentName, ComponentConfig.registrationMode);
-                registrar.set_ComponentExtraInfo(ComponentConfig.registrationMode, ComponentConfig.progID, "Icon", ComponentConfig.iconPath);
-              
-                if (!ComponentConfig.paradigmName.Equals("*"))
-                {
-                    registrar.Associate(
-                       ComponentConfig.progID,
-                        ComponentConfig.paradigmName,
-                        ComponentConfig.registrationMode);
-                }
+                ComponentConfig.iconPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + '\\' + ComponentConfig.iconName;
             }
-            catch (Exception e)
+
+            MgaRegistrar registrar = new MgaRegistrar();
+            CheckGMEInterfaceVersion(registrar);
+            registrar.RegisterComponent(ComponentConfig.progID, ComponentConfig.componentType, ComponentConfig.componentName, ComponentConfig.registrationMode);
+            registrar.set_ComponentExtraInfo(ComponentConfig.registrationMode, ComponentConfig.progID, "Icon", ComponentConfig.iconPath);
+
+            if (!ComponentConfig.paradigmName.Equals("*"))
             {
-                System.Windows.Forms.MessageBox.Show(e.Message);
+                registrar.Associate(
+                   ComponentConfig.progID,
+                    ComponentConfig.paradigmName,
+                    ComponentConfig.registrationMode);
             }
-
         }
 
-
-        public static void UnregisterComponentsInGMERegistry()
+        private static void CheckGMEInterfaceVersion(MgaRegistrar registrar)
         {
-            try
+            if ((int)GMEInterfaceVersion_enum.GMEInterfaceVersion_Current != (int)((IGMEVersionInfo)registrar).version)
             {
+                throw new RegistrationException("GMEInterfaceVersion mismatch: this assembly is using " +
+                    (int)GMEInterfaceVersion_enum.GMEInterfaceVersion_Current +
+                    " but the GME interface version is " + (int)((IGMEVersionInfo)registrar).version +
+                    "\n\nPlease install a compatible GME version or update the interop dlls.");
+            }
 
-                MgaRegistrar registrar = new MgaRegistrar();
-                if ((int)GMEInterfaceVersion_enum.GMEInterfaceVersion_Current != (int)((IGMEVersionInfo)registrar).version)
-                {
-                    throw new RegistrationException("GMEInterfaceVersion mismatch: this assembly is using " +
-                        (int)GMEInterfaceVersion_enum.GMEInterfaceVersion_Current +
-                        " but the GME interface version is " + (int)((IGMEVersionInfo)registrar).version +
-                        "\n\nPlease install a compatible GME version or update the interop dlls.");
-                }
+        }
 
-                registrar.UnregisterComponent(ComponentConfig.progID, ComponentConfig.registrationMode);
 
-            }
-            catch (Exception e)
-            {
-                System.Windows.Forms.MessageBox.Show(e.Message);
-            }
+        public static void UnregisterComponentsInGMERegistry()
+        {
+            MgaRegistrar registrar = new MgaRegistrar();
+            CheckGMEInterfaceVersion(registrar);
 
+            registrar.UnregisterComponent(ComponentConfig.progID, ComponentConfig.registrationMode);
         }
     }
 }


More information about the gme-commit mailing list