public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();

			this.BtnClose.Activated += (object sender, EventArgs e) => {
				this.Close ();
				NSApplication.SharedApplication.StopModalWithCode (0);
			};
			this.BtnAdd.Activated += OnClickAddGroupButton;

			foreach(NSTableColumn column in GroupsTableView.TableColumns())
			{
				GroupsTableView.RemoveColumn (column);
			}
			GroupsTableView.Delegate = new TableDelegate ();
			var groupInfo = new GroupMembershipDto (){ Groups = new List<GroupDto> () };
			ActionHelper.Execute (delegate() {
				var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken (ServerDto.ServerName);
				groupInfo = SnapInContext.Instance.ServiceGateway.Tenant.Search (ServerDto, TenantName, DomainName, MemberType.GROUP, SearchType.NAME, auth.Token);
			});
				
			var listView = new GroupsDataSource { Entries = groupInfo.Groups };
			var columnNames = new List<ColumnOptions> {
				new ColumnOptions{ Id = "Name", DisplayName = "Group Name", DisplayOrder = 1, Width = 500 },
				};
			var columns = ListViewHelper.ToNSTableColumns (columnNames);
			foreach (var column in columns) {
				GroupsTableView.AddColumn (column);
			}
			GroupsTableView.DataSource = listView;
			GroupsTableView.ReloadData ();
		}
		public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();
			UserDto = UserDtoOriginal.DeepCopy ();
			Groups = new List<GroupDto> (GroupsOriginal);

			TxtUsername.StringValue = UserDto.Name == null ? string.Empty : UserDto.Name;
			TxtFirstName.StringValue = UserDto.PersonDetails.FirstName == null ? string.Empty : UserDto.PersonDetails.FirstName;
			TxtLastName.StringValue = UserDto.PersonDetails.LastName == null ? string.Empty : UserDto.PersonDetails.LastName;
			TxtEmail.StringValue = UserDto.PersonDetails.EmailAddress == null ? string.Empty : UserDto.PersonDetails.EmailAddress;
			TxtDescription.StringValue = UserDto.PersonDetails.Description == null ? string.Empty : UserDto.PersonDetails.Description;
			if (UserDto.PasswordDetails != null) {
				TxtPasswordLastChanged.StringValue = DateTimeHelper.UnixToWindows ((long)UserDto.PasswordDetails.LastSet).ToString ("dd-MMM-yyyy hh:mm:ss");
				TxtPasswordDaysUntilExpiry.StringValue = SecondsToDaysAndHours (UserDto.PasswordDetails.Lifetime);
			}
			ChDisabled.StringValue = UserDto.Disabled ? "0" : "1";
			//Window.Title = UserDto.Name + "@" + UserDto.Domain + " Properties";
			ChActAsUser.StringValue = UserDto.ActAsUsers ? "1" : "0";
			ChIdpAdmin.StringValue = UserDto.IsIdpAdmin ? "1" : "0";
			RdoRoleGroup.SelectCell (new NSCell (UserDto.Role.ToString ()));

			lstGroups = new NSTableView ();
			lstGroups.Delegate = new TableDelegate ();
			this.LstGroupMembershipView.AddSubview (lstGroups);
			var listView = new GroupsDataSource { Entries = Groups };
			var columnNames = new List<ColumnOptions> {
				new ColumnOptions{ Id = "Name", DisplayName = "Group Name", DisplayOrder = 1, Width = 300 }
			};
			var columns = ListViewHelper.ToNSTableColumns (columnNames);
			foreach (var column in columns) {
				lstGroups.AddColumn (column);
			}
			lstGroups.DataSource = listView;
			lstGroups.ReloadData ();

			//Events
			this.BtnSave.Activated += OnClickSaveButton;
			this.BtnAddGroup.Activated += OnClickAddGroupButton;
			this.BtnRemoveGroup.Activated += OnClickRemoveGroupButton;
//			this.BtnClose.Activated += (object sender, EventArgs e) => {
//				this.Close ();
//				NSApplication.SharedApplication.StopModalWithCode (0);
//			};
		}
		public override void AwakeFromNib ()
		{
			base.AwakeFromNib ();
			UserDto = UserDtoOriginal.DeepCopy ();
			Groups = new List<GroupDto> (GroupsOriginal);

			TxtUsername.StringValue = UserDto.Name == null ? string.Empty : UserDto.Name.Length > 10 ? UserDto.Name.Substring(0,10) + "..." : UserDto.Name;
			TxtFirstName.StringValue = UserDto.PersonDetails.FirstName == null ? string.Empty : UserDto.PersonDetails.FirstName;
			TxtLastName.StringValue = UserDto.PersonDetails.LastName == null ? string.Empty : UserDto.PersonDetails.LastName;
			TxtEmail.StringValue = UserDto.PersonDetails.EmailAddress == null ? string.Empty : UserDto.PersonDetails.EmailAddress;
			TxtDescription.StringValue = UserDto.PersonDetails.Description == null ? string.Empty : UserDto.PersonDetails.Description;
			chkActive.StringValue = UserDto.Disabled ? "0" : "1";
			ChActAsUser.StringValue = UserDto.ActAsUsers ? "1" : "0";
			ChIdpAdmin.StringValue = UserDto.IsIdpAdmin ? "1" : "0";
			var roleTag = UserDto.Role == UserRole.Administrator ? 1 : UserDto.Role == UserRole.RegularUser ? 2 : 3; 
			RdoRoleGroup.SelectCellWithTag ((nint)roleTag);

			foreach(NSTableColumn column in MemberTableView.TableColumns())
			{
				MemberTableView.RemoveColumn (column);
			}
			MemberTableView.Delegate = new TableDelegate ();
			var listView = new GroupsDataSource { Entries = Groups };
			var columnNames = new List<ColumnOptions> {
				new ColumnOptions{ Id = "Name", DisplayName = "Group Name", DisplayOrder = 1, Width = 200 }
			};
			var columns = ListViewHelper.ToNSTableColumns (columnNames);
			foreach (var column in columns) {
				MemberTableView.AddColumn (column);
			}
			MemberTableView.DataSource = listView;
			MemberTableView.ReloadData ();
			BtnApply.Hidden = !IsSystemDomain;

			//Events
			this.BtnApply.Activated += OnClickSaveButton;
			this.BtnAddGroup.Activated += OnClickAddGroupButton;
			this.BtnRemoveGroup.Activated += OnClickRemoveGroupButton;
		}
		public void OnClickRemoveGroupButton (object sender, EventArgs e)
		{
			if (MemberTableView.SelectedRows.Count > 0) {
				foreach (var row in MemberTableView.SelectedRows) {
					Groups.RemoveAt ((int)row);
				}
				var listView = new GroupsDataSource { Entries = Groups };
				MemberTableView.DataSource = listView;
				MemberTableView.ReloadData ();
			}
		}
		public void OnClickAddGroupButton (object sender, EventArgs e)
		{
			NSApplication.SharedApplication.StopModal ();
			var form = new ShowAllGroupsController ();
			form.ServerDto = ServerDto;
			form.TenantName = TenantName;
			form.DomainName = UserDto.Domain;
			var result = NSApplication.SharedApplication.RunModalForWindow (form.Window);
			if (result == VMIdentityConstants.DIALOGOK) {
				foreach (var group in form.SelectedGroups) {
					var principalName = group.GroupName + "@" + group.GroupDomain;
					if (!Groups.Exists (x => (x.GroupName + "@" + x.GroupDomain) == principalName))
						Groups.Add (group);
				}
				var listView = new GroupsDataSource { Entries = Groups };
				MemberTableView.DataSource = listView;
				MemberTableView.ReloadData ();
			}
		}
		public void OnClickRemoveGroupButton (object sender, EventArgs e)
		{
			foreach (var row in lstGroups.SelectedRows) {
				Groups.RemoveAt ((int)row);
			}
			var listView = new GroupsDataSource { Entries = Groups };
			lstGroups.DataSource = listView;
			lstGroups.ReloadData ();
		}
            public override void SelectionDidChange (NSNotification notification)
            {
                nint row = _controller.splitViewController.MainOutlineView.SelectedRow;
                if (row >= (nint)0) {
                    NSObject item = _controller.splitViewController.MainOutlineView.ItemAtRow ((int)row);
                    _controller.SetDefaultView ();
                    //NSTableView table = _controller.splitViewController.MainTableView;
                    _controller.RemoveTableColumns ();
                    List<ColumnOptions> columns = new List<ColumnOptions> ();
                    NSTableViewDataSource datasource;
                    _controller.CurrentSelectedNode = item as ScopeNode;
                    if (item is TrustedCertificateNode) {
                        _controller.AddToolbarItem.Label = "Add Cert Chain";
                        _controller.AddToolbarItem.Active = true;
                        _controller.DeleteToolbarItem.Label = "Delete Cert Chain";
                        _controller.DeleteToolbarItem.Active = true;
                        var trustedCertificateNode = item as TrustedCertificateNode;
                        var certificates = trustedCertificateNode.GetCertificates ();
                        datasource = new TrustedCertificatesDataSource{ Entries = certificates };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 100 },
                            new ColumnOptions{ DisplayName = "Status", Id = "Status", Width = 40 },
                            new ColumnOptions{ DisplayName = "Issued By", Id = "IssuedBy", Width = 150 },
                            new ColumnOptions{ DisplayName = "Issued On", Id = "IssuedOn", Width = 60 },
                            new ColumnOptions{ DisplayName = "Expiration", Id = "Expiration", Width = 60 },
                            new ColumnOptions{ DisplayName = "Purpose", Id = "Purpose", Width = 250 },
                        };
                    } else if (item is UsersNode) {
                        var node = item as UsersNode;
                        _controller.AddToolbarItem.Label = "Add User";
                        _controller.AddToolbarItem.Active = node.IsSystemDomain;
                        _controller.DeleteToolbarItem.Label = "Delete User";
                        _controller.DeleteToolbarItem.Active = node.IsSystemDomain;
                        var users = node.GetUsers (string.Empty);
                        datasource = new UsersDataSource{ Entries = users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 100 },
                            new ColumnOptions{ DisplayName = "First Name", Id = "FirstName", Width = 100 },
                            new ColumnOptions{ DisplayName = "Last Name", Id = "LastName", Width = 100 },
                            new ColumnOptions{ DisplayName = "Email", Id = "Email", Width = 80 },
                            new ColumnOptions{ DisplayName = "Description", Id = "Description", Width = 150 }
                        };
                    } else if (item is SolutionUsersNode) {
                        var node = item as SolutionUsersNode;
                        _controller.AddToolbarItem.Label = "Add Solution User";
                        _controller.AddToolbarItem.Active = node.IsSystemDomain;
                        _controller.DeleteToolbarItem.Label = "Delete Solution User";
                        _controller.DeleteToolbarItem.Active = node.IsSystemDomain;
                        var users = node.GetUsers (string.Empty);
                        datasource = new SolutionUsersDataSource{ Entries = users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 100 },
                            new ColumnOptions{ DisplayName = "Description", Id = "Description", Width = 150 },
                            new ColumnOptions{ DisplayName = "Disabled", Id = "Disabled", Width = 60 }
                        };
                    } else if (item is GroupsNode) {
                        var node = item as GroupsNode;
                        _controller.AddToolbarItem.Label = "Add Group";
                        _controller.AddToolbarItem.Active = node.IsSystemDomain;
                        _controller.DeleteToolbarItem.Label = "Delete Group";
                        _controller.DeleteToolbarItem.Active = node.IsSystemDomain;
                        var users = node.GetGroups (string.Empty);
                        datasource = new GroupsDataSource{ Entries = users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 100 },
                            new ColumnOptions{ DisplayName = "Domain", Id = "Domain", Width = 100 },
                            new ColumnOptions{ DisplayName = "Description", Id = "Description", Width = 150 }
                        };
                    } else if (item is RelyingPartyNode) {
                        _controller.AddToolbarItem.Label = "Add Relying Party";
                        _controller.AddToolbarItem.Active = true;
                        _controller.DeleteToolbarItem.Label = "Delete Relying Party";
                        _controller.DeleteToolbarItem.Active = true;
                        var node = item as RelyingPartyNode;
                        var users = node.GetRelyingParty ();
                        datasource = new RelyingPartyDataSource{ Entries = users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 100 },
                            new ColumnOptions{ DisplayName = "Url", Id = "Url", Width = 150 }
                        };
                    } else if (item is IdentityProvidersNode) {
                        _controller.AddToolbarItem.Label = "Add External IDP";
                        _controller.AddToolbarItem.Active = true;
                        _controller.DeleteToolbarItem.Label = "Delete External IDP";
                        _controller.DeleteToolbarItem.Active = true;
                        var node = item as IdentityProvidersNode;
                        var users = node.GetIdentityProviders ();
                        datasource = new IdentityProvidersDataSource{ Entries = users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Entity Id", Id = "Name", Width = 150 },
                            new ColumnOptions{ DisplayName = "Jit", Id = "Jit", Width = 60 }
                        };
                    } else if (item is OidcClientNode) {
                        _controller.AddToolbarItem.Label = "Add OIDC Client";
                        _controller.AddToolbarItem.Active = true;
                        _controller.DeleteToolbarItem.Label = "Delete OIDC Client";
                        _controller.DeleteToolbarItem.Active = true;
                        var node = item as OidcClientNode;
                        var users = node.GetOidcClients ();
                        datasource = new OidcClientDataSource{ Entries = users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Client Id", Id = "Name", Width = 150 },
                            new ColumnOptions{ DisplayName = "Certificate DN", Id = "CertificateDN", Width = 150 }
                        };
                    } else if (item is ServerNode) {
                        _controller.AddToolbarItem.Label = "Add Server";
                        _controller.AddToolbarItem.Active = false;
                        _controller.DeleteToolbarItem.Label = "Delete Server";
                        _controller.DeleteToolbarItem.Active = false;
                        ScopeNode node = item as ScopeNode;
                        datasource = new NodesDefaultDataSource{ Entries = node.Children };
                        columns = new List<ColumnOptions> { new ColumnOptions {
                                DisplayName = "Name",
                                Id = "Name",
                                Width = 250,
                            }
                        };
                    } else if (item is ExternalDomainsNode) {
                        _controller.AddToolbarItem.Label = "External Domain";
                        _controller.AddToolbarItem.Active = true;
                        _controller.DeleteToolbarItem.Label = "Delete External Domain";
                        _controller.DeleteToolbarItem.Active = false;
                        ScopeNode node = item as ScopeNode;
                        datasource = new NodesDefaultDataSource{ Entries = node.Children };
                        columns = new List<ColumnOptions> { new ColumnOptions {
                                DisplayName = "Name",
                                Id = "Name",
                                Width = 250,
                            }
                        };
                    } else if (item is ExternalDomainNode) {
                        _controller.AddToolbarItem.Label = "External Domain";
                        _controller.AddToolbarItem.Active = true;
                        _controller.DeleteToolbarItem.Label = "Delete External Domain";
                        _controller.DeleteToolbarItem.Active = true;
                        ScopeNode node = item as ScopeNode;
                        datasource = new NodesDefaultDataSource{ Entries = node.Children };
                        columns = new List<ColumnOptions> { new ColumnOptions {
                                DisplayName = "Name",
                                Id = "Name",
                                Width = 250,
                            }
                        };
                    } else if (item is TenantNode) {
                        _controller.AddToolbarItem.Label = "Add Tenant";
                        _controller.AddToolbarItem.Active = ((TenantNode)item).IsSystemTenant;
                        _controller.DeleteToolbarItem.Label = "Delete Tenant";
                        _controller.DeleteToolbarItem.Active = true;
                        _controller.PropertiesToolbarItem.Label = "Tenant Configuration";
                        ScopeNode node = item as ScopeNode;
                        datasource = new NodesDefaultDataSource{ Entries = node.Children };
                        columns = new List<ColumnOptions> { new ColumnOptions {
                                DisplayName = "Name",
                                Id = "Name",
                                Width = 250,
                            }
                        };
                    } else {
                        _controller.AddToolbarItem.Active = false;
                        _controller.DeleteToolbarItem.Active = false;
                        ScopeNode node = item as ScopeNode;
                        datasource = new NodesDefaultDataSource{ Entries = node.Children };
                        columns = new List<ColumnOptions> { new ColumnOptions {
                                DisplayName = "Name",
                                Id = "Name",
                                Width = 250,
                            }
                        };
                    }

                    foreach (var col in ListViewHelper.ToNSTableColumns (columns)) {
                        _controller.splitViewController.MainTableView.AddColumn (col);
                    }
                    _controller.splitViewController.MainTableView.DataSource = datasource;
                    _controller.splitViewController.MainTableView.ReloadData ();
                    var previous = _controller.splitViewController.MainTableView.SelectedRow;
                    if (_controller.splitViewController.MainTableView.RowCount > 0)
                        _controller.splitViewController.MainTableView.SelectRow (0, false);
                    if (previous == 0)
                        _controller.splitViewController.MainTableView.Delegate.SelectionDidChange (notification);

                    //GetCrumb (_controller.CurrentSelectedNode, _controller.CrumbTableView);
                }
            }
        partial void SearchText (AppKit.NSSearchField sender)
        {
            ActionHelper.Execute (delegate() {
                var item = CurrentSelectedNode;
                INSTableViewDataSource datasource = null; 
                var columns = new List<ColumnOptions> ();
                RemoveTableColumns ();
                if ((item is UsersNode) || (item is SolutionUsersNode) || (item is GroupsNode)) {
                    var domainName = string.Empty;
                    var tenant = item.GetTenant ();
                    var auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken (_serverDto.ServerName);
                    domainName = (item is UsersNode) 
						? ((UsersNode)item).DomainName : (item is SolutionUsersNode) 
						? ((SolutionUsersNode)item).DomainName : (item is GroupsNode) 
						? ((GroupsNode)item).DomainName : auth.Login.DomainName;
                    var memberType = (item is UsersNode) ? MemberType.USER : (item is SolutionUsersNode) ? MemberType.SOLUTIONUSER : MemberType.GROUP;
                    var result = SnapInContext.Instance.ServiceGateway.Tenant.Search (_serverDto, tenant, domainName, memberType, SearchType.NAME, auth.Token, sender.StringValue);
                    if (item is UsersNode) {
                        datasource = new UsersDataSource{ Entries = result.Users };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 100 },
                            new ColumnOptions{ DisplayName = "First Name", Id = "FirstName", Width = 100 },
                            new ColumnOptions{ DisplayName = "Last Name", Id = "LastName", Width = 100 },
                            new ColumnOptions{ DisplayName = "Email", Id = "Email", Width = 80 },
                            new ColumnOptions{ DisplayName = "Description", Id = "Description", Width = 150 }
                        };
                    } else if (item is SolutionUsersNode) {
                        datasource = new SolutionUsersDataSource{ Entries = result.SolutionUsers };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 150 },
                            new ColumnOptions{ DisplayName = "Disabled", Id = "Disabled", Width = 60 },
                            new ColumnOptions{ DisplayName = "Description", Id = "Description", Width = 250 }
                        };
                    } else if (item is GroupsNode) {
                        datasource = new GroupsDataSource{ Entries = result.Groups };
                        columns = new List<ColumnOptions> { 
                            new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 150 },
                            new ColumnOptions{ DisplayName = "Domain", Id = "Domain", Width = 150 },
                            new ColumnOptions{ DisplayName = "Description", Id = "Description", Width = 250 }
                        };
                    } 
                } else if (item is RelyingPartyNode) {
                    var source = splitViewController.MainTableView.DataSource as RelyingPartyDataSource;
                    var nodes = source.Entries.FindAll (x => x.Name.Contains (sender.StringValue));
                    datasource = new RelyingPartyDataSource{ Entries = nodes };
                    columns = new List<ColumnOptions> { 
                        new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 150 },
                        new ColumnOptions{ DisplayName = "Url", Id = "Url", Width = 250 }
                    };
                } else if (item is TrustedCertificateNode) {
                    var source = splitViewController.MainTableView.DataSource as TrustedCertificatesDataSource;
                    var nodes = source.Entries.FindAll (x => x.Chain.Contains (sender.StringValue));
                    datasource = new TrustedCertificatesDataSource{ Entries = nodes };
                    columns = new List<ColumnOptions> { 
                        new ColumnOptions{ DisplayName = "Name", Id = "Name", Width = 80 },
                        new ColumnOptions{ DisplayName = "Status", Id = "Status", Width = 40 },
                        new ColumnOptions{ DisplayName = "Issued By", Id = "IssuedBy", Width = 100 },
                        new ColumnOptions{ DisplayName = "Issued On", Id = "IssuedOn", Width = 80 },
                        new ColumnOptions{ DisplayName = "Expiration", Id = "Expiration", Width = 80 },
                        new ColumnOptions{ DisplayName = "Purpose", Id = "Purpose", Width = 150 },
                    };
                } else if (item is IdentityProvidersNode) {
                    var source = splitViewController.MainTableView.DataSource as IdentityProvidersDataSource;
                    var nodes = source.Entries.FindAll (x => x.EntityID.Contains (sender.StringValue));
                    datasource = new IdentityProvidersDataSource{ Entries = nodes };
                    columns = new List<ColumnOptions> { 
                        new ColumnOptions{ DisplayName = "Entity Id", Id = "Name", Width = 250 },
                        new ColumnOptions{ DisplayName = "Jit", Id = "Jit", Width = 60 }
                    };
                } else if (item is OidcClientNode) {
                    var source = splitViewController.MainTableView.DataSource as OidcClientDataSource;
                    var nodes = source.Entries.FindAll (x => x.ClientId.Contains (sender.StringValue));
                    datasource = new OidcClientDataSource{ Entries = nodes };
                    columns = new List<ColumnOptions> { 
                        new ColumnOptions{ DisplayName = "Client Id", Id = "Name", Width = 250 },
                        new ColumnOptions{ DisplayName = "Certificate DN", Id = "CertificateDN", Width = 350 }
                    };
                } else {
                    var source = splitViewController.MainTableView.DataSource as NodesDefaultDataSource;
                    var nodes = source.Entries.FindAll (x => x.DisplayName.Contains (sender.StringValue));
                    datasource = new NodesDefaultDataSource (){ Entries = nodes };
                    columns = new List<ColumnOptions> { new ColumnOptions {
                            DisplayName = "Name",
                            Id = "Name",
                            Width = 250,
                        }
                    };
                }
                foreach (var col in ListViewHelper.ToNSTableColumns (columns)) {
                    splitViewController.MainTableView.AddColumn (col);
                }
                splitViewController.MainTableView.DataSource = datasource;
            });
        }