Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Communication.Data/StateTransitionView.cs @ 10445

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

Put the GPL license in the files from the communication framework and simulation optimization project (branch 3.1)

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31
32namespace HeuristicLab.Communication.Data {
33  public partial class StateTransitionView : ViewBase {
34
35    public StateTransition StateTransition {
36      get { return (StateTransition)Item; }
37      set { base.Item = value; }
38    }
39
40    public StateTransitionView() {
41      InitializeComponent();
42    }
43
44    public StateTransitionView(StateTransition stateTransition)
45      : this() {
46      StateTransition = stateTransition;
47    }
48
49    protected override void RemoveItemEvents() {
50      StateTransition.Changed -= new EventHandler(StateTransition_Changed);
51      ConstrainedItemList request = StateTransition.SourceState.SendingData;
52      ConstrainedItemList response = StateTransition.SourceState.ReceivingData;
53
54      request.Changed -= new EventHandler(VariablesList_Changed);
55      request.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Parameter_Added);
56      request.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
57      for (int i = 0 ; i < request.Count ; i++)
58        request[i].Changed -= new EventHandler(Parameter_Changed);
59      response.Changed -= new EventHandler(VariablesList_Changed);
60      response.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Parameter_Added);
61      response.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
62      for (int i = 0 ; i < response.Count ; i++)
63        response[i].Changed -= new EventHandler(Parameter_Changed);
64      base.RemoveItemEvents();
65    }
66    protected override void AddItemEvents() {
67      base.AddItemEvents();
68      StateTransition.Changed += new EventHandler(StateTransition_Changed);
69      ConstrainedItemList request = StateTransition.SourceState.SendingData;
70      ConstrainedItemList response = StateTransition.SourceState.ReceivingData;
71
72      request.Changed += new EventHandler(VariablesList_Changed);
73      request.ItemAdded += new EventHandler<ItemIndexEventArgs>(Parameter_Added);
74      request.ItemRemoved += new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
75      for (int i = 0 ; i < request.Count ; i++)
76        request[i].Changed += new EventHandler(Parameter_Changed);
77      response.Changed += new EventHandler(VariablesList_Changed);
78      response.ItemAdded += new EventHandler<ItemIndexEventArgs>(Parameter_Added);
79      response.ItemRemoved += new EventHandler<ItemIndexEventArgs>(Parameter_Removed);
80      for (int i = 0 ; i < response.Count ; i++) {
81        response[i].Changed += new EventHandler(Parameter_Changed);
82      }
83    }
84
85    protected override void UpdateControls() {
86      base.UpdateControls();
87      if (StateTransition == null) {
88        transitionFunctionProgrammableOperatorView.Enabled = false;
89        targetStateComboBox.Enabled = false;
90        targetStateComboBox.DataSource = null;
91        targetStateComboBox.Items.Clear();
92      } else {
93        targetStateComboBox.Enabled = true;
94        transitionFunctionProgrammableOperatorView.ProgrammableOperator = StateTransition.TransitionCondition;
95        transitionFunctionProgrammableOperatorView.Enabled = true;
96        BuildTargetStateComboBox();
97        BuildVariablesListBox();
98      }
99    }
100
101    void StateTransition_Changed(object sender, EventArgs e) {
102      Refresh();
103    }
104
105    void VariablesList_Changed(object sender, EventArgs e) {
106      BuildVariablesListBox();
107    }
108
109    #region Events to synchronize changes in request/response variables to state transition
110    void Parameter_Changed(object sender, EventArgs e) {
111      BuildVariablesListBox();
112    }
113    void Parameter_Added(object sender, ItemIndexEventArgs e) {
114      ((IItem)e.Item).Changed += new EventHandler(Parameter_Changed);
115    }
116    void Parameter_Removed(object sender, ItemIndexEventArgs e) {
117      ((IItem)e.Item).Changed -= new EventHandler(Parameter_Changed);
118    }
119    #endregion
120
121    private void BuildTargetStateComboBox() {
122      ComboBox comboBox = targetStateComboBox;
123      ItemList<ProtocolState> states;
124      if (StateTransition.SourceState == null || StateTransition.SourceState.Protocol == null)
125        states = null;
126      else
127        states = StateTransition.SourceState.Protocol.States;
128      ProtocolState selected = StateTransition.TargetState;
129      ProtocolState forbidden = StateTransition.SourceState;
130
131      if (states != null && states.Count > 0) {
132        IList<ProtocolState> sourceList = new List<ProtocolState>(states.Count);
133        int selectedIndex = -1, counter = -1;
134        for (int i = 0 ; i < states.Count ; i++) {
135          if (forbidden == null || !states[i].Guid.Equals(forbidden.Guid)) {
136            sourceList.Add((ProtocolState)states[i]);
137            counter++;
138          }
139          if (selected != null && states[i].Guid.Equals(selected.Guid))
140            selectedIndex = counter;
141        }
142        comboBox.SelectedIndexChanged -= new EventHandler(targetStateComboBox_SelectedIndexChanged);
143        BindingList<ProtocolState> bl = new BindingList<ProtocolState>(sourceList);
144        comboBox.DataSource = bl;
145        comboBox.DisplayMember = "Name";
146        comboBox.ValueMember = "Guid";
147        comboBox.SelectedIndex = selectedIndex;
148        comboBox.SelectedIndexChanged += new EventHandler(targetStateComboBox_SelectedIndexChanged);
149      }
150    }
151
152    private void BuildVariablesListBox() {
153      ListBox variables = variablesListBox;
154      ConstrainedItemList request = StateTransition.SourceState.SendingData;
155      ConstrainedItemList response = StateTransition.SourceState.ReceivingData;
156      variables.Items.Clear();
157      variables.Items.Add("==Sending==");
158      for (int i = 0 ; i < request.Count ; i++) {
159        variables.Items.Add(request[i]);
160      }
161      variables.Items.Add("==Receiving==");
162      for (int i = 0 ; i < response.Count ; i++) {
163        variables.Items.Add(response[i]);
164      }
165    }
166
167    private void targetStateComboBox_SelectedIndexChanged(object sender, EventArgs e) {
168      if (targetStateComboBox.SelectedIndex >= 0) {
169        StateTransition.TargetState = (ProtocolState)targetStateComboBox.SelectedItem;
170      }
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.