File: TestWindowController.cs

package info (click to toggle)
monodevelop 3.0.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 153,256 kB
  • sloc: cs: 1,020,242; xml: 751,053; makefile: 9,596; sh: 1,529; objc: 302; sql: 129; ansic: 96
file content (322 lines) | stat: -rw-r--r-- 11,671 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

using System;
using System.Collections.Generic;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;

namespace NSTableViewBinding
{
	public partial class TestWindowController : MonoMac.AppKit.NSWindowController
	{
		
		internal static NSString FIRST_NAME = new NSString("firstname");
		internal static NSString LAST_NAME = new NSString("lastname");
		internal static NSString PHONE = new NSString("phone");
		
		EditController myEditController = null;
		
		internal static List<NSString> Keys = new List<NSString> { FIRST_NAME, LAST_NAME, PHONE};			
		internal const int FIRST_NAME_IDX = 0;
		internal const int LAST_NAME_IDX = 1;
		internal const int PHONE_IDX = 2;
		
		#region Constructors

		// Called when created from unmanaged code
		public TestWindowController (IntPtr handle) : base(handle)
		{
		}

		// Called when created directly from a XIB file
		[Export("initWithCoder:")]
		public TestWindowController (NSCoder coder) : base(coder)
		{
		}

		// Call to load from the XIB/NIB file
		public TestWindowController () : base("TestWindow")
		{
		}

		#endregion

		//strongly typed window accessor
		public new TestWindow Window {
			get { return (TestWindow)base.Window; }
		}
		
		#region Overrides
		
		public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();
	
			//-----------------------------------------------------------------------------

			// Compiler directive USE_BINDINGS_BY_CODE set in the projects Options
			// 1) From the MonoDevelop Menu select Project -> NSTableViewBinding Options
			// 		This will bring up the project options panel.
			// 2) Under the category Build -> Compiler look for the field labeled Define Symbols half way 
			// 		down the panel page.
			// 3) Add the symbol USE_BINDINGS_BY_CODE 
			
			//-----------------------------------------------------------------------------
			// Your NSTableView's content needs to use Cocoa Bindings,
			// use Interface Builder to setup the bindings like so:
			//
			// Each column in the NSTableView needs to use Cocoa Bindings,
			// use Interface Builder to setup the bindings like so:
			//
			//		columnIdentifier: "firstname"
			//			"value" = arrangedObjects.firstname [NSTableArray (NSArrayController)]
			//				Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "arrangedObjects"
			//				Model Key Path = "firstname" ("firstname" is a key in "TableArray")
			//
			//		columnIdentifier: "lastname"
			//			"value" = arrangedObjects.lastname [NSTableArray (NSArrayController)]
			//				Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "arrangedObjects"
			//				Model Key Path = "lastname" ("lastname" is a key in "TableArray")
			//
			//		columnIdentifier: "phone"
			//			"value" = arrangedObjects.phone [NSTableArray (NSArrayController)]
			//				Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "arrangedObjects"
			//				Model Key Path = "phone" ("phone" is a key in "TableArray")
			//
			// or do bindings by code:
				
#if USE_BINDINGS_BY_CODE
			
			NSTableColumn firstNameColumn = myTableView.FindTableColumn(FIRST_NAME);
			firstNameColumn.Bind("value",myContentArray,"arrangedObjects.firstname",null);

			NSTableColumn lastNameColumn = myTableView.FindTableColumn(LAST_NAME);
			lastNameColumn.Bind("value",myContentArray,"arrangedObjects.lastname",null);

			NSTableColumn phoneColumn = myTableView.FindTableColumn(PHONE);
			phoneColumn.Bind("value",myContentArray,"arrangedObjects.phone",null);

#endif
			
