diff options
Diffstat (limited to 'main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring')
4 files changed, 54 insertions, 453 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs index a7359939a2..348ce2aa23 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/BaseProgressMonitor.cs @@ -38,317 +38,27 @@ using MonoDevelop.Ide.Gui.Dialogs; namespace MonoDevelop.Ide.ProgressMonitoring { - public class BaseProgressMonitor: GuiSyncObject, IProgressMonitor, IAsyncOperation + public static class ProgressHelper { - class MbrWrapper { - public ManualResetEvent waitEvent; - - //workaround for "** ERROR **: file icall.c: line 2419 (ves_icall_InternalExecute): assertion failed" bug when - //handling the CancelRequested event - public event MonitorHandler cancelRequestedEvent; - - public void RaiseEvent (IProgressMonitor monitor) { - if (cancelRequestedEvent != null) - cancelRequestedEvent (monitor); - } - } - - MbrWrapper c = new MbrWrapper (); - ProgressTracker progressTracker; - LogTextWriter logger; - bool canceled; - bool runningPendingEvents; - - event OperationHandler completedEvent; - - StringCollection errorsMessages = new StringCollection (); - StringCollection successMessages = new StringCollection (); - StringCollection warningMessages = new StringCollection (); - Exception errorException; - - public BaseProgressMonitor () - { - progressTracker = new ProgressTracker (); - logger = new LogTextWriter (); - logger.TextWritten += new LogTextEventHandler (WriteLogInternal); - } - - [FreeDispatch] - public object SyncRoot { - [FreeDispatch] - get { - // Dont return 'this'. Locking on proxies doesn't look like a good idea. - return progressTracker; - } - } - - [AsyncDispatch] - public virtual void BeginTask (string name, int totalWork) - { - progressTracker.BeginTask (name, totalWork); - OnProgressChanged (); - } - - [AsyncDispatch] - public virtual void BeginStepTask (string name, int totalWork, int stepSize) - { - progressTracker.BeginStepTask (name, totalWork, stepSize); - OnProgressChanged (); - } - - [AsyncDispatch] - public virtual void EndTask () - { - progressTracker.EndTask (); - } - - [AsyncDispatch] - public virtual void Step (int work) - { - progressTracker.Step (work); - OnProgressChanged (); - } - - public TextWriter Log { - [FreeDispatch] - get { - return logger; - } - } - - [FreeDispatch] - public virtual void ReportSuccess (string message) - { - successMessages.Add (message); - } - - [FreeDispatch] - public virtual void ReportWarning (string message) - { - warningMessages.Add (message); - } - - [FreeDispatch] - public virtual void ReportError (string message, Exception ex) - { - if (message == null && ex != null) { - var userEx = ex as UserException; - if (userEx != null) - message = string.Format ("{0}{1}{1}{2}", userEx.Message, Environment.NewLine, userEx.Details); - else - message = ex.Message; - } else if (message != null && ex != null) { - if (!message.EndsWith (".")) message += "."; - message += " " + ex.Message; - } - - errorsMessages.Add (message); - if (ex != null) { - LoggingService.LogError (ex.ToString ()); - errorException = ex; - } - } - - [FreeDispatch] - public virtual bool IsCancelRequested { - [FreeDispatch] - get { return canceled; } - } - - [AsyncDispatch] - public virtual void Dispose() - { - DispatchService.RunPendingEvents (); - - lock (progressTracker) { - progressTracker.Done (); - if (c.waitEvent != null) - c.waitEvent.Set (); - } - try { - OnCompleted (); - } catch (Exception ex) { - string msg = "Unhandled exception in monitor cancel event handler"; - LoggingService.LogError (msg, ex); - MessageService.ShowException (ex, msg); - } - } - - [FreeDispatch] - public IAsyncOperation AsyncOperation - { - [FreeDispatch] - get { return this; } - } - - [FreeDispatch] - void IAsyncOperation.Cancel () - { - OnCancelRequested (); - } - - [FreeDispatch] - void IAsyncOperation.WaitForCompleted () - { - if (IsCompleted) return; - - if (DispatchService.IsGuiThread) { - while (!IsCompleted) { - DispatchService.RunPendingEvents (); - Thread.Sleep (100); - } - } else { - lock (progressTracker) { - if (!progressTracker.InProgress) return; - if (c.waitEvent == null) - c.waitEvent = new ManualResetEvent (false); - } - c.waitEvent.WaitOne (); - } - } - - [FreeDispatch] - bool IAsyncOperation.Success { - [FreeDispatch] - get { return errorsMessages.Count == 0; } - } - - [FreeDispatch] - bool IAsyncOperation.SuccessWithWarnings { - [FreeDispatch] - get { return errorsMessages.Count == 0 && warningMessages.Count > 0; } - } - - public bool IsCompleted { - [FreeDispatch] - get { return !progressTracker.InProgress; } - } - - public event OperationHandler Completed { - add { - bool alreadyCompleted = false; - lock (progressTracker) { - completedEvent += value; - alreadyCompleted = !progressTracker.InProgress; - } - if (alreadyCompleted) value (this); - } - remove { - lock (progressTracker) { - completedEvent -= value; - } - } - } - - public event MonitorHandler CancelRequested { - [FreeDispatch] - add { - bool alreadyCanceled = false; - lock (progressTracker) { - c.cancelRequestedEvent += value; - alreadyCanceled = canceled; - } - if (alreadyCanceled) value (this); - } - [FreeDispatch] - remove { - lock (progressTracker) { - c.cancelRequestedEvent -= value; - } - } - } - - public Exception ErrorException { - get { return errorException; } - } - - public StringCollection Errors { - get { return errorsMessages; } - } - - public StringCollection SuccessMessages { - get { return successMessages; } - } - - public StringCollection Warnings { - get { return warningMessages; } - } - - protected string CurrentTask { - get { return progressTracker.CurrentTask; } - } - - protected double CurrentTaskWork { - get { return progressTracker.CurrentTaskWork; } - } - - protected double GlobalWork { - get { return progressTracker.GlobalWork; } - } - - protected bool UnknownWork { - get { return progressTracker.UnknownWork; } - } - - protected virtual void OnCompleted () - { - if (completedEvent != null) - completedEvent (AsyncOperation); - } - - protected virtual void OnCancelRequested () - { - lock (progressTracker) { - canceled = true; - } - - c.RaiseEvent(this); - } - - [AsyncDispatch] - void WriteLogInternal (string text) - { - OnWriteLog (text); - } - - protected virtual void OnWriteLog (string text) - { - } - - protected virtual void OnProgressChanged () - { - } - - protected void RunPendingEvents () - { - if (!runningPendingEvents) { - try { - runningPendingEvents = true; - DispatchService.RunPendingEvents (); - } finally { - runningPendingEvents = false; - } - } - } - - protected void ShowResultDialog () + public static void ShowResultDialog (this ProgressMonitor monitor) { - if (Errors.Count == 1 && Warnings.Count == 0) { - if (ErrorException != null) - MessageService.ShowException (ErrorException, Errors[0]); + if (monitor.Errors.Length == 1 && !monitor.HasWarnings) { + var error = monitor.Errors [0]; + if (error.Exception != null) + MessageService.ShowException (error.Exception, error.Message); else - MessageService.ShowError (Errors[0]); + MessageService.ShowError (error.Message); } - else if (Errors.Count == 0 && Warnings.Count == 1) { - MessageService.ShowWarning (Warnings[0]); + else if (!monitor.HasErrors && monitor.Warnings.Length == 1) { + MessageService.ShowWarning (monitor.Warnings[0]); } - else if (Errors.Count > 0 || Warnings.Count > 0) { + else if (monitor.HasErrors || monitor.HasWarnings) { var resultDialog = new MultiMessageDialog () { Modal = true, }; - foreach (string m in Errors) - resultDialog.AddError (m); - foreach (string m in Warnings) + foreach (var m in monitor.Errors) + resultDialog.AddError (m.Message); + foreach (var m in monitor.Warnings) resultDialog.AddWarning (m); MessageService.ShowCustomDialog (resultDialog); } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MessageDialogProgressMonitor.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MessageDialogProgressMonitor.cs index 8a5304d435..c26cb908e9 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MessageDialogProgressMonitor.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MessageDialogProgressMonitor.cs @@ -34,7 +34,7 @@ namespace MonoDevelop.Ide.ProgressMonitoring { // Progress monitor that reports errors and warnings in message dialogs. - public class MessageDialogProgressMonitor: BaseProgressMonitor + public class MessageDialogProgressMonitor: ProgressMonitor { ProgressDialog dialog; bool hideWhenDone; @@ -64,77 +64,64 @@ namespace MonoDevelop.Ide.ProgressMonitoring dialog.Message = ""; MessageService.PlaceDialog (dialog, MessageService.RootWindow); dialog.Show (); - dialog.AsyncOperation = AsyncOperation; - dialog.OperationCancelled += delegate { - OnCancelRequested (); - }; - RunPendingEvents (); + dialog.CancellationTokenSource = CancellationTokenSource; + DispatchService.RunPendingEvents (); this.hideWhenDone = hideWhenDone; this.showDetails = showDetails; } } - + protected override void OnWriteLog (string text) { if (dialog != null) { dialog.WriteText (text); - RunPendingEvents (); + DispatchService.RunPendingEvents (); } } protected override void OnProgressChanged () { if (dialog != null) { - dialog.Message = CurrentTask; - dialog.Progress = GlobalWork; - RunPendingEvents (); - } - } - - public override void BeginTask (string name, int totalWork) - { - if (dialog != null) { - dialog.BeginTask (name); + dialog.Message = CurrentTaskName; + dialog.Progress = Progress; + DispatchService.RunPendingEvents (); } - base.BeginTask (name, totalWork); } - public override void BeginStepTask (string name, int totalWork, int stepSize) + protected override void OnBeginTask (string name, int totalWork, int stepWork) { if (dialog != null) { dialog.BeginTask (name); } - base.BeginStepTask (name, totalWork, stepSize); } - - public override void EndTask () + + protected override void OnEndTask (string name, int totalWork, int stepWork) { if (dialog != null) { dialog.EndTask (); } - base.EndTask (); - RunPendingEvents (); + base.OnEndTask (name, totalWork, stepWork); + DispatchService.RunPendingEvents (); } - - public override void ReportWarning (string message) + + protected override void OnWarningReported (string message) { - base.ReportWarning (message); if (dialog != null) { dialog.WriteText (GettextCatalog.GetString ("WARNING: ") + message + "\n"); - RunPendingEvents (); + DispatchService.RunPendingEvents (); } + base.OnWarningReported (message); } - - public override void ReportError (string message, Exception ex) + + protected override void OnErrorReported (string message, Exception exception) { - base.ReportError (message, ex); - if (dialog != null) { - dialog.WriteText (GettextCatalog.GetString ("ERROR: ") + Errors [Errors.Count - 1] + "\n"); - RunPendingEvents (); + dialog.WriteText (GettextCatalog.GetString ("ERROR: ") + Errors [Errors.Length - 1] + "\n"); + DispatchService.RunPendingEvents (); } + base.OnErrorReported (message, exception); } - + protected override void OnCompleted () { DispatchService.GuiDispatch (new MessageHandler (ShowDialogs)); @@ -144,7 +131,7 @@ namespace MonoDevelop.Ide.ProgressMonitoring void ShowDialogs () { if (dialog != null) { - dialog.ShowDone (Warnings.Count > 0, Errors.Count > 0); + dialog.ShowDone (Warnings.Length > 0, Errors.Length > 0); if (hideWhenDone) dialog.Destroy (); } @@ -152,7 +139,7 @@ namespace MonoDevelop.Ide.ProgressMonitoring if (showDetails) return; - ShowResultDialog (); + this.ShowResultDialog (); } } } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MultiTaskDialogProgressMonitor.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MultiTaskDialogProgressMonitor.cs index 75ab2adfe4..8e24e95f31 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MultiTaskDialogProgressMonitor.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/MultiTaskDialogProgressMonitor.cs @@ -39,7 +39,7 @@ namespace MonoDevelop.Ide.ProgressMonitoring { - public class MultiTaskDialogProgressMonitor : BaseProgressMonitor + public class MultiTaskDialogProgressMonitor : ProgressMonitor { StringCollection errorsMessages = new StringCollection (); StringCollection warningMessages = new StringCollection (); @@ -75,7 +75,7 @@ namespace MonoDevelop.Ide.ProgressMonitoring }; MessageService.PlaceDialog (dialog, parent); Mono.TextEditor.GtkWorkarounds.PresentWindowWithNotification (dialog); - dialog.AsyncOperation = AsyncOperation; + dialog.CancellationTokenSource = CancellationTokenSource; DispatchService.RunPendingEvents (); this.showDetails = showDetails; } @@ -106,46 +106,39 @@ namespace MonoDevelop.Ide.ProgressMonitoring protected override void OnProgressChanged () { if (dialog != null) { - dialog.SetProgress (CurrentTaskWork); + dialog.SetProgress (Progress); DispatchService.RunPendingEvents (); } } - public override void BeginTask (string name, int totalWork) + protected override void OnBeginTask (string name, int totalWork, int stepWork) { if (dialog != null) { dialog.BeginTask (name); } - base.BeginTask (name, totalWork); + base.OnBeginTask (name, totalWork, stepWork); } - - public override void BeginStepTask (string name, int totalWork, int stepSize) - { - if (dialog != null) { - dialog.BeginTask (name); - } - base.BeginStepTask (name, totalWork, stepSize); - } - - public override void EndTask () + + protected override void OnEndTask (string name, int totalWork, int stepWork) { if (dialog != null) { dialog.EndTask (); } - base.EndTask (); DispatchService.RunPendingEvents (); + base.OnEndTask (name, totalWork, stepWork); } - - public override void ReportWarning (string message) + + protected override void OnWarningReported (string message) { if (dialog != null) { dialog.WriteText (GettextCatalog.GetString ("WARNING: ") + message + "\n"); DispatchService.RunPendingEvents (); } warningMessages.Add (message); + base.OnWarningReported (message); } - - public override void ReportError (string message, Exception ex) + + protected override void OnErrorReported (string message, Exception ex) { if (message == null && ex != null) message = ex.Message; @@ -153,17 +146,18 @@ namespace MonoDevelop.Ide.ProgressMonitoring if (!message.EndsWith (".")) message += "."; message += " " + ex.Message; } - + errorsMessages.Add (message); if (ex != null) { LoggingService.LogError (ex.ToString ()); errorException = ex; } - + if (dialog != null) { dialog.WriteText (GettextCatalog.GetString ("ERROR: ") + message + "\n"); DispatchService.RunPendingEvents (); } + base.OnErrorReported (message, ex); } protected override void OnCompleted () diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/ProgressBarMonitor.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/ProgressBarMonitor.cs deleted file mode 100644 index 81a3602f34..0000000000 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.ProgressMonitoring/ProgressBarMonitor.cs +++ /dev/null @@ -1,90 +0,0 @@ -// -// ProgressBarMonitor.cs -// -// Author: -// Lluis Sanchez Gual <lluis@novell.com> -// -// Copyright (c) 2011 Novell, Inc (http://www.novell.com) -// -// 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.Linq; -using MonoDevelop.Core; - -namespace MonoDevelop.Ide.ProgressMonitoring -{ - [System.ComponentModel.ToolboxItem(true)] - public partial class ProgressBarMonitor : Gtk.Bin - { - public ProgressBarMonitor () - { - this.Build (); - } - - public bool AllowCancel { - get { return buttonCancel.Visible; } - set { buttonCancel.Visible = value; } - } - - public bool ShowErrorsDialog { get; set; } - - public IProgressMonitor CreateProgressMonitor () - { - return new InternalProgressBarMonitor (this); - } - - class InternalProgressBarMonitor: BaseProgressMonitor - { - ProgressBarMonitor widget; - - public InternalProgressBarMonitor (ProgressBarMonitor widget) - { - this.widget = widget; - widget.buttonCancel.Clicked += OnCancelClicked; - } - - void OnCancelClicked (object sender, EventArgs e) - { - AsyncOperation.Cancel (); - } - - protected override void OnProgressChanged () - { - widget.progressBar.Fraction = GlobalWork; - RunPendingEvents (); - } - - protected override void OnCompleted () - { - base.OnCompleted (); - if (!AsyncOperation.Success && widget.ShowErrorsDialog) { - string err = string.Join ("\n", Errors.Cast<string> ().ToArray ()); - MessageService.ShowError (GettextCatalog.GetString ("Add-in installation failed"), err); - } - } - - public override void Dispose () - { - widget.buttonCancel.Clicked -= OnCancelClicked; - base.Dispose (); - } - } - } -} - |