// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net)
// All rights reserved.
//
// This file is part of Regionerate.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System;
using EnvDTE;
using EnvDTE80;
using Rauchy.Regionerate.Shared.Components;
namespace Rauchy.Regionerate.Presentation.Addins.VisualStudio2005
{
///
/// Interacts the application's user interface with the class.
///
internal static class OutputMonitor
{
#region Fields (3)
private static bool _connected;
private static OutputWindowPane _outputWindow;
private static StatusBar _statusBar;
#endregion Fields
#region Methods (5)
// Public Methods (2)
// [rgn] Public Methods (2)
///
/// Connects to the application.
///
/// The application.
public static void ConnectTo( DTE2 application )
{
if ( !_connected )
{
_connected = true;
// Get the Output window
Window win = application.Windows.Item( Constants.vsWindowKindOutput );
OutputWindow ow = ( OutputWindow )win.Object;
if ( ow.OutputWindowPanes.Count > 0 )
{
_outputWindow = ow.OutputWindowPanes.Item( 1 );
_outputWindow.Activate();
}
// Get the Status Bar
_statusBar = application.StatusBar;
// Register for Output events
Output.OnInformationSubmitted += Output_OnInformationSubmitted;
Output.OnWarningSubmitted += Output_OnWarningSubmitted;
Output.OnProgressChanged += Output_OnProgressChanged;
}
else
{
throw new InvalidOperationException( "The output monitor is already connected, please disconnect first" );
}
}
///
/// Disconnects from an application.
///
public static void Disconnect()
{
if ( _connected )
{
_outputWindow = null;
_statusBar = null;
Output.OnInformationSubmitted -= Output_OnInformationSubmitted;
Output.OnWarningSubmitted -= Output_OnWarningSubmitted;
Output.OnProgressChanged -= Output_OnProgressChanged;
}
else
{
throw new InvalidOperationException( "The output monitor is disconnected, please connect first" );
}
}
// Private Methods (3)
// [rgn] Private Methods (3)
///
/// Occurs when new information is submitted.
///
/// The new information text.
private static void Output_OnInformationSubmitted( string text )
{
string formattedText = string.Format( "[{0}] Regionerate: Information: {1}.{2}",
DateTime.Now.ToLongTimeString(), text, Environment.NewLine );
_outputWindow.OutputString( formattedText );
_statusBar.Text = formattedText;
}
///
/// Occurs when an operation's progress is increased.
///
/// The current progress value.
/// The maximum value that represents a complete operation.
private static void Output_OnProgressChanged( long value, long targetValue )
{
int value32 = ( int )( value < Int32.MaxValue ? value : Int32.MaxValue );
int targetValue32 = ( int )( targetValue < Int32.MaxValue ? targetValue : Int32.MaxValue );
_statusBar.Progress( true, _statusBar.Text, value32, targetValue32 );
}
///
/// Occurs when a new warning is submitted.
///
/// The new warning text.
/// The level of warning (critical, non-critical etc.)
private static void Output_OnWarningSubmitted( string text, Output.WarningLevel level )
{
string formattedText = string.Format( "[{0}] Regionerate: {1} warning: {2}.{3}",
DateTime.Now.ToLongTimeString(), level, text, Environment.NewLine );
_outputWindow.OutputString( formattedText );
_statusBar.Text = formattedText;
}
#endregion Methods
}
}