private void RibbonDropDownButton_ActiveItemList_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                StartLoadingUI();

                this.RibbonDropDownButton_ActiveItemList.Items.Clear();
                ActiveWindow aw = new ActiveWindow();
                foreach (InfoItem ii in aw.GetActiveItemList())
                {
                    MenuItem mi = new MenuItem();
                    if (ii.Title.Length > ACTIVEITEM_TITLE_LENGTH)
                    {
                        mi.Header = ii.Title.Substring(0, ACTIVEITEM_TITLE_LENGTH) + "...";
                    }
                    else
                    {
                        mi.Header = ii.Title;
                    }
                    switch (ii.Type)
                    {
                        case InfoItemType.Web:
                            mi.Icon = new Image
                            {
                                Source = new BitmapImage(new Uri(FileTypeHandler.GetIcon(ElementAssociationType.Web, ii.Uri), UriKind.Absolute))
                            };
                            break;
                        case InfoItemType.File:
                            mi.Icon = new Image
                            {
                                Source = new BitmapImage(new Uri(FileTypeHandler.GetIcon(ElementAssociationType.File, ii.Uri), UriKind.Absolute))
                            };
                            break;
                        case InfoItemType.Email:
                            mi.Icon = new Image
                            {
                                Source = new BitmapImage(new Uri(FileTypeHandler.GetIcon(ElementAssociationType.Email, ii.Uri), UriKind.Absolute))
                            };
                            break;
                    };
                    mi.Tag = ii;
                    mi.ToolTip = ii.Title;
                    mi.Click += new RoutedEventHandler(RibbonDropDownButton_ActiveItem_Click);
                    this.RibbonDropDownButton_ActiveItemList.Items.Add(mi);
                }

                if (this.RibbonDropDownButton_ActiveItemList.Items.Count == 0)
                {
                    MenuItem mi = new MenuItem();
                    mi.Header = "No active item found";
                    this.RibbonDropDownButton_ActiveItemList.Items.Add(mi);
                }

                EndLoadingUI();
            }
            catch (Exception ex)
            {
                this.RibbonDropDownButton_ActiveItemList.Items.Clear();
                //MessageBox.Show("RibbonDropDownButton_ActiveItemList_MouseEnter\n" + ex.Message);
            }
        }
        public void On_Activate()
        {
            ActiveWindow aw = new ActiveWindow();
            TextSelHandler textsel = new TextSelHandler(this);

            IntPtr hWnd = IntPtr.Zero;
            InfoItem activeItem = null;
            System.Windows.Clipboard.Clear();

            try
            {
                //support for synchronizing droplist with filesystem dynamically

                hWnd = aw.GetActiveWindowHandle();

                textsel.SendCtrlC(hWnd);

                //wait for the copy to be completed
                Thread.Sleep(100);

                activeItem = aw.GetActiveItem();

                newNoteText = System.Windows.Clipboard.GetText().Replace("\r\n", " ").Replace("\t", " ").Trim();
                if (newNoteText.Length > StartProcess.MAX_EXTRACTTEXT_LENGTH)
                    newNoteText = newNoteText.Substring(0, StartProcess.MAX_EXTRACTTEXT_LENGTH) + "...";

                if ((activeItem != null) && (activeItem.Uri!=null) && (activeItem.Uri.Length!=0))
                {
                    newInfoItem = activeItem;

                    if (newNoteText.Length != 0)
                        newNoteText = "\"" + newNoteText + "\" ";
                    else if(newInfoItem.Title!=null)
                        newNoteText = "\"" + newInfoItem.Title + "\" ";

                    string title;
                    if (newInfoItem.Title == null || newInfoItem.Title.Length == 0)
                    {
                        title = newNoteText.Replace("/", "").Replace("\\", "").Replace("*", "").Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "").Replace(":", "");
                        if (title.Length > StartProcess.MAX_EXTRACTNAME_LENGTH)
                            title = title.Substring(0, StartProcess.MAX_EXTRACTNAME_LENGTH);
                    }
                    else
                        title = newInfoItem.Title;

                    checkBox_URI.IsEnabled = true;
                    checkBox_URI.IsChecked = true;

                    if (title.Length > ACTIVEITEM_TITLE_LENGTH)
                        checkBox_URI.Content = "Include link to: " + "\"" + title.Substring(0, ACTIVEITEM_TITLE_LENGTH) + "..." + "\"";
                    else
                        checkBox_URI.Content = "Include link to: " + "\"" + title + "\"";
                }
                textBox_Note.Text = newNoteText;
            }
            catch (Exception)
            {
                ReInitilize();
            }

            updateComboBox_SaveLoc();
            int selectedIndex = 0;
            int i;
            for (i = 0; i < comboBox_SaveLoc.Items.Count; i++)
            {
                ComboBoxItem cbi_Item = (ComboBoxItem)comboBox_SaveLoc.Items[i];
                if (cbi_Item.Tag.ToString() == selectedItem.Tag.ToString() && cbi_Item.Content.ToString() == selectedItem.Content.ToString())
                    selectedIndex = i;
            }
            comboBox_SaveLoc.SelectedIndex = selectedIndex;

            this.Visibility = Visibility.Visible;
            this.Activate();
            textBox_Note.Focus();
            textBox_Note.Select(newNoteText.Length, 0);
            bNoteModified = false;
            this.Topmost = true;
            System.Windows.Clipboard.Clear();

            aw = null;
            textsel = null;
            hWnd = IntPtr.Zero;
            activeItem = null;
            GC.Collect();
        }
        private void ElementTextBox_ContextMenuItem_Link_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                StartLoadingUI();

                MenuItem mi_link = sender as MenuItem;
                while (mi_link.Items.Count > 2)
                {
                    mi_link.Items.RemoveAt(0);
                }

                int index = 0;

                ActiveWindow aw = new ActiveWindow();
                foreach (InfoItem ii in aw.GetActiveItemList())
                {
                    MenuItem mi = new MenuItem();
                    if (ii.Title.Length > ACTIVEITEM_TITLE_LENGTH)
                    {
                        mi.Header = ii.Title.Substring(0, ACTIVEITEM_TITLE_LENGTH) + "...";
                    }
                    else
                    {
                        mi.Header = ii.Title;
                    }
                    switch (ii.Type)
                    {
                        case InfoItemType.Web:
                            mi.Icon = new Image
                            {
                                Source = new BitmapImage(new Uri(FileTypeHandler.GetIcon(ElementAssociationType.Web, ii.Uri), UriKind.Absolute)),
                                Width = 18,
                                Height = 18,
                            };
                            break;
                        case InfoItemType.File:
                            mi.Icon = new Image
                            {
                                Source = new BitmapImage(new Uri(FileTypeHandler.GetIcon(ElementAssociationType.File, ii.Uri), UriKind.Absolute)),
                                Width = 18,
                                Height = 18,
                            };
                            break;
                        case InfoItemType.Email:
                            mi.Icon = new Image
                            {
                                Source = new BitmapImage(new Uri(FileTypeHandler.GetIcon(ElementAssociationType.Email, ii.Uri), UriKind.Absolute)),
                                Width = 18,
                                Height = 18,
                            };
                            break;
                    };
                    mi.Tag = ii;
                    mi.ToolTip = ii.Title;
                    mi.Click += new RoutedEventHandler(ElementTextBox_ContextMenuItem_ActiveItem_Click);

                    mi_link.Items.Insert(index++, mi);
                }

                EndLoadingUI();
            }
            catch (Exception ex)
            {
                MenuItem mi_link = sender as MenuItem;
                while (mi_link.Items.Count > 2)
                {
                    mi_link.Items.RemoveAt(0);
                }

                //MessageBox.Show("ElementTextBox_ContextMenuItem_Link_MouseEnter\n" + ex.Message);
            }
        }
        public void AddAssociation(Element element, string fileFullName, ElementAssociationType type, string text)
        {
            string noteText = String.Empty;
            string shortcutName = String.Empty;
            string folderPath = element.Path;
            string title = string.Empty;

            List<Element> emailAttachmentElementList = new List<Element>();

            if (text != null)
            {
                noteText = text;
                if (noteText.Length > StartProcess.MAX_EXTRACTTEXT_LENGTH)
                {
                    noteText = noteText.Substring(0, StartProcess.MAX_EXTRACTTEXT_LENGTH) + "...";
                }
                title = noteText.Replace("/", "").Replace("\\", "").Replace("*", "").Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "").Replace(":", "");
                if (title.Length > StartProcess.MAX_EXTRACTNAME_LENGTH)
                    title = title.Substring(0, StartProcess.MAX_EXTRACTNAME_LENGTH);
                while (title.EndsWith("."))
                    title.TrimEnd('.');
            }

            if (type == ElementAssociationType.File)
            {

            }
            else if (type == ElementAssociationType.FileShortcut)
            {
                if (text == null)
                {
                    noteText = noteText = System.IO.Path.GetFileNameWithoutExtension(fileFullName);
                    title = noteText;
                }

                string fs_fileName = title + System.IO.Path.GetExtension(fileFullName);
                shortcutName = ShortcutNameConverter.GenerateShortcutNameFromFileName(fs_fileName, folderPath);
            }
            else if (type == ElementAssociationType.FolderShortcut)
            {
                if (fileFullName.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
                {
                    fileFullName = fileFullName.Substring(0, fileFullName.Length - 1);
                }
                noteText = System.IO.Path.GetFileName(fileFullName);

                string fs_fileName = noteText;
                shortcutName = ShortcutNameConverter.GenerateShortcutNameFromFileName(fs_fileName, folderPath);
            }
            else if (type == ElementAssociationType.Web)
            {
                if (text == null)
                {
                    ActiveWindow activeWindow = new ActiveWindow();
                    title = activeWindow.GetActiveWindowText(activeWindow.GetActiveWindowHandle());
                    if(title.Contains(" - Windows Internet Explorer"))
                    {
                        // IE
                        int labelIndex1 = title.LastIndexOf(" - Windows Internet Explorer");
                        if (labelIndex1 != -1)
                        {
                            title = title.Remove(labelIndex1);
                            noteText = title;
                        }
                    }else if(title.Contains(" - Mozilla Firefox"))
                    {
                        // Firefox
                        int labelIndex2 = title.LastIndexOf(" - Mozilla Firefox");
                        if (labelIndex2 != -1)
                        {
                            title = title.Remove(labelIndex2);
                            noteText = title;
                        }
                    }else
                    {
                        noteText = fileFullName;
                        title = string.Empty;
                    }

                    if (noteText.Length > StartProcess.MAX_EXTRACTTEXT_LENGTH)
                    {
                        noteText = noteText.Substring(0, StartProcess.MAX_EXTRACTTEXT_LENGTH) + "...";
                    }
                }
                shortcutName = ShortcutNameConverter.GenerateShortcutNameFromWebTitle(title, folderPath);

            }
            else if (type == ElementAssociationType.Email)
            {
                Outlook.Application outlookApp = new Outlook.Application();
                Outlook.MailItem mailItem = null;
                if (outlookApp.ActiveExplorer().Selection.Count > 0)
                {
                    mailItem = outlookApp.ActiveExplorer().Selection[1] as Outlook.MailItem;

                    if (mailItem == null)
                        return;

                    if (mailItem != null)
                    {
                        Element associatedElement = element;
                        if (element.IsHeading && element.IsCollapsed)
                        {
                            associatedElement = element.ParentElement;
                        }

                        Regex byproduct = new Regex("att[0-9]+[.txt|.c]");

                        foreach (Outlook.Attachment attachment in mailItem.Attachments)
                        {
                            string attachmentFileName = attachment.FileName;

                            if (!byproduct.IsMatch(attachmentFileName.ToLower()))
                            {
                                string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(attachmentFileName);
                                string fileNameExt = System.IO.Path.GetExtension(attachmentFileName);
                                string copyPath = associatedElement.Path + attachmentFileName;
                                int index = 2;
                                while (FileNameChecker.Exist(copyPath))
                                {
                                    copyPath = associatedElement.Path + fileNameWithoutExt + " (" + index.ToString() + ")" + fileNameExt;
                                    index++;
                                }
                                attachment.SaveAsFile(copyPath);

                                Element attachmentElement = CreateNewElement(ElementType.Note, " --- " + fileNameWithoutExt);
                                attachmentElement.AssociationType = ElementAssociationType.File;
                                attachmentElement.AssociationURI = System.IO.Path.GetFileName(copyPath);
                                attachmentElement.TailImageSource = FileTypeHandler.GetIcon((ElementAssociationType)attachmentElement.AssociationType, copyPath);
                                emailAttachmentElementList.Add(attachmentElement);
                            }
                        }
                    }
                }
                if ((fileFullName == null) && (mailItem != null))
                {
                    noteText = mailItem.Subject;
                    if (noteText.Length > StartProcess.MAX_EXTRACTTEXT_LENGTH)
                    {
                        noteText = noteText.Substring(0, StartProcess.MAX_EXTRACTTEXT_LENGTH) + "...";
                    }
                    fileFullName = mailItem.EntryID;
                    shortcutName = ShortcutNameConverter.GenerateShortcutNameFromEmailSubject(mailItem.Subject, folderPath);
                }
                else
                {
                    shortcutName = ShortcutNameConverter.GenerateShortcutNameFromEmailSubject(title, folderPath);
                }
            }

            switch (element.Type)
            {
                case ElementType.Heading:
                    if (element.IsExpanded)
                    {
                        Element firstElement = CreateNewElement(ElementType.Note, noteText);
                        InsertElement(firstElement, element, 0);
                        AssignAssociationInfo(firstElement, fileFullName, shortcutName, type);
                        currentElement = firstElement;
                    }
                    else
                    {
                        Element newElement = CreateNewElement(ElementType.Note, noteText);
                        InsertElement(newElement, element.ParentElement, element.Position + 1);
                        AssignAssociationInfo(newElement, fileFullName, shortcutName, type);
                        currentElement = newElement;
                    }
                    break;
                case ElementType.Note:
                    if (element.HasAssociation == false)
                    {
                        if (element.NoteText.Trim() == String.Empty)
                        {
                            element.NoteText = noteText;
                        }
                        AssignAssociationInfo(element, fileFullName, shortcutName, type);
                        currentElement = element;
                    }
                    else
                    {
                        Element newElement = CreateNewElement(ElementType.Note, noteText);
                        InsertElement(newElement, element.ParentElement, element.Position + 1);
                        AssignAssociationInfo(newElement, fileFullName, shortcutName, type);
                        currentElement = newElement;
                    }
                    break;
            };

            int curr_index = currentElement.Position;
            foreach (Element emailAttachmentElement in emailAttachmentElementList)
            {
                InsertElement(emailAttachmentElement, currentElement.ParentElement, curr_index);
                curr_index++;
            }
        }