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

View File

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

View File

@ -101,6 +101,11 @@ namespace zaaReloaded2.Controller.Comments
/// </summary>
public string Definition { get; private set; }
/// <summary>
/// Gets or sets whether this comment has been cancelled.mo
/// </summary>
public bool IsCancelled { get; set; }
#endregion
#region Event
@ -154,10 +159,33 @@ namespace zaaReloaded2.Controller.Comments
{
OnFillInComment();
if (String.IsNullOrEmpty(Value))
return String.Empty;
string value = Value;
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
@ -172,10 +200,7 @@ namespace zaaReloaded2.Controller.Comments
EventHandler<ItemCommentEventArgs> h = FillInComment;
if (h != null)
{
ItemCommentEventArgs args = new ItemCommentEventArgs(this);
h(this, args);
if (args.IsCancelled)
args.Comment.Value = String.Empty;
h(this, new ItemCommentEventArgs(this));
}
}

View File

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

View File

@ -21,6 +21,7 @@ using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace zaaReloaded2.Formatter
{
@ -192,6 +193,7 @@ namespace zaaReloaded2.Formatter
{
string[] substrings = _markupRegex.Split(text);
Selection sel = Document.ActiveWindow.Selection;
Highlights hightlights = new Highlights();
foreach (string substring in substrings)
{
switch (substring)
@ -214,6 +216,12 @@ namespace zaaReloaded2.Formatter
case "</u>":
sel.Font.Underline = WdUnderline.wdUnderlineNone;
break;
case "<highlight>":
hightlights.Start(sel.Range);
break;
case "</highlight>":
hightlights.Stop(sel.Range);
break;
case "</style>":
sel.ClearCharacterStyle();
break;
@ -230,6 +238,7 @@ namespace zaaReloaded2.Formatter
break;
}
}
hightlights.ApplyHighlights();
}
#endregion
@ -241,8 +250,53 @@ namespace zaaReloaded2.Formatter
// The splitting pattern must not contain subgroups!
static readonly Regex _markupRegex = new Regex(@"(<[^ >]+>)");
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
}
}

View File

@ -246,5 +246,14 @@ namespace zaaReloaded2.Properties {
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">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="ManualCommentPrompt" Type="System.String" Scope="Application">
<Value Profile="(Default)">BITTE_ERGÄNZEN</Value>
</Setting>
</Settings>
</SettingsFile>

View File

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

View File

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

View File

@ -36,8 +36,8 @@
FontSize="16" FontWeight="Bold"
Target="{Binding ElementName=ValueTextBox}" Padding="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="Abbrechen" Command="{Binding CancelCommand}" IsCancel="True" Margin="5 0 0 0" />
<Button Content="OK" Command="{Binding SaveCommand}" IsDefault="True" Margin="0 0 5 0" />
<Button Content="Abbrechen" Command="{Binding CloseViewCommand}" IsCancel="True" Margin="5 0 0 0" />
</UniformGrid>
<DockPanel Margin="0 10 0 10">
<TextBlock DockPanel.Dock="Left" Text="{Binding Prefix}" VerticalAlignment="Center" />

View File

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