~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to samples/ComponentInspector/ComponentInspector.Core/Src/ObjectBrowser/Dialogs/ProgressDialog.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// <file>
 
2
//     <copyright see="prj:///doc/copyright.txt"/>
 
3
//     <license see="prj:///doc/license.txt"/>
 
4
//     <owner name="Oakland Software Incorporated" email="general@oaklandsoftware.com"/>
 
5
//     <version>$Revision$</version>
 
6
// </file>
 
7
 
 
8
using System;
 
9
using System.Drawing;
 
10
using System.Threading;
 
11
using System.Windows.Forms;
 
12
 
 
13
using NoGoop.Util;
 
14
 
 
15
namespace NoGoop.ObjBrowser.Dialogs
 
16
{
 
17
        internal class ProgressDialog : Dialog
 
18
        {
 
19
                protected ProgressBar           _progress;
 
20
                protected RichTextBox           _textBox;
 
21
                protected Label                 _label;
 
22
                protected bool                  _done;
 
23
 
 
24
                // Parameters
 
25
                protected String                _title;
 
26
                protected String                _bodyText;
 
27
                protected int                   _maxValue;
 
28
                protected bool                  _hasProgressText;
 
29
                protected bool                  _final;
 
30
 
 
31
                protected Cursor                _saveCursor;
 
32
 
 
33
                // Indicates this should be considered finished when the
 
34
                // end is reached on this progress bar.  If this is false,
 
35
                // the progress dialog is still kept up because it expects
 
36
                // more work to do.
 
37
                internal const bool              FINAL = true;
 
38
 
 
39
                internal const bool              HAS_PROGRESS_TEXT = true;
 
40
 
 
41
                // Used for the maxValue
 
42
                internal const int               NO_PROGRESS_BAR = -1;
 
43
 
 
44
                public ProgressDialog() : base(!INCLUDE_BUTTONS)
 
45
                {
 
46
                        Height = 100;
 
47
                        Width = 450;
 
48
 
 
49
                        // Can't close me
 
50
                        ControlBox = false;
 
51
                        
 
52
                        StartPosition = FormStartPosition.Manual;
 
53
 
 
54
                        /****
 
55
                                This does not seem to work
 
56
                        TopLevel = false;
 
57
                        TopMost = true;
 
58
                        Parent = ObjectBrowser.ObjBrowser;
 
59
                        StartPosition = FormStartPosition.CenterParent;
 
60
                        ****/
 
61
 
 
62
                        // Have to manually compute location based on parent's location
 
63
                        if (ObjectBrowserForm.Instance != null) {
 
64
                                Size parentSize = ObjectBrowserForm.Instance.Size;
 
65
                                Point loc = ObjectBrowserForm.Instance.DesktopLocation;
 
66
                                
 
67
                                Point ourLoc = new Point();
 
68
                                ourLoc.Y = loc.Y + (int)((parentSize.Height - Size.Height) / 2);
 
69
                                ourLoc.X = loc.X + (int)((parentSize.Width - Size.Width) / 2);
 
70
        
 
71
                                Location = ourLoc;
 
72
                                DesktopLocation = ourLoc;
 
73
                        }
 
74
                }
 
75
 
 
76
                public void Setup(String title,
 
77
                                                  String bodyText,
 
78
                                                  int maxValue,
 
79
                                                  bool hasProgressText,
 
80
                                                  bool final)
 
81
                {
 
82
                        TraceUtil.WriteLineInfo(this, "Progress Setup " 
 
83
                                                                           + bodyText + " max: " + maxValue);
 
84
 
 
85
                        // When transitioning from phase to phase, if this is the 
 
86
                        // last phase, the maxValue might be zero
 
87
                        if (maxValue == 0 && final)
 
88
                        {
 
89
                                TraceUtil.WriteLineInfo(this, "Setup final close");
 
90
                                Finished();
 
91
                                return;
 
92
                        }
 
93
 
 
94
                        _title = title;
 
95
                        _bodyText = bodyText;
 
96
                        _maxValue = maxValue;
 
97
                        _hasProgressText = hasProgressText;
 
98
                        _final = final;
 
99
 
 
100
                        // We are resetting, use the existing controls
 
101
                        if (Controls.Count > 0)
 
102
                        {
 
103
                                _done = false;
 
104
                                _textBox.Text = _bodyText;
 
105
                                _progress.Value = 0;
 
106
                                _progress.Maximum = _maxValue;
 
107
                                if (_label != null)
 
108
                                        _label.Text = "";
 
109
                                TraceUtil.WriteLineInfo(this, "Using existing controls");
 
110
                                return;
 
111
                        }
 
112
 
 
113
                        Text = _title;
 
114
 
 
115
                        if (_hasProgressText)
 
116
                        {
 
117
                                _label = new Label();
 
118
                                _label.Dock = DockStyle.Bottom;
 
119
                                _label.Text = "";
 
120
                                // Give space for two lines incase it wraps
 
121
                                _label.Height = 30;
 
122
                                Controls.Add(_label);
 
123
                        }
 
124
 
 
125
                        if (_maxValue != NO_PROGRESS_BAR)
 
126
                        {
 
127
                                _progress = new ProgressBar();
 
128
                                _progress.Maximum = _maxValue;
 
129
                                _progress.Dock = DockStyle.Top;
 
130
                                Controls.Add(_progress);
 
131
                                Height += 80;
 
132
                        }
 
133
 
 
134
                        _textBox = Utils.MakeDescText(_bodyText, this);
 
135
                        _textBox.Dock = DockStyle.Top;
 
136
                        Controls.Add(_textBox);
 
137
 
 
138
                }
 
139
                
 
140
                internal void UpdateProgress(int increment)
 
141
                {
 
142
                        _progress.Value += increment;
 
143
                        
 
144
                        TraceUtil.WriteLineVerbose(this, 
 
145
                                                                           Thread.CurrentThread.Name 
 
146
                                                                           + " Update progress: "
 
147
                                                                           + increment 
 
148
                                                                           + " current: " 
 
149
                                                                           + _progress.Value);
 
150
                        if (_progress.Value >= _progress.Maximum &&
 
151
                                _final)
 
152
                        {
 
153
                                TraceUtil.WriteLineInfo(this, Thread.CurrentThread.Name 
 
154
                                                                                + " Update progress done");
 
155
                                Finished();
 
156
                        }
 
157
                }
 
158
 
 
159
                internal void UpdateProgressText(String text)
 
160
                {
 
161
                        _label.Text = text;
 
162
                }
 
163
 
 
164
 
 
165
                internal void Finished()
 
166
                {
 
167
                        lock (this)
 
168
                        {
 
169
                                TraceUtil.WriteLineInfo(this, "Finished - enter");
 
170
                                _done = true;
 
171
                                Cursor.Current = _saveCursor;
 
172
                        }
 
173
 
 
174
                        TraceUtil.WriteLineInfo
 
175
                                (this, "Finished - doing close");
 
176
 
 
177
                        try
 
178
                        {
 
179
                                Close();
 
180
                        }
 
181
                        catch (Exception ex)
 
182
                        {
 
183
                                TraceUtil.WriteLineWarning
 
184
                                        (this, "Finished - exception on close " + ex);
 
185
                        }
 
186
                        TraceUtil.WriteLineInfo
 
187
                                (this, "Finished - after close");
 
188
 
 
189
                }
 
190
 
 
191
                internal void ShowIfNotDone()
 
192
                {
 
193
                        TraceUtil.WriteLineInfo(this, "Progress - Show if not done");
 
194
                        lock (this) {
 
195
                                if (_done)
 
196
                                        return;
 
197
                                TraceUtil.WriteLineInfo(this, "Progress - Showing");
 
198
 
 
199
                                // Set hourglass cursor
 
200
                                _saveCursor = Cursor.Current;
 
201
                                Cursor.Current = Cursors.WaitCursor;
 
202
                        }
 
203
 
 
204
                        // Setup and add the controls on the same thread
 
205
                        // as the one we show it
 
206
                        TraceUtil.WriteLineInfo(this, "Progress ShowThread - setup complete");
 
207
                        
 
208
                        Show();
 
209
 
 
210
                        // Make sure text and all associated with the dialog 
 
211
                        // is present
 
212
                        Application.DoEvents();
 
213
                }
 
214
        }
 
215
}