diff --git a/zaaReloaded2/Formatter/Formatter.cs b/zaaReloaded2/Formatter/Formatter.cs index 83c2d02..cf62545 100755 --- a/zaaReloaded2/Formatter/Formatter.cs +++ b/zaaReloaded2/Formatter/Formatter.cs @@ -119,7 +119,7 @@ namespace zaaReloaded2.Formatter /// current position of the cursor). public void Run() { - if (!CanRun) throw new InvalidOperationException("No laboratory data to format."); + if (!CanRun) throw new NoLaboratoryDataException("No laboratory data to format."); CreateParagraphStyle(); _secondaryBuffer.ParagraphStyle = zaaReloaded2.Properties.Settings.Default.ParagraphStyleName; diff --git a/zaaReloaded2/Formatter/NoLaboratoryDataException.cs b/zaaReloaded2/Formatter/NoLaboratoryDataException.cs new file mode 100755 index 0000000..df47011 --- /dev/null +++ b/zaaReloaded2/Formatter/NoLaboratoryDataException.cs @@ -0,0 +1,35 @@ +/* NoLaboratoryDataException.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.Runtime.Serialization; + +namespace zaaReloaded2.Formatter +{ + [Serializable] + class NoLaboratoryDataException : Exception + { + public NoLaboratoryDataException() { } + public NoLaboratoryDataException(string message) : base(message) { } + public NoLaboratoryDataException(string message, + Exception innerException) + : base(message, innerException) { } + public NoLaboratoryDataException(SerializationInfo info, + StreamingContext context) + : base(info, context) { } + } +} diff --git a/zaaReloaded2/Ribbon.cs b/zaaReloaded2/Ribbon.cs index 958e328..a556054 100755 --- a/zaaReloaded2/Ribbon.cs +++ b/zaaReloaded2/Ribbon.cs @@ -31,6 +31,9 @@ using zaaReloaded2.ViewModels; using zaaReloaded2.Importer.ZaaImporter; using zaaReloaded2.Formatter; using zaaReloaded2.Controller; +using Word = Microsoft.Office.Interop.Word; +using Bovender.Mvvm.Actions; +using Bovender.Mvvm.Messaging; // TODO: Follow these steps to enable the Ribbon (XML) item: @@ -79,7 +82,8 @@ namespace zaaReloaded2 public void Ribbon_Load(Office.IRibbonUI ribbonUI) { _ribbon = ribbonUI; - Globals.ThisAddIn.Application.WindowSelectionChange += Application_WindowSelectionChange; + Microsoft.Office.Interop.Word.Application word = Globals.ThisAddIn.Application; + word.WindowSelectionChange += Application_WindowSelectionChange; } /// @@ -152,9 +156,19 @@ namespace zaaReloaded2 } } + /// + /// Returns true if there is at least one paragraph selected. + /// + /// + /// 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. + /// public bool CanFormat(Office.IRibbonControl control) { - return Globals.ThisAddIn.Application.Selection.Paragraphs.Count > 0; + Word.Selection s = Globals.ThisAddIn.Application.ActiveWindow.Selection; + return s.Paragraphs.Count > 1 || + (s.Text.Length > 1 && s.Text.EndsWith("\r")); } #endregion @@ -182,12 +196,23 @@ namespace zaaReloaded2 void DoFormat(Settings settings) { ZaaImporter importer = new ZaaImporter(); - importer.Import(Globals.ThisAddIn.Application.Selection.Text); - Formatter.Formatter formatter =new Formatter.Formatter( + 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; - formatter.Run(); + 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 DoChooseSettings() diff --git a/zaaReloaded2/ViewModels/SettingsRepositoryViewModel.cs b/zaaReloaded2/ViewModels/SettingsRepositoryViewModel.cs index 2c1b155..f96350c 100755 --- a/zaaReloaded2/ViewModels/SettingsRepositoryViewModel.cs +++ b/zaaReloaded2/ViewModels/SettingsRepositoryViewModel.cs @@ -26,6 +26,7 @@ using zaaReloaded2.Controller; using System.Collections.ObjectModel; using System.Runtime.Serialization.Formatters.Soap; using System.IO; +using Microsoft.Office.Interop.Word; namespace zaaReloaded2.ViewModels { @@ -314,7 +315,12 @@ namespace zaaReloaded2.ViewModels bool CanUseSettings() { - return LastSelected != null && LastSelected.IsSelected; + Selection selection = Globals.ThisAddIn.Application.ActiveWindow.Selection; + return LastSelected != null && LastSelected.IsSelected && + ( + selection.Paragraphs.Count > 1 + || (selection.Text.Length > 1 && selection.Text.EndsWith("\r")) + ); } void DoAddSettings() diff --git a/zaaReloaded2/zaaReloaded2.csproj b/zaaReloaded2/zaaReloaded2.csproj index c2c542e..37e1ccc 100755 --- a/zaaReloaded2/zaaReloaded2.csproj +++ b/zaaReloaded2/zaaReloaded2.csproj @@ -213,6 +213,7 @@ +