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

« back to all changes in this revision

Viewing changes to external/monomac/samples/CFNetwork/Views/LogViewerController.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.Views.LogViewerController
 
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.IO;
 
31
using System.Text;
 
32
using System.Diagnostics;
 
33
using System.Collections.Generic;
 
34
using System.Linq;
 
35
using MonoMac.Foundation;
 
36
using MonoMac.AppKit;
 
37
 
 
38
namespace MonoMac.CFNetwork.Test.Views {
 
39
 
 
40
        public partial class LogViewerController : MonoMac.AppKit.NSWindowController {
 
41
                #region Constructors
 
42
                
 
43
                // Called when created from unmanaged code
 
44
                public LogViewerController (IntPtr handle) : base (handle)
 
45
                {
 
46
                        Initialize ();
 
47
                }
 
48
                
 
49
                // Called when created directly from a XIB file
 
50
                [Export ("initWithCoder:")]
 
51
                public LogViewerController (NSCoder coder) : base (coder)
 
52
                {
 
53
                        Initialize ();
 
54
                }
 
55
                
 
56
                // Call to load from the XIB/NIB file
 
57
                public LogViewerController () : base ("LogViewer")
 
58
                {
 
59
                        Initialize ();
 
60
                }
 
61
                
 
62
                // Shared initialization code
 
63
                void Initialize ()
 
64
                {
 
65
                        writer = new OutputTextWriter (this);
 
66
                        listener = new Listener (this);
 
67
                }
 
68
                
 
69
                #endregion
 
70
 
 
71
                NSFont font;
 
72
                TraceListener listener;
 
73
                OutputTextWriter writer;
 
74
 
 
75
                public override void AwakeFromNib ()
 
76
                {
 
77
                        base.AwakeFromNib ();
 
78
                        font = NSFont.SystemFontOfSize (16.0f);
 
79
                }
 
80
 
 
81
                //strongly typed window accessor
 
82
                public new LogViewer Window {
 
83
                        get {
 
84
                                return (LogViewer)base.Window;
 
85
                        }
 
86
                }
 
87
 
 
88
                public NSTextStorage Storage {
 
89
                        get {
 
90
                                return Text.TextStorage;
 
91
                        }
 
92
                }
 
93
 
 
94
                public TextWriter TextWriter {
 
95
                        get {
 
96
                                return writer;
 
97
                        }
 
98
                }
 
99
 
 
100
                public override void ShowWindow (NSObject sender)
 
101
                {
 
102
                        base.ShowWindow (sender);
 
103
                        Debug.Listeners.Add (listener);
 
104
                }
 
105
 
 
106
                partial void Clear (NSObject sender)
 
107
                {
 
108
                        Storage.DeleteRange (new NSRange (0, Storage.Length));
 
109
                }
 
110
 
 
111
                partial void Quit (NSObject sender)
 
112
                {
 
113
                        Debug.Listeners.Remove (listener);
 
114
                        Window.Close ();
 
115
                }
 
116
 
 
117
                #region Appending Text
 
118
 
 
119
                public void Append (string text)
 
120
                {
 
121
                        InvokeOnMainThread (() => DoAppend (text));
 
122
                }
 
123
 
 
124
                public void AppendLine (string message)
 
125
                {
 
126
                        InvokeOnMainThread (() => DoAppend (message + Environment.NewLine));
 
127
                }
 
128
 
 
129
                public void AppendLine (NSColor color, string text)
 
130
                {
 
131
                        InvokeOnMainThread (() => DoAppend (color, text + Environment.NewLine));
 
132
                }
 
133
 
 
134
                void DoAppend (string text)
 
135
                {
 
136
                        var pos = Storage.Length;
 
137
                        Storage.Append (new NSAttributedString (text));
 
138
                        Text.SetFont (font, new NSRange (pos, text.Length));
 
139
                }
 
140
 
 
141
                void DoAppend (NSColor color, string text)
 
142
                {
 
143
                        var pos = Storage.Length;
 
144
                        Storage.Append (new NSAttributedString (text));
 
145
                        var range = new NSRange (pos, text.Length);
 
146
                        Text.SetFont (font, range);
 
147
                        Text.SetTextColor (color, range);
 
148
                }
 
149
 
 
150
                #endregion
 
151
 
 
152
                #region Text Writer
 
153
 
 
154
                class OutputTextWriter : TextWriter {
 
155
                        LogViewerController controller;
 
156
 
 
157
                        public OutputTextWriter (LogViewerController controller)
 
158
                        {
 
159
                                this.controller = controller;
 
160
                        }
 
161
 
 
162
                        public override Encoding Encoding {
 
163
                                get { return Encoding.Default; }
 
164
                        }
 
165
 
 
166
                        public override void Write (char value)
 
167
                        {
 
168
                                controller.Append (value.ToString ());
 
169
                        }
 
170
 
 
171
                        public override void Write (string value)
 
172
                        {
 
173
                                controller.Append (value);
 
174
                        }
 
175
                }
 
176
 
 
177
                class Listener : TraceListener {
 
178
                        LogViewerController controller;
 
179
 
 
180
                        public Listener (LogViewerController controller)
 
181
                        {
 
182
                                this.controller = controller;
 
183
                        }
 
184
 
 
185
                        #region implemented abstract members of TraceListener
 
186
                        public override void Write (string message)
 
187
                        {
 
188
                                controller.Append (message);
 
189
                        }
 
190
 
 
191
                        public override void WriteLine (string message)
 
192
                        {
 
193
                                controller.AppendLine (message);
 
194
                        }
 
195
 
 
196
                        public override void Fail (string message)
 
197
                        {
 
198
                                Fail (message, "");
 
199
                        }
 
200
 
 
201
                        public override void Fail (string message, string detailMessage)
 
202
                        {
 
203
                                controller.AppendLine ("---- DEBUG ASSERTION FAILED ----");
 
204
                                controller.AppendLine ("---- Assert Short Message ----");
 
205
                                controller.AppendLine (NSColor.Red, message);
 
206
                                controller.AppendLine ("---- Assert Long Message ----");
 
207
                                controller.AppendLine (NSColor.Orange, detailMessage);
 
208
                                controller.AppendLine ("");
 
209
                        }
 
210
                        #endregion
 
211
                }
 
212
 
 
213
                #endregion
 
214
        }
 
215
}
 
216