// Copyright (C) Stichting Deltares 2024. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Reflection;
using Deltares.Dam.Data;
using Deltares.Standard;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.EventPublisher.Enum;
using Deltares.Standard.Forms;
using Deltares.Standard.Forms.DExpress;
using Deltares.Standard.Language;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
namespace Deltares.Dam.Forms
{
public class LocationScenariosControl : GridViewControl, IPropertyControl
{
private readonly BarEditItem userContextFilterBarEditItem;
private Location location;
public LocationScenariosControl()
{
userContextFilterBarEditItem = new BarEditItem(barManager1);
userContextFilterBarEditItem.Name = "UserContextFilterBarEditItem";
userContextFilterBarEditItem.Caption = "UserContextFilterBarEdit";
Toolbar.AddItem(userContextFilterBarEditItem);
userContextFilterBarEditItem.Width = 110;
userContextFilterBarEditItem.Edit = new RepositoryItemComboBox();
SelectedContextFilter = CategoryAll;
Name = "Scenarios";
LocalizationSupport.Register(typeof(Location), this);
InitializeBinding();
BindSupport.Assign(ControlPanel, this);
PropertyEditorReactionType = PropertyEditorReactionType.Ignore;
}
public bool HasItems { get; private set; }
public bool IsVisible
{
get
{
return Data.Location.DamProjectType == DamProjectType.Design;
}
}
public object SelectedObject
{
get
{
return location;
}
set
{
if (value is Location)
{
location = (Location) value;
}
else if (value is LocationJob)
{
location = ((LocationJob) value).Location;
}
BindSupport.Assign(this, location);
}
}
protected override void Dispose(bool disposing)
{
LocalizationManager.CultureChanged -= LocalizationManager_CultureChanged;
DataEventPublisher.OnChanged -= DataEventPublisherOnOnAfterChange;
GridView.DataSourceChanged -= GridViewOnDataSourceChanged;
DataEventPublisher.OnDataListModified -= DataEventPublisher_OnDataListModified;
LocalizationSupport.Unregister(typeof(Location), this);
base.Dispose(disposing);
}
internal CategoryDisplayItem SelectedContextFilter { get; set; }
private void InitializeBinding()
{
BindSupport.Bind(this, this, typeof(Location), "Scenarios");
BindSupport.Bind(ControlPanel, userContextFilterBarEditItem, x => x.SelectedContextFilter, BindingType.LabelAndValue);
LocalizationManager.CultureChanged += LocalizationManager_CultureChanged;
DataEventPublisher.OnChanged += DataEventPublisherOnOnAfterChange;
DataEventPublisher.AddListenerForSenders(EventType.Changed, this, this);
GridView.DataSourceChanged += GridViewOnDataSourceChanged;
DataEventPublisher.OnDataListModified += DataEventPublisher_OnDataListModified;
}
private void DataEventPublisher_OnDataListModified(object sender, PublishEventArgs e)
{
if (sender == GridControl.DataSource)
{
UpdateColumns();
}
}
private void GridViewOnDataSourceChanged(object sender, EventArgs eventArgs)
{
UpdateFilters();
}
private void UpdateFilters()
{
HasItems = false;
var combobox = userContextFilterBarEditItem.Edit as RepositoryItemComboBox;
var context = Context.CurrentContext as DamContext;
if (null != context && null != combobox && null != ItemType)
{
IEnumerable categories = context.GetColumnNamedFilters(ItemType);
int currentIndex = combobox.Items.IndexOf(userContextFilterBarEditItem.EditValue);
if (GridControl.DataSource != null)
{
combobox.Items.Clear();
combobox.Items.Add(CategoryAll);
foreach (string objectCategory in categories)
{
combobox.Items.Add(new CategoryDisplayItem(objectCategory, LocalizationManager.GetTranslatedText(ItemType, objectCategory)));
HasItems = true;
}
if (currentIndex > -1)
{
SelectedContextFilter = (CategoryDisplayItem) combobox.Items[currentIndex];
BindSupport.Update(ControlPanel);
}
}
}
}
private void DataEventPublisherOnOnAfterChange(object sender, PublishEventArgs publishEventArgs)
{
UpdateColumns();
}
private void LocalizationManager_CultureChanged(object sender, EventArgs e)
{
UpdateFilters();
}
private void UpdateColumns()
{
var combobox = userContextFilterBarEditItem.Edit as RepositoryItemComboBox;
var context = Context.CurrentContext as DamContext;
if (null != context && null != combobox)
{
IList visibleColumns = context.GetFilteredColumns(ItemType, SelectedContextFilter.Text);
foreach (GridColumn column in gridView1.Columns)
{
column.Visible = visibleColumns == null || visibleColumns.Contains(((MemberInfo) column.Tag).Name);
}
}
}
}
}