public void SetStoppedIcon(string taskid, string taskname)
        {
            if (base.Nodes == null || base.Nodes.Count == 0)
            {
                return;
            }

            BaseNode bn = base.Nodes[0] as BaseNode;

            if (bn != null)
            {
                foreach (TreeNode item in bn.Nodes)
                {
                    TaskNode tn = item as TaskNode;
                    if (tn != null)
                    {
                        if (tn.Task.TaskId == taskid && tn.Task.TaskName == taskname)
                        {
                            //icon
                            tn.ImageIndex         = DataConvert.GetInt32(IconType.Task);
                            tn.SelectedImageIndex = DataConvert.GetInt32(IconType.Task);
                            _executingTaskIdList.Remove(tn.Task.TaskId);
                            break;
                        }
                    }
                }
            }
        }
        private void OnDeleteClick(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure to delete?", Constants.MESSAGEBOX_CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    TaskNode tn   = this.SelectedNode as TaskNode;
                    TaskInfo task = null;
                    if (tn != null)
                    {
                        task = tn.Task;
                    }
                    if (task != null)
                    {
                        string ret = ConfigCtrl.DeleteTask(task);
                        if (ret != Constants.STATUS_SUCCESS)
                        {
                            MessageBox.Show(ret, Constants.MESSAGEBOX_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        BaseNode bn = (BaseNode)tn.Parent;
                        bn.Nodes.Remove(tn);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }
        private void OnDoubleClick(object sender, EventArgs e)
        {
            try
            {
                TaskNode tn = this.SelectedNode as TaskNode;
                if (tn != null)
                {
                    TaskEventArgs te = new TaskEventArgs();
                    te.Task = tn.Task;
                    OnOpenTaskEditor(te);
                    return;
                }

                /*
                 * OperationNode on = this.SelectedNode as OperationNode;
                 * if (on != null)
                 * {
                 *  AccountEventArgs te = new AccountEventArgs();
                 *  tn = on.Parent as TaskNode;
                 *  te.Group = tn.Task.GroupName;
                 *  te.Account = on.Operation.Account;
                 *  OnOpenAccount(te);
                 * }*/
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }
        private void OnStopAllTasksClick(object sender, EventArgs e)
        {
            try
            {
                BaseNode bn = this.SelectedNode as BaseNode;
                if (bn != null)
                {
                    foreach (TreeNode node in bn.Nodes)
                    {
                        TaskNode tn = node as TaskNode;
                        if (tn != null)
                        {
                            if (_executingTaskIdList.Contains(tn.Task.TaskId))
                            {
                                //icon
                                tn.ImageIndex         = DataConvert.GetInt32(IconType.Task);
                                tn.SelectedImageIndex = DataConvert.GetInt32(IconType.Task);

                                TaskEventArgs te = new TaskEventArgs();
                                te.Task = tn.Task;
                                OnStopTask(te);
                                _executingTaskIdList.Remove(tn.Task.TaskId);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }
 private void OnOpenTaskConfigFileClick(object sender, EventArgs e)
 {
     try
     {
         TaskNode tn = this.SelectedNode as TaskNode;
         if (tn != null)
         {
             TaskEventArgs te = new TaskEventArgs();
             te.Task = tn.Task;
             OnOpenTaskConfigFile(te);
         }
     }
     catch (Exception ex)
     {
         ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
     }
 }
 private void OnRenameTaskClick(object sender, EventArgs e)
 {
     try
     {
         TaskNode tn = this.SelectedNode as TaskNode;
         if (tn != null)
         {
             DlgAddTask adu = new DlgAddTask();
             adu.Task = tn.Task;
             if (adu.ShowDialog() == DialogResult.OK)
             {
                 tn.Text = adu.Task.TaskName;
             }
         }
     }
     catch (Exception ex)
     {
         ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
     }
 }
        public void InitialNodes()
        {
            //set nodes
            base.Nodes.Clear();

            //build Root node
            BaseNode Root = new BaseNode("All Tasks");

            base.Nodes.Add(Root);

            //build Task node
            Collection <TaskInfo> tasks = ConfigCtrl.GetTasks();

            foreach (TaskInfo task in tasks)
            {
                if (task != null)
                {
                    TaskNode tn = new TaskNode(task);
                    Root.Nodes.Add(tn);

                    /*
                     * foreach (AccountInfo account in task.Accounts)
                     * {
                     *  OperationInfo operation = ConfigCtrl.GetOperation(task.GroupName, account);
                     *  OperationNode on = new OperationNode(operation);
                     *  tn.Nodes.Add(on);
                     * }*/
                    //set the started icon if the task is running
                    if (_executingTaskIdList.Contains(task.TaskId))
                    {
                        tn.ImageIndex         = DataConvert.GetInt32(IconType.Started);
                        tn.SelectedImageIndex = DataConvert.GetInt32(IconType.Started);
                    }
                }
            }

            if (Root.Nodes.Count > 0)
            {
                Root.Expand();
            }
        }
        private void OnTaskStopClick(object sender, EventArgs e)
        {
            try
            {
                TaskNode tn = this.SelectedNode as TaskNode;
                if (tn != null)
                {
                    //icon
                    tn.ImageIndex         = DataConvert.GetInt32(IconType.Task);
                    tn.SelectedImageIndex = DataConvert.GetInt32(IconType.Task);

                    TaskEventArgs te = new TaskEventArgs();
                    te.Task = tn.Task;
                    OnStopTask(te);
                    _executingTaskIdList.Remove(tn.Task.TaskId);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }
 private void TaskTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
 {
     try
     {
         if (e.Node is TaskNode)
         {
             TaskNode tn = e.Node as TaskNode;
             if (tn != null)
             {
                 _task = tn.Task;
             }
         }
         else
         {
             e.CancelEdit = true;
         }
     }
     catch (Exception ex)
     {
         ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
     }
 }
        private void OnAddTaskClick(object sender, EventArgs e)
        {
            try
            {
                DlgAddTask adt = new DlgAddTask();
                if (adt.ShowDialog() == DialogResult.OK)
                {
                    BaseNode bn = (BaseNode)this.SelectedNode;
                    TaskNode tn = new TaskNode(adt.Task);
                    bn.Nodes.Add(tn);
                    if (bn.Parent != null && bn.Parent.Nodes.Count > 0)
                    {
                        bn.Parent.Expand();
                    }
                }

                if (this.SelectedNode is BaseNode)
                {
                    BaseNode bn = this.SelectedNode as BaseNode;
                    if (bn != null)
                    {
                        RootNodeEventArgs re = new RootNodeEventArgs();
                        re.Tasks = new Collection <TaskInfo>();
                        foreach (TaskNode tasknode in bn.Nodes)
                        {
                            re.Tasks.Add(tasknode.Task);
                        }
                        OnRootNodeSelected(re);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }
        private void TaskTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (e.Node is TaskNode)
                {
                    TaskNode      tn = e.Node as TaskNode;
                    TaskEventArgs te = new TaskEventArgs();
                    if (tn != null)
                    {
                        te.Task = ConfigCtrl.GetTask(tn.Task.TaskId, tn.Task.TaskName);

                        /*
                         * te.Operations = new Collection<OperationInfo>();
                         * foreach (AccountInfo account in te.Task.Accounts)
                         * {
                         *  te.Operations.Add(ConfigCtrl.GetOperation(te.Task.GroupName, account));
                         * }*/

                        //te.Task = tn.Task;
                        //te.Operations = new Collection<OperationInfo>();
                        //foreach (OperationNode on in tn.Nodes)
                        //{
                        //    te.Operations.Add(on.Operation);
                        //}
                    }
                    else
                    {
                        te.Task = null;
                    }
                    OnTaskNodeSelected(te);
                }
                else if (this.SelectedNode is TaskNode)
                {
                    TaskNode      tn = this.SelectedNode as TaskNode;
                    TaskEventArgs te = new TaskEventArgs();
                    if (tn != null)
                    {
                        te.Task = tn.Task;

                        /*
                         * te.Operations = new Collection<OperationInfo>();
                         * foreach (OperationNode on in tn.Nodes)
                         * {
                         *  te.Operations.Add(on.Operation);
                         * }*/
                    }
                    else
                    {
                        te.Task = null;
                    }
                    OnTaskNodeSelected(te);
                }

                /*
                 * else if (this.SelectedNode is OperationNode)
                 * {
                 * OperationNode on = this.SelectedNode as OperationNode;
                 * OperationEventArgs oe = new OperationEventArgs();
                 * if (on != null)
                 *  oe.Operation = on.Operation;
                 * else
                 *  oe.Operation = null;
                 * OnOperationNodeSelected(oe);
                 * }*/
                else if (this.SelectedNode is BaseNode)
                {
                    BaseNode bn = this.SelectedNode as BaseNode;
                    if (bn != null)
                    {
                        RootNodeEventArgs re = new RootNodeEventArgs();
                        re.Tasks = new Collection <TaskInfo>();
                        foreach (TaskNode tasknode in bn.Nodes)
                        {
                            re.Tasks.Add(tasknode.Task);
                        }
                        OnRootNodeSelected(re);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }
        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    contextmenustrip.Items.Clear();

                    //onfocus when right click on the node
                    BaseNode bn = (BaseNode)this.GetNodeAt(e.X, e.Y);
                    if (bn == null)
                    {
                        return;
                    }
                    else
                    {
                        this.SelectedNode = bn;
                    }

                    switch (bn.nodeType)
                    {
                    //BaseNode
                    case NodeType.Base:
                        contextmenustrip.Items.Add("Add Task");
                        contextmenustrip.Items.Add("Refresh");
                        contextmenustrip.Items.Add(new ToolStripSeparator());
                        contextmenustrip.Items.Add("Start All");
                        contextmenustrip.Items.Add("Stop All");
                        contextmenustrip.Items[0].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.AddTask)];
                        contextmenustrip.Items[0].Click += new EventHandler(OnAddTaskClick);
                        contextmenustrip.Items[1].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Refresh)];
                        contextmenustrip.Items[1].Click += new EventHandler(OnRefreshTasksClick);
                        contextmenustrip.Items[3].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Start)];
                        contextmenustrip.Items[3].Click += new EventHandler(OnStartAllTasksClick);
                        contextmenustrip.Items[4].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Stop)];
                        contextmenustrip.Items[4].Click += new EventHandler(OnStopAllTasksClick);
                        if (_executingTaskIdList != null && _executingTaskIdList.Count == bn.Nodes.Count)
                        {
                            contextmenustrip.Items[3].Enabled = false;
                        }
                        else
                        {
                            contextmenustrip.Items[3].Enabled = true;
                        }
                        if (_executingTaskIdList == null || _executingTaskIdList.Count == 0)
                        {
                            contextmenustrip.Items[4].Enabled = false;
                        }
                        else
                        {
                            contextmenustrip.Items[4].Enabled = true;
                        }
                        break;

                    //TaskNode
                    case NodeType.Task:
                        contextmenustrip.Items.Add("Edit Task");
                        contextmenustrip.Items.Add("Open Task Config File");
                        contextmenustrip.Items.Add(new ToolStripSeparator());
                        contextmenustrip.Items.Add("Refresh");
                        contextmenustrip.Items.Add("Rename");
                        contextmenustrip.Items.Add("Delete");
                        contextmenustrip.Items.Add(new ToolStripSeparator());
                        contextmenustrip.Items.Add("Start");
                        contextmenustrip.Items.Add("Stop");

                        contextmenustrip.Items[0].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.AddTask)];
                        contextmenustrip.Items[0].Click += new EventHandler(OnOpenTaskEditorClick);
                        contextmenustrip.Items[1].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Xml)];
                        contextmenustrip.Items[1].Click += new EventHandler(OnOpenTaskConfigFileClick);
                        contextmenustrip.Items[3].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Refresh)];
                        contextmenustrip.Items[3].Click += new EventHandler(OnRefreshOperationsClick);
                        contextmenustrip.Items[4].Click += new EventHandler(OnRenameTaskClick);
                        contextmenustrip.Items[5].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Delete)];
                        contextmenustrip.Items[5].Click += new EventHandler(OnDeleteClick);
                        contextmenustrip.Items[7].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Start)];
                        contextmenustrip.Items[7].Click += new EventHandler(OnTaskStartClick);
                        contextmenustrip.Items[8].Image  = imageListIcon.Images[DataConvert.GetInt32(IconType.Stop)];
                        contextmenustrip.Items[8].Click += new EventHandler(OnTaskStopClick);
                        TaskNode tn = bn as TaskNode;
                        if (_executingTaskIdList.Contains(tn.Task.TaskId))
                        {
                            contextmenustrip.Items[7].Enabled = false;
                        }
                        else
                        {
                            contextmenustrip.Items[8].Enabled = false;
                        }
                        break;

                    //OperationNode

                    /*case NodeType.Operation:
                     *  contextmenustrip.Items.Add("ÅäÖÃ");
                     *  contextmenustrip.Items[0].Image = imageListIcon.Images[DataConvert.GetInt32(IconType.Operation)];
                     *  contextmenustrip.Items[0].Click += new EventHandler(OnOpenUserClick);
                     *  break;*/
                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowMessageBox(CGConstants.MODULE_TASKTREE, ex);
            }
        }