private void 更改文件名ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                YmlFile      file = (YmlFile)item.Tag;

                string    oldName = file.localName;
                string    msg     = "请输入文件的新名称";
                InputForm form    = new InputForm(msg, oldName, new InputForm.FormResult((newName) =>
                {
                    if (oldName != newName)
                    {
                        file.localName = newName;
                        string dirs    = file.remotePath;
                        string path1   = dirs + Utils.getLinuxName(oldName);
                        string path2   = dirs + Utils.getLinuxName(newName);
                        monitorForm.RunShell(string.Format("mv {0} {1}", path1, path2), false, true);

                        file.remoteName = file.localName;
                        item.Text       = file.localName;
                    }
                }));
                form.ShowDialog(this);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                YmlFile      file = (YmlFile)item.Tag;

                string content = "";
                if (tabControl1.SelectedIndex == 0)
                {
                    List <string> list = getTreeNodeContent(_treeView.Items);
                    foreach (string line in list)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            content += line + "\n";
                        }
                    }
                }
                else
                {
                    content = ymlEditor.Text;
                }

                bool result = Validate(item, content);
                if (result)
                {
                    resContent = content;
                    YSTools.YSFile.writeFileByString(file.localPath + file.localName, content);
                    btn_save.Enabled = false;
                }
            }
        }
        private void CentralServerConfigForm_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                String[]     files     = e.Data.GetData(DataFormats.FileDrop, false) as String[];
                YmlFile      yml       = null;
                FileInfo     file      = null;
                FileInfo     firstFile = null;
                ListViewItem item      = null;
                // Copy file from external application
                foreach (string srcfile in files)
                {
                    try
                    {
                        file           = new FileInfo(srcfile);
                        yml            = new YmlFile();
                        yml.correct    = true;
                        yml.localName  = file.Name;
                        yml.localPath  = file.DirectoryName.Replace("\\", "/") + "/";
                        yml.remoteName = "";
                        yml.remotePath = "";
                        yml.status     = YmlFileState.NoModif;

                        if (!existFile(yml))
                        {
                            item            = new ListViewItem();
                            item.Text       = file.Name;
                            item.Tag        = yml;
                            item.ImageIndex = 1;

                            if (null == firstFile)
                            {
                                firstFile = file;
                            }

                            listView1.Items.Add(item);
                        }
                    }
                    catch { }
                }

                if (null != firstFile)
                {
                    listView1.Items[0].Selected = true;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, " Error ",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private bool existFile(YmlFile yml)
        {
            YmlFile file = null;

            foreach (ListViewItem item in listView1.Items)
            {
                file = (YmlFile)item.Tag;
                if ((file.localPath + file.localName) == (yml.localPath + yml.localName))
                {
                    return(true);
                }
            }
            return(false);
        }
        private void  载到本地ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                YmlFile      file = (YmlFile)item.Tag;

                string remotePath = file.remotePath + file.remoteName;
                string localPath  = file.localPath + file.localName;

                downloadFile(remotePath, localPath);

                listView1_SelectedIndexChanged(null, null);
            }
        }
        private void ymlEditor_KeyUp(object sender, KeyEventArgs e)
        {
            if (resContent != ymlEditor.Text)
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    ListViewItem item = listView1.SelectedItems[0];
                    YmlFile      file = (YmlFile)item.Tag;
                    file.status     = YmlFileState.Modif;
                    item.ImageIndex = 2;

                    btn_save.Enabled = true;
                }
            }
            Ranks();
        }
        private void  除文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                YmlFile      file = (YmlFile)item.Tag;

                DialogResult dr = MessageBox.Show(this, "您确定要删除此文件吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    monitorForm.RunShell(string.Format("rm -rf {0}", file.remotePath + file.remoteName), false, true);

                    listView1.Items.Remove(item);
                }
            }
        }
        private void   到服务器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                YmlFile      file = (YmlFile)item.Tag;

                string remotePath = file.remotePath + file.remoteName;
                string localPath  = file.localPath + file.localName;

                monitorForm.getSftp().put(localPath, remotePath, ChannelSftp.OVERWRITE);

                item.ImageIndex = 1;
                file.status     = YmlFileState.NoModif;
            }
        }
        private bool Validate(ListViewItem item, string content)
        {
            bool     result = true;
            YmlFile  file   = (YmlFile)item.Tag;
            YmlError error  = YmlFormatUtil.ValidateYml(content);

            if (error != null)
            {
                try
                {
                    if (tabControl1.SelectedIndex == 1)
                    {
                        ymlEditor.SelectionStart = error.index - 1;
                        ymlEditor.ScrollToCaret();
                        ymlEditor.Select(error.index, 0);
                        ymlEditor.Focus();
                    }
                }
                catch { }

                MessageBox.Show(this, error.msg);
                file.correct    = false;
                item.ImageIndex = 3;

                result = false;
            }
            else
            {
                if (file.status == YmlFileState.NoModif)
                {
                    item.ImageIndex = 1;
                }
                else if (file.status == YmlFileState.Modif)
                {
                    item.ImageIndex = 2;
                }
                else if (file.status == YmlFileState.NoAsync)
                {
                    item.ImageIndex = 0;
                }
            }
            return(result);
        }
示例#10
0
        public void LoadRemoteYmls()
        {
            btn_reload.Enabled = false;
            ArrayList fileList = monitorForm.getDirFiles(remoteCfgPath);

            if (null != fileList)
            {
                if (fileList.Count > 0)
                {
                    label1.Visible = false;

                    listView1.Items.Clear();
                    ListViewItem        item = null;
                    ChannelSftp.LsEntry file = null;
                    YmlFile             yml = null;
                    string remotePath = "", localPath = "";
                    for (int i = 0; i < fileList.Count; i++)
                    {
                        object obj = fileList[i];
                        if (obj is ChannelSftp.LsEntry)
                        {
                            file = (ChannelSftp.LsEntry)obj;
                            try
                            {
                                yml            = new YmlFile();
                                yml.correct    = true;
                                yml.localName  = file.getFilename();
                                yml.localPath  = cfgDir + "/";
                                yml.remoteName = file.getFilename();
                                yml.remotePath = remoteCfgPath + "/";
                                yml.status     = YmlFileState.NoModif;

                                item            = new ListViewItem();
                                item.Text       = file.getFilename();
                                item.Tag        = yml;
                                item.ImageIndex = 1;

                                remotePath = yml.remotePath + yml.remoteName;
                                localPath  = yml.localPath + yml.localName;

                                downloadFile(remotePath, localPath);

                                listView1.Items.Add(item);
                            }
                            catch { }
                        }
                    }
                }
                else
                {
                    label1.Text    = "暂无文件";
                    label1.Visible = true;
                }
            }
            else
            {
                label1.Text    = "加载失败";
                label1.Visible = true;
            }

            btn_reload.Enabled = true;
        }
示例#11
0
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                foreach (ListViewItem item2 in listView1.Items)
                {
                    item2.ForeColor = Color.Black;
                }
                item.ForeColor = Color.Red;

                YmlFile file    = (YmlFile)item.Tag;
                string  path    = file.localPath + file.localName;
                string  content = YSTools.YSFile.readFileToString(path);
                if ((file.remotePath + file.remoteName) == "")
                {
                    file_label.Text = file.localPath + file.localName;
                }
                else
                {
                    file_label.Text = file.remotePath + file.remoteName;
                }

                // 渲染
                if (tabControl1.SelectedIndex == 0)
                {
                    if (resContent != content || _treeView.Items.Count == 0)
                    {
                        resContent       = content;
                        btn_save.Enabled = false;
                        btn_tree.Text    = "展开全部节点";

                        // 树形
                        _treeView.Items.Clear();
                        List <YmlItem>   lists = YmlFormatUtil.FormatYmlToTree(content);
                        TreeListViewItem viewItem = null, parentItem = null;
                        Dictionary <string, TreeListViewItem> _cache = new Dictionary <string, TreeListViewItem>();
                        foreach (YmlItem obj in lists)
                        {
                            viewItem            = new TreeListViewItem();
                            viewItem.Tag        = obj;
                            viewItem.Text       = obj.Key;
                            viewItem.ImageIndex = obj.ImageIndex;

                            viewItem.SubItems.Add(obj.Value);
                            viewItem.SubItems.Add("" + obj.Level);
                            viewItem.SubItems.Add(obj.Common);

                            if (obj.Level == 0)
                            {
                                _treeView.Items.Add(viewItem);
                            }
                            else if (_cache.ContainsKey(obj.Parent.Uuid))
                            {
                                parentItem = _cache[obj.Parent.Uuid];
                                if (null != parentItem)
                                {
                                    parentItem.Items.Add(viewItem);
                                }
                            }
                            else
                            {
                                _treeView.Items.Add(viewItem);
                            }

                            _cache.Add(obj.Uuid, viewItem);
                        }

                        Validate(item, content);
                    }
                }
                else
                {
                    if (resContent != content || ymlEditor.Text == "")
                    {
                        // YML编辑器
                        ymlEditor.Text = "";
                        List <YmlLine> list = YmlFormatUtil.FormatYml(content);
                        foreach (YmlLine line in list)
                        {
                            ymlEditor.SelectionColor = line.Color;
                            ymlEditor.SelectionFont  = line.Font;
                            ymlEditor.AppendText(line.Text);
                        }

                        ymlEditor.ClearUndo();

                        Validate(item, content);
                    }
                }
            }
        }