Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Data/StateTransitionView.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: 6.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10
11namespace HeuristicLab.Communication.Data {
12  public partial class StateTransitionView : ViewBase {
13
14    public StateTransition StateTransition {
15      get { return (StateTransition)Item; }
16      set { base.Item = value; }
17    }
18
19    public StateTransitionView() {
20      InitializeComponent();
21    }
22
23    public StateTransitionView(StateTransition stateTransition)
24      : this() {
25      StateTransition = stateTransition;
26    }
27
28    protected override void RemoveItemEvents() {
29      StateTransition.Changed -= new EventHandler(StateTransition_Changed);
30      ConstrainedItemList request = StateTransition.SourceState.SendingData;
31      ConstrainedItemList response = StateTransition.SourceState.ReceivingData;
32
33      request.Changed -= new EventHandler(VariablesList_Changed);
34      request.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Parameter_Added);
35      request.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
36      for (int i = 0 ; i < request.Count ; i++)
37        request[i].Changed -= new EventHandler(Parameter_Changed);
38      response.Changed -= new EventHandler(VariablesList_Changed);
39      response.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Parameter_Added);
40      response.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
41      for (int i = 0 ; i < response.Count ; i++)
42        response[i].Changed -= new EventHandler(Parameter_Changed);
43      base.RemoveItemEvents();
44    }
45    protected override void AddItemEvents() {
46      base.AddItemEvents();
47      StateTransition.Changed += new EventHandler(StateTransition_Changed);
48      ConstrainedItemList request = StateTransition.SourceState.SendingData;
49      ConstrainedItemList response = StateTransition.SourceState.ReceivingData;
50
51      request.Changed += new EventHandler(VariablesList_Changed);
52      request.ItemAdded += new EventHandler<ItemIndexEventArgs>(Parameter_Added);
53      request.ItemRemoved += new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
54      for (int i = 0 ; i < request.Count ; i++)
55        request[i].Changed += new EventHandler(Parameter_Changed);
56      response.Changed += new EventHandler(VariablesList_Changed);
57      response.ItemAdded += new EventHandler<ItemIndexEventArgs>(Parameter_Added);
58      response.ItemRemoved += new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
59      for (int i = 0 ; i < response.Count ; i++) {
60        response[i].Changed += new EventHandler(Parameter_Changed);
61      }
62    }
63
64    protected override void UpdateControls() {
65      base.UpdateControls();
66      if (StateTransition == null) {
67        transitionFunctionProgrammableOperatorView.Enabled = false;
68        targetStateComboBox.Enabled = false;
69        targetStateComboBox.DataSource = null;
70        targetStateComboBox.Items.Clear();
71      } else {
72        targetStateComboBox.Enabled = true;
73        transitionFunctionProgrammableOperatorView.ProgrammableOperator = StateTransition.TransitionCondition;
74        transitionFunctionProgrammableOperatorView.Enabled = true;
75        BuildTargetStateComboBox();
76        BuildVariablesListBox();
77      }
78    }
79
80    void StateTransition_Changed(object sender, EventArgs e) {
81      Refresh();
82    }
83
84    void VariablesList_Changed(object sender, EventArgs e) {
85      BuildVariablesListBox();
86    }
87
88    #region Events to synchronize changes in request/response variables to state transition
89    void Parameter_Changed(object sender, EventArgs e) {
90      BuildVariablesListBox();
91    }
92    void Parameter_Added(object sender, ItemIndexEventArgs e) {
93      ((IItem)e.Item).Changed += new EventHandler(Parameter_Changed);
94    }
95    void Parameter_Removed(object sender, ItemIndexEventArgs e) {
96      ((IItem)e.Item).Changed -= new EventHandler(Parameter_Changed);
97    }
98    #endregion
99
100    private void BuildTargetStateComboBox() {
101      ComboBox comboBox = targetStateComboBox;
102      ItemList<ProtocolState> states;
103      if (StateTransition.SourceState == null || StateTransition.SourceState.Protocol == null)
104        states = null;
105      else
106        states = StateTransition.SourceState.Protocol.States;
107      ProtocolState selected = StateTransition.TargetState;
108      ProtocolState forbidden = StateTransition.SourceState;
109
110      if (states != null && states.Count > 0) {
111        IList<ProtocolState> sourceList = new List<ProtocolState>(states.Count);
112        int selectedIndex = -1, counter = -1;
113        for (int i = 0 ; i < states.Count ; i++) {
114          if (forbidden == null || !states[i].Guid.Equals(forbidden.Guid)) {
115            sourceList.Add((ProtocolState)states[i]);
116            counter++;
117          }
118          if (selected != null && states[i].Guid.Equals(selected.Guid))
119            selectedIndex = counter;
120        }
121        comboBox.SelectedIndexChanged -= new EventHandler(targetStateComboBox_SelectedIndexChanged);
122        BindingList<ProtocolState> bl = new BindingList<ProtocolState>(sourceList);
123        comboBox.DataSource = bl;
124        comboBox.DisplayMember = "Name";
125        comboBox.ValueMember = "Guid";
126        comboBox.SelectedIndex = selectedIndex;
127        comboBox.SelectedIndexChanged += new EventHandler(targetStateComboBox_SelectedIndexChanged);
128      }
129    }
130
131    private void BuildVariablesListBox() {
132      ListBox variables = variablesListBox;
133      ConstrainedItemList request = StateTransition.SourceState.SendingData;
134      ConstrainedItemList response = StateTransition.SourceState.ReceivingData;
135      variables.Items.Clear();
136      variables.Items.Add("==Sending==");
137      for (int i = 0 ; i < request.Count ; i++) {
138        variables.Items.Add(request[i]);
139      }
140      variables.Items.Add("==Receiving==");
141      for (int i = 0 ; i < response.Count ; i++) {
142        variables.Items.Add(response[i]);
143      }
144    }
145
146    private void targetStateComboBox_SelectedIndexChanged(object sender, EventArgs e) {
147      if (targetStateComboBox.SelectedIndex >= 0) {
148        StateTransition.TargetState = (ProtocolState)targetStateComboBox.SelectedItem;
149      }
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.