/// <summary>
        ///
        /// </summary>
        private void Page_dialog_AppliedChanges(object sender, CancelRoutedEventArgs e)
        {
            // Call the univeral applied change handler
            Dialog_AppliedChanges <Oltp.PageRow>(Page_dialog, "Editing Page", _listTable._listView, e);

            // Disable editing page value
            //_urlField.IsReadOnly = true;

            // Set correct data template
            Oltp.PageRow dataItem = Page_dialog.TargetContent as Oltp.PageRow;
            ListViewItem item     = _listTable._listView.ItemContainerGenerator.ContainerFromItem(dataItem) as ListViewItem;

            (this.Resources["NameTemplateSelector"] as MasterPagesLocal.NameTemplateSelector)
            .ApplyTemplate(dataItem, item);
        }
        /*=========================*/
        #endregion

        #region Keywords
        /*=========================*/

        /// <summary>
        /// Add a gateway to an dataItem
        /// </summary>
        private void Page_dialog_New(object sender, RoutedEventArgs e)
        {
            // Create an editable new row
            Oltp.PageRow editVersion = Dialog_MakeEditVersion <Oltp.PageDataTable, Oltp.PageRow>(_pages, null);
            editVersion.AccountID   = this.Window.CurrentAccount.ID;
            editVersion.IsMonitored = true;

            // Show the dialog
            Page_dialog.Title = "New Page";

            _reservationTable = new Oltp.GatewayReservationDataTable();
            _reservationItems = new ObservableCollection <DataRow>();
            _reservationListView.ListView.ItemsSource = _reservationItems;

            Page_dialog.BeginEdit(editVersion, _pages);
        }
        public void ApplyTemplate(Oltp.PageRow dataItem, ListViewItem item)
        {
            if (item == null)
            {
                return;
            }

            Button nameButton = Visual.GetDescendant <Button>(item, "_itemName");

            if (nameButton == null)
            {
                return;
            }

            ContentPresenter cp = VisualTreeHelper.GetParent(nameButton) as ContentPresenter;

            cp.ContentTemplate = SelectTemplate(dataItem, item);
        }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Oltp.PageRow dataItem = item as Oltp.PageRow;

            if (dataItem == null)
            {
                return(new DataTemplate());
            }

            if (dataItem.Title != null && dataItem.Title.Trim().Length > 0)
            {
                return(App.CurrentPage
                       .FindResource("MonitoredNameTemplate") as DataTemplate);
                //.FindResource(dataItem.IsMonitored == true ? "MonitoredNameTemplate" : "UnmonitoredNameTemplate") as DataTemplate;
            }
            else
            {
                return(App.CurrentPage
                       .FindResource("MonitoredUrlTemplate") as DataTemplate);
                //.FindResource(dataItem.IsMonitored == true ? "MonitoredUrlTemplate" : "UnmonitoredUrlTemplate") as DataTemplate;
            }
        }
        /// <summary>
        /// Open the dialog
        /// </summary>
        private void Page_dialog_Open(object sender, RoutedEventArgs e)
        {
            // Set dataItem as current item
            ListViewItem currentItem = _listTable.GetParentListViewItem(e.OriginalSource as FrameworkElement);

            Oltp.PageRow row = currentItem.Content as Oltp.PageRow;

            // Show the dialog
            Page_dialog.Title = "Editing Page";

            // Disable editing page value
            //_urlField.IsReadOnly = true;

            Page_dialog.BeginEdit(
                Dialog_MakeEditVersion <Oltp.PageDataTable, Oltp.PageRow>(_pages, row),
                row
                );

            using (OltpLogicClient proxy = new OltpLogicClient())
            {
                _reservationTable = proxy.Service.GatewayReservation_GetByPage(Window.CurrentAccount.ID, row.GK);
            }

            _reservationItems = new ObservableCollection <DataRow>();
            foreach (DataRow r in _reservationTable.Rows)
            {
                _reservationItems.Add(r);
            }

            _reservationListView.ListView.ItemsSource = _reservationItems;

            // When opening, select it only if no more than one is already selected
            if (_listTable._listView.SelectedItems.Count < 2)
            {
                _listTable._listView.SelectedItems.Clear();
                currentItem.IsSelected = true;
            }
        }
        private void GatewayReservation_Add(object sender, RoutedEventArgs e)
        {
            Oltp.PageRow currentPage = Page_dialog.Content as Oltp.PageRow;
            long         fromID      = 0;
            long         toID        = 0;

            if (!Int64.TryParse(_reservationFrom.Text, out fromID) || !Int64.TryParse(_reservationTo.Text, out toID))
            {
                // Validation
                MessageBoxError("Please enter 2 valid numbers.", null);
                _reservationFrom.SelectAll();
                _reservationFrom.Focus();
                return;
            }

            long fromIDTemp = fromID;

            fromID = fromID <= toID ? fromID : toID;
            toID   = toID >= fromIDTemp ? toID: fromIDTemp;

            // Get other accounts to check against
            string[] otherAccountStrings = Window.CurrentAccount.GatewayAccountScope != null?Window.CurrentAccount.GatewayAccountScope.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) : new string[0];

            int[] otherAccounts = Array.ConvertAll <string, int>(otherAccountStrings, new Converter <string, int>(delegate(string input)
            {
                int val;
                if (!Int32.TryParse(input, out val))
                {
                    val = -1;
                }
                return(val);
            }));


            // Check if any gateways are already in use or reserved
            int nonReservableCount = 0;

            using (OltpLogicClient proxy = new OltpLogicClient())
            {
                Oltp.GatewayReservationDataTable existingReservations =
                    proxy.Service.GatewayReservation_GetByOverlap(Window.CurrentAccount.ID, fromID, toID, otherAccounts);

                // Get local additions as well
                DataRow[] localReservations =
                    _reservationTable.Select(String.Format(
                                                 "({0} >= {2} AND {0} <= {3}) OR ({1} >= {2} AND {1} <= {3})",
                                                 _reservationTable.FromGatewayColumn.ColumnName,
                                                 _reservationTable.ToGatewayColumn.ColumnName,
                                                 fromID,
                                                 toID
                                                 ));

                if (existingReservations.Rows.Count > 0 || localReservations.Length > 0)
                {
                    Oltp.GatewayReservationRow existingReservation =
                        (existingReservations.Rows.Count > 0 ?  existingReservations.Rows[0] : localReservations[0]) as Oltp.GatewayReservationRow;

                    MessageBoxError(String.Format(
                                        "An overlapping range ({0} to {1}) has already been reserved for {2} page.",
                                        existingReservation.FromGateway, existingReservation.ToGateway, existingReservation.PageGK == currentPage.GK ? "this" : "another"
                                        ), null);
                    _reservationFrom.SelectAll();
                    _reservationFrom.Focus();
                    return;
                }

                nonReservableCount = proxy.Service.Gateway_CountByRange(Window.CurrentAccount.ID, fromID, toID, otherAccounts);
            }

            if (nonReservableCount > 0)
            {
                MessageBoxResult result = MessageBox.Show(
                    String.Format("{0} gateway{1} already in use within the specified range. Only available numbers within the range will be reserved.\n\nProceed?",
                                  nonReservableCount > toID - fromID ? "All" : nonReservableCount.ToString(),
                                  nonReservableCount > 1 ? "s are" : " is"
                                  ),
                    "Confirmation",
                    MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK
                    );

                if (result == MessageBoxResult.Cancel)
                {
                    _reservationFrom.SelectAll();
                    _reservationFrom.Focus();
                    return;
                }
            }

            Oltp.GatewayReservationRow reservation = _reservationTable.NewGatewayReservationRow();
            reservation.AccountID          = Window.CurrentAccount.ID;
            reservation.PageGK             = currentPage.GK;
            reservation.FromGateway        = fromID;
            reservation.ToGateway          = toID;
            reservation.ReservedByUserID   = Window.CurrentUser.ID;
            reservation.ReservedByUserName = Window.CurrentUser.Name;
            _reservationTable.AddGatewayReservationRow(reservation);
            _reservationItems.Add(reservation);
        }