Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Communication.Data/ProtocolEditor.cs @ 15160

Last change on this file since 15160 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: 4.8 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.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31
32namespace HeuristicLab.Communication.Data {
33  public partial class ProtocolEditor : EditorBase {
34    public Protocol Protocol {
35      get { return (Protocol)Item; }
36      set { base.Item = value; }
37    }
38
39    public ProtocolEditor() {
40      InitializeComponent();
41    }
42    public ProtocolEditor(Protocol protocol)
43      : this() {
44      Protocol = protocol;
45    }
46
47    protected override void RemoveItemEvents() {
48      Protocol.Changed -= new EventHandler(Protocol_Changed);
49      Protocol.StatesChanged -= new EventHandler(States_Changed);
50      Protocol.Name.Changed -= new EventHandler(ProtocolName_Changed);
51      base.RemoveItemEvents();
52    }
53    protected override void AddItemEvents() {
54      base.AddItemEvents();
55      Protocol.Changed += new EventHandler(Protocol_Changed);
56      Protocol.StatesChanged += new EventHandler(States_Changed);
57      Protocol.Name.Changed += new EventHandler(ProtocolName_Changed);
58    }
59
60    private void BuildInitialStateComboBox() {
61      // Rebuild the two ComboBoxes depicting the initial and final state
62      IList<ProtocolState> states = new List<ProtocolState>(Protocol.States.Count);
63      int initialSelectedIndex = -1;
64      for (int i = 0 ; i < Protocol.States.Count ; i++) {
65        states.Add((ProtocolState)Protocol.States[i]);
66        if (Protocol.States[i].Guid.Equals(Protocol.InitialState.Guid))
67          initialSelectedIndex = i;
68      }
69      initialStateComboBox.SelectedIndexChanged -= new EventHandler(initialStateComboBox_SelectedIndexChanged);
70      BindingList<ProtocolState> bl = new BindingList<ProtocolState>(states);
71      initialStateComboBox.DataSource = bl;
72      initialStateComboBox.DisplayMember = "Name";
73      initialStateComboBox.ValueMember = "Guid";
74      initialStateComboBox.SelectedIndex = initialSelectedIndex;
75      initialStateComboBox.SelectedIndexChanged += new EventHandler(initialStateComboBox_SelectedIndexChanged);
76    }
77
78    protected override void UpdateControls() {
79      base.UpdateControls();
80      if (Protocol == null) {
81        Caption = "Protocol";
82        nameViewControl.Enabled = false;
83        nameViewControl.StringData = null;
84        statesItemListView.Enabled = false;
85        statesItemListView.ItemList = null;
86        initialStateComboBox.Enabled = false;
87        initialStateComboBox.DataSource = null;
88        initialStateComboBox.Items.Clear();
89      } else {
90        Caption = Protocol.Name.Data;
91        nameViewControl.StringData = Protocol.Name;
92        nameViewControl.Enabled = true;
93        statesItemListView.ItemList = Protocol.States;
94        statesItemListView.Enabled = true;
95        BuildInitialStateComboBox();
96        initialStateComboBox.Enabled = true;
97      }
98    }
99
100    #region Custom events
101    void Protocol_Changed(object sender, EventArgs e) {
102      Refresh();
103    }
104
105    void States_Changed(object sender, EventArgs e) {
106      Refresh();
107    }
108
109    void ProtocolName_Changed(object sender, EventArgs e) {
110      Caption = Protocol.Name.Data;
111    }
112    #endregion
113
114    #region ComboBox events
115    private void initialStateComboBox_SelectedIndexChanged(object sender, EventArgs e) {
116      if (initialStateComboBox.SelectedIndex >= 0)
117        Protocol.InitialState = (ProtocolState)initialStateComboBox.SelectedItem;
118    }
119    #endregion
120
121    private void invertButton_Click(object sender, EventArgs e) {
122      for (int i = 0 ; i < Protocol.States.Count ; i++) {
123        ConstrainedItemList tmp = ((ProtocolState)Protocol.States[i]).SendingData;
124        ((ProtocolState)Protocol.States[i]).SendingData = ((ProtocolState)Protocol.States[i]).ReceivingData;
125        ((ProtocolState)Protocol.States[i]).ReceivingData = tmp;
126      }
127      Refresh();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.