1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Data;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Text;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 |
|
---|
11 | namespace 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 | } |
---|