using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Interop.Word; 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.Bates { public class BatesTemplateListContentViewModel : BindableBase, INavigationAware { private CPDFViewer PDFViewer; public IEventAggregator eventAggregator; public BatesInfo BatesInfo = new BatesInfo(); public BatesHeaderFooterItem BatesItem = new BatesHeaderFooterItem(); public int TemplateIndex = 0; private ObservableCollection batesModCollection = new ObservableCollection(); public ObservableCollection BatesModCollection { get { return batesModCollection; } set { batesModCollection = 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 BatesTemplateListContentViewModel(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.BatesTemplateList == null) { Settings.Default.BatesTemplateList = new BatesTemplateList(); } GetBatesSource(); } 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 BatesItemunicode) { BatesHeaderFooterItem batesItem = BatesItemunicode.Status; Settings.Default.BatesTemplateList[TemplateIndex] = batesItem; Settings.Default.Save(); GetBatesSource(); } 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.BatesTemplateList.IndexOf(template); template.listIndex = TemplateIndex; ConvertItemToInfo(template, ref BatesInfo); this.eventAggregator.GetEvent().Publish(new BatesHeaderFooterItemUnicode { Unicode = Unicode, Type = "BatesType", Status = template }); } public void DeleteTemplateItem(object e) { var btn = e as System.Windows.Controls.Control; if (btn == null) { return; } BatesItem = btn.DataContext as BatesHeaderFooterItem; if (BatesItem == null) { return; } Settings.Default.BatesTemplateList.Remove(BatesItem); Settings.Default.Save(); BatesModCollection.Remove(BatesItem); GetBatesSource(); } public void DeleteAllTemplateItem() { List removebatesItems = new List(); for (int i = 0; i < Settings.Default.BatesTemplateList.Count;i++) { var template = Settings.Default.BatesTemplateList[i]; removebatesItems.Add(template); } foreach (var removetemplate in removebatesItems) { Settings.Default.BatesTemplateList.Remove(removetemplate); Settings.Default.Save(); BatesModCollection.Remove(removetemplate); } GetBatesSource(); } public void SelectTemplateItem(object e) { var listBox = e as ListBox; BatesHeaderFooterItem BatesItem = listBox.SelectedItem as BatesHeaderFooterItem; SendTemplateItemToDocument(BatesItem); } public void SendTemplateItemToDocument(BatesHeaderFooterItem template) { if (template != null) { ConvertItemToInfo(template, ref BatesInfo); eventAggregator.GetEvent().Publish(new BatesInfoUnicode { Unicode = Unicode, Status = BatesInfo }); } } private void GetBatesSource() { List batesModColorTemplateList = new List(); for (int temp = 0; temp < Settings.Default.BatesTemplateList.Count; temp++) { Settings.Default.BatesTemplateList[temp].ItemName = (temp + 1).ToString(); batesModColorTemplateList.Add(Settings.Default.BatesTemplateList[temp]); } BatesModCollection = new ObservableCollection(batesModColorTemplateList); CheckTemplateListIsEmpty(batesModColorTemplateList); } public void ConvertItemToInfo(BatesHeaderFooterItem batesItem, ref BatesInfo batesInfo) { batesInfo.TextData = batesItem.TextData; batesInfo.ItemName = batesItem.ItemName; batesInfo.DigitNumber = batesItem.DigitNumber; batesInfo.Prefix=batesItem.Prefix; batesInfo.Suffix=batesItem.Suffix; batesInfo.StarPagetNumber = batesItem.StarPagetNumber; batesInfo.margin = batesItem.margin; batesInfo.PageRangeIndex = batesItem.PageRangeIndex; batesInfo.PageRange= batesItem.PageRange; } private void CheckTemplateListIsEmpty(List batesTemplateList) { if (batesTemplateList.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(); } } }