Move command methods to static Commands class.
This commit is contained in:
parent
1d03ff0bbc
commit
06aa271056
199
zaaReloaded2/Commands.cs
Executable file
199
zaaReloaded2/Commands.cs
Executable file
@ -0,0 +1,199 @@
|
||||
using Bovender.Mvvm.Actions;
|
||||
/* Commands.cs
|
||||
* part of zaaReloaded2
|
||||
*
|
||||
* Copyright 2015 Daniel Kraus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using zaaReloaded2.Controller;
|
||||
using zaaReloaded2.Controller.Comments;
|
||||
using zaaReloaded2.Formatter;
|
||||
using zaaReloaded2.Importer.ZaaImporter;
|
||||
using zaaReloaded2.ViewModels;
|
||||
using zaaReloaded2.Views;
|
||||
using Word = Microsoft.Office.Interop.Word;
|
||||
|
||||
namespace zaaReloaded2
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
static class Commands
|
||||
{
|
||||
#region Command methods
|
||||
|
||||
public static void Format()
|
||||
{
|
||||
// If no "real" selection exists, attempt to auto-detect the lab data.
|
||||
// (NB Technically, there is never _no_ selection in a document.)
|
||||
Word.Window activeWindow = Globals.ThisAddIn.Application.ActiveWindow;
|
||||
Word.Selection sel = activeWindow.Selection;
|
||||
if (!(sel.Paragraphs.Count > 1
|
||||
|| (sel.Text.Length > 1 && sel.Text.EndsWith("\r"))))
|
||||
{
|
||||
if (!AutoDetect.Detect(activeWindow.Document))
|
||||
{
|
||||
NotificationAction a = new NotificationAction();
|
||||
a.Caption = "Formatieren nicht möglich";
|
||||
a.Message = "Das Dokument scheint keine Lauris-Labordaten zu enthalten.";
|
||||
a.OkButtonLabel = "Schließen";
|
||||
a.Invoke();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (CanFormat())
|
||||
{
|
||||
SettingsRepository repository = SettingsRepository.Load();
|
||||
Guid lastSettingsUid = Properties.Settings.Default.LastSettings;
|
||||
Settings lastSettings = repository.FindByGuid(lastSettingsUid);
|
||||
if (lastSettings != null)
|
||||
{
|
||||
DoFormat(lastSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
ChooseSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CanFormat()
|
||||
{
|
||||
return Globals.ThisAddIn.Application.ActiveDocument != null;
|
||||
//Word.Selection s = Globals.ThisAddIn.Application.ActiveWindow.Selection;
|
||||
//return s.Paragraphs.Count > 1 ||
|
||||
// (s.Text.Length > 1 && s.Text.EndsWith("\r"));
|
||||
}
|
||||
|
||||
public static void ChooseSettings()
|
||||
{
|
||||
SettingsRepository repository = SettingsRepository.Load();
|
||||
SettingsRepositoryViewModel vm = new SettingsRepositoryViewModel(repository);
|
||||
vm.UseSettingsMessage.Sent += (sender, args) =>
|
||||
{
|
||||
SettingsViewModel settingsVM = args.Content.ViewModel as SettingsViewModel;
|
||||
Settings settings = settingsVM.RevealModelObject() as Settings;
|
||||
DoFormat(settings);
|
||||
Properties.Settings.Default.LastSettings = settings.Uid;
|
||||
Properties.Settings.Default.Save();
|
||||
};
|
||||
vm.InjectInto<SettingsRepositoryView>().ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the embedded demo document.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If Word is running in an embedded environment (e.g. in the ZAA),
|
||||
/// adding a document causes a COMException. Unfortunately, it is
|
||||
/// not trivial to test if Word is running embedded, so we use a
|
||||
/// try...catch structure and catch all COMExceptions. The error
|
||||
/// message might be not quite right if the exception was caused by
|
||||
/// a different problem.
|
||||
/// See http://davecra.com/2013/04/10/how-to-determine-if-an-excel-workbook-is-embedded-and-more
|
||||
/// </remarks>
|
||||
public static void LoadDemo()
|
||||
{
|
||||
try
|
||||
{
|
||||
Demo.Demo.OpenDemoDocument();
|
||||
}
|
||||
catch (System.Runtime.InteropServices.COMException e)
|
||||
{
|
||||
// HRESULT comparison according to http://stackoverflow.com/a/1426198/270712
|
||||
// Fix for exception ID 65a5c34e
|
||||
if (e.ErrorCode == unchecked((int)0x800A11FD))
|
||||
{
|
||||
NotificationAction a = new NotificationAction();
|
||||
a.Caption = "Kann Demo-Dokument nicht laden";
|
||||
a.Message = "Das Demo-Dokument kann nicht geladen werden, "
|
||||
+ "wenn Word in der Zentralen Arztbriefablage ausgeführt wird.\r"
|
||||
+ "Bitte Word als eigenständige Anwendung starten und dann "
|
||||
+ "noch einmal versuchen.";
|
||||
a.OkButtonLabel = "Schließen";
|
||||
a.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void ShowAbout()
|
||||
{
|
||||
ViewModels.AboutViewModel vm = new ViewModels.AboutViewModel();
|
||||
vm.InjectInto<Views.AboutView>().ShowDialog();
|
||||
}
|
||||
|
||||
public static void ShowPreferences()
|
||||
{
|
||||
ViewModels.PreferencesViewModel.Default.InjectInto<Views.PreferencesView>()
|
||||
.ShowDialog();
|
||||
}
|
||||
|
||||
public static void ApplyDanielsStyle()
|
||||
{
|
||||
Formatter.DanielsStyle.Apply(
|
||||
Globals.ThisAddIn.Application.ActiveDocument,
|
||||
Globals.ThisAddIn.Application.Selection);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
|
||||
private static void DoFormat(Settings settings)
|
||||
{
|
||||
ZaaImporter importer = new ZaaImporter();
|
||||
importer.Import(Globals.ThisAddIn.Application.ActiveWindow.Selection.Text);
|
||||
Formatter.Formatter formatter = new Formatter.Formatter(
|
||||
Globals.ThisAddIn.Application.ActiveDocument);
|
||||
formatter.Settings = settings;
|
||||
formatter.Laboratory = importer.Laboratory;
|
||||
CommentPool.Default.Reset();
|
||||
CommentPool.Default.FillInComment += CommentPool_FillInComment;
|
||||
try
|
||||
{
|
||||
formatter.Run();
|
||||
}
|
||||
catch (NoLaboratoryDataException)
|
||||
{
|
||||
NotificationAction a = new NotificationAction();
|
||||
a.Caption = "Formatieren nicht möglich";
|
||||
a.Message = "Die aktuelle Markierung scheint keine Labordaten zu enthalten.";
|
||||
a.OkButtonLabel = "Schließen";
|
||||
a.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private static void CommentPool_FillInComment(object sender, ItemCommentEventArgs e)
|
||||
{
|
||||
ItemCommentViewModel vm = new ItemCommentViewModel(e.Comment);
|
||||
vm.CancelMessage.Sent += (cancelSender, cancelArgs) =>
|
||||
{
|
||||
e.IsCancelled = true;
|
||||
};
|
||||
vm.InjectInto<ItemCommentView>().ShowDialog();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -23,14 +23,6 @@ using System.Windows;
|
||||
using System.Drawing;
|
||||
using System.Windows.Resources;
|
||||
using Office = Microsoft.Office.Core;
|
||||
using zaaReloaded2.Views;
|
||||
using zaaReloaded2.ViewModels;
|
||||
using zaaReloaded2.Importer.ZaaImporter;
|
||||
using zaaReloaded2.Formatter;
|
||||
using zaaReloaded2.Controller;
|
||||
using zaaReloaded2.Controller.Comments;
|
||||
using Word = Microsoft.Office.Interop.Word;
|
||||
using Bovender.Mvvm.Actions;
|
||||
|
||||
// TODO: Follow these steps to enable the Ribbon (XML) item:
|
||||
|
||||
@ -96,26 +88,22 @@ namespace zaaReloaded2
|
||||
switch (control.Id)
|
||||
{
|
||||
case "zrlFormat":
|
||||
DoFormat();
|
||||
Commands.Format();
|
||||
break;
|
||||
case "zrlSettings":
|
||||
DoChooseSettings();
|
||||
Commands.ChooseSettings();
|
||||
break;
|
||||
case "zrlAbout":
|
||||
ViewModels.AboutViewModel vm = new ViewModels.AboutViewModel();
|
||||
vm.InjectInto<Views.AboutView>().ShowDialog();
|
||||
Commands.ShowAbout();
|
||||
break;
|
||||
case "zrlPreferences":
|
||||
ViewModels.PreferencesViewModel.Default.InjectInto<Views.PreferencesView>()
|
||||
.ShowDialog();
|
||||
Commands.ShowPreferences();
|
||||
break;
|
||||
case "zrlDaniel":
|
||||
Formatter.DanielsStyle.Apply(
|
||||
Globals.ThisAddIn.Application.ActiveDocument,
|
||||
Globals.ThisAddIn.Application.Selection);
|
||||
Commands.ApplyDanielsStyle();
|
||||
break;
|
||||
case "zrlDemo":
|
||||
DoLoadDemo();
|
||||
Commands.LoadDemo();
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("No operation defined for " + control.Id);
|
||||
@ -157,152 +145,15 @@ namespace zaaReloaded2
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if there is at least one paragraph selected.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Selection object in Word is a bit tricky: Its Length property
|
||||
/// is never 0, because even if no text passage is selected, the character
|
||||
/// after the cursor is the content of the Selection.
|
||||
/// </remarks>
|
||||
public bool CanFormat(Office.IRibbonControl control)
|
||||
{
|
||||
return Globals.ThisAddIn.Application.ActiveDocument != null;
|
||||
//Word.Selection s = Globals.ThisAddIn.Application.ActiveWindow.Selection;
|
||||
//return s.Paragraphs.Count > 1 ||
|
||||
// (s.Text.Length > 1 && s.Text.EndsWith("\r"));
|
||||
return Commands.CanFormat();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
|
||||
void DoFormat()
|
||||
{
|
||||
// If no "real" selection exists, attempt to auto-detect the lab data.
|
||||
// (NB Technically, there is never _no_ selection in a document.)
|
||||
Word.Window activeWindow = Globals.ThisAddIn.Application.ActiveWindow;
|
||||
Word.Selection sel = activeWindow.Selection;
|
||||
if (!(sel.Paragraphs.Count > 1
|
||||
|| (sel.Text.Length > 1 && sel.Text.EndsWith("\r"))))
|
||||
{
|
||||
if (!AutoDetect.Detect(activeWindow.Document))
|
||||
{
|
||||
NotificationAction a = new NotificationAction();
|
||||
a.Caption = "Formatieren nicht möglich";
|
||||
a.Message = "Das Dokument scheint keine Lauris-Labordaten zu enthalten.";
|
||||
a.OkButtonLabel = "Schließen";
|
||||
a.Invoke();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (CanFormat(null))
|
||||
{
|
||||
SettingsRepository repository = SettingsRepository.Load();
|
||||
Guid lastSettingsUid = Properties.Settings.Default.LastSettings;
|
||||
Settings lastSettings = repository.FindByGuid(lastSettingsUid);
|
||||
if (lastSettings != null)
|
||||
{
|
||||
DoFormat(lastSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
DoChooseSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DoFormat(Settings settings)
|
||||
{
|
||||
ZaaImporter importer = new ZaaImporter();
|
||||
importer.Import(Globals.ThisAddIn.Application.ActiveWindow.Selection.Text);
|
||||
Formatter.Formatter formatter = new Formatter.Formatter(
|
||||
Globals.ThisAddIn.Application.ActiveDocument);
|
||||
formatter.Settings = settings;
|
||||
formatter.Laboratory = importer.Laboratory;
|
||||
CommentPool.Default.Reset();
|
||||
CommentPool.Default.FillInComment += CommentPool_FillInComment;
|
||||
try
|
||||
{
|
||||
formatter.Run();
|
||||
}
|
||||
catch (NoLaboratoryDataException)
|
||||
{
|
||||
NotificationAction a = new NotificationAction();
|
||||
a.Caption = "Formatieren nicht möglich";
|
||||
a.Message = "Die aktuelle Markierung scheint keine Labordaten zu enthalten.";
|
||||
a.OkButtonLabel = "Schließen";
|
||||
a.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
void CommentPool_FillInComment(object sender, ItemCommentEventArgs e)
|
||||
{
|
||||
ItemCommentViewModel vm = new ItemCommentViewModel(e.Comment);
|
||||
vm.CancelMessage.Sent += (cancelSender, cancelArgs) =>
|
||||
{
|
||||
e.IsCancelled = true;
|
||||
};
|
||||
vm.InjectInto<ItemCommentView>().ShowDialog();
|
||||
}
|
||||
|
||||
void DoChooseSettings()
|
||||
{
|
||||
SettingsRepository repository = SettingsRepository.Load();
|
||||
SettingsRepositoryViewModel vm = new SettingsRepositoryViewModel(repository);
|
||||
vm.UseSettingsMessage.Sent += (sender, args) =>
|
||||
{
|
||||
SettingsViewModel settingsVM = args.Content.ViewModel as SettingsViewModel;
|
||||
Settings settings = settingsVM.RevealModelObject() as Settings;
|
||||
DoFormat(settings);
|
||||
Properties.Settings.Default.LastSettings = settings.Uid;
|
||||
Properties.Settings.Default.Save();
|
||||
};
|
||||
vm.InjectInto<SettingsRepositoryView>().ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the embedded demo document.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If Word is running in an embedded environment (e.g. in the ZAA),
|
||||
/// adding a document causes a COMException. Unfortunately, it is
|
||||
/// not trivial to test if Word is running embedded, so we use a
|
||||
/// try...catch structure and catch all COMExceptions. The error
|
||||
/// message might be not quite right if the exception was caused by
|
||||
/// a different problem.
|
||||
/// See http://davecra.com/2013/04/10/how-to-determine-if-an-excel-workbook-is-embedded-and-more
|
||||
/// </remarks>
|
||||
void DoLoadDemo()
|
||||
{
|
||||
try
|
||||
{
|
||||
Demo.Demo.OpenDemoDocument();
|
||||
}
|
||||
catch (System.Runtime.InteropServices.COMException e)
|
||||
{
|
||||
// HRESULT comparison according to http://stackoverflow.com/a/1426198/270712
|
||||
// Fix for exception ID 65a5c34e
|
||||
if (e.ErrorCode == unchecked((int)0x800A11FD))
|
||||
{
|
||||
NotificationAction a = new NotificationAction();
|
||||
a.Caption = "Kann Demo-Dokument nicht laden";
|
||||
a.Message = "Das Demo-Dokument kann nicht geladen werden, "
|
||||
+ "wenn Word in der Zentralen Arztbriefablage ausgeführt wird.\r"
|
||||
+ "Bitte Word als eigenständige Anwendung starten und dann "
|
||||
+ "noch einmal versuchen.";
|
||||
a.OkButtonLabel = "Schließen";
|
||||
a.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)
|
||||
{
|
||||
_ribbon.Invalidate();
|
||||
|
@ -192,6 +192,7 @@
|
||||
can be found.
|
||||
-->
|
||||
<ItemGroup>
|
||||
<Compile Include="Commands.cs" />
|
||||
<Compile Include="Importer\ZaaImporter\AutoDetect.cs" />
|
||||
<Compile Include="Controller\Comments\CommentPool.cs" />
|
||||
<Compile Include="Controller\Elements\ControlElementBase.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user