Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/3.2/LogView.cs @ 3155

Last change on this file since 3155 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 5.0 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.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Logging {
34  /// <summary>
35  /// Visual representation of the <see cref="Log"/> class.
36  /// </summary>
37  public partial class LogView : ViewBase {
38    /// <summary>
39    /// Gets or sets the Log item to represent visually.
40    /// </summary>
41    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
42    /// No own data storage present.</remarks>
43    public Log Log {
44      get { return (Log)base.Item; }
45      set { base.Item = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="LogView"/>.
50    /// </summary>
51    public LogView() {
52      InitializeComponent();
53      Caption = "Log View";
54    }
55    /// <summary>
56    /// Initializes a new instance of <see cref="LogView"/> with the given <paramref name="log"/>.
57    /// </summary>
58    /// <param name="log">The log object to represent visually.</param>
59    public LogView(Log log)
60      : this() {
61      Log = log;
62    }
63
64    /// <summary>
65    /// Removes the event handlers from the underlying <see cref="Log"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
68    protected override void RemoveItemEvents() {
69      if (Log != null) {
70        Log.Items.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(Items_ItemAdded);
71        Log.Items.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(Items_ItemRemoved);
72      }
73      base.RemoveItemEvents();
74    }
75    /// <summary>
76    /// Adds event handlers to the underlying <see cref="Log"/>.
77    /// </summary>
78    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
79    protected override void AddItemEvents() {
80      base.AddItemEvents();
81      if (Log != null) {
82        Log.Items.ItemAdded += new EventHandler<EventArgs<IItem, int>>(Items_ItemAdded);
83        Log.Items.ItemRemoved += new EventHandler<EventArgs<IItem, int>>(Items_ItemRemoved);
84      }
85    }
86
87    /// <summary>
88    /// Updates all controls with the latest data of the model.
89    /// </summary>
90    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class
91    /// <see cref="UpdateControls"/>.</remarks>
92    protected override void UpdateControls() {
93      base.UpdateControls();
94      qualityLogTextBox.Clear();
95      if (Log == null) {
96        qualityLogTextBox.Enabled = false;
97      } else {
98        string[] lines = new string[Log.Items.Count];
99        for (int i = 0; i < Log.Items.Count; i++) {
100          lines[i] = Log.Items[i].ToString().Replace(';','\t');
101        }
102        qualityLogTextBox.Lines = lines;
103        qualityLogTextBox.Enabled = true;
104      }
105    }
106
107    #region ItemList Events
108    private delegate void IndexDelegate(int index);
109    private void Items_ItemRemoved(object sender, EventArgs<IItem, int> e) {
110      RemoveItem(e.Value2);
111    }
112    private void RemoveItem(int index) {
113      if (InvokeRequired)
114        Invoke(new IndexDelegate(RemoveItem), index);
115      else {
116        string[] lines = new string[qualityLogTextBox.Lines.Length - 1];
117        Array.Copy(qualityLogTextBox.Lines, 0, lines, 0, index);
118        Array.Copy(qualityLogTextBox.Lines, index + 1, lines, index, lines.Length - index);
119        qualityLogTextBox.Lines = lines;
120      }
121    }
122    private void Items_ItemAdded(object sender, EventArgs<IItem, int> e) {
123      AddItem(e.Value2);
124    }
125    private void AddItem(int index) {
126      if (InvokeRequired)
127        Invoke(new IndexDelegate(AddItem), index);
128      else {
129        string[] lines = new string[qualityLogTextBox.Lines.Length + 1];
130        Array.Copy(qualityLogTextBox.Lines, 0, lines, 0, index);
131        Array.Copy(qualityLogTextBox.Lines, index, lines, index + 1, qualityLogTextBox.Lines.Length - index);
132        lines[index] = Log.Items[index].ToString().Replace(';', '\t');
133        qualityLogTextBox.Lines = lines;
134      }
135    }
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.