Files
BeWoPlaner/BeWo/View/Detail/VorlageEditView.xaml.cs
2024-10-08 10:56:28 +02:00

280 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BeWo.Controls;
using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.Validation;
using BeWo.View.Search;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BeWo.Core.Service;
using BS.Shared;
using BS.Shared.Extensions;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.ClientPartials;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Services;
using BS.Shared.Translation;
using DevExpress.Xpf.RichEdit;
using SupportConceptService = BeWo.Core.Service.SupportConceptService;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
using DevExpress.Office.Utils;
using System.Windows.Media.Imaging;
namespace BeWo.View.Detail
{
public partial class VorlageEditView
{
List<PlatzhalterVM> Templates = new List<PlatzhalterVM>();
Dictionary<string, BitmapImage> ImageTypeToImage = new Dictionary<string, BitmapImage>();
private DokumentvorlageVM _ViewModel;
public DokumentvorlageDC Vorlage;
public event Action ButtonSavedClicked;
public event Action ButtonCancelClicked;
public DokumentvorlageVM ViewModel { get; set; }
public VorlageEditView()
{
InitializeComponent();
}
public override bool IsDirty
{
get
{
//if (ViewModel.SelectedVMForEditing != null && _SelectedEmployee != null &&
// !_SelectedEmployee.Equals(ViewModel.SelectedVMForEditing.Employee))
// return true;
return _ViewModel != null && _ViewModel.IsDirty;
}
}
public void InitView(DokumentvorlageVM pVM, long? tabelle)
{
InitializeComponent();
ViewModel = pVM;
_ViewModel = pVM;
DataContext = pVM;
if(pVM.Name != null)
{
TextEdit_Vorlagenname.EditValue = pVM.Name;
TextEdit_Vorlagenname.DataContext = pVM.Name;
TextEdit_Vorlagenname.Text = pVM.Name;
}
if (pVM.Beschreibung != null)
{
TextEdit_Beschreibung.EditValue = pVM.Beschreibung;
TextEdit_Beschreibung.DataContext = pVM.Beschreibung;
TextEdit_Beschreibung.Text = pVM.Beschreibung;
}
if (pVM.Text != null)
{
EditorControl.RtfText = pVM.RTFText;
}
switch(tabelle)
{
case 1: Templates = TemplatesForSupportConcepts();break;
case 2: Templates = TemplatesForCustomers();break;
case 3: Templates = TemplatesForEmployee();break;
}
//LoadImagesForNodes(Templates);
TreeListControlTemplates.ItemsSource = Templates;
Focus();
}
private void LoadImagesForNodes(List<PlatzhalterVM> templates)
{
// Icons in ImageTypeToImage laden
foreach (var item in templates)
{
if (item.Children != null)
LoadImagesForNodes(item.Children);
if (item.IconType != null)
item.Icon = LoadImage(item.IconType);
}
}
private BitmapImage LoadImage(string imageName)
{
return new BitmapImage(new Uri("/Ressources/Icons/" + imageName + ".png", UriKind.Relative));
}
private PlatzhalterVM ConvertDataContractToViewModel(PlatzhalterDC dc)
{
var vm = new PlatzhalterVM()
{
Bezeichnung = dc.Bezeichnung,
Children = new List<PlatzhalterVM>(),
FieldName = dc.FieldName,
Icon = LoadImage(dc.IconType),
IconType = dc.IconType,
Platzhalter = dc.Platzhalter
};
if (dc.Children != null && dc.Children.Count > 0)
{
foreach (var item in dc.Children)
vm.Children.Add(ConvertDataContractToViewModel(item));
}
return vm;
}
private List<PlatzhalterVM> ConvertListOfDataContractsToViewModels(List<PlatzhalterDC> dcs)
{
List<PlatzhalterVM> vms = new List<PlatzhalterVM>();
foreach (var dc in dcs)
vms.Add(ConvertDataContractToViewModel(dc));
return vms;
}
//private List<PlatzhalterDC> LoadTemplates(long? tabelle)
//{
// List<PlatzhalterDC> templates = new List<PlatzhalterDC>();
// List<PlatzhalterDC> platzhalterDCs = new List<PlatzhalterDC>();
// switch (tabelle)
// {
// case 1: platzhalterDCs = ServiceFacade.DoOperationsServiceSync(s => s.GetPlatzhalterForSupportConcept()); break;
// case 2: platzhalterDCs = TemplatesForCustomers(); break;
// case 3: platzhalterDCs = TemplatesForEmployee(); break;
// }
// return templates;
//}
private List<PlatzhalterVM> TemplatesForSupportConcepts()
{
return ConvertListOfDataContractsToViewModels(ServiceFacade.DoOperationsServiceSync(s => s.GetPlatzhalterForSupportConcept()));
}
private List<PlatzhalterVM> TemplatesForCustomers()
{
return ConvertListOfDataContractsToViewModels(ServiceFacade.DoOperationsServiceSync(s => s.GetPlatzhalterForCustomer()));
}
private List<PlatzhalterVM> TemplatesForEmployee()
{
return ConvertListOfDataContractsToViewModels(ServiceFacade.DoOperationsServiceSync(s => s.GetPlatzhalterForEmployee()));
}
/*
private void combobox_templates_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (combobox_templates.SelectedItem == null)
return;
var selected = combobox_templates.SelectedItem as PlatzhalterDC;
if(selected.ToString().StartsWith("-") && selected.ToString().EndsWith("-"))
{
MessageBox.Show("Bindestriche kennzeichnen eine Vorlagenkategorie. Wählen Sie eine Vorlage ohne Bindestriche.", "BeWoPlaner", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
EditorControl.Document.InsertText(EditorControl.Document.CaretPosition, "[" + selected.Platzhalter + "]");
//EditorControl.Text += "[" + selected.Platzhalter + "]";
//EditorControl.RtfText += "[" + selected.Platzhalter + "]";
combobox_templates.SelectedItem = null;
}
*/
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if(TextEdit_Vorlagenname.Text.Equals(""))
{
MessageBox.Show("Bitte geben sie einen Namen für die Vorlage ein.", "BeWoPlaner", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
ButtonSavedClicked?.Invoke();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
bool close = true;
if (ViewModel.IsDirty)
{
if (MessageBox.Show(
Translator.Translate("Möchten Sie wirklich abbrechen und die Änderungen verwerfen?"),
"BeWoPlaner", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel)
{
close = false;
}
}
if (close)
{
ButtonCancelClicked?.Invoke();
}
}
private void TextEdit_Vorlagenname_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
{
if (TextEdit_Vorlagenname.EditValue != null)
ViewModel.Name = TextEdit_Vorlagenname.EditValue.ToString();
}
private void TextEdit_Beschreibung_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
{
if (TextEdit_Beschreibung.EditValue != null)
ViewModel.Beschreibung = TextEdit_Beschreibung.EditValue.ToString();
}
// Mouseup, um die aktuelle Position des Cursors zu speichern, um dort den Platzhalter an der aktuellen Stelle einzusetzen
// Klappt nur, wenn in Text geklickt wird und nicht hinter ein Wort..
// Hat aber keine verwendbaren Inhalte
private void richEditControl1_MouseUp(object sender, MouseButtonEventArgs e)
{
RichEditControl richEditControl = sender as RichEditControl;
System.Windows.Point point = e.GetPosition(richEditControl);
System.Drawing.Point docPoint = new System.Drawing.Point((int)point.X, (int)point.Y);
docPoint = Units.PixelsToDocuments(docPoint, richEditControl.DpiX, richEditControl.DpiY);
DocumentPosition pos = richEditControl.GetPositionFromPoint(docPoint);
//pos.
}
private void EditorControl_RtfTextChanged(object sender, EventArgs e)
{
ViewModel.Text = EditorControl.Text;
ViewModel.RTFText = EditorControl.RtfText;
}
private void TemplatesTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var platzhalter = TreeListControlTemplates.SelectedItem as PlatzhalterVM;
if (platzhalter.Platzhalter != null)
{
var doc = EditorControl.Document;
var selection = doc.Selection;
if(selection.Length > 0)
{
doc.Delete(selection);
}
var range = EditorControl.Document.InsertText(EditorControl.Document.CaretPosition, "[" + platzhalter.Platzhalter + "]");
EditorControl.Document.Selection = range;
}
}
}
}