Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Communication.Data/ProtocolStateView.cs @ 3494

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

Put the GPL license in the files from the communication framework and simulation optimization project

File size: 7.4 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 ProtocolStateView : ViewBase {
34    public ProtocolState ProtocolState {
35      get { return (ProtocolState)Item; }
36      set { base.Item = value; }
37    }
38
39    public ProtocolStateView() {
40      InitializeComponent();
41    }
42    public ProtocolStateView(ProtocolState protocolState)
43      : this() {
44      ProtocolState = protocolState;
45    }
46
47    protected override void RemoveItemEvents() {
48      ProtocolState.Changed -= new EventHandler(ProtocolState_Changed);
49      ProtocolState.Protocol.StatesChanged -= new EventHandler(States_Changed);
50      if (ProtocolState.StateTransitions != null) {
51        for (int i = 0 ; i < ProtocolState.StateTransitions.Count ; i++) {
52          ProtocolState.StateTransitions[i].Changed -= new EventHandler(StateTransitions_Changed);
53        }
54      }
55      base.RemoveItemEvents();
56    }
57    protected override void AddItemEvents() {
58      base.AddItemEvents();
59      ProtocolState.Changed += new EventHandler(ProtocolState_Changed);
60      ProtocolState.Protocol.StatesChanged += new EventHandler(States_Changed);
61      if (ProtocolState.StateTransitions != null) {
62        for (int i = 0 ; i < ProtocolState.StateTransitions.Count ; i++) {
63          ProtocolState.StateTransitions[i].Changed += new EventHandler(StateTransitions_Changed);
64        }
65      }
66    }
67
68    protected override void UpdateControls() {
69      base.UpdateControls();
70      if (ProtocolState == null) {
71        nameStringDataView.Enabled = false;
72        nameStringDataView.StringData = null;
73        acceptingStateBoolDataView.Enabled = false;
74        acceptingStateBoolDataView.BoolData = null;
75        outboundCommunicationDataView.Enabled = false;
76        outboundCommunicationDataView.ConstrainedItemList = null;
77        inboundCommunicationDataView.Enabled = false;
78        inboundCommunicationDataView.ConstrainedItemList = null;
79        stateTransitionTabControl.Controls.Clear();
80      } else {
81        nameStringDataView.StringData = ProtocolState.Name;
82        nameStringDataView.Enabled = true;
83        acceptingStateBoolDataView.Enabled = true;
84        acceptingStateBoolDataView.BoolData = ProtocolState.AcceptingState;
85        outboundCommunicationDataView.ConstrainedItemList = ProtocolState.SendingData;
86        outboundCommunicationDataView.Enabled = true;
87        inboundCommunicationDataView.ConstrainedItemList = ProtocolState.ReceivingData;
88        inboundCommunicationDataView.Enabled = true;
89
90        addTransitionButton.Enabled = (ProtocolState.Protocol.States.Count > 1);
91        removeTransitionButton.Enabled = (ProtocolState.StateTransitions != null && ProtocolState.StateTransitions.Count > 0);
92
93        stateTransitionTabControl.Controls.Clear();
94        ItemList<StateTransition> stateTransitions = ProtocolState.StateTransitions;
95        if (stateTransitions != null) {
96          for (int i = 0 ; i < stateTransitions.Count ; i++) {
97            StateTransition stateTransition = (StateTransition)stateTransitions[i];
98            TabPage newTransitionTabPage = new TabPage((stateTransition.TargetState == null) ? ("no target") : (stateTransition.TargetState.Name.ToString()));
99            Control stateTransitionView = (Control)stateTransition.CreateView();
100            stateTransitionTabControl.Controls.Add(newTransitionTabPage);
101
102            newTransitionTabPage.Controls.Add(stateTransitionView);
103            newTransitionTabPage.Location = new Point(4, 22);
104            newTransitionTabPage.Padding = new Padding(3);
105            newTransitionTabPage.Size = stateTransitionTabControl.ClientSize;
106            newTransitionTabPage.UseVisualStyleBackColor = true;
107
108            stateTransitionView.Location = new Point(0, 0);
109            stateTransitionView.Size = newTransitionTabPage.ClientSize;
110            stateTransitionView.Dock = DockStyle.Fill;
111            stateTransitionView.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
112          }
113        }
114      }
115    }
116
117    #region ProtocolState change handling (main underlying object)
118    void ProtocolState_Changed(object sender, EventArgs e) {
119      Refresh();
120    }
121    #endregion
122
123    #region Protocol.States change handling (parent object)
124    void States_Changed(object sender, EventArgs e) {
125      for (int i = 0 ; i < stateTransitionTabControl.TabCount ; i++)
126        stateTransitionTabControl.TabPages[i].Controls[0].Refresh();
127      addTransitionButton.Enabled = (ProtocolState.Protocol.States.Count > 1);
128    }
129    #endregion
130
131    #region StateTransitions change handling (child object)
132    void StateTransitions_Changed(object sender, EventArgs e) {
133      for (int i = 0 ; i < stateTransitionTabControl.TabCount ; i++) {
134        if (((StateTransition)ProtocolState.StateTransitions[i]).TargetState != null)
135          stateTransitionTabControl.TabPages[i].Text = ((StateTransition)ProtocolState.StateTransitions[i]).TargetState.Name.ToString();
136      }
137    }
138    #endregion
139
140    #region Button events
141    private void addTransitionButton_Click(object sender, EventArgs e) {
142      //ProtocolState.AcceptingState = new BoolData(false);
143      StateTransition stateTransition = new StateTransition();
144      stateTransition.Changed += new EventHandler(StateTransitions_Changed);
145      stateTransition.SourceState = ProtocolState;
146
147      if (ProtocolState.StateTransitions == null) {
148        ItemList<StateTransition> temp = new ItemList<StateTransition>();
149        temp.Add(stateTransition);
150        ProtocolState.StateTransitions = temp;
151      } else {
152        ProtocolState.StateTransitions.Add(stateTransition);
153        Refresh();
154      }
155      removeTransitionButton.Enabled = true;
156    }
157
158    private void removeTransitionButton_Click(object sender, EventArgs e) {
159      if (stateTransitionTabControl.TabCount > 0) {
160        int selIdx = stateTransitionTabControl.SelectedIndex;
161        ProtocolState.StateTransitions[selIdx].Changed -= new EventHandler(StateTransitions_Changed);
162        stateTransitionTabControl.Controls.RemoveAt(selIdx);
163        ProtocolState.StateTransitions.RemoveAt(selIdx);
164        if (ProtocolState.StateTransitions.Count == 0) {
165          //ProtocolState.AcceptingState = new BoolData(true);
166          ProtocolState.StateTransitions = null;
167          removeTransitionButton.Enabled = false;
168        }
169      }
170    }
171    #endregion
172  }
173}
Note: See TracBrowser for help on using the repository browser.