Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Data/ProtocolEditor.cs @ 1332

Last change on this file since 1332 was 704, checked in by abeham, 16 years ago

[TICKET #297] communication restructuring

File size: 6.2 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      statesListBox.DrawMode = DrawMode.OwnerDrawFixed;
42      statesListBox.DrawItem += new DrawItemEventHandler(statesListBox_DrawItem);
43    }
44
45    void statesListBox_DrawItem(object sender, DrawItemEventArgs e) {
46      if (e.Index >= 0) { // during Items.Clear() this method is called with index -1
47        ListBox lb = (ListBox)sender;
48        ProtocolState state = (ProtocolState)lb.Items[e.Index];
49        e.DrawBackground();
50        e.DrawFocusRectangle();
51        SolidBrush textColor = new SolidBrush(Color.Black);
52        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) textColor.Color = Color.White;
53        if (Protocol.InitialState.Equals(state))
54          e.Graphics.DrawString(state.Name, new Font(e.Font.FontFamily, e.Font.Size, FontStyle.Bold), textColor, e.Bounds);
55        else
56          e.Graphics.DrawString(state.Name, e.Font, textColor, e.Bounds);
57      }
58    }
59
60    public ProtocolEditor(Protocol protocol)
61      : this() {
62      Protocol = protocol;
63    }
64
65    protected override void RemoveItemEvents() {
66      nameTextBox.DataBindings.Clear();
67      Protocol.Changed -= new EventHandler(Protocol_Changed);
68      base.RemoveItemEvents();
69    }
70    protected override void AddItemEvents() {
71      base.AddItemEvents();
72      Protocol.Changed += new EventHandler(Protocol_Changed);
73      nameTextBox.DataBindings.Add("Text", Protocol, "Name");
74    }
75
76    protected override void UpdateControls() {
77      base.UpdateControls();
78      if (Protocol == null) {
79        Caption = "Protocol";
80        nameTextBox.Enabled = false;
81        setAsInitialStateButton.Enabled = false;
82        addStateButton.Enabled = false;
83        removeStateButton.Enabled = false;
84        statesListBox.Enabled = false;
85
86        nameTextBox.Text = "";
87        statesListBox.Items.Clear();
88      } else {
89        Caption = Protocol.Name;
90
91        statesListBox.Items.Clear();
92        foreach (ProtocolState state in Protocol.States)
93          statesListBox.Items.Add(state);
94
95        statesListBox.Enabled = true;
96        addStateButton.Enabled = true;
97        removeStateButton.Enabled = true;
98        setAsInitialStateButton.Enabled = true;
99        nameTextBox.Enabled = true;
100      }
101    }
102
103    void Protocol_Changed(object sender, EventArgs e) {
104      Refresh();
105    }
106
107    private void addStateButton_Click(object sender, EventArgs e) {
108      ProtocolState tmp = new ProtocolState();
109      int index = statesListBox.SelectedIndex;
110      if (index < 0) {
111        Protocol.States.Add(tmp);
112      } else {
113        Protocol.States.Insert(index, tmp);
114      }
115      Refresh();
116      statesListBox.SelectedIndex = index;
117    }
118
119    private void removeStateButton_Click(object sender, EventArgs e) {
120      if (statesListBox.SelectedIndex >= 0) {
121        int index = statesListBox.SelectedIndex;
122        Protocol.States.RemoveAt(statesListBox.SelectedIndex);
123        Refresh();
124        if (Protocol.States.Count > 0)
125          statesListBox.SelectedIndex = ((index < Protocol.States.Count) ? (index) : (Protocol.States.Count - 1));
126      }
127    }
128
129    private void setAsInitialStateButton_Click(object sender, EventArgs e) {
130      if (statesListBox.SelectedIndex >= 0) {
131        Protocol.InitialState = (ProtocolState)statesListBox.SelectedItem;
132        statesListBox.Refresh();
133      }
134    }
135
136    private void statesListBox_DoubleClick(object sender, EventArgs e) {
137      if (lastDeselectedIndex >= 0 || lastSelectedIndex >= 0) {
138        statesListBox.SelectedIndex = (lastDeselectedIndex >= 0) ? (lastDeselectedIndex) : (lastSelectedIndex);
139        ProtocolState selected = (ProtocolState)statesListBox.Items[(lastDeselectedIndex >= 0) ? (lastDeselectedIndex) : (lastSelectedIndex)];
140        bool editingInitial = (Protocol.InitialState == selected);
141        ProtocolState selectedClone = (ProtocolState)selected.Clone(new Dictionary<Guid, object>());
142        IView stateView = selectedClone.CreateView();
143        using (WindowedView display = new WindowedView(stateView as UserControl)) {
144          display.ShowDialog(this);
145          if (display.DialogResult == DialogResult.OK) {
146            Protocol.States[(lastDeselectedIndex >= 0) ? (lastDeselectedIndex) : (lastSelectedIndex)] = selectedClone;
147            if (editingInitial) Protocol.InitialState = selectedClone;
148            Refresh();
149          }
150        }
151      }
152    }
153
154    private int lastSelectedIndex = -1;
155    private int lastDeselectedIndex = -1;
156
157    private void statesListBox_SelectedIndexChanged(object sender, EventArgs e) {
158      if (statesListBox.SelectedIndex >= 0) {
159        if (lastSelectedIndex == statesListBox.SelectedIndex) {
160          lastDeselectedIndex = statesListBox.SelectedIndex;
161          statesListBox.SelectedIndex = -1;
162          lastSelectedIndex = -1;
163        } else {
164          lastSelectedIndex = statesListBox.SelectedIndex;
165          lastDeselectedIndex = -1;
166        }
167      }
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.