			// for NSTableView "double-click row" to work you need to use Cocoa Bindings,
			// use Interface Builder to setup the bindings like so:
			//
			//	NSTableView:
			//		"doubleClickArgument":
			//			Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "selectedObjects"
			//				Selector Name = "inspect:" (don't forget the ":")
			//
			//		"doubleClickTarget":
			//			Bind To: (File's Owner) MyWindowController
			//				Model Key Path = "self"
			//				Selector Name = "inspect:" (don't forget the ":")
			//
			//	... also make sure none of the NSTableColumns are "editable".
			//
			// or do bindings by code:
#if USE_BINDINGS_BY_CODE
	
			
			List<NSObject> doubleClickObjects = new List<NSObject> {new NSString("inspect:"),
																	NSNumber.FromBoolean(true),
																	NSNumber.FromBoolean(true)};
			
			List<NSString> doubleClickKeys = new List<NSString> {new NSString("NSSelectorName"),
																	new NSString("NSConditionallySetsHidden"),
																	new NSString("NSRaisesForNotApplicableKeys")};
			
			NSDictionary doubleClickOptionsDict = NSDictionary.FromObjectsAndKeys(doubleClickObjects.ToArray(),doubleClickKeys.ToArray());
			
			myTableView.Bind("doubleClickArgument",myContentArray,"selectedObjects",doubleClickOptionsDict);
			myTableView.Bind("doubleClickTarget",this,"self",doubleClickOptionsDict);
			
#endif
			
			// the enabled states of the two buttons "Add", "Remove" are bound to "canRemove" 
			// use Interface Builder to setup the bindings like so:
			//
			//	NSButton ("Add")
			//		"enabled":
			//			Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "canAdd"
			//
			//	NSButton ("Remove")
			//		"enabled":
			//			Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "canRemove"
			//
			// or do bindings by code:
#if USE_BINDINGS_BY_CODE
			
			List<NSObject> enableOptionsObjects = new List<NSObject> {NSNumber.FromBoolean(true)};
			
			List<NSString> enableOptionsKeys = new List<NSString> {new NSString("NSRaisesForNotApplicableKeys")};
			
			NSDictionary enableOptionsDict = NSDictionary.FromObjectsAndKeys(enableOptionsObjects.ToArray(),enableOptionsKeys.ToArray());
			addButton.Bind("enabled",myContentArray,"canAdd",enableOptionsDict);
			removeButton.Bind("enabled",myContentArray,"canRemove",enableOptionsDict);
						
#endif			
			// the NSForm's text fields is bound to the current selection in the NSTableView's content array controller,
			// use Interface Builder to setup the bindings like so:
			//
			//	NSFormCell:
			//		"value":
			//			Bind To: "TableArray" object (NSArrayController)
			//				Controller Key = "selection"
			//				Model Key Path = "firstname"
			//
			// or do bindings by code:
#if USE_BINDINGS_BY_CODE

			List<NSObject> valueOptionsObjects = new List<NSObject> {NSNumber.FromBoolean(true),
																		NSNumber.FromBoolean(true),
																		NSNumber.FromBoolean(true)};
			
			List<NSString> valueOptionsKeys = new List<NSString> {new NSString("NSAllowsEditingMultipleValuesSelection"),
																	new NSString("NSConditionallySetsEditable"),
																	new NSString("NSRaisesForNotApplicableKeys")};
			
			NSDictionary valueOptionsDict = NSDictionary.FromObjectsAndKeys(valueOptionsObjects.ToArray(),valueOptionsKeys.ToArray());
	
			myFormFields.CellAtIndex(FIRST_NAME_IDX).Bind("value",myContentArray,"selection.firstname",valueOptionsDict);
			myFormFields.CellAtIndex(LAST_NAME_IDX).Bind("value",myContentArray,"selection.lastname",valueOptionsDict);
			myFormFields.CellAtIndex(PHONE_IDX).Bind("value",myContentArray,"selection.phone",valueOptionsDict);
			
					
#endif
			// start listening for selection changes in our NSTableView's array controller
			myContentArray.AddObserver(this,new NSString("selectionIndexes"),NSKeyValueObservingOptions.New,IntPtr.Zero);
			
			// finally, add the first record in the table as a default value.
			//
			// note: to allow the external NSForm fields to alter the table view selection through the "value" bindings,
			// added objects to the content array needs to be an "NSMutableDictionary" -
			//
			List<NSString> objects = new List<NSString> {new NSString("John"),
														new NSString("Doe"),
														new NSString("(333) 333-3333)")};

			var dict = NSMutableDictionary.FromObjectsAndKeys(objects.ToArray(), Keys.ToArray());
			myContentArray.AddObject(dict);
			
		}
		
		#endregion
		
		// 
		// Inspect our selected objects (user double-clicked them).
		// 
		// Note: this method will not get called until you make all columns in the table
		// as "non-editable".  So long as they are editable, double clicking a row will
		// cause the current cell to be editied.
		// 
		partial void inspect (NSArray sender)
		{
			NSArray selectedObjects = sender;
			Console.WriteLine("inspect");
			
			int index;
			uint numItems = selectedObjects.Count;
			for (index = 0; index < numItems; index++)
			{
				NSDictionary objectDict =  new NSDictionary(selectedObjects.ValueAt(0));

				if (objectDict != null)
				{
					Console.WriteLine(string.Format("inspector item: [ {0} {1}, {2} ]",
					                                (NSString)objectDict[FIRST_NAME].ToString(),
					                                (NSString)objectDict[LAST_NAME].ToString(),
					                                (NSString)objectDict[PHONE].ToString()));
				}
				
				// setup the edit sheet controller if one hasn't been setup already
				if (myEditController == null)
					myEditController = new EditController();
				
				// remember which selection index we are changing
				int savedSelectionIndex = myContentArray.SelectionIndex;
				
				NSDictionary editItem =  new NSDictionary(selectedObjects.ValueAt(0));
				
				// get the current selected object and start the edit sheet
				NSMutableDictionary newValues = myEditController.edit(editItem, this);

				if (!myEditController.Cancelled)
				{
					// remove the current selection and replace it with the newly edited one
					var currentObjects = myContentArray.SelectedObjects;
					myContentArray.Remove(currentObjects);
					
					// make sure to add the new entry at the same selection location as before
					myContentArray.Insert(newValues,savedSelectionIndex);
				}
			}
		}
		
		/// <summary>
		/// This method demonstrates how to observe selection changes in our NSTableView's array controller
		/// </summary>
		/// <param name="keyPath">
		/// A <see cref="NSString"/>
		/// </param>
		/// <param name="ofObject">
		/// A <see cref="NSArrayController"/>
		/// </param>
		/// <param name="change">
		/// A <see cref="NSDictionary"/>
		/// </param>
		/// <param name="context">
		/// A <see cref="IntPtr"/>
		/// </param>
		[Export("observeValueForKeyPath:ofObject:change:context:")]
		private void observeValueForKeyPath(NSString keyPath, NSArrayController ofObject, NSDictionary change, IntPtr context)
		{
			Console.Write(String.Format("Table selection changed: keyPath = {0} : ",
			                                keyPath.ToString()));
			for(uint idx = 0; idx < ofObject.SelectionIndexes.Count; idx++)
			{
				Console.Write(ofObject.SelectionIndexes.IndexGreaterThanOrEqual(idx) + " ");
			}
			Console.WriteLine();
		}

		/// <summary>
		/// Ask the edit form to display itself to enter new record values
		/// </summary>
		/// <param name="sender">
		/// A <see cref="NSButton"/>
		/// </param>
		partial void add (NSButton sender)
		{
				// setup the edit sheet controller if one hasn't been setup already
				if (myEditController == null)
					myEditController = new EditController();
				
				// ask our edit sheet for information on the record we want to add
				NSMutableDictionary newValues = myEditController.edit(null, this);

				if (!myEditController.Cancelled)
				{
					// add the new entry
					myContentArray.AddObject(newValues);
				}			
		}
		
		/// <summary>
		/// Remove an entry.
		/// </summary>
		/// <param name="sender">
		/// A <see cref="NSButton"/>
		/// </param>
		partial void remove (NSButton sender)
		{
			myContentArray.RemoveAt(myContentArray.SelectionIndex);
		}
	}
}