Implement highlighting for item comments.

This commit is contained in:
Daniel Kraus 2015-09-06 11:48:43 +02:00
parent bf15d264ba
commit c733c238de
11 changed files with 143 additions and 67 deletions

View File

@ -39,24 +39,6 @@ namespace zaaReloaded2
public static void Format() 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()) if (CanFormat())
{ {
SettingsRepository repository = SettingsRepository.Load(); SettingsRepository repository = SettingsRepository.Load();
@ -162,6 +144,24 @@ namespace zaaReloaded2
private static void DoFormat(Settings settings) private static void DoFormat(Settings settings)
{ {
// 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;
}
}
ZaaImporter importer = new ZaaImporter(); ZaaImporter importer = new ZaaImporter();
importer.Import(Globals.ThisAddIn.Application.ActiveWindow.Selection.Text); importer.Import(Globals.ThisAddIn.Application.ActiveWindow.Selection.Text);
Formatter.Formatter formatter = new Formatter.Formatter( Formatter.Formatter formatter = new Formatter.Formatter(
@ -186,12 +186,15 @@ namespace zaaReloaded2
private static void CommentPool_FillInComment(object sender, ItemCommentEventArgs e) private static void CommentPool_FillInComment(object sender, ItemCommentEventArgs e)
{ {
ItemCommentViewModel vm = new ItemCommentViewModel(e.Comment); if (Preferences.Default.SuppressItemCommentInteraction)
vm.CancelMessage.Sent += (cancelSender, cancelArgs) =>
{ {
e.IsCancelled = true; e.Comment.IsCancelled = true;
}; }
vm.InjectInto<ItemCommentView>().ShowDialog(); else
{
ItemCommentViewModel vm = new ItemCommentViewModel(e.Comment);
vm.InjectInto<ItemCommentView>().ShowDialog();
}
} }
#endregion #endregion

View File

@ -126,9 +126,6 @@ namespace zaaReloaded2.Controller.Comments
protected virtual void OnFillInComment(ItemCommentEventArgs args) protected virtual void OnFillInComment(ItemCommentEventArgs args)
{ {
if (Preferences.Default.SuppressItemCommentInteraction)
return;
EventHandler<ItemCommentEventArgs> h = FillInComment; EventHandler<ItemCommentEventArgs> h = FillInComment;
if (h != null) if (h != null)
{ {

View File

@ -101,6 +101,11 @@ namespace zaaReloaded2.Controller.Comments
/// </summary> /// </summary>
public string Definition { get; private set; } public string Definition { get; private set; }
/// <summary>
/// Gets or sets whether this comment has been cancelled.mo
/// </summary>
public bool IsCancelled { get; set; }
#endregion #endregion
#region Event #region Event
@ -154,10 +159,33 @@ namespace zaaReloaded2.Controller.Comments
{ {
OnFillInComment(); OnFillInComment();
if (String.IsNullOrEmpty(Value)) string value = Value;
return String.Empty; if (String.IsNullOrEmpty(value))
{
// If the comment was not cancelled and an empty
// value was deliberately entered, return an
// empty string.
if (!IsCancelled)
{
return String.Empty;
}
// If the comment was cancelled and the default
// value is an empty string, insert a prompt
// to manually enter the comment.
else
{
value = Properties.Settings.Default.ManualCommentPrompt;
}
}
return Prefix + Value + Suffix; if (!IsCancelled)
{
return Prefix + value + Suffix;
}
else
{
return Prefix + "<highlight>" + value + "</highlight>" + Suffix;
}
} }
#endregion #endregion
@ -172,10 +200,7 @@ namespace zaaReloaded2.Controller.Comments
EventHandler<ItemCommentEventArgs> h = FillInComment; EventHandler<ItemCommentEventArgs> h = FillInComment;
if (h != null) if (h != null)
{ {
ItemCommentEventArgs args = new ItemCommentEventArgs(this); h(this, new ItemCommentEventArgs(this));
h(this, args);
if (args.IsCancelled)
args.Comment.Value = String.Empty;
} }
} }

View File

@ -34,11 +34,6 @@ namespace zaaReloaded2.Controller.Comments
/// </summary> /// </summary>
public ItemComment Comment { get; private set; } public ItemComment Comment { get; private set; }
/// <summary>
/// Gets or sets whether the commenting was cancelled.
/// </summary>
public bool IsCancelled { get; set; }
#endregion #endregion
#region Constructor #region Constructor

View File

@ -21,6 +21,7 @@ using System.Linq;
using System.Text; using System.Text;
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace zaaReloaded2.Formatter namespace zaaReloaded2.Formatter
{ {
@ -192,6 +193,7 @@ namespace zaaReloaded2.Formatter
{ {
string[] substrings = _markupRegex.Split(text); string[] substrings = _markupRegex.Split(text);
Selection sel = Document.ActiveWindow.Selection; Selection sel = Document.ActiveWindow.Selection;
Highlights hightlights = new Highlights();
foreach (string substring in substrings) foreach (string substring in substrings)
{ {
switch (substring) switch (substring)
@ -214,6 +216,12 @@ namespace zaaReloaded2.Formatter
case "</u>": case "</u>":
sel.Font.Underline = WdUnderline.wdUnderlineNone; sel.Font.Underline = WdUnderline.wdUnderlineNone;
break; break;
case "<highlight>":
hightlights.Start(sel.Range);
break;
case "</highlight>":
hightlights.Stop(sel.Range);
break;
case "</style>": case "</style>":
sel.ClearCharacterStyle(); sel.ClearCharacterStyle();
break; break;
@ -230,6 +238,7 @@ namespace zaaReloaded2.Formatter
break; break;
} }
} }
hightlights.ApplyHighlights();
} }
#endregion #endregion
@ -241,7 +250,52 @@ namespace zaaReloaded2.Formatter
// The splitting pattern must not contain subgroups! // The splitting pattern must not contain subgroups!
static readonly Regex _markupRegex = new Regex(@"(<[^ >]+>)"); static readonly Regex _markupRegex = new Regex(@"(<[^ >]+>)");
static readonly Regex _styleRegex = new Regex(@"<style:(?<style>[^>]+)>"); static readonly Regex _styleRegex = new Regex(@"<style:(?<style>[^>]+)>");
static Range _highlightStart;
#endregion
#region Helper class for highlighting
/// <summary>
/// Embedded helper class to manage highlights.
/// </summary>
class Highlights
{
public void Start(Range start)
{
if (_currentHighlight == null)
{
_currentHighlight = start;
_highlights.Add(_currentHighlight);
}
}
public void Stop(Range stop)
{
if (_currentHighlight != null)
{
_currentHighlight.End = stop.End;
_currentHighlight = null;
}
}
public void ApplyHighlights()
{
foreach (Range r in _highlights)
{
r.HighlightColorIndex = WdColorIndex.wdYellow;
}
}
public Highlights()
{
_highlights = new Collection<Range>();
_currentHighlight = null;
}
Collection<Range> _highlights;
Range _currentHighlight;
}
#endregion #endregion
} }

View File

@ -246,5 +246,14 @@ namespace zaaReloaded2.Properties {
this["FirstRunWizardShown"] = value; this["FirstRunWizardShown"] = value;
} }
} }
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("BITTE_ERGÄNZEN")]
public string ManualCommentPrompt {
get {
return ((string)(this["ManualCommentPrompt"]));
}
}
} }
} }

View File

@ -71,5 +71,8 @@
<Setting Name="FirstRunWizardShown" Type="System.Boolean" Scope="User"> <Setting Name="FirstRunWizardShown" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value> <Value Profile="(Default)">False</Value>
</Setting> </Setting>
<Setting Name="ManualCommentPrompt" Type="System.String" Scope="Application">
<Value Profile="(Default)">BITTE_ERGÄNZEN</Value>
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

View File

@ -45,7 +45,7 @@ namespace zaaReloaded2.ViewModels
} }
set set
{ {
_itemComment.Value = value; _value = value;
OnPropertyChanged("Value"); OnPropertyChanged("Value");
} }
} }
@ -56,32 +56,16 @@ namespace zaaReloaded2.ViewModels
#region Commands #region Commands
DelegatingCommand CancelCommand DelegatingCommand SaveCommand
{ {
get get
{ {
if (_cancelCommand == null) if (_saveCommand == null)
{ {
_cancelCommand = new DelegatingCommand( _saveCommand = new DelegatingCommand(
param => DoCancel()); param => DoSave());
} }
return _cancelCommand; return _saveCommand;
}
}
#endregion
#region Message
public Message<ViewModelMessageContent> CancelMessage
{
get
{
if (_cancelMessage == null)
{
_cancelMessage = new Message<ViewModelMessageContent>();
}
return _cancelMessage;
} }
} }
@ -92,6 +76,8 @@ namespace zaaReloaded2.ViewModels
public ItemCommentViewModel(ItemComment itemComment) public ItemCommentViewModel(ItemComment itemComment)
{ {
_itemComment = itemComment; _itemComment = itemComment;
_value = _itemComment.Value;
_itemComment.IsCancelled = true;
} }
#endregion #endregion
@ -107,9 +93,10 @@ namespace zaaReloaded2.ViewModels
#region Private methods #region Private methods
void DoCancel() void DoSave()
{ {
CancelMessage.Send(new ViewModelMessageContent(this)); _itemComment.Value = Value;
_itemComment.IsCancelled = false;
DoCloseView(); DoCloseView();
} }
@ -117,8 +104,8 @@ namespace zaaReloaded2.ViewModels
#region Fields #region Fields
DelegatingCommand _cancelCommand; string _value;
Message<ViewModelMessageContent> _cancelMessage; DelegatingCommand _saveCommand;
ItemComment _itemComment; ItemComment _itemComment;
#endregion #endregion

View File

@ -319,7 +319,7 @@ namespace zaaReloaded2.ViewModels
bool CanUseSettings() bool CanUseSettings()
{ {
Commands.CanFormat(); return Commands.CanFormat();
} }
void DoAddSettings() void DoAddSettings()

View File

@ -36,8 +36,8 @@
FontSize="16" FontWeight="Bold" FontSize="16" FontWeight="Bold"
Target="{Binding ElementName=ValueTextBox}" Padding="0" /> Target="{Binding ElementName=ValueTextBox}" Padding="0" />
<UniformGrid DockPanel.Dock="Bottom" HorizontalAlignment="Right" Columns="2" Rows="1" Margin="0 10 0 0"> <UniformGrid DockPanel.Dock="Bottom" HorizontalAlignment="Right" Columns="2" Rows="1" Margin="0 10 0 0">
<Button Content="OK" Command="{Binding CloseViewCommand}" IsDefault="True" Margin="0 0 5 0" /> <Button Content="OK" Command="{Binding SaveCommand}" IsDefault="True" Margin="0 0 5 0" />
<Button Content="Abbrechen" Command="{Binding CancelCommand}" IsCancel="True" Margin="5 0 0 0" /> <Button Content="Abbrechen" Command="{Binding CloseViewCommand}" IsCancel="True" Margin="5 0 0 0" />
</UniformGrid> </UniformGrid>
<DockPanel Margin="0 10 0 10"> <DockPanel Margin="0 10 0 10">
<TextBlock DockPanel.Dock="Left" Text="{Binding Prefix}" VerticalAlignment="Center" /> <TextBlock DockPanel.Dock="Left" Text="{Binding Prefix}" VerticalAlignment="Center" />

View File

@ -80,6 +80,9 @@
<setting name="AbnormalStyle" serializeAs="String"> <setting name="AbnormalStyle" serializeAs="String">
<value>None</value> <value>None</value>
</setting> </setting>
<setting name="ManualCommentPrompt" serializeAs="String">
<value>BITTE_ERGÄNZEN</value>
</setting>
</zaaReloaded2.Properties.Settings> </zaaReloaded2.Properties.Settings>
</applicationSettings> </applicationSettings>
<userSettings> <userSettings>