[r338]: tags / v1.0 / Tools / MVImportTool / LogPanel.cs  Maximize  Restore  History

Download this file

200 lines (160 with data), 6.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/********************************************************************
The Multiverse Platform is made available under the MIT License.
Copyright (c) 2012 The Multiverse Foundation
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace MVImportTool
{
public partial class LogPanel : UserControl
{
public ILog Log
{
get { return m_Log; }
}
RedirectedLog m_Log;
public LogPanel()
{
InitializeComponent();
m_Log = new RedirectedLog( WriteToLogPanel );
}
private void WriteToLogPanel( object sender, LogEventArgs e )
{
SetLogText( (sender as RedirectedLog).Text, e.IsHighlighted );
}
public class LogEventArgs : EventArgs
{
public bool IsHighlighted;
public LogEventArgs( bool isHighlighted )
{
IsHighlighted = isHighlighted;
}
}
public delegate void LogEventHandler( object sender, LogEventArgs e );
#region Cross-thread technique
// This is a thread-save technique for manipulating a Forms control
// [Ref. MS doc "Make Thread-Safe Calls to Windows Forms Controls"]
delegate void SetTextCallback( string text, bool isHighlighted );
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.
private void SetLogText( string text, bool isHighlighted )
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if( this.LogTextBox.InvokeRequired )
{
SetTextCallback d = new SetTextCallback( SetLogText );
this.Invoke( d, new object[] { text, isHighlighted } );
}
else
{
if( ! isHighlighted )
{
LogTextBox.Text = text;
}
else
{
// TODO: Hmm, this doesn't actually change the text color...
Color foreColor = LogTextBox.ForeColor;
Color backColor = LogTextBox.BackColor;
LogTextBox.ForeColor = Color.DarkGreen;
LogTextBox.BackColor = Color.AntiqueWhite;
LogTextBox.Text = text;
LogTextBox.ForeColor = foreColor;
LogTextBox.BackColor = backColor;
}
}
}
#endregion Cross-thread technique
/// <summary>
/// This class buffers text; it provides notification when the buffer
/// gets updated. The handler that receives the notification reads
/// the buffer from the Text property on this class.
///
/// The class provides an event handler suitable for capturing
/// asynchronous output streams from the System.Diagnostics.Process
/// class.
/// </summary>
internal class RedirectedLog : ILog
{
internal string Text
{
get { return m_LogStream.ToString(); }
}
internal RedirectedLog( LogEventHandler updateHandler )
{
m_LogStream = new StringWriter();
UpdateEvent += updateHandler;
}
#region ILog Members
public void SetHighlight( bool isHighlighted )
{
m_IsHighlighted = isHighlighted;
}
public void Clear()
{
m_LogStream.Close();
m_LogStream = new StringWriter();
Update();
}
public void Append( string text )
{
m_LogStream.Write( text );
m_LogStream.Flush();
Update();
}
public DataReceivedEventHandler DataReceivedHandler
{
get { return RedirectedDataHandler; }
}
#endregion
bool m_IsHighlighted;
StringWriter m_LogStream;
event LogEventHandler UpdateEvent;
private void Update()
{
if( null != UpdateEvent )
{
UpdateEvent.Invoke( this, new LogEventArgs( m_IsHighlighted ) );
}
}
private void RedirectedDataHandler( object sender, DataReceivedEventArgs args )
{
if( ! String.IsNullOrEmpty( args.Data ) )
{
Append( args.Data );
}
}
}
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks