Prevent running the formatter without appropriate selection.

- VERBESSERT: Formatieren läßt sich nicht mehr starten, ohne daß Labor-Text markiert ist.
This commit is contained in:
Daniel Kraus 2015-08-14 17:04:50 +02:00
parent 7a105d7aad
commit 863dfa071a
5 changed files with 74 additions and 7 deletions

View File

@ -119,7 +119,7 @@ namespace zaaReloaded2.Formatter
/// current position of the cursor).</param>
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;

View File

@ -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) { }
}
}

View File

@ -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;
}
/// <summary>
@ -152,9 +156,19 @@ 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.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,13 +196,24 @@ 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;
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()
{

View File

@ -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()

View File

@ -213,6 +213,7 @@
<Compile Include="ExceptionHandler\SubmissionSuccessView.xaml.cs" />
<Compile Include="Formatter\DanielsStyle.cs" />
<Compile Include="Formatter\DocumentWriter.cs" />
<Compile Include="Formatter\NoLaboratoryDataException.cs" />
<Compile Include="Ribbon.cs" />
<Compile Include="Thesaurus\ThesaurusBase.cs" />
<Compile Include="Formatter\IItemFormatterDictionary.cs" />