Server/FormMain.cs
changeset 231 4c706feaf706
parent 229 7c631055b94b
child 232 5a739e2e5255
     1.1 --- a/Server/FormMain.cs	Sun Jul 31 12:03:52 2016 +0200
     1.2 +++ b/Server/FormMain.cs	Fri Aug 12 20:25:05 2016 +0200
     1.3 @@ -322,23 +322,33 @@
     1.4              Event currentEvent = CurrentEvent();
     1.5              SharpLib.Ear.Action currentAction = CurrentAction();
     1.6              TreeNode treeNodeToSelect = null;
     1.7 -
     1.8 +            
     1.9              //Reset our tree
    1.10              iTreeViewEvents.Nodes.Clear();
    1.11              //Populate registered events
    1.12 -            foreach (string key in ManagerEventAction.Current.Events.Keys)
    1.13 +            foreach (SharpLib.Ear.Event e in ManagerEventAction.Current.Events)
    1.14              {
    1.15 -                Event e = ManagerEventAction.Current.Events[key];
    1.16 -                TreeNode eventNode = iTreeViewEvents.Nodes.Add(key, e.Name);
    1.17 -                eventNode.Tag = e;
    1.18 -                eventNode.Nodes.Add(key + ".Description", e.Description);
    1.19 -                TreeNode actionsNodes = eventNode.Nodes.Add(key + ".Actions", "Actions");
    1.20 +                //Create our event node
    1.21 +                TreeNode eventNode = iTreeViewEvents.Nodes.Add(e.Name);
    1.22 +                eventNode.Tag = e; //For easy access to our event
    1.23 +                if (!e.Enabled)
    1.24 +                {
    1.25 +                    //Dim our nodes if disabled
    1.26 +                    eventNode.ForeColor = Color.DimGray;
    1.27 +                }
    1.28 +
    1.29 +                //Add event description as child node
    1.30 +                eventNode.Nodes.Add(e.Description).ForeColor = eventNode.ForeColor; 
    1.31 +                //Create child node for actions root
    1.32 +                TreeNode actionsNodes = eventNode.Nodes.Add("Actions");
    1.33 +                actionsNodes.ForeColor = eventNode.ForeColor;
    1.34  
    1.35                  // Add our actions for that event
    1.36                  foreach (SharpLib.Ear.Action a in e.Actions)
    1.37                  {
    1.38                      TreeNode actionNode = actionsNodes.Nodes.Add(a.Brief());
    1.39                      actionNode.Tag = a;
    1.40 +                    actionNode.ForeColor = eventNode.ForeColor;
    1.41                      if (a == currentAction)
    1.42                      {
    1.43                          treeNodeToSelect = actionNode;
    1.44 @@ -360,7 +370,14 @@
    1.45                      iTreeViewEvents.SelectedNode.Nodes[1].Nodes[
    1.46                          iTreeViewEvents.SelectedNode.Nodes[1].GetNodeCount(false) - 1];
    1.47              }
    1.48 -
    1.49 +            else if (iTreeViewEvents.SelectedNode == null && iTreeViewEvents.Nodes.Count > 0)
    1.50 +            {
    1.51 +                //Still no selected node select the first one then
    1.52 +                iTreeViewEvents.SelectedNode = iTreeViewEvents.Nodes[0];
    1.53 +            }
    1.54 +
    1.55 +
    1.56 +            UpdateEventView();
    1.57          }
    1.58  
    1.59          /// <summary>
    1.60 @@ -2727,12 +2744,13 @@
    1.61                  return;
    1.62              }
    1.63  
    1.64 -            string key = aEvent.GetType().Name;
    1.65 -            TreeNode[] res = iTreeViewEvents.Nodes.Find(key, false);
    1.66 -            if (res.Length > 0)
    1.67 +            foreach (TreeNode node in iTreeViewEvents.Nodes)
    1.68              {
    1.69 -                iTreeViewEvents.SelectedNode = res[0];
    1.70 -                iTreeViewEvents.Focus();
    1.71 +                if (node.Tag == aEvent)
    1.72 +                {
    1.73 +                    iTreeViewEvents.SelectedNode = node;
    1.74 +                    iTreeViewEvents.Focus();
    1.75 +                }
    1.76              }
    1.77          }
    1.78  
    1.79 @@ -2789,12 +2807,12 @@
    1.80                  return;
    1.81              }
    1.82  
    1.83 -            FormEditAction ea = new FormEditAction();
    1.84 +            FormEditObject<SharpLib.Ear.Action> ea = new FormEditObject<SharpLib.Ear.Action>();
    1.85              ea.Text = "Add action";
    1.86              DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
    1.87              if (res == DialogResult.OK)
    1.88              {
    1.89 -                selectedEvent.Actions.Add(ea.Action);
    1.90 +                selectedEvent.Actions.Add(ea.Object);
    1.91                  Properties.Settings.Default.Actions = ManagerEventAction.Current;
    1.92                  Properties.Settings.Default.Save();
    1.93                  PopulateEventsTreeView();
    1.94 @@ -2816,15 +2834,15 @@
    1.95                  return;
    1.96              }
    1.97  
    1.98 -            FormEditAction ea = new FormEditAction();
    1.99 +            FormEditObject<SharpLib.Ear.Action> ea = new FormEditObject<SharpLib.Ear.Action>();
   1.100              ea.Text = "Edit action";
   1.101 -            ea.Action = selectedAction;
   1.102 +            ea.Object = selectedAction;
   1.103              int actionIndex = iTreeViewEvents.SelectedNode.Index;
   1.104              DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
   1.105              if (res == DialogResult.OK)
   1.106              {
   1.107                  //Update our action
   1.108 -                selectedEvent.Actions[actionIndex]=ea.Action;
   1.109 +                selectedEvent.Actions[actionIndex]=ea.Object;
   1.110                  //Save and rebuild our event tree view
   1.111                  Properties.Settings.Default.Actions = ManagerEventAction.Current;
   1.112                  Properties.Settings.Default.Save();
   1.113 @@ -2863,8 +2881,7 @@
   1.114              SharpLib.Ear.Action a = CurrentAction();
   1.115              if (a != null)
   1.116              {
   1.117 -                Console.WriteLine("Action testing:");
   1.118 -                a.Execute();
   1.119 +                a.Test();
   1.120              }
   1.121              iTreeViewEvents.Focus();
   1.122          }
   1.123 @@ -2939,8 +2956,7 @@
   1.124              Event earEvent = CurrentEvent();
   1.125              if (earEvent != null)
   1.126              {
   1.127 -                Console.WriteLine("Event testing:");
   1.128 -                earEvent.Trigger();
   1.129 +                earEvent.Test();
   1.130              }
   1.131          }
   1.132  
   1.133 @@ -2951,9 +2967,22 @@
   1.134          /// <param name="e"></param>
   1.135          private void iTreeViewEvents_AfterSelect(object sender, TreeViewEventArgs e)
   1.136          {
   1.137 +            UpdateEventView();
   1.138 +        }
   1.139 +
   1.140 +        /// <summary>
   1.141 +        /// 
   1.142 +        /// </summary>
   1.143 +        private void UpdateEventView()
   1.144 +        {
   1.145 +            //One can always add an event
   1.146 +            buttonEventAdd.Enabled = true;
   1.147 +
   1.148              //Enable buttons according to selected item
   1.149              buttonActionAdd.Enabled =
   1.150              buttonEventTest.Enabled =
   1.151 +            buttonEventDelete.Enabled =
   1.152 +            buttonEventEdit.Enabled =
   1.153                  CurrentEvent() != null;
   1.154  
   1.155              SharpLib.Ear.Action currentAction = CurrentAction();
   1.156 @@ -2962,7 +2991,7 @@
   1.157              buttonActionDelete.Enabled =
   1.158              buttonActionMoveUp.Enabled =
   1.159              buttonActionMoveDown.Enabled =
   1.160 -            buttonActionEdit.Enabled = 
   1.161 +            buttonActionEdit.Enabled =
   1.162                      currentAction != null;
   1.163  
   1.164              if (currentAction != null)
   1.165 @@ -2972,8 +3001,65 @@
   1.166                  buttonActionMoveDown.Enabled = iTreeViewEvents.SelectedNode.Index <
   1.167                                                 iTreeViewEvents.SelectedNode.Parent.Nodes.Count - 1;
   1.168              }
   1.169 -
   1.170          }
   1.171  
   1.172 +        private void buttonEventAdd_Click(object sender, EventArgs e)
   1.173 +        {
   1.174 +            FormEditObject<SharpLib.Ear.Event> ea = new FormEditObject<SharpLib.Ear.Event>();
   1.175 +            ea.Text = "Add event";
   1.176 +            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
   1.177 +            if (res == DialogResult.OK)
   1.178 +            {
   1.179 +                ManagerEventAction.Current.Events.Add(ea.Object);
   1.180 +                Properties.Settings.Default.Actions = ManagerEventAction.Current;
   1.181 +                Properties.Settings.Default.Save();
   1.182 +                PopulateEventsTreeView();
   1.183 +                SelectEvent(ea.Object);
   1.184 +            }
   1.185 +        }
   1.186 +
   1.187 +        private void buttonEventDelete_Click(object sender, EventArgs e)
   1.188 +        {
   1.189 +            SharpLib.Ear.Event currentEvent = CurrentEvent();
   1.190 +            if (currentEvent == null)
   1.191 +            {
   1.192 +                //Must select action node
   1.193 +                return;
   1.194 +            }
   1.195 +
   1.196 +            ManagerEventAction.Current.Events.Remove(currentEvent);
   1.197 +            Properties.Settings.Default.Actions = ManagerEventAction.Current;
   1.198 +            Properties.Settings.Default.Save();
   1.199 +            PopulateEventsTreeView();
   1.200 +        }
   1.201 +
   1.202 +        private void buttonEventEdit_Click(object sender, EventArgs e)
   1.203 +        {
   1.204 +            Event selectedEvent = CurrentEvent();
   1.205 +            if (selectedEvent == null)
   1.206 +            {
   1.207 +                //We did not find a corresponding event
   1.208 +                return;
   1.209 +            }
   1.210 +
   1.211 +            FormEditObject<SharpLib.Ear.Event> ea = new FormEditObject<SharpLib.Ear.Event>();
   1.212 +            ea.Text = "Edit event";
   1.213 +            ea.Object = selectedEvent;
   1.214 +            int actionIndex = iTreeViewEvents.SelectedNode.Index;
   1.215 +            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
   1.216 +            if (res == DialogResult.OK)
   1.217 +            {
   1.218 +                //Save and rebuild our event tree view
   1.219 +                Properties.Settings.Default.Actions = ManagerEventAction.Current;
   1.220 +                Properties.Settings.Default.Save();
   1.221 +                PopulateEventsTreeView();
   1.222 +            }
   1.223 +        }
   1.224 +
   1.225 +        private void iTreeViewEvents_Leave(object sender, EventArgs e)
   1.226 +        {
   1.227 +            //Make sure our event tree never looses focus
   1.228 +            ((TreeView) sender).Focus();
   1.229 +        }
   1.230      }
   1.231  }