Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/3.3/LogView.cs @ 3199

Last change on this file since 3199 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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