Beispiel #1
0
        public MainForm()
        {
            InitializeComponent();

            try {
                config = XMLSerializer.Deserialize<Config>(Config.SettingPath, new Config());
            }
            catch (Exception) {
                //throw;
                config = null;
            }
            if (config == null) {
                config = new Config();
            }

            config.htmlPath = Path.GetFullPath(@"..\..\html\wiki_parser.html");
            config.ScriptDirPath = Path.GetFullPath(@"..\..\scripts");
            config.MigemoDictPath = Path.GetFullPath(@"..\..\migemo\dict\migemo-dict");
            config.DataDirPath = Path.GetFullPath(@".\data");
            config.SnippetListPath = Path.GetFullPath(@".\SnippetList.xml");
            config.LoadSnippetList();
            this.WindowState = config.WindowState;
            if (config.WindowState != FormWindowState.Maximized) {
                this.Size = config.WindowSize;
            }
            this.Location = config.WindowPos;
            splitContainer1.SplitterDistance = config.CategoryListViewW;
            ViewEditorSplitContainer.SplitterDistance = config.BrowserH;
            ListViewSplitContainer.Orientation = config.TabListView_BrowserOri;
            ListViewSplitContainer.SplitterDistance = config.ListViewSize;

            externalEditor = new ExternalEditor(Path.GetFullPath(@"."));
            externalEditor.UpDateEvent += (s, e) => {

                //var te= this.Invoke(new TextDelegate(ReadText), e.ID.ToString()+ ".txt");
                this.Invoke(new FocusDelegate(category.UpDateText), e.ID, e.Text);
                //category.UpDateText(e.ID, e.Text);

                //var i = category.GetItem(e.ID);
                //i.Text = e.Text;
                //this.Invoke(new FocusDelegate(this.editContent), i);
                //this.Invoke(InvokeScript);
            };

            initKeyMap();
            initEditor();
            initSearch();
            initBrowser();

            sm.init();
            sm.ScriptDir = config.ScriptDirPath;

            reqparam.Add("method", string.Empty);
            reqparam.Add("url", string.Empty);
            reqparam.Add("id", string.Empty);
            reqparam.Add("data", string.Empty);

            httpServer = new HttpServer(config.Port);
            httpServer.RequestEvent += (sender, e) => {
                var url = e.Request.RawUrl;
                if (url.IndexOf('?') >= 0) {
                    url = e.Request.RawUrl.Split('?')[0];
                }
                var httpmathod = e.Request.HttpMethod;
                e.Response = "accept";

                var m = regID.Match(url);
                if (m.Success) {
                    var idlist = new List<int>();
                    var idstr = m.Groups[1].Value;
                    if (idstr.IndexOf(',') > 0) {
                        var ids = idstr.Split(',');
                        foreach (var id in ids) {
                            idlist.Add(int.Parse(id));
                        }
                    }
                    else {
                        var id = int.Parse(idstr);
                        idlist.Add(id);
                    }
                    //var manager = category.getManger(getSelectedCategory());
                    //var items = manager.GetItem(idlist);
                    var items = category.GetItem(idlist);
                    var res = string.Empty;
                    switch (httpmathod) {
                        case "DELETE":
                            reqparam["method"] = "delete";
                            reqparam["id"] = m.Groups[1].Value;
                            serveBW.ReportProgress(1, reqparam);
                            break;
                        case "GET":
                            List<Data> list = items;// new List<Data>() { item };
                            res = JsonSerializer.Serialize(list);
                            e.Response = res;
                            break;
                        default:
                            break;
                    }

                    return;
                }

                //m = regEdit.Match(url);
                //if (m.Success) {
                //    var id = m.Groups[1].Value;

                //    reqparam["method"] = "edit";
                //    //reqparam["url"] = url;
                //    reqparam["id"] = id;
                //    serveBW.ReportProgress(1, reqparam);
                //    return;
                //}
                //m = regMove.Match(url);
                //if (m.Success) {
                //    var id = m.Groups[1].Value;

                //    reqparam["method"] = "move";
                //    //reqparam["url"] = url;
                //    reqparam["id"] = id;
                //    serveBW.ReportProgress(1, reqparam);
                //    return;
                //}
                //m = regComeFrom.Match(url);
                //if (m.Success) {
                //    reqparam["method"] = "comefrom";
                //    var _RequestBody = new StreamReader(e.Request.InputStream).ReadToEnd();
                //    reqparam["data"] = _RequestBody;
                //    serveBW.ReportProgress(1, reqparam);
                //    return;
                //}
                m = regIDMethod.Match(url);
                if (m.Success) {
                    var id = m.Groups[1].Value;
                    var method = m.Groups[2].Value;
                    reqparam["id"] = id;
                    reqparam["method"] = method;
                    if (httpmathod == "POST") {
                        var _RequestBody = new StreamReader(e.Request.InputStream).ReadToEnd();
                        reqparam["data"] = _RequestBody;
                    }
                    serveBW.ReportProgress(1, reqparam);
                    return;
                }

                //m = regExe.Match(url);
                //if (m.Success) {
                //    var _RequestBody = new StreamReader(e.Request.InputStream).ReadToEnd();
                //    reqparam["method"] = "exe";
                //    reqparam["data"] = _RequestBody;
                //    serveBW.ReportProgress(1, reqparam);
                //    return;
                //}
                //m = regGoto.Match(url);
                //if (m.Success) {
                //    reqparam["method"] = "goto";
                //    var _RequestBody = new StreamReader(e.Request.InputStream).ReadToEnd();
                //    reqparam["data"] = _RequestBody;
                //    serveBW.ReportProgress(1, reqparam);
                //    return;
                //}
                //m = regScript.Match(url);
                //if (m.Success) {
                //    var _RequestBody = new StreamReader(e.Request.InputStream).ReadToEnd();
                //    reqparam["method"] = "script";
                //    reqparam["data"] = _RequestBody;
                //    serveBW.ReportProgress(1, reqparam);
                //    return;
                //}
                m = regMethod.Match(url);
                if (m.Success) {
                    var method = m.Groups[1].Value;
                    reqparam["method"] = method;
                    if (httpmathod == "POST") {
                        var _RequestBody = new StreamReader(e.Request.InputStream).ReadToEnd();
                        reqparam["data"] = _RequestBody;
                    }
                    serveBW.ReportProgress(1, reqparam);
                    return;
                }
            };
            serveBW = new BackgroundWorker();
            serveBW.WorkerReportsProgress = true;
            serveBW.WorkerSupportsCancellation = true;
            serveBW.ProgressChanged += (sender, e) => {

                if (serveBW.CancellationPending) {
                    httpServer.stop();
                    return;
                }
                var param = e.UserState as Dictionary<string, string>;
                switch (param["method"]) {
                    case "edit": {
                            var id = int.Parse(param["id"]);
                            this.EditItem(id, true);
                        }
                        break;
                    case "editor": {
                            var id = int.Parse(param["id"]);
                            //this.EditItem(id, true);
                        var item = category.GetItem(id);
                        if (item != null) {
                            //externalEditor.stopFw();
                            //var filepath = id.ToString() + ".txt";
                            //File.WriteAllText(filepath, item.Text, Encoding.UTF8);
                            //externalEditor.Process(@"C:\Program Files\sakura\sakura.exe", filepath);
                            //externalEditor.startFw();
                            externalEditor.write(id, item.Text);
                        }
                        }
                        break;
                    case "delete": {
                            var id = int.Parse(param["id"]);
                            this.DeleteItem(id);
                        }
                        break;
                    case "exe":
                        var args = param["data"];
                        sm.Run("test.js", args);
                        break;
                    case "script":
                        var script = param["data"];
                        sm.Eval(script);
                        break;
                    case "move": {
                            var id = int.Parse(param["id"]);
                            this.Moves(id);
                        }
                        break;
                    case "select": {
                            var id = int.Parse(param["id"]);
                            var lv = GetSelctedTabListViewControl();
                            var index = lv.DataItems.FindIndex(n => {
                                return n.ID == id;
                            });
                            if (index >= 0 && index<lv.Items.Count) {
                                lv.Items[index].Selected = true;
                                lv.Items[index].EnsureVisible();
                            }
                        }
                        break;
                    case "goto": {
                            var word = param["data"];
                            var s = CreateSearchObj(word, SearchMode.Text);
                            //ItemTabControl.SelectedTab = CreateListViewTabPage(word, s);
                            var seltabc = getTabControl(getSelectedCategory());
                            seltabc.SelectedTab = CreateListViewTabPage(getSelectedCategory(), seltabc, s);
                        }
                        break;
                    case "comefrom": {

                        }
                        break;
                    default:
                        break;
                }
            };

            serveBW.DoWork += (sender, e) => {
                try {
                    httpServer.start();
                }
                //catch (System.Net.HttpListenerException ex) {
                catch (Exception ex) {
                    throw;
                }
            };
            serveBW.RunWorkerCompleted += (sender, e) => {
                if (e.Error is System.Net.HttpListenerException) {
                    var ret = MessageBox.Show("port", "ERROR", MessageBoxButtons.OKCancel);
                    this.Option();
                }

            };
            serveBW.RunWorkerAsync();

            this.Load += (sender1, e1) => {

                initPage();

            };
            this.FormClosing += (sender, e) => {

                //if (manager.IsDirty) {
                //    manager.Save();
                //}
                category.Save();
                if (migemo != null) {
                    migemo.Dispose();
                }
                serveBW.CancelAsync();
                if (serveBW.IsBusy) {
                    serveBW.ReportProgress(0);
                }
                //httpServer.stop();

                config.WindowState = this.WindowState;
                config.WindowSize = this.Size;
                config.WindowPos = this.Location;
                config.CategoryListViewW = splitContainer1.SplitterDistance;
                config.BrowserH = ViewEditorSplitContainer.SplitterDistance;
                config.TabListView_BrowserOri = ListViewSplitContainer.Orientation;
                config.ListViewSize = ListViewSplitContainer.SplitterDistance;

                config.EdiorWrap = EditorWrapToolStripButton.Checked;

                config.SaveSnippetList();

                var TabList = new Dictionary<string, List<KeyValuePair<string, SearchMode>>>();
                for (int i = 0; i < CategoryListView.Items.Count; i++) {
                    var list = new List<KeyValuePair<string, SearchMode>>();
                    var item = CategoryListView.Items[i];
                    var tabs = ((TabControl)TabPanel.Controls[item.Name]).TabPages;
                    for (int j = 0; j < tabs.Count; j++) {
                        var listview = GetTabListViewControl(tabs[j]);
                        var s = listview.search;
                        if (s.Mode != SearchMode.All) {
                            var mode = s.Mode;
                            var pt = s.Pattern;
                            list.Add(new KeyValuePair<string, SearchMode>(pt, mode));
                        }
                    }
                    TabList.Add(item.Name, list);
                }

                config.TabListJson = JsonSerializer.Serialize(TabList);
                XMLSerializer.Serialize<Config>(Config.SettingPath, config);
            };

            _editor.TextChanged += (sender, e) => {

                if (dirty) {
                    //var manager = category.getManger(getSelectedCategory());
                    //var item = manager.EditingData;
                    //var item = category.GetItem(category.EditingID);
                    //if (item != null) {
                        //item.Text = _editor.Text;
                        //manager.UpDate(item);
                        //category.UpDate(item);
                        category.UpDateText(category.EditingID, _editor.Text);
                    //}
                }
                dirty = true;
            };

            NewItemToolStripButton.Click += (sender, e) => {
                CreateItem();
            };

            NewFileToolStripButton.Click += (sender, e) => {
                CreateNewFile("new");
            };

            category = new Category();
            category.DataDir = config.DataDirPath;
            category.eventHandler = (sender, e) => {
                switch (e.type) {
                    case ChangeType.Create: {
                            //if (Tabs[e.Name] == GetCurrentTabControl()) {
                            var name = category.getCategoryName(e.FromCategoryID);
                            if (TabPanel.Controls[name] == GetCurrentTabControl()) {
                                //ItemTabControl.SelectedTab = AllPage;
                                GetCurrentTabControl().SelectedIndex = 0;
                                var listview = GetSelctedTabListViewControl();// GetTabListViewControl(AllPage);
                                var iindex = listview.AddItem(e.Item);
                                if (iindex == (listview.DataItems.Count - 1)) {
                                    reBuild(e.Item);
                                }
                                else {
                                    reBuild(listview.DataItems[iindex + 1].ID, e.Item);
                                }
                                this.EditItem(e.Item.ID, true);
                            }
                            else {
                                //var tabc = Tabs[e.Name];

                                //var listview = GetTabListViewControl(Tabs[e.Name].TabPages[0]);
                                var listview = GetTabListViewControl(((TabControl)TabPanel.Controls[name]).TabPages[0]);
                                listview.AddItem(e.Item);
                            }
                        }
                        break;
                    case ChangeType.UpDateText: {
                            //if (Tabs[e.Name] == GetCurrentTabControl()) {
                            var name = category.getCategoryName(e.FromCategoryID);
                            if (TabPanel.Controls[name] == GetCurrentTabControl()) {
                                editContent(e.Item);
                                //var cf = InvokeScript("js_getComeFrom");
                                //var list = JsonSerializer.Deserialize<List<string>>(cf.ToString());
                                //config.ComeFormWords.Union(list);
                            }
                        }
                        break;
                    case ChangeType.UpDateCreationTime: {
                            var name = category.getCategoryName(e.FromCategoryID);
                            var tabc = getTabControl(name);
                            var s = new DateTimeComparer();
                            for (int i = 0; i < tabc.TabPages.Count; i++) {
                                var lv = GetTabListViewControl(tabc.TabPages[i]);
                                if (lv.DataItems.IndexOf(e.Item) >= 0) {
                                    lv.DataItems.Sort(s);
                                    break;
                                }
                            }
                        }
                        break;
                    case ChangeType.UpDateCategory: {
                            var form = e.FromCategoryID;
                            var to = e.ToCategoryID;

                            //if (Tabs[e.Name] == GetCurrentTabControl()) {
                            var fromname = category.getCategoryName(e.FromCategoryID);
                            var fromtabc = getTabControl(fromname);
                            for (int i = 0; i < fromtabc.TabPages.Count; i++) {
                                var lv = GetTabListViewControl(fromtabc.TabPages[i]);
                                if (lv.DataItems.IndexOf(e.Item) >= 0) {
                                    lv.DeleteItem(e.Item);
                                    break;
                                }
                            }

                            var toname = category.getCategoryName(e.ToCategoryID);
                            var totabc = getTabControl(toname);
                            for (int i = 0; i < totabc.TabPages.Count; i++) {
                                var lv = GetTabListViewControl(totabc.TabPages[i]);
                                lv.AddItem(e.Item);
                            }
                        }
                        break;
                    case ChangeType.Delete:
                        //foreach (var item in Tabs.Values) {
                        //    for (int i = 0; i < item.TabPages.Count; i++) {
                        //        var listview = GetTabListViewControl(item.TabPages[i]);
                        //        listview.DeleteItem(e.Item);
                        //    }
                        //}
                        ////for (int i = 0; i < ItemTabControl.TabPages.Count; i++) {
                        ////    var listview = GetTabListViewControl(ItemTabControl.TabPages[i]);
                        ////    listview.DeleteItem(e.Item);
                        ////}
                        //if (Tabs[e.Name] == GetCurrentTabControl()) {
                        //    InvokeScript("js_Remove", e.Item.ID.ToString());
                        //}

                        {
                            for (int i = 0; i < CategoryListView.Items.Count; i++) {
                                var item = CategoryListView.Items[i];
                                var tabpages = ((TabControl)TabPanel.Controls[item.Name]).TabPages;
                                for (int j = 0; j < tabpages.Count; j++) {
                                    var listview = GetTabListViewControl(tabpages[j]);
                                    listview.DeleteItem(e.Item);
                                }
                            }
                            var name = category.getCategoryName(e.FromCategoryID);
                            if (name == GetCurrentTabControl().Name) {
                                InvokeScript("js_Remove", e.Item.ID.ToString());
                            }
                        }
                        break;
                    case ChangeType.Clear:
                        InvokeScript("js_ClearAll");
                        break;
                    default:
                        break;
                }
            };
            category.Load();

            //if (config.Categorys.Count == 0) {
            //    config.Categorys.Add("new");
            //}
            //if (!config.Categorys.Contains("Trust")) {
            //    config.Categorys.Add("Trust");
            //}
            //category.Load(config.Categorys);

            {
                var TabList = JsonSerializer.Deserialize<Dictionary<string, List<KeyValuePair<string, SearchMode>>>>(config.TabListJson);
                //foreach (var c in config.Categorys) {
                foreach (var c in category.categorylist) {
                    //CategoryListView.Items.Add(item);
                    var item = new ListViewItem(c.Name, c.Name == "Trust" ? 1 : 0);
                    item.Name = c.Name;
                    CategoryListView.Items.Add(item);

                    if (TabList != null) {
                        var tabc = getTabControl(c.Name);
                        if (TabList.ContainsKey(c.Name)) {
                            var list = TabList[c.Name];
                            foreach (var l in list) {
                                var sobj = CreateSearchObj(l.Key, l.Value);
                                var p = CreateListViewTabPage(c.Name, tabc, sobj);
                                tabc.TabPages.Add(p);
                                //t.BringToFront();
                                //t.SelectedTab = p;
                            }
                        }
                    }
                }

            }
            //reBuild(newlistview.DataItems);
            //webBrowser1.ScriptErrorsSuppressed = true;
            //webBrowser1.ScrollBarsEnabled = true;
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.IsWebBrowserContextMenuEnabled = false;
            //var p = config.htmlPath;// Path.GetFullPath(@"..\..\html\wiki_parser.html");
            webBrowser1.Navigate(config.htmlPath);
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            //webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
            webBrowser1.StatusTextChanged += (sender, e) => {
                BrowserToolStripStatusLabel.Text = webBrowser1.StatusText;
            };
            ScriptErrorToolStripStatusLabel.Click+=(s,e)=>{

            };

            webBrowser1.Navigated += (s, e) => {
                webBrowser1.Document.Window.Error += (ss, se) => {
                    //se.
                    se.Handled = true;

                    ScriptErrorToolStripStatusLabel.Text = "ScriptError";
                };
            };

            this.KeyDown += (sender, e) => {
                var ee = e.KeyValue;
            };

            PrevPageToolStripButton.Click += (sender, e) => {

                var l = GetSelctedTabListViewControl();
                l.Page--;

                var elist = getCurrentPageDatas(l, config.ShowNum);
                InvokeScript("js_ClearAll");
                reBuild(elist);

                if ((l.Page - 1) * config.ShowNum <= 0) {
                    PrevPageToolStripButton.Enabled = false;
                }
                NextPageToolStripButton.Enabled = true;
            };
            NextPageToolStripButton.Click += (sender, e) => {

                var l = GetSelctedTabListViewControl();
                l.Page++;

                var elist = getCurrentPageDatas(l, config.ShowNum);
                InvokeScript("js_ClearAll");
                reBuild(elist);

                if ((l.Page + 1) * config.ShowNum >= l.DataItems.Count) {
                    NextPageToolStripButton.Enabled = false;
                }
                PrevPageToolStripButton.Enabled = true;
            };
            //initPage();

            ReloadToolStripButton.Click += (sender, e) => {
                //InvokeScript("jsview.jsmsg", new string[] { "jsview.jsmsg test" });
                //manager.Insert(new Data { ID = manager.GetNewID(), Text = "after", CreationTime = new DateTime(DateTime.Now.Ticks * 2) });
                webBrowser1.Navigate(config.htmlPath);
                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            };

            OptionToolStripButton.Click += (sender, e) => {
                Option();
            };

            HorizontalToolStripButton.Click += (sender, e) => {
                ListViewSplitContainer.Orientation = Orientation.Horizontal;
            };

            VerticalToolStripButton.Click += (sender, e) => {
                ListViewSplitContainer.Orientation = Orientation.Vertical;
            };

            CategoryContextMenuStrip.Opened += (s, e) => {

                if (CategoryListView.SelectedItems.Count > 0) {
                    //CategoryContextMenuStrip.Enabled = true;
                    var item = CategoryListView.SelectedItems[0];
                    //CategoryNewFileToolStripMenuItem.Enabled = item.Name != Category.Trust;
                    CategoryDeleteToolStripMenuItem.Enabled = item.Name != Category.Trust;
                    CategoryEmptyToolStripMenuItem.Enabled = item.Name == Category.Trust;
                } else {
                    //CategoryContextMenuStrip.Enabled = false;
                    CategoryDeleteToolStripMenuItem.Enabled = false;
                    CategoryEmptyToolStripMenuItem.Enabled = false;
                }
                //CategoryNewFileToolStripMenuItem.Enabled = true;
            };

            CategoryNewFileToolStripMenuItem.Click += (sender, e) => {
                CreateNewFile("new");
            };
            CategoryDeleteToolStripMenuItem.Click += (sender, e) => {
                if (CategoryListView.SelectedItems.Count > 0) {
                    var name = CategoryListView.SelectedItems[0].Name;
                    DeleteFile(name);
                }
            };
            CategoryEmptyToolStripMenuItem.Click += (sender, e) => {
                this.ClearItem();
            };

            CategoryListView.Items[0].Selected = true;
            CategoryListView.ItemSelectionChanged += (s, e) => {
                if (e.IsSelected) {
                    var item = e.Item;
                    categoryname = item.Text;
                    var tabc = getTabControl(item.Text);
                    tabc.BringToFront();

                    if (tabc.SelectedTab != null) {
                        var listview = GetTabListViewControl(tabc.SelectedTab);
                        var datas = getCurrentPageDatas(listview, config.ShowNum);
                        InvokeScript("js_ClearAll");
                        reBuild(datas);

                        if (datas.Count > 0 && listview.SelectedIndices.Count == 0) {
                            listview.Items[0].Selected = true;
                        }
                        else if (datas.Count > 0) {
                            var index  = listview.SelectedIndices[0];
                            this.EditItem(listview.DataItems[index].ID, false);
                        }

                        initPage();
                    }
                }
            };

            CategoryListView.KeyDown += (s, e) => {
                if (_CategoryLsitViewKeyMap.ContainsKey(e.KeyData)) {
                    _CategoryLsitViewKeyMap[e.KeyData](this);
                    e.Handled = true;
                    e.SuppressKeyPress = true;
                }
            };

            CategoryListView.ListViewItemSorter = new ListViewIndexComparer();
            CategoryListView.AllowDrop = true;

            CategoryListView.MouseMove += (s, e) => {
                if (e.Button == MouseButtons.Left) {
                    if (CategoryListView.SelectedItems.Count > 0) {
                        var item = CategoryListView.SelectedItems[0];
                        CategoryListView.DoDragDrop(item.Name, DragDropEffects.Move);
                    }
                }
            };

            CategoryListView.DragEnter += (s, e) => {
                if (e.Data.GetDataPresent(typeof(Data)) || e.Data.GetDataPresent(typeof(string)))
                    e.Effect = DragDropEffects.Move;
                else
                    e.Effect = DragDropEffects.None;

            };
            CategoryListView.DragOver += (s, e) => {
                var point = CategoryListView.PointToClient(new Point(e.X, e.Y));
                var item = CategoryListView.GetItemAt(point.X, point.Y);
                if (e.Data.GetDataPresent(typeof(Data)) && (item != null && item.Name != getSelectedCategory()))
                    e.Effect = DragDropEffects.Move;
                else if(e.Data.GetDataPresent(typeof(string)) && (item.Name != getSelectedCategory()))
                    e.Effect = DragDropEffects.Move;
                else
                    e.Effect = DragDropEffects.None;

            };
            CategoryListView.DragDrop += (s, e) => {
                if (e.Data.GetDataPresent(typeof(Data))) {
                    var data = (Data)e.Data.GetData(typeof(Data));
                    var point = CategoryListView.PointToClient(new Point(e.X, e.Y));
                    var item = CategoryListView.GetItemAt(point.X, point.Y);
                    if (item != null && item.Name != getSelectedCategory()) {
                        DeleteItem(data.ID);
                        //category.getManger(item.Name).Insert(data);
                    }
                }
                else if (e.Data.GetDataPresent(typeof(string))) {
                    var name = (string)e.Data.GetData(typeof(string));
                    var point = CategoryListView.PointToClient(new Point(e.X, e.Y));
                    var item = CategoryListView.GetItemAt(point.X, point.Y);

                    var index = CategoryListView.Items.IndexOfKey(name);
                    if (index >= 0) {
                        var key = item.Name;
                        var removeitem = CategoryListView.Items[index];

                        var i = CategoryListView.Items.IndexOfKey(item.Name);
                        if (i == 0 || (i < CategoryListView.Items.Count - 1)) {
                            CategoryListView.Items.Remove(removeitem);
                            var ni = CategoryListView.Items.IndexOfKey(item.Name);
                            var ins = i == 0 ? 0 : ni + 1;
                            CategoryListView.Items.Insert(ins, removeitem);
                            //config.Categorys.Remove(name);
                            //config.Categorys.Insert(ins, name);
                            //CategoryListView.Sort();
                        }

                        removeitem.Selected = true;
                    }
                }
            };

            CategoryListView.AfterLabelEdit += (s, e) => {
                //e.CancelEdit = true;
                if (File.Exists(Path.Combine(config.DataDirPath, e.Label + ".xml"))) {
                    e.CancelEdit = true;
                }
                else {
                    var item = CategoryListView.Items[e.Item];
                    if (category.RenameFile(item.Name, e.Label)) {
                        item.Text = e.Label;
                        item.Name = e.Label;
                    }
                }
            };

            TabListViewEditItemToolStripMenuItem.Click += (s, e) => {
                var lv = GetSelctedTabListViewControl();
                if (lv.SelectedIndices.Count > 0) {
                    var index = lv.SelectedIndices[0];
                    EditItem(lv.DataItems[index].ID, true);
                }
            };
            TabListViewNewItemToolStripMenuItem.Click += (s, e) => {
                this.CreateItem();
            };
            TabListViewDeleteItemToolStripMenuItem.Click += (s, e) => {
                this.DeleteItem();
            };
            TabListViewEditDateTimeToolStripMenuItem.Click += (s, e) => {
                this.EditDateTime();
            };
        }