private void RegisterForms() { for (int i = 0; i < eCommon.SBO_Application.Forms.Count; i++) { if (!oOpenForms.Contains(eCommon.SBO_Application.Forms.Item(i).UniqueID)) { FormAttribute oAttrib = Forms[eCommon.SBO_Application.Forms.Item(i).TypeEx] as FormAttribute; if (oAttrib != null) { try { //Execute the constructor System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); Type oType = asm.GetType(oAttrib.TypeName); System.Reflection.ConstructorInfo ctor = oType.GetConstructor(new Type[1] { typeof(String) }); if (ctor != null) { object oForm = ctor.Invoke(new Object[1] { eCommon.SBO_Application.Forms.Item(i).UniqueID }); } else { throw new Exception("No constructor which accepts the formUID found for form type - " + oAttrib.FormType); } } catch (Exception ex) { eCommon.SBO_Application.MessageBox(ex.Message); } } } } }
/// <summary> /// Constructor /// </summary> public SBOAddon() { try { //Application forms Forms = eCommon.CollectFormsAttribute(); //--------------- remove and load menus ----------- // Change if needed --------------------------- if (eCommon.SBO_Application.Menus.Exists(SBOAddon.gcAddOnName)) { eCommon.SBO_Application.Menus.RemoveEx(SBOAddon.gcAddOnName); } eCommon.SBO_Application.Menus.Item("43520").SubMenus.Add(SBOAddon.gcAddOnName, gcAddonString, BoMenuType.mt_POPUP, 99); // Change if needed --------------------------- List <FormAttribute> oList = new List <FormAttribute>(); foreach (string Key in Forms.Keys) { FormAttribute oAttr = (FormAttribute)Forms[Key]; if (oAttr.HasMenu) { if (eCommon.SBO_Application.Menus.Exists(oAttr.FormType)) { eCommon.SBO_Application.Menus.RemoveEx(oAttr.FormType); } oList.Add(oAttr); } } oList.Sort((n1, n2) => n1.Position.CompareTo(n2.Position)); //Add the menu foreach (FormAttribute oAttr in oList) { eCommon.SBO_Application.Menus.Item(oAttr.ParentMenu).SubMenus.Add(oAttr.FormType, oAttr.MenuName, BoMenuType.mt_STRING, oAttr.Position); } try { eCommon.SBO_Application.Menus.Item(SBOAddon.gcAddOnName).Image = Environment.CurrentDirectory + "\\Logo.JPG"; } catch { } //Register Events RegisterAppEvents(); RegisterFormEvents(); //Register currently opened forms - initialized opened forms so it is ready to use. RegisterForms(); //Need to change //Add SP //eCommon.AddSP("TWM_OPS_DBTrans_GET_DOCUMENT_LIST", SBOAddon.QueriesRM.GetString("TWM_OPS_DBTrans_GET_DOCUMENT_LIST")); //Need to change //Create Authorization AddAuthorizationTree(); //Notify the users the addon is ready to use. eCommon.SBO_Application.StatusBar.SetText("Addon " + SBOAddon.gcAddOnName + " is ready.", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Success); } catch (Exception ex) { Connected = false; MessageBox.Show("Failed initializing addon. " + ex.Message); } finally { } }
public void OnMenuEvent(ref SAPbouiCOM.MenuEvent pVal, out bool Bubble) { Bubble = true; try { if (pVal.BeforeAction == true) { SAPbouiCOM.Form oForm = null; oForm = eCommon.SBO_Application.Forms.ActiveForm; String sXML = oForm.GetAsXML(); switch (pVal.MenuUID) { case "1293": break; case "1285": //Restore Form break; } } else { //After Menu SAPbouiCOM.Form oActiveForm = null; switch (pVal.MenuUID) { case "1293": //Delete Row Menu oActiveForm = eCommon.SBO_Application.Forms.ActiveForm; break; case "1282": //Add Menu pressed oActiveForm = eCommon.SBO_Application.Forms.ActiveForm; break; case "1281": //Find Menu oActiveForm = eCommon.SBO_Application.Forms.ActiveForm; break; default: FormAttribute oAttrib = Forms[pVal.MenuUID] as FormAttribute; if (oAttrib != null) { try { //Execute the constructor System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); Type oType = asm.GetType(oAttrib.TypeName); System.Reflection.ConstructorInfo ctor = oType.GetConstructor(new Type[0]); if (ctor != null) { object oForm = ctor.Invoke(new Object[0]); } else { throw new Exception("No default constructor found for form type - " + oAttrib.FormType); } } catch (Exception Ex) { eCommon.SBO_Application.MessageBox(Ex.Message); } } break; } } } catch (Exception Ex) { eCommon.SBO_Application.StatusBar.SetText(Ex.Message, BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Error); } }
/// <summary> /// Get the form events based on the Form Event attribute declared on the methods in each of the class /// </summary> public void RegisterFormEvents() { string NameSpace = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; Type FormEventAttrType = Type.GetType(string.Format("{0}.FormEventAttribute", NameSpace)); foreach (System.Reflection.Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { if (asm.FullName.StartsWith("mscorlib")) { continue; } if (asm.FullName.StartsWith("Interop")) { continue; } if (asm.FullName.StartsWith("System")) { continue; } if (asm.FullName.StartsWith("Microsoft")) { continue; } foreach (Type type in asm.GetTypes()) { Type FormAttr = Type.GetType(string.Format("{0}.FormAttribute", NameSpace)); FormAttribute frmAttr = null; foreach (System.Attribute Attr in type.GetCustomAttributes(FormAttr, false)) { frmAttr = (FormAttribute)Attr; } //Get the methods attribute foreach (System.Reflection.MethodInfo method in type.GetMethods()) { foreach (System.Attribute Attr in method.GetCustomAttributes(FormEventAttrType, false)) { SAPbouiCOM.EventForm oEvent = null; FormEventAttribute frmEventAttr = (FormEventAttribute)Attr; String sKey = string.Format("{0}_{1}", frmAttr.FormType, frmEventAttr.oEventType.ToString()); if (!SBOAddon.oFormEvents.Contains(frmAttr.FormType)) { oEvent = eCommon.SBO_Application.Forms.GetEventForm(frmAttr.FormType); SBOAddon.oFormEvents.Add(frmAttr.FormType, oEvent); } else { oEvent = (SAPbouiCOM.EventForm)SBOAddon.oFormEvents[frmAttr.FormType]; } if (SBOAddon.oRegisteredFormEvents.Contains(sKey)) { throw new Exception(string.Format("The form event method type [{0}] can not be registered twice", sKey)); } else { SBOAddon.oRegisteredFormEvents.Add(sKey, ""); } Type EventClass = oEvent.GetType(); System.Reflection.EventInfo oInfo = EventClass.GetEvent(frmEventAttr.oEventType.ToString()); if (oInfo == null) { throw new Exception(string.Format("Invalid method info name. [{0}]", frmEventAttr.oEventType.ToString())); } Delegate d = Delegate.CreateDelegate(oInfo.EventHandlerType, method); oInfo.AddEventHandler(oEvent, d); } } } } }