using ComPDFKitViewer.PdfViewer; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.EditTools.Bates; using PDF_Master.Model.EditTools.HeaderFooter; using PDF_Master.Properties; using PDFSettings; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; namespace PDF_Master.ViewModels.EditTools.HeaderFooter { public class HeaderFooterTemplateListContentViewModel : BindableBase, INavigationAware { public IEventAggregator eventAggregator; private CPDFViewer PDFViewer; public HeaderFooterInfo HeaderFooterInfo = new HeaderFooterInfo(); public BatesHeaderFooterItem HeaderFooterItem = new BatesHeaderFooterItem(); public int TemplateIndex = 0; private ObservableCollection headerFooterModCollection = new ObservableCollection(); public ObservableCollection HeaderFooterModCollection { get { return headerFooterModCollection; } set { headerFooterModCollection = value; RaisePropertyChanged(); } } private Visibility _createTemplateVisible; public Visibility CreateTemplateVisible { get { return _createTemplateVisible; } set { SetProperty(ref _createTemplateVisible, value); } } public DelegateCommand AddTemplateCommand { get; set; } public DelegateCommand DeleteTemplateItemCommand { get; set; } public DelegateCommand EditTemplateItemCommand { get; set; } public DelegateCommand DeleteAllTemplateItemCommand { get; set; } public DelegateCommand SelectTemplateItemCommand { get; set; } public DelegateCommand EnterCreateCommand { get; set; } public string Unicode = null; public HeaderFooterTemplateListContentViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; Unicode = App.mainWindowViewModel.SelectedItem.Unicode; EnterCreateCommand = new DelegateCommand(EnterCreate); AddTemplateCommand = new DelegateCommand(AddTemplate); EditTemplateItemCommand = new DelegateCommand(RequestEditTemplateItem); DeleteTemplateItemCommand = new DelegateCommand(DeleteTemplateItem); DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem); SelectTemplateItemCommand = new DelegateCommand(SelectTemplateItem); } private void InitBackgroundTemplateList() { if (Settings.Default.HeaderFooterTemplateList == null) { Settings.Default.HeaderFooterTemplateList = new HeaderFooterTemplateList(); } GetHeaderFooterSource(); } public void EnterCreate() { this.eventAggregator.GetEvent().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusCreate }); } public void AddTemplate() { this.eventAggregator.GetEvent().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusCreate }); } public void SaveEditedBatesTemplateItem(BatesHeaderFooterItemUnicode HeaderFooterItemunicode) { BatesHeaderFooterItem headerFooterItem = HeaderFooterItemunicode.Status; Settings.Default.HeaderFooterTemplateList[TemplateIndex] = headerFooterItem; Settings.Default.Save(); GetHeaderFooterSource(); } public void RequestEditTemplateItem(object e) { var control = e as Control; if (control == null) { return; } var template = control.DataContext as BatesHeaderFooterItem; if (template == null) { return; } TemplateIndex = Settings.Default.HeaderFooterTemplateList.IndexOf(template); template.listIndex = TemplateIndex; ConvertItemToInfo(template, ref HeaderFooterInfo); this.eventAggregator.GetEvent().Publish(new BatesHeaderFooterItemUnicode { Unicode = Unicode, Type = "HeaderFooterType", Status = template }); } public void DeleteTemplateItem(object e) { var btn = e as System.Windows.Controls.Control; if (btn == null) { return; } HeaderFooterItem = btn.DataContext as BatesHeaderFooterItem; if (HeaderFooterItem == null) { return; } Settings.Default.HeaderFooterTemplateList.Remove(HeaderFooterItem); Settings.Default.Save(); HeaderFooterModCollection.Remove(HeaderFooterItem); GetHeaderFooterSource(); } public void DeleteAllTemplateItem() { List removeheaderfooterItems = new List(); for (int i = 0; i < Settings.Default.HeaderFooterTemplateList.Count; i++) { var template = Settings.Default.HeaderFooterTemplateList[i]; removeheaderfooterItems.Add(template); } foreach (var removetemplate in removeheaderfooterItems) { Settings.Default.HeaderFooterTemplateList.Remove(removetemplate); Settings.Default.Save(); HeaderFooterModCollection.Remove(removetemplate); } GetHeaderFooterSource(); } public void SelectTemplateItem(object e) { var listBox = e as ListBox; BatesHeaderFooterItem HeaderFooterItem = listBox.SelectedItem as BatesHeaderFooterItem; SendTemplateItemToDocument(HeaderFooterItem); } public void SendTemplateItemToDocument(BatesHeaderFooterItem template) { if (template != null) { ConvertItemToInfo(template, ref HeaderFooterInfo); eventAggregator.GetEvent().Publish(new HeaderFooterInfoUnicode { Unicode = Unicode, Status = HeaderFooterInfo }); } } private void GetHeaderFooterSource() { List headerFooterModColorTemplateList = new List(); for (int temp = 0; temp < Settings.Default.HeaderFooterTemplateList.Count; temp++) { Settings.Default.HeaderFooterTemplateList[temp].ItemName = (temp + 1).ToString(); headerFooterModColorTemplateList.Add(Settings.Default.HeaderFooterTemplateList[temp]); } HeaderFooterModCollection = new ObservableCollection(headerFooterModColorTemplateList); CheckTemplateListIsEmpty(headerFooterModColorTemplateList); } public void ConvertItemToInfo(BatesHeaderFooterItem headerFooterItem, ref HeaderFooterInfo headerFooterInfo) { headerFooterInfo.TextData = headerFooterItem.TextData; headerFooterInfo.ItemName=headerFooterItem.ItemName; headerFooterInfo.DateTimeFormat = headerFooterItem.DateTimeFormat; headerFooterInfo.StarPagetNumber = headerFooterItem.StarPagetNumber; headerFooterInfo.PageNumberFormat = headerFooterItem.PageNumberFormat; headerFooterInfo.margin = headerFooterItem.margin; headerFooterInfo.PageRangeIndex = headerFooterItem.PageRangeIndex; headerFooterInfo.PageRange=headerFooterItem.PageRange; } private void CheckTemplateListIsEmpty(List headerFooterTemplateList) { if (headerFooterTemplateList.Count() == 0) { CreateTemplateVisible = Visibility.Visible; } else { CreateTemplateVisible = Visibility.Collapsed; } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); InitBackgroundTemplateList(); } } }