Server/MainForm.cs
changeset 141 6f1da2b5c2ec
parent 140 4dff57d255c9
child 142 45afadb954ba
     1.1 --- a/Server/MainForm.cs	Thu Jun 11 17:21:15 2015 +0200
     1.2 +++ b/Server/MainForm.cs	Fri Jun 19 17:12:06 2015 +0200
     1.3 @@ -555,7 +555,7 @@
     1.4          /// This will take care of applying our client layout and set data fields.
     1.5          /// </summary>
     1.6          /// <param name="aSessionId"></param>
     1.7 -        void SetCurrentClient(string aSessionId)
     1.8 +        void SetCurrentClient(string aSessionId, bool aForce=false)
     1.9          {
    1.10              if (aSessionId == iCurrentClientSessionId)
    1.11              {
    1.12 @@ -564,8 +564,20 @@
    1.13                  return;
    1.14              }
    1.15  
    1.16 +
    1.17 +            //Check when was the last time we switched to that client
    1.18 +            double lastSwitchToClientSecondsAgo = (DateTime.Now - iClients[aSessionId].LastSwitchTime).TotalSeconds;
    1.19 +            //TODO: put that hard coded delay in settings
    1.20 +            if (!aForce && (lastSwitchToClientSecondsAgo < 10))
    1.21 +            {
    1.22 +                //Don't switch clients too often
    1.23 +                return;
    1.24 +            }
    1.25 +
    1.26              //Set current client ID.
    1.27              iCurrentClientSessionId = aSessionId;
    1.28 +            //Set the time we last switched to that client
    1.29 +            iClients[aSessionId].LastSwitchTime = DateTime.Now;
    1.30              //Fetch and set current client data.
    1.31              iCurrentClientData = iClients[aSessionId];
    1.32              //Apply layout and set data fields.
    1.33 @@ -1320,7 +1332,20 @@
    1.34  
    1.35          private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
    1.36          {
    1.37 +            //Root node must have at least one child
    1.38 +            if (e.Node.Nodes.Count == 0)
    1.39 +            {
    1.40 +                return;
    1.41 +            }
    1.42  
    1.43 +            //If the selected node is the root node of a client then switch to it
    1.44 +            string sessionId=e.Node.Nodes[0].Text; //First child of a root node is the sessionId
    1.45 +            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
    1.46 +            {
    1.47 +                //We have a valid session just switch to that client
    1.48 +                SetCurrentClient(sessionId,true);
    1.49 +            }
    1.50 +            
    1.51          }
    1.52  
    1.53  
    1.54 @@ -1405,8 +1430,8 @@
    1.55                  ClientData client = iClients[aSessionId];
    1.56                  if (client != null)
    1.57                  {
    1.58 +                    //Set our client layout then
    1.59                      client.Layout = aLayout;
    1.60 -                    UpdateTableLayoutPanel(client);
    1.61                      //
    1.62                      UpdateClientTreeViewNode(client);
    1.63                  }
    1.64 @@ -1440,63 +1465,69 @@
    1.65          /// <param name="aSessionId"></param>
    1.66          /// <param name="aField"></param>
    1.67          private void SetClientField(string aSessionId, DataField aField)
    1.68 -        {
    1.69 -            SetCurrentClient(aSessionId);
    1.70 +        {   
    1.71 +            //TODO: should check if the field actually changed?
    1.72 +
    1.73              ClientData client = iClients[aSessionId];
    1.74 -            if (client != null)
    1.75 +            bool layoutChanged = false;
    1.76 +
    1.77 +            //Make sure all our fields are in place
    1.78 +            while (client.Fields.Count < (aField.Index + 1))
    1.79              {
    1.80 -                bool somethingChanged = false;
    1.81 +                //Add a text field with proper index
    1.82 +                client.Fields.Add(new DataField(client.Fields.Count));
    1.83 +                layoutChanged = true;
    1.84 +            }
    1.85  
    1.86 -                //Make sure all our fields are in place
    1.87 -                while (client.Fields.Count < (aField.Index + 1))
    1.88 +            if (client.Fields[aField.Index].IsSameLayout(aField))
    1.89 +            {
    1.90 +                //Same layout just update our field
    1.91 +                client.Fields[aField.Index] = aField;
    1.92 +                //If we are updating a field in our current client we need to update it in our panel
    1.93 +                if (aSessionId == iCurrentClientSessionId)
    1.94                  {
    1.95 -                    //Add a text field with proper index
    1.96 -                    client.Fields.Add(new DataField(client.Fields.Count));
    1.97 -                    somethingChanged = true;
    1.98 -                }
    1.99 -
   1.100 -                if (client.Fields[aField.Index].IsSameLayout(aField))
   1.101 -                {
   1.102 -                    //Same layout just update our field
   1.103 -                    client.Fields[aField.Index] = aField;
   1.104 -                    //
   1.105 -                    if (aField.IsText && tableLayoutPanel.Controls[aField.Index] is MarqueeLabel)
   1.106 +                    if (aField.IsText && aField.Index < tableLayoutPanel.Controls.Count && tableLayoutPanel.Controls[aField.Index] is MarqueeLabel)
   1.107                      {
   1.108                          //Text field control already in place, just change the text
   1.109                          MarqueeLabel label = (MarqueeLabel)tableLayoutPanel.Controls[aField.Index];
   1.110 -                        somethingChanged = (label.Text != aField.Text || label.TextAlign != aField.Alignment);
   1.111 +                        layoutChanged = (label.Text != aField.Text || label.TextAlign != aField.Alignment);
   1.112                          label.Text = aField.Text;
   1.113                          label.TextAlign = aField.Alignment;
   1.114                      }
   1.115 -                    else if (aField.IsBitmap && tableLayoutPanel.Controls[aField.Index] is PictureBox)
   1.116 +                    else if (aField.IsBitmap && aField.Index < tableLayoutPanel.Controls.Count && tableLayoutPanel.Controls[aField.Index] is PictureBox)
   1.117                      {
   1.118 -                        somethingChanged = true; //TODO: Bitmap comp or should we leave that to clients?
   1.119 +                        layoutChanged = true; //TODO: Bitmap comp or should we leave that to clients?
   1.120                          //Bitmap field control already in place just change the bitmap
   1.121                          PictureBox pictureBox = (PictureBox)tableLayoutPanel.Controls[aField.Index];
   1.122                          pictureBox.Image = aField.Bitmap;
   1.123                      }
   1.124                      else
   1.125                      {
   1.126 -                        somethingChanged = true;
   1.127 -                        //The requested control in our layout it not of the correct type
   1.128 -                        //Wrong control type, re-create them all
   1.129 -                        UpdateTableLayoutPanel(iCurrentClientData);
   1.130 +                        layoutChanged = true;
   1.131                      }
   1.132                  }
   1.133 -                else
   1.134 +            }
   1.135 +            else
   1.136 +            {
   1.137 +                layoutChanged = true;
   1.138 +                //Different layout, need to rebuild it
   1.139 +                client.Fields[aField.Index] = aField;
   1.140 +            }
   1.141 +
   1.142 +            //
   1.143 +            if (layoutChanged)
   1.144 +            {
   1.145 +                UpdateClientTreeViewNode(client);
   1.146 +                //Our layout has changed, if we are already the current client we need to update our panel
   1.147 +                if (aSessionId == iCurrentClientSessionId)
   1.148                  {
   1.149 -                    somethingChanged = true;
   1.150 -                    //Different layout, need to rebuild it
   1.151 -                    client.Fields[aField.Index] = aField;
   1.152 +                    //Apply layout and set data fields.
   1.153                      UpdateTableLayoutPanel(iCurrentClientData);
   1.154                  }
   1.155 +            }
   1.156  
   1.157 -                //
   1.158 -                if (somethingChanged)
   1.159 -                {
   1.160 -                    UpdateClientTreeViewNode(client);
   1.161 -                }
   1.162 -            }
   1.163 +            //When a client field is set we try switching to this client to present the new information to our user
   1.164 +            SetCurrentClient(aSessionId);
   1.165          }
   1.166  
   1.167          /// <summary>
   1.168 @@ -1646,6 +1677,7 @@
   1.169              TableLayout layout = aClient.Layout;
   1.170              int fieldCount = 0;
   1.171  
   1.172 +            //First clean our current panel
   1.173              tableLayoutPanel.Controls.Clear();
   1.174              tableLayoutPanel.RowStyles.Clear();
   1.175              tableLayoutPanel.ColumnStyles.Clear();