Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Data/ProtocolEditor.cs @ 584

Last change on this file since 584 was 584, checked in by abeham, 16 years ago

merged communication framework to trunk (ticket #279)

File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10
11namespace HeuristicLab.Communication.Data {
12  public partial class ProtocolEditor : EditorBase {
13    public Protocol Protocol {
14      get { return (Protocol)Item; }
15      set { base.Item = value; }
16    }
17
18    public ProtocolEditor() {
19      InitializeComponent();
20    }
21    public ProtocolEditor(Protocol protocol)
22      : this() {
23      Protocol = protocol;
24    }
25
26    protected override void RemoveItemEvents() {
27      Protocol.Changed -= new EventHandler(Protocol_Changed);
28      Protocol.StatesChanged -= new EventHandler(States_Changed);
29      Protocol.Name.Changed -= new EventHandler(ProtocolName_Changed);
30      base.RemoveItemEvents();
31    }
32    protected override void AddItemEvents() {
33      base.AddItemEvents();
34      Protocol.Changed += new EventHandler(Protocol_Changed);
35      Protocol.StatesChanged += new EventHandler(States_Changed);
36      Protocol.Name.Changed += new EventHandler(ProtocolName_Changed);
37    }
38
39    private void BuildInitialStateComboBox() {
40      // Rebuild the two ComboBoxes depicting the initial and final state
41      IList<ProtocolState> states = new List<ProtocolState>(Protocol.States.Count);
42      int initialSelectedIndex = -1;
43      for (int i = 0 ; i < Protocol.States.Count ; i++) {
44        states.Add((ProtocolState)Protocol.States[i]);
45        if (Protocol.States[i].Guid.Equals(Protocol.InitialState.Guid))
46          initialSelectedIndex = i;
47      }
48      initialStateComboBox.SelectedIndexChanged -= new EventHandler(initialStateComboBox_SelectedIndexChanged);
49      BindingList<ProtocolState> bl = new BindingList<ProtocolState>(states);
50      initialStateComboBox.DataSource = bl;
51      initialStateComboBox.DisplayMember = "Name";
52      initialStateComboBox.ValueMember = "Guid";
53      initialStateComboBox.SelectedIndex = initialSelectedIndex;
54      initialStateComboBox.SelectedIndexChanged += new EventHandler(initialStateComboBox_SelectedIndexChanged);
55    }
56
57    protected override void UpdateControls() {
58      base.UpdateControls();
59      if (Protocol == null) {
60        Caption = "Protocol";
61        nameViewControl.Enabled = false;
62        nameViewControl.StringData = null;
63        statesItemListView.Enabled = false;
64        statesItemListView.ItemList = null;
65        initialStateComboBox.Enabled = false;
66        initialStateComboBox.DataSource = null;
67        initialStateComboBox.Items.Clear();
68      } else {
69        Caption = Protocol.Name.Data;
70        nameViewControl.StringData = Protocol.Name;
71        nameViewControl.Enabled = true;
72        statesItemListView.ItemList = Protocol.States;
73        statesItemListView.Enabled = true;
74        BuildInitialStateComboBox();
75        initialStateComboBox.Enabled = true;
76      }
77    }
78
79    #region Custom events
80    void Protocol_Changed(object sender, EventArgs e) {
81      Refresh();
82    }
83
84    void States_Changed(object sender, EventArgs e) {
85      Refresh();
86    }
87
88    void ProtocolName_Changed(object sender, EventArgs e) {
89      Caption = Protocol.Name.Data;
90    }
91    #endregion
92
93    #region ComboBox events
94    private void initialStateComboBox_SelectedIndexChanged(object sender, EventArgs e) {
95      if (initialStateComboBox.SelectedIndex >= 0)
96        Protocol.InitialState = (ProtocolState)initialStateComboBox.SelectedItem;
97    }
98    #endregion
99
100    private void invertButton_Click(object sender, EventArgs e) {
101      for (int i = 0 ; i < Protocol.States.Count ; i++) {
102        ConstrainedItemList tmp = ((ProtocolState)Protocol.States[i]).SendingData;
103        ((ProtocolState)Protocol.States[i]).SendingData = ((ProtocolState)Protocol.States[i]).ReceivingData;
104        ((ProtocolState)Protocol.States[i]).ReceivingData = tmp;
105      }
106      Refresh();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.