123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using InABox.Wpf;
- using System.Threading;
- namespace PRSDesktop
- {
- internal class JobScopeGrid : DynamicDataGrid<JobScope>, IMasterDetailControl<Job,JobScope>
- {
- private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
-
- public Job? Master { get; set; }
- public Filter<JobScope> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
- ? new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Master.ID)
- : new Filter<JobScope>().None();
-
- public JobScopeGrid()
- {
- HiddenColumns.Add(x=>x.Job.DefaultScope.ID);
- HiddenColumns.Add(x=>x.Job.ID);
- ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.End });
- }
-
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.RecordCount = true;
- options.SelectColumns = true;
- options.FilterRows = true;
- }
- public override void DeleteItems(params CoreRow[] rows)
- {
- base.DeleteItems(rows);
- var row = rows.FirstOrDefault(x => x.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == x.Get<JobScope, Guid>(x => x.ID));
- if (row is not null)
- {
- var job = Client.Query(
- new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
- Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
- ).Rows.FirstOrDefault()?.ToObject<Job>();
- if (job != null)
- {
- job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
- Client.Save(job, "Updated Default Scope");
- }
- }
- }
- private BitmapImage? IsDefaultImage(CoreRow? row)
- {
- return row == null
- ? tick
- : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
- ? tick
- : null;
- }
- private bool SelectDefaultAction(CoreRow? row)
- {
- if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
- return false;
- using (new WaitCursor())
- {
- var job = new Client<Job>().Query(
- new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
- Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
- ).Rows.FirstOrDefault()?.ToObject<Job>();
- if (job != null)
- {
- job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
- new Client<Job>().Save(job, "Updated Default Scope");
- return true;
- }
- }
- return false;
- }
- protected override bool CanCreateItems()
- {
- return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
- }
-
- public override JobScope CreateItem()
- {
- var result = base.CreateItem();
- result.Job.ID = Master?.ID ?? Guid.Empty;
- result.Job.Synchronise(Master ?? new Job());
- result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
- return result;
- }
- protected override void Reload(
- Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(MasterDetailFilter);
- base.Reload(criteria, columns, ref sort, token, action);
- }
-
- private DynamicGridTreeUIComponent<JobScope, Guid>? _uiComponent;
- private DynamicGridTreeUIComponent<JobScope, Guid> UIComponent
- {
- get
- {
- if(_uiComponent is null)
- {
- _uiComponent = new DynamicGridTreeUIComponent<JobScope, Guid>(
- x => x.ID,
- x => x.Parent.ID,
- Guid.Empty)
- {
- Parent = this,
- MaxRowHeight = 30,
- };
- //_uiComponent.OnContextMenuOpening += JobDocumentSetFolderTree_OnContextMenuOpening;
- }
- return _uiComponent;
- }
- }
- protected override IDynamicGridUIComponent<JobScope> CreateUIComponent()
- {
- return UIComponent;
- }
-
- }
- }
|