www.pudn.com > OracleServiceManager.zip > DialogObserver.cs


using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Drawing; 
using System.IO; 
using System.Net; 
using System.Resources; 
using System.ServiceProcess; 
using System.Windows.Forms; 
using kae.ServiceStatePublisher; 
 
namespace kae.OracleServiceManager 
{ 
	///  
	/// DialogObserver is a concrete observer listening to the current subject for changes 
	/// DialogObserver manages the dialog interface of OSM 
	///  
	public class DialogObserver : Form 
	{ 
		protected delegate void UpdateFunction( ServiceSubject subject); 
		protected delegate void ServiceAction(); 
 
		private readonly string START; 
		private readonly string PAUSE; 
		private readonly string STOP; 
 
		private OsmResources _resources; 
		private IContainer components; 
		private Label serverLabel; 
		private ComboBox serverCombo; 
		private Label serviceLabel; 
		private ComboBox serviceCombo; 
		private Button refreshButton; 
		private PictureBox statePicture; 
		private Label startServiceLabel; 
		private Button startServiceButton; 
		private Label pauseServiceLabel; 
		private Button pauseServiceButton; 
		private Label stopServiceLabel; 
		private Button stopServiceButton; 
		private StatusBar statusBar; 
		private bool _updating; 
		private UpdateFunction serverChanged; 
		private UpdateFunction serviceChanged; 
		private UpdateFunction serviceStateChanged; 
		private ServiceAction startService; 
		private ServiceAction pauseService; 
		private ServiceAction stopService; 
 
		public DialogObserver( ServiceSubject subject) 
		{ 
			if (subject != null) 
			{ 
				subject.ServerChanged += new EventHandler( this.OnServerChanged); 
				subject.ServiceChanged += new EventHandler( this.OnServiceChanged); 
				subject.StateChanged += new EventHandler( this.OnStateChanged); 
			} 
 
			serverChanged = new UpdateFunction( this.ServerChanged); 
			serviceChanged = new UpdateFunction( this.ServiceChanged); 
			serviceStateChanged = new UpdateFunction( this.ServiceStateChanged); 
 
			startService = new ServiceAction( App.Subject.Start); 
			pauseService = new ServiceAction( App.Subject.Pause); 
			stopService = new ServiceAction( App.Subject.Stop); 
 
			_resources = App.Resources; 
			START = GetAppString( "Start"); 
			PAUSE = GetAppString( "Pause"); 
			STOP = GetAppString( "Stop"); 
 
			InitializeComponent(); 
		} 
 
		///  
		/// Clean up any resources being used. 
		///  
		protected override void Dispose( bool disposing) 
		{ 
			if (disposing) 
			{ 
				if (components != null) 
					components.Dispose(); 
			} 
			base.Dispose( disposing); 
		} 
 
		protected OracleServiceManager App 
		{ 
			get { return OracleServiceManager.Instance; } 
		} 
 
		protected bool Updating 
		{ 
			get { return _updating; } 
			set { _updating = value; } 
		} 
 
		internal ISynchronizeInvoke SynchronizingObject 
		{ 
			get { return serviceCombo; } 
		} 
 
		#region Manually generated Form code 
		private void InitializeComponent() 
		{ 
			const int _leftBorder = 5; 
			const int _rightBorder = 5; 
			const int _topBorder = 5; 
			const int BUTTON_WIDTH = 25; 
			const int BUTTON_HEIGHT = 25; 
			int oneThirdWidth, top = _topBorder; 
			int labelTop; 
 
			components = new System.ComponentModel.Container(); 
			serverLabel = new Label(); 
			serverCombo = new ComboBox(); 
			serviceLabel = new Label(); 
			serviceCombo = new ComboBox(); 
			refreshButton = new Button(); 
			startServiceButton = new Button(); 
			startServiceLabel = new Label(); 
			pauseServiceButton = new Button(); 
			pauseServiceLabel = new Label(); 
			stopServiceButton = new Button(); 
			stopServiceLabel = new Label(); 
			statePicture = new PictureBox(); 
			statusBar = new StatusBar(); 
			SuspendLayout(); 
			//  
			// DialogObserver 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
			this.ClientSize = new System.Drawing.Size(286, 230); 
			this.Controls.AddRange( new Control[]  
				{ 
					serverLabel, serverCombo, serviceLabel, serviceCombo, refreshButton, 
					startServiceButton, startServiceLabel, pauseServiceButton, 
					pauseServiceLabel, stopServiceButton, stopServiceLabel, 
					statePicture, statusBar 
				}); 
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 
			this.MaximizeBox = false; 
			this.Name = "DialogObserver"; 
			this.Text = _resources.ApplicationName; 
			this.Icon = GetSmallStateImage( ServiceStateUnknown.Instance); 
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; 
			this.Closing += new System.ComponentModel.CancelEventHandler( this.ClosingDialog); 
			oneThirdWidth = ClientSize.Width - _leftBorder - _rightBorder; 
			oneThirdWidth /= 3; 
			// 
			// serverCombo 
			// 
			serverCombo.Name = "serverCombo"; 
			serverCombo.TabIndex = 2; 
			serverCombo.Location = new System.Drawing.Point( oneThirdWidth + _leftBorder, top); 
			serverCombo.Width = oneThirdWidth * 2; 
			serverCombo.CausesValidation = true; 
			serverCombo.Validating += new CancelEventHandler( this.ServerComboValidating); 
			// 
			// serverLabel 
			// 
			serverLabel.Name = "serverLabel"; 
			serverLabel.AutoSize = true; 
			serverLabel.UseMnemonic = true; 
			serverLabel.TabIndex = 1; 
			serverLabel.Text = GetStringFromResources( "Server"); 
			labelTop = top + ((serverCombo.Height / 2) - (serverLabel.Height / 2)); 
			serverLabel.Location = new System.Drawing.Point( _leftBorder, labelTop); 
			top += serverCombo.Height + _topBorder; 
			// 
			// serviceCombo 
			// 
			serviceCombo.Name = "serviceCombo"; 
			serviceCombo.TabIndex = 4; 
			serviceCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
			serviceCombo.Location = new System.Drawing.Point( oneThirdWidth + _leftBorder, top); 
			serviceCombo.Width = oneThirdWidth * 2; 
			serviceCombo.SelectedIndexChanged += new EventHandler( this.ServiceComboChangedIndex); 
			// 
			// serviceLabel 
			// 
			serviceLabel.Name = "serviceLabel"; 
			serviceLabel.AutoSize = true; 
			serviceLabel.UseMnemonic = true; 
			serviceLabel.TabIndex = 3; 
			serviceLabel.Text = GetStringFromResources( "Service"); 
			labelTop = top + ((serviceCombo.Height / 2) - (serviceLabel.Height / 2)); 
			serviceLabel.Location = new System.Drawing.Point( _leftBorder, labelTop); 
			top += serviceCombo.Height + _topBorder; 
			// 
			// refreshButton 
			// 
			refreshButton.Name = "refreshButton"; 
			refreshButton.TabIndex = 5; 
			refreshButton.Text = GetStringFromResources( "RefreshServices"); 
			refreshButton.Location = new System.Drawing.Point( oneThirdWidth + _leftBorder, top); 
			refreshButton.Width = serviceCombo.Width; 
			refreshButton.Click += new EventHandler( this.RefreshClick); 
			top += refreshButton.Height + (3 * _topBorder); 
			// 
			// startServiceButton 
			// 
			startServiceButton.Name = "startServiceButton"; 
			startServiceButton.TabIndex = 7; 
			startServiceButton.Image = Image.FromStream( GetActionImage( "Start", false)); 
			startServiceButton.Size = new System.Drawing.Size( BUTTON_WIDTH, BUTTON_HEIGHT); 
			startServiceButton.Location = new System.Drawing.Point( oneThirdWidth + _leftBorder, top); 
			startServiceButton.Click += new EventHandler( this.StartClick); 
			top += startServiceButton.Height + (2 * _topBorder); 
			// 
			// startServiceLabel 
			// 
			startServiceLabel.Name = "startServiceLabel"; 
			startServiceLabel.AutoSize = true; 
			startServiceLabel.UseMnemonic = true; 
			startServiceLabel.TabIndex = 6; 
			startServiceLabel.Text = GetStringFromResources( "StartContinue"); 
			labelTop = startServiceButton.Top + ((startServiceButton.Height / 2) - (startServiceLabel.Height / 2)); 
			startServiceLabel.Location = new System.Drawing.Point(  
				startServiceButton.Right + (2 * _leftBorder), 
				labelTop); 
			// 
			// pauseServiceButton 
			// 
			pauseServiceButton.Name = "pauseServiceButton"; 
			pauseServiceButton.TabIndex = 9; 
			pauseServiceButton.Image = Image.FromStream( GetActionImage( "Pause", false)); 
			pauseServiceButton.Size = new System.Drawing.Size( BUTTON_WIDTH, BUTTON_HEIGHT); 
			pauseServiceButton.Location = new System.Drawing.Point( oneThirdWidth + _leftBorder, top); 
			pauseServiceButton.Click += new EventHandler( this.PauseClick); 
			top += pauseServiceButton.Height + (2 * _topBorder); 
			// 
			// pauseServiceLabel 
			// 
			pauseServiceLabel.Name = "pauseServiceLabel"; 
			pauseServiceLabel.AutoSize = true; 
			pauseServiceLabel.UseMnemonic = true; 
			pauseServiceLabel.TabIndex = 8; 
			pauseServiceLabel.Text = GetStringFromResources( "Pause"); 
			labelTop = pauseServiceButton.Top + ((pauseServiceButton.Height / 2) - (pauseServiceLabel.Height / 2)); 
			pauseServiceLabel.Location = new System.Drawing.Point(  
				pauseServiceButton.Right + (2 * _leftBorder),  
				labelTop); 
			// 
			// stopServiceButton 
			// 
			stopServiceButton.Name = "stopServiceButton"; 
			stopServiceButton.TabIndex = 11; 
			stopServiceButton.Image = Image.FromStream( GetActionImage( "Stop", false)); 
			stopServiceButton.Size = new System.Drawing.Size( BUTTON_WIDTH, BUTTON_HEIGHT); 
			stopServiceButton.Location = new System.Drawing.Point( oneThirdWidth + _leftBorder, top); 
			stopServiceButton.Click += new EventHandler( this.StopClick); 
			top += stopServiceButton.Height + _topBorder; 
			// 
			// stopServiceLabel 
			// 
			stopServiceLabel.Name = "stopServiceLabel"; 
			stopServiceLabel.AutoSize = true; 
			stopServiceLabel.UseMnemonic = true; 
			stopServiceLabel.TabIndex = 10; 
			stopServiceLabel.Text = GetStringFromResources( "Stop"); 
			labelTop = stopServiceButton.Top + ((stopServiceButton.Height / 2) - (stopServiceLabel.Height / 2)); 
			stopServiceLabel.Location = new System.Drawing.Point(  
				stopServiceButton.Right + (2 * _leftBorder),  
				labelTop); 
			// 
			// statePicture 
			// 
			System.Drawing.Size pictureSize = new System.Drawing.Size( 48, 48); 
			int leftCenter = (serviceCombo.Left / 2) - (pictureSize.Width / 2); 
			int topCenter = ((startServiceButton.Top + stopServiceButton.Bottom) / 2) - (pictureSize.Height / 2); 
			statePicture.Name = "statePicture";  
			statePicture.Location = new System.Drawing.Point( leftCenter, topCenter); 
			statePicture.SizeMode = PictureBoxSizeMode.StretchImage; 
			statePicture.Size = pictureSize; 
			statePicture.Image = Image.FromStream( GetLargeStateImage( ServiceStateUnknown.Instance)); 
			// 
			// statusBar 
			// 
			statusBar.Name = "statusBar"; 
			statusBar.Anchor = System.Windows.Forms.AnchorStyles.Bottom; 
			statusBar.Dock = System.Windows.Forms.DockStyle.Bottom; 
			statusBar.SizingGrip = false; 
			statusBar.ShowPanels = true; 
			statusBar.Panels.Add( new StatusBarPanel()); 
			statusBar.Panels[0].Text = GetStringFromResources( "Ready"); 
			statusBar.Panels[0].BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.Sunken; 
			statusBar.Panels[0].AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring; 
 
			// The form should now process the layout directions 
			ResumeLayout( false); 
		} 
		#endregion 
		 
		#region Implementation of Listener/Observer 
		protected void OnServerChanged( object sender, System.EventArgs e) 
		{ 
			ServiceSubject subject = (ServiceSubject) sender; 
			Update( serverChanged, subject); 
		} 
 
		protected void OnServiceChanged( object sender, System.EventArgs e) 
		{ 
			ServiceSubject subject = (ServiceSubject) sender; 
			Update( serviceChanged, subject); 
		} 
 
		protected void OnStateChanged( object sender, System.EventArgs e) 
		{ 
			ServiceSubject subject = (ServiceSubject) sender; 
			Update( serviceStateChanged, subject); 
		} 
 
		private void Update( UpdateFunction function, ServiceSubject subject) 
		{ 
			if (!Updating) 
			{ 
				Updating = true; 
 
				function( subject); 
 
				Updating = false; 
			} 
		} 
 
		private void ServerChanged( ServiceSubject subject) 
		{ 
			ClearServiceItems(); 
 
			if (subject.ServerName != null) 
			{ 
				AddServer( subject.ServerName); 
				SetServerItem( subject.ServerName); 
				SetStatusBarText( GetStringFromResources( "Connected"), subject.ServerName); 
				AddOracleServicesToList( subject.ServerName); 
			} 
			else 
			{ 
				if (serverCombo.Text.Length > 0) 
					SetStatusBarText( GetStringFromResources( "NotConnected"), serverCombo.Text); 
				else 
					SetStatusBarText( GetStringFromResources( "Ready")); 
			} 
		} 
 
		private void ServiceChanged( ServiceSubject subject) 
		{ 
			if (subject.Controller != null) 
			{ 
				SetServiceItem( subject.Controller.DisplayName); 
				ServiceStateChanged( subject); 
			} 
			else 
			{ 
				DisableActionButtons(); 
				SetDefaultServiceStateImage(); 
			} 
		} 
 
		private void ServiceStateChanged( ServiceSubject subject) 
		{ 
			if (subject.State != ServiceStateUnknown.Instance) 
			{ 
				SetActionButtonState( subject); 
				SetServiceStateImage( subject.State); 
				SetStatusBarText( subject.State.Status, subject.ServerName, subject.Controller.ServiceName); 
			} 
			else 
			{ 
				DisableActionButtons(); 
				SetDefaultServiceStateImage(); 
			} 
		} 
		#endregion 
 
		#region User action implementation 
		private void ClosingDialog( object sender, CancelEventArgs e) 
		{ 
			if (sender == this) 
			{ 
				e.Cancel = true; 
				this.Hide(); 
			} 
		} 
 
		private void ServerComboValidating( object sender, CancelEventArgs e) 
		{ 
			string serverName = serverCombo.Text.ToUpper(); 
			ServiceSubject subject = App.Subject; 
 
			if (subject.ServerName != serverName) 
			{ 
				SetStatusBarText( GetStringFromResources( "Connecting"), serverName); 
				if (IsServerValid( serverName)) 
				{ 
					subject.ServerName = serverName; 
				} 
				else 
				{ 
					string msg = GetStringFromResources( "NoNetwork"); 
					MessageBox.Show( this, msg, App.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
					subject.ServerName = null; 
					e.Cancel = true; 
				} 
			} 
		} 
 
		private void ServiceComboChangedIndex( object sender, EventArgs e) 
		{ 
			ServiceSubject subject = App.Subject; 
 
			subject.Controller = new ServiceController( serviceCombo.Text, subject.ServerName); 
		} 
 
		private void RefreshClick( object sender, EventArgs e) 
		{ 
			App.Subject.RefreshServices(); 
		} 
 
		private void StartClick( object sender, EventArgs e) 
		{ 
			ActionClick( startService, START); 
		} 
 
		private void PauseClick( object sender, EventArgs e) 
		{ 
			ActionClick( pauseService, PAUSE); 
		} 
 
		private void StopClick( object sender, EventArgs e) 
		{ 
			ActionClick( stopService, STOP); 
		} 
 
		private void ActionClick( ServiceAction action, string actionName) 
		{ 
			Cursor.Current = Cursors.WaitCursor; 
			if (VerifyUserAction( actionName)) 
			{ 
				this.Enabled = false; 
				action(); 
				this.Enabled = true; 
			} 
			Cursor.Current = Cursors.Default; 
		} 
		#endregion 
 
		#region Dialog support functions 
		private void SetStatusBarText( string status) 
		{ 
			statusBar.Panels[0].Text = status; 
		} 
 
		private void SetStatusBarText( string status, string machineName) 
		{ 
			statusBar.Panels[0].Text = String.Format( @"{0} - \\{1}", 
				status, machineName); 
		} 
 
		private void SetStatusBarText( string status, string machineName, string serviceName) 
		{ 
			statusBar.Panels[0].Text = String.Format( @"{0} - \\{1} - {2}", 
				status, machineName, serviceName); 
		} 
 
		private bool IsServerValid( string serverName) 
		{ 
			bool isValid = true; 
 
			try 
			{ 
				if (SyntaxCheck.CheckMachineName( serverName)) 
				{ 
					IPHostEntry hostEntry = Dns.Resolve( serverName); 
					ServiceController[] services = ServiceController.GetServices( serverName); 
				} 
				else 
				{ 
					isValid = false; 
				} 
			} 
			catch (Exception) 
			{ 
				isValid = false; 
			} 
 
			return isValid; 
		} 
 
		private void AddServer( string serverName) 
		{ 
			if (!serverCombo.Items.Contains( serverName)) 
				serverCombo.Items.Add( serverName); 
		} 
 
		private void SetServerItem( string serverName) 
		{ 
			if (serverCombo.Items.Contains( serverName) &&  
				serverCombo.Text != serverName) 
			{ 
				int index = serverCombo.Items.IndexOf( serverName); 
				serverCombo.SelectedIndex = index; 
			} 
		} 
 
		private void ClearServiceItems() 
		{ 
			serviceCombo.Items.Clear(); 
		} 
 
		private void AddOracleServicesToList( string serverName) 
		{ 
			float width = serviceCombo.Width; 
			Graphics g = Graphics.FromHwnd( this.Handle); 
 
			ServiceController[] services = ServiceController.GetServices( serverName); 
			foreach (ServiceController service in services) 
			{ 
				// HACK: Modify to better allow localized application + monitor different databases 
				if (service.DisplayName.StartsWith( "Oracle")) 
				{ 
					serviceCombo.Items.Add( service.DisplayName); 
					SizeF size = g.MeasureString( service.DisplayName, serviceCombo.Font); 
					if (width < size.Width) 
						width = size.Width; 
				} 
			} 
 
			serviceCombo.DropDownWidth = Convert.ToInt32( width) + 7; 
		} 
 
		private void SetServiceItem( string serviceName) 
		{ 
			int index = serviceCombo.FindStringExact( serviceName); 
			serviceCombo.SelectedIndex = index; 
		} 
 
		private void DisableActionButtons() 
		{ 
			SetActionItemState( START, startServiceButton, startServiceLabel, false); 
			SetActionItemState( PAUSE, pauseServiceButton, pauseServiceLabel, false); 
			SetActionItemState( STOP, stopServiceButton, stopServiceLabel, false); 
		} 
 
		private void SetActionButtonState( ServiceSubject subject) 
		{ 
			if (subject != null) 
			{ 
				SetActionItemState( START, startServiceButton, startServiceLabel, subject.CanStart); 
				SetActionItemState( PAUSE, pauseServiceButton, pauseServiceLabel, subject.CanPause); 
				SetActionItemState( STOP, stopServiceButton, stopServiceLabel, subject.CanStop); 
			} 
			else 
				DisableActionButtons(); 
		} 
 
		private void SetActionItemState( string action, Button button, Label label, bool isEnabled) 
		{ 
			label.Enabled = isEnabled; 
			button.Enabled = isEnabled; 
			button.Image = Image.FromStream( GetActionImage( action, isEnabled)); 
		} 
 
		private void SetDefaultServiceStateImage() 
		{ 
			SetServiceStateImage( ServiceStateUnknown.Instance); 
		} 
 
		private void SetServiceStateImage( ServiceState state) 
		{ 
			statePicture.Image = Image.FromStream( GetLargeStateImage( state)); 
			this.Icon = GetSmallStateImage( state); 
		} 
 
		private Stream GetActionImage( string action, bool isEnabled) 
		{ 
			MemoryStream memStream = new MemoryStream(); 
			_resources.GetActionImage( action, isEnabled).Save( memStream); 
			return memStream; 
		} 
 
		private Icon GetSmallStateImage( ServiceState state) 
		{ 
			return _resources.GetSmallStateImage( state); 
		} 
 
		private Stream GetLargeStateImage( ServiceState state) 
		{ 
			MemoryStream memStream = new MemoryStream(); 
			_resources.GetLargeStateImage( state).Save( memStream); 
			return memStream; 
		} 
 
		private bool VerifyUserAction( string action) 
		{ 
			string serviceName = App.Subject.Controller.DisplayName; 
			string serverName = App.Subject.ServerName; 
 
			return App.VerifyUserAction( action, serviceName, serverName); 
		} 
 
		private string GetStringFromResources( string name) 
		{ 
			return _resources.GetString( typeof(DialogObserver), name); 
		} 
 
		private string GetAppString( string name) 
		{ 
			return _resources.GetAppString( name); 
		} 
		#endregion 
	} 
}