~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/monomac/samples/CFNetwork/MainWindowController.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MonoMac.CFNetwork.Test.MainWindowController
 
3
//
 
4
// Authors:
 
5
//      Martin Baulig (martin.baulig@gmail.com)
 
6
//
 
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
 
8
//
 
9
//
 
10
// Permission is hereby granted, free of charge, to any person obtaining
 
11
// a copy of this software and associated documentation files (the
 
12
// "Software"), to deal in the Software without restriction, including
 
13
// without limitation the rights to use, copy, modify, merge, publish,
 
14
// distribute, sublicense, and/or sell copies of the Software, and to
 
15
// permit persons to whom the Software is furnished to do so, subject to
 
16
// the following conditions:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
28
//
 
29
using System;
 
30
using System.Threading;
 
31
using System.Threading.Tasks;
 
32
using System.Collections.Generic;
 
33
using System.Linq;
 
34
using MonoMac.Foundation;
 
35
using MonoMac.AppKit;
 
36
 
 
37
namespace MonoMac.CFNetwork.Test {
 
38
 
 
39
        using Models;
 
40
        using Views;
 
41
 
 
42
        public partial class MainWindowController : MonoMac.AppKit.NSWindowController {
 
43
                #region Constructors
 
44
                
 
45
                // Called when created from unmanaged code
 
46
                public MainWindowController (IntPtr handle) : base (handle)
 
47
                {
 
48
                        Initialize ();
 
49
                }
 
50
                
 
51
                // Called when created directly from a XIB file
 
52
                [Export ("initWithCoder:")]
 
53
                public MainWindowController (NSCoder coder) : base (coder)
 
54
                {
 
55
                        Initialize ();
 
56
                }
 
57
                
 
58
                // Call to load from the XIB/NIB file
 
59
                public MainWindowController () : base ("MainWindow")
 
60
                {
 
61
                        Initialize ();
 
62
                }
 
63
                
 
64
                // Shared initialization code
 
65
                void Initialize ()
 
66
                {
 
67
                        var runner = AsyncTaskRunnerController.Instance;
 
68
                        runner.StateChangedEvent += (sender, e) => {
 
69
                                IsRunning = e.IsRunning;
 
70
 
 
71
                                if (!e.IsRunning)
 
72
                                        ShowProgress = false;
 
73
                                else
 
74
                                        ShowProgress = runner.CanReportProgress;
 
75
                        };
 
76
                        runner.ProgressChangedEvent += (sender, e) => {
 
77
                                if (e.Total != null)
 
78
                                        CurrentProgress = 100.0 * e.Current / e.Total.Value;
 
79
                        };
 
80
                        runner.MessageEvent += (sender, e) => {
 
81
                                StatusLabel.StringValue = e.Message;
 
82
                        };
 
83
 
 
84
                        getStringController = new GetStringViewController ();
 
85
 
 
86
                        checkHeadersViewController = new CheckHeadersViewController ();
 
87
                        downloadDataViewController = new DownloadDataViewController ();
 
88
                        benchmarkViewController = new BenchmarkViewController ();
 
89
                }
 
90
                
 
91
                #endregion
 
92
                
 
93
                //strongly typed window accessor
 
94
                public new MainWindow Window {
 
95
                        get {
 
96
                                return (MainWindow)base.Window;
 
97
                        }
 
98
                }
 
99
 
 
100
                public bool AutoRedirect {
 
101
                        get;
 
102
                        set;
 
103
                }
 
104
 
 
105
                public override void AwakeFromNib ()
 
106
                {
 
107
                        base.AwakeFromNib ();
 
108
 
 
109
                        urlController = new URLViewController ();
 
110
                        urlController.View.AutoresizingMask = NSViewResizingMask.WidthSizable;
 
111
                        urlController.View.Frame = URLView.Bounds;
 
112
                        URLView.AddSubview (urlController.View);
 
113
                }
 
114
 
 
115
                URLViewController urlController;
 
116
                GetStringViewController getStringController;
 
117
                DownloadDataViewController downloadDataViewController;
 
118
                CheckHeadersViewController checkHeadersViewController;
 
119
                BenchmarkViewController benchmarkViewController;
 
120
                NSViewController currentController;
 
121
 
 
122
                internal CheckHeadersViewController HeaderController {
 
123
                        get { return checkHeadersViewController; }
 
124
                }
 
125
 
 
126
                internal BenchmarkViewController BenchmarkController {
 
127
                        get { return benchmarkViewController; }
 
128
                }
 
129
 
 
130
                public void DownloadData ()
 
131
                {
 
132
                        SwitchView (downloadDataViewController, "Download Data");
 
133
                }
 
134
 
 
135
                public void GetString ()
 
136
                {
 
137
                        SwitchView (getStringController, "Get String");
 
138
                }
 
139
 
 
140
                public void CheckHeaders ()
 
141
                {
 
142
                        SwitchView (checkHeadersViewController, "Check Headers");
 
143
                }
 
144
 
 
145
                public void Benchmark ()
 
146
                {
 
147
                        SwitchView (benchmarkViewController, "Benchmark");
 
148
                }
 
149
 
 
150
                void SwitchView (NSViewController controller, string name)
 
151
                {
 
152
                        if (currentController != null)
 
153
                                currentController.View.RemoveFromSuperview ();
 
154
 
 
155
                        currentController = controller;
 
156
                        AsyncTaskRunnerController.Switch (((IAsyncViewController)controller).TaskRunner);
 
157
                        View.AddSubview (controller.View);
 
158
                        currentController.View.Frame = View.Bounds;
 
159
                        currentController.View.AutoresizingMask = NSViewResizingMask.HeightSizable |
 
160
                                NSViewResizingMask.WidthSizable;
 
161
 
 
162
                        Window.Title = name;
 
163
                }
 
164
 
 
165
                bool isRunning;
 
166
                bool showProgress;
 
167
                double currentProgress;
 
168
 
 
169
                [Export("IsRunning")]
 
170
                public bool IsRunning {
 
171
                        get { return isRunning; }
 
172
                        set {
 
173
                                WillChangeValue ("IsRunning");
 
174
                                isRunning = value;
 
175
                                View.NeedsDisplay = true;
 
176
                                DidChangeValue ("IsRunning");
 
177
                        }
 
178
                }
 
179
 
 
180
                [Export("ShowProgress")]
 
181
                public bool ShowProgress {
 
182
                        get { return showProgress; }
 
183
                        set {
 
184
                                WillChangeValue ("ShowProgress");
 
185
                                showProgress = value;
 
186
                                DidChangeValue ("ShowProgress");
 
187
                        }
 
188
                }
 
189
 
 
190
                [Export("CurrentProgress")]
 
191
                public double CurrentProgress {
 
192
                        get { return currentProgress; }
 
193
                        set {
 
194
                                WillChangeValue ("CurrentProgress");
 
195
                                currentProgress = value;
 
196
                                View.NeedsDisplay = true;
 
197
                                DidChangeValue ("CurrentProgress");
 
198
                                if (!ShowProgress)
 
199
                                        ShowProgress = true;
 
200
                        }
 
201
                }
 
202
 
 
203
                public void Load (string url)
 
204
                {
 
205
                        AsyncTaskRunnerController.Instance.Run (new Uri (url));
 
206
                }
 
207
 
 
208
                public void Stop ()
 
209
                {
 
210
                        AsyncTaskRunnerController.Instance.Cancel ();
 
211
                }
 
212
        }
 
213
}
 
214