- Timestamp:
- 10/29/08 16:01:41 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Communication.Data/ProtocolEditor.cs
r591 r704 39 39 public ProtocolEditor() { 40 40 InitializeComponent(); 41 statesListBox.DrawMode = DrawMode.OwnerDrawFixed; 42 statesListBox.DrawItem += new DrawItemEventHandler(statesListBox_DrawItem); 41 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 42 60 public ProtocolEditor(Protocol protocol) 43 61 : this() { … … 46 64 47 65 protected override void RemoveItemEvents() { 66 nameTextBox.DataBindings.Clear(); 48 67 Protocol.Changed -= new EventHandler(Protocol_Changed); 49 Protocol.StatesChanged -= new EventHandler(States_Changed);50 Protocol.Name.Changed -= new EventHandler(ProtocolName_Changed);51 68 base.RemoveItemEvents(); 52 69 } … … 54 71 base.AddItemEvents(); 55 72 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); 73 nameTextBox.DataBindings.Add("Text", Protocol, "Name"); 76 74 } 77 75 … … 80 78 if (Protocol == null) { 81 79 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(); 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(); 89 88 } 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; 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; 97 100 } 98 101 } 99 102 100 #region Custom events101 103 void Protocol_Changed(object sender, EventArgs e) { 102 104 Refresh(); 103 105 } 104 106 105 void States_Changed(object sender, EventArgs e) { 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 } 106 115 Refresh(); 116 statesListBox.SelectedIndex = index; 107 117 } 108 118 109 void ProtocolName_Changed(object sender, EventArgs e) { 110 Caption = Protocol.Name.Data; 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 } 111 127 } 112 #endregion113 128 114 #region ComboBox events 115 private void initialStateComboBox_SelectedIndexChanged(object sender, EventArgs e) { 116 if (initialStateComboBox.SelectedIndex >= 0) 117 Protocol.InitialState = (ProtocolState)initialStateComboBox.SelectedItem; 129 private void setAsInitialStateButton_Click(object sender, EventArgs e) { 130 if (statesListBox.SelectedIndex >= 0) { 131 Protocol.InitialState = (ProtocolState)statesListBox.SelectedItem; 132 statesListBox.Refresh(); 133 } 118 134 } 119 #endregion120 135 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; 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 } 126 151 } 127 Refresh(); 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 } 128 168 } 129 169 }
Note: See TracChangeset
for help on using the changeset viewer.