private static void OnDropDownToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RibbonSplitMenuItem splitMenuItem = (RibbonSplitMenuItem)d;

            if (splitMenuItem._partArrowButton != null)
            {
                splitMenuItem._partArrowButton.CoerceValue(FrameworkElement.ToolTipProperty);
            }
        }
        private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RibbonSplitMenuItem splitMenuItem = (RibbonSplitMenuItem)d;

            RibbonToggleButton toggleButton = splitMenuItem._headerButton as RibbonToggleButton;

            if (toggleButton != null)
            {
                toggleButton.IsChecked = splitMenuItem.IsChecked;
            }
        }
        private static void OnBorderThicknessChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            RibbonSplitMenuItem splitMenuItem = (RibbonSplitMenuItem)sender;

            splitMenuItem.SetBorderThickness();
        }
        private static void OnIsPressedInternalChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            RibbonSplitMenuItem splitMenuItem = (RibbonSplitMenuItem)sender;

            splitMenuItem.IsPressed = splitMenuItem.IsPressedInternal;
        }
        /// <summary>
        /// Load the bundle-model (all available components) to the ribbon menu
        /// </summary>
        private void LoadBundle(String pathToBundleFile)
        {
            // check, if model is valid against the deployment_model schema
            String xmlError;
            string fName;
            try {
                if (pathToBundleFile == null) {
                    if (File.Exists(ini.IniReadValue("model", "bundle_model_startup"))) {
                        fName = ini.IniReadValue("model", "bundle_model_startup");
                    } else if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + ini.IniReadValue("model", "bundle_model"))) {
                        fName = AppDomain.CurrentDomain.BaseDirectory + ini.IniReadValue("model", "bundle_model");
                    } else {
                        fName = ini.IniReadValue("model", "bundle_model");
                    }
                } else {
                    fName = pathToBundleFile;
                }
                activeBundle = System.IO.Path.GetFileNameWithoutExtension(fName);
                if (activeBundle == "bundle")
                    activeBundle = "default";

                XmlValidation xv = new XmlValidation();
                //xmlError = xv.validateXml(fName, ini.IniReadValue("model", "bundle_schema"));
                if (File.Exists(ini.IniReadValue("model", "bundle_schema"))) {
                    xmlError = xv.validateXml(fName, ini.IniReadValue("model", "bundle_schema"));
                } else {
                    xmlError = xv.validateXml(fName, AppDomain.CurrentDomain.BaseDirectory + ini.IniReadValue("model", "bundle_schema"));
                }
            } catch (Exception ex) {
                MessageBox.Show(Properties.Resources.ReadBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error); // called twice to be shown once. Some kind of initialisation problem
                traceSource.TraceEvent(TraceEventType.Error, 3, ex.Message);
                xmlError = "Error opening bundle file";
                fName = "";
            }

            // if valid, xml-file will be written
            if (xmlError.Equals("")) {
                try {
                    XmlSerializer ser = new XmlSerializer(typeof(Asterics.ACS2.componentTypes));
                    //string fName;
                    //if (File.Exists(ini.IniReadValue("model", "bundle_model"))) {
                    //    fName = ini.IniReadValue("model", "bundle_model");
                    //}
                    //else {
                    //    fName = AppDomain.CurrentDomain.BaseDirectory + ini.IniReadValue("model", "bundle_model");
                    //}
                    StreamReader sr = new StreamReader(fName);
                    Asterics.ACS2.componentTypes allComponents = (Asterics.ACS2.componentTypes)ser.Deserialize(sr);
                    sr.Close();
                    foreach (object o in allComponents.componentType) {
                        if (o is Asterics.ACS2.componentTypesComponentType) {
                            Asterics.ACS2.componentTypesComponentType comp = (Asterics.ACS2.componentTypesComponentType)o;
                            comp.InitGraphPorts(comp.id);
                            componentList.Add(comp.id, comp);
                        }
                    }
                    foreach (Asterics.ACS2.componentTypesComponentType component in componentList.Values) {
                        // making each element in the plugin selection focusable
                        component.ComponentCanvas.Focusable = true;
                    }

                    // setting the ribbon 'components', adding the components to the
                    // four categories
                    actuatorDropDown.Items.Clear();
                    processorDropDown.Items.Clear();
                    sensorDropDown.Items.Clear();
                    specialDropDown.Items.Clear();
                    groupDropDown.Items.Clear();
                    Dictionary<string, RibbonSplitMenuItem> actuatorSubmenus = new Dictionary<string, RibbonSplitMenuItem>();
                    Dictionary<string, RibbonSplitMenuItem> processorSubmenus = new Dictionary<string, RibbonSplitMenuItem>();
                    Dictionary<string, RibbonSplitMenuItem> sensorSubmenus = new Dictionary<string, RibbonSplitMenuItem>();
                    Dictionary<string, RibbonSplitMenuItem> specialSubmenus = new Dictionary<string, RibbonSplitMenuItem>();
                    foreach (RibbonSplitMenuItem rsmi in actuatorSubmenus.Values) {
                        if (rsmi != null)
                            rsmi.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
                    }
                    foreach (Asterics.ACS2.componentTypesComponentType component in componentList.Values) {
                        RibbonApplicationSplitMenuItem i = new RibbonApplicationSplitMenuItem();
                        string header = component.id;
                        header = TrimComponentName(header);
                        i.Header = header;
                        i.Click += AddComponentFromRibbonMenu;
                        i.CommandParameter = component.id;
                        RibbonSplitMenuItem rmi = new RibbonSplitMenuItem();
                        rmi.StaysOpenOnClick = true;
                        rmi.Height = 37;
                        rmi.Header = component.type.subtype;

                        switch (component.type.Value) {
                            case Asterics.ACS2.componentTypeDataTypes.actuator:
                                if (component.type.subtype == null || component.type.subtype.Equals(""))
                                    actuatorDropDown.Items.Add(i);
                                else {
                                    if (actuatorSubmenus.ContainsKey(component.type.subtype) == false) {
                                        rmi.Items.Add(i);
                                        actuatorDropDown.Items.Add(rmi);
                                        actuatorSubmenus.Add(component.type.subtype, rmi);
                                    }
                                    else {
                                        rmi = actuatorSubmenus[component.type.subtype];
                                        rmi.Items.Add(i);
                                    }
                                }
                                break;
                            case Asterics.ACS2.componentTypeDataTypes.processor:
                                if (component.type.subtype == null || component.type.subtype.Equals(""))
                                    processorDropDown.Items.Add(i);
                                else {
                                    if (processorSubmenus.ContainsKey(component.type.subtype) == false) {
                                        rmi.Items.Add(i);
                                        processorDropDown.Items.Add(rmi);
                                        processorSubmenus.Add(component.type.subtype, rmi);
                                    }
                                    else {
                                        rmi = processorSubmenus[component.type.subtype];
                                        rmi.Items.Add(i);
                                    }
                                }
                                break;
                            case Asterics.ACS2.componentTypeDataTypes.sensor:
                                if (component.type.subtype == null || component.type.subtype.Equals(""))
                                    sensorDropDown.Items.Add(i);
                                else {
                                    if (sensorSubmenus.ContainsKey(component.type.subtype) == false) {
                                        rmi.Items.Add(i);
                                        sensorDropDown.Items.Add(rmi);
                                        sensorSubmenus.Add(component.type.subtype, rmi);
                                    }
                                    else {
                                        rmi = sensorSubmenus[component.type.subtype];
                                        rmi.Items.Add(i);
                                    }
                                }
                                break;
                            case Asterics.ACS2.componentTypeDataTypes.special:
                                if (component.type.subtype == null || component.type.subtype.Equals(""))
                                    specialDropDown.Items.Add(i);
                                else {
                                    if (specialSubmenus.ContainsKey(component.type.subtype) == false) {
                                        rmi.Items.Add(i);
                                        specialDropDown.Items.Add(rmi);
                                        specialSubmenus.Add(component.type.subtype, rmi);
                                    }
                                    else {
                                        rmi = specialSubmenus[component.type.subtype];
                                        rmi.Items.Add(i);
                                    }
                                }
                                break;
                        }
                    }
                    // Sorting the lists alphabetically
                    sensorDropDown.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
                    actuatorDropDown.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
                    processorDropDown.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
                    specialDropDown.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
                    // sort Submenus
                    sortComponentSubmenu(actuatorSubmenus.Values.ToArray());
                    sortComponentSubmenu(sensorSubmenus.Values.ToArray());
                    sortComponentSubmenu(processorSubmenus.Values.ToArray());
                    sortComponentSubmenu(specialSubmenus.Values.ToArray());
                    //move others at the end of the submenus
                    moveOthersMenuItemBack(sensorDropDown);
                    moveOthersMenuItemBack(processorDropDown);
                    moveOthersMenuItemBack(actuatorDropDown);
                    moveOthersMenuItemBack(specialDropDown);
                    if (pathToBundleFile != null) {
                        MessageBox.Show(Properties.Resources.ReadBundleText, Properties.Resources.ReadBundleHeader, MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                } catch (Exception e) {
                    actuatorDropDown.Items.Clear();
                    processorDropDown.Items.Clear();
                    sensorDropDown.Items.Clear();
                    specialDropDown.Items.Clear();
                    groupDropDown.Items.Clear();
                    componentList.Clear();
                    if (pathToBundleFile == null) {
                        MessageBox.Show(Properties.Resources.ReadBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                        MessageBox.Show(Properties.Resources.ReadBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error); // called twice to be shown once. Some kind of initialisation problem
                        traceSource.TraceEvent(TraceEventType.Error, 3, e.Message);
                        //Application.Current.Shutdown();
                        //Environment.Exit(0);
                    } else {
                        MessageBox.Show(Properties.Resources.ReadDownloadedBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                        componentList.Clear();
                        LoadBundle(null);
                    }
                }

                // loading the presaved groups
                string errorStr = "unknown";
                try {
                    string[] filesInGroupsFolder = null;
                    if (ini.IniReadValue("Options", "useAppDataFolder").Equals("true")) {
                        if (Directory.Exists(Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)+"\\AsTeRICS\\ACS\\groups\\")) {
                            filesInGroupsFolder = Directory.GetFiles(Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)+"\\AsTeRICS\\ACS\\groups\\", "*.agr");
                        }
                    } else {
                        if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\groups\\")) {
                            filesInGroupsFolder = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "\\groups\\", "*.agr");
                        }
                    }
                    if (filesInGroupsFolder != null) {
                        foreach (string filename in filesInGroupsFolder) {
                            errorStr = filename;
                            RibbonApplicationSplitMenuItem i = new RibbonApplicationSplitMenuItem();
                            string header = filename.Substring(filename.LastIndexOf('\\') + 1);
                            i.Header = header.Substring(0, header.LastIndexOf('.'));
                            i.Click += AddGroupFromRibbonMenu;
                            i.CommandParameter = filename;
                            groupDropDown.Items.Add(i);
                        }
                    }
                } catch (Exception e) {
                    MessageBox.Show(Properties.Resources.GroupingErrorReadingGroupFormat(errorStr), Properties.Resources.GroupingErrorReadingGroupHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                    traceSource.TraceEvent(TraceEventType.Error, 3, e.Message);
                }

            } else {
                actuatorDropDown.Items.Clear();
                processorDropDown.Items.Clear();
                sensorDropDown.Items.Clear();
                specialDropDown.Items.Clear();
                groupDropDown.Items.Clear();
                componentList.Clear();
                if (pathToBundleFile == null) {
                    MessageBox.Show(Properties.Resources.ReadBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                    MessageBox.Show(Properties.Resources.ReadBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error); // called twice to be shown once. Some kind of initialisation problem
                    traceSource.TraceEvent(TraceEventType.Error, 3, xmlError);
                    //Application.Current.Shutdown();
                    //Environment.Exit(0);
                } else {
                    MessageBox.Show(Properties.Resources.ReadDownloadedBundleErrorText, Properties.Resources.ReadBundleErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                    componentList.Clear();
                    LoadBundle(null);
                }

            }
        }
 /// <summary>
 /// sorts the submenu Items of the Menuitems alphabetically
 /// </summary>
 /// <param name="rsmis">Menuitem to sort</param>
 private void sortComponentSubmenu(RibbonSplitMenuItem[] rsmis)
 {
     foreach (RibbonSplitMenuItem rsmi in rsmis) {
         if (rsmi != null)
             rsmi.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
     }
 }