Skip to content

Commit

Permalink
Save As
Browse files Browse the repository at this point in the history
  • Loading branch information
chandramouleswaran committed Sep 9, 2013
1 parent 7356beb commit d3435ed
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 17 deletions.
24 changes: 24 additions & 0 deletions Src/Test/WideMD.Core/CoreModule.cs
Expand Up @@ -117,12 +117,14 @@ private void LoadCommands()
var openCommand = new DelegateCommand(OpenModule);
var exitCommand = new DelegateCommand(CloseCommandExecute);
var saveCommand = new DelegateCommand(SaveDocument, CanExecuteSaveDocument);
var saveAsCommand = new DelegateCommand(SaveAsDocument, CanExecuteSaveAsDocument);
var themeCommand = new DelegateCommand<string>(ThemeChangeCommand);
var loggerCommand = new DelegateCommand(ToggleLogger);


manager.RegisterCommand("OPEN", openCommand);
manager.RegisterCommand("SAVE", saveCommand);
manager.RegisterCommand("SAVEAS", saveAsCommand);
manager.RegisterCommand("EXIT", exitCommand);
manager.RegisterCommand("LOGSHOW", loggerCommand);
manager.RegisterCommand("THEMECHANGE", themeCommand);
Expand Down Expand Up @@ -169,6 +171,12 @@ private void LoadMenus()
@"pack://application:,,,/WideMD.Core;component/Icons/Save_6530.png")),
manager.GetCommand("SAVE"),
new KeyGesture(Key.S, ModifierKeys.Control, "Ctrl + S")));
menuService.Get("_File").Add(new SaveAsMenuItemViewModel("Save As..", 6,
new BitmapImage(
new Uri(
@"pack://application:,,,/WideMD.Core;component/Icons/Save_6530.png")),
manager.GetCommand("SAVEAS"),null,false,false,_container));

menuService.Get("_File").Add(new MenuItemViewModel("Close", 8, null, manager.GetCommand("CLOSE"),
new KeyGesture(Key.F4, ModifierKeys.Control, "Ctrl + F4")));

Expand Down Expand Up @@ -264,6 +272,12 @@ private bool CanExecuteSaveDocument()
return false;
}

private bool CanExecuteSaveAsDocument()
{
IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
return (workspace.ActiveDocument != null) ;
}

private void SaveDocument()
{
IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
Expand All @@ -272,6 +286,16 @@ private void SaveDocument()
manager.Refresh();
}

private void SaveAsDocument()
{
IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
ICommandManager manager = _container.Resolve<ICommandManager>();
if (workspace.ActiveDocument != null)
{
workspace.ActiveDocument.Handler.SaveContent(workspace.ActiveDocument, true);
manager.Refresh();
}
}
#endregion

#region Theme
Expand Down
31 changes: 29 additions & 2 deletions Src/Test/WideMD.Core/MDHandler.cs
Expand Up @@ -18,6 +18,7 @@
using Wide.Core.Attributes;
using Wide.Interfaces;
using Wide.Interfaces.Services;
using Microsoft.Win32;

namespace WideMD.Core
{
Expand All @@ -33,6 +34,8 @@ internal class MDHandler : IContentHandler
/// The injected logger service
/// </summary>
private readonly ILoggerService _loggerService;

private SaveFileDialog _dialog;

/// <summary>
/// Constructor of MDHandler - all parameters are injected
Expand All @@ -43,6 +46,7 @@ public MDHandler(IUnityContainer container, ILoggerService loggerService)
{
_container = container;
_loggerService = loggerService;
_dialog = new SaveFileDialog();
}

#region IContentHandler Members
Expand Down Expand Up @@ -182,8 +186,31 @@ public virtual bool SaveContent(ContentViewModel contentViewModel, bool saveAs =

if (saveAs)
{
//TODO: Save as...?
//contentViewModel.Model.Location = "tesT";
if (location != null)
_dialog.InitialDirectory = Path.GetDirectoryName(location);

_dialog.CheckPathExists = true;
_dialog.DefaultExt = "md";
_dialog.Filter = "Markdown files (*.md)|*.md";

if (_dialog.ShowDialog() == true)
{
location = _dialog.FileName;
mdModel.SetLocation(location);
mdViewModel.Title = Path.GetFileName(location);
try
{
File.WriteAllText(location, mdModel.Document.Text);
mdModel.SetDirty(false);
return true;
}
catch (Exception exception)
{
_loggerService.Log(exception.Message, LogCategory.Exception, LogPriority.High);
_loggerService.Log(exception.StackTrace, LogCategory.Exception, LogPriority.High);
return false;
}
}
}
else
{
Expand Down
62 changes: 62 additions & 0 deletions Src/Test/WideMD.Core/SaveAsMenuItemViewModel.cs
@@ -0,0 +1,62 @@
#region License

// Copyright (c) 2013 Chandramouleswaran Ravichandran
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#endregion

using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Events;
using Wide.Interfaces.Events;

namespace Wide.Interfaces
{
/// <summary>
/// Class SaveAsMenuItemViewModel - simple menu implementation with events
/// </summary>
public sealed class SaveAsMenuItemViewModel : AbstractMenuItem
{
#region CTOR

/// <summary>
/// Initializes a new instance of the <see cref="MenuItemViewModel"/> class.
/// </summary>
/// <param name="header">The header.</param>
/// <param name="priority">The priority.</param>
/// <param name="icon">The icon.</param>
/// <param name="command">The command.</param>
/// <param name="gesture">The gesture.</param>
/// <param name="isCheckable">if set to <c>true</c> this menu acts as a checkable menu.</param>
/// <param name="hideDisabled">if set to <c>true</c> this menu is not visible when disabled.</param>
/// <param name="container">The container.</param>
public SaveAsMenuItemViewModel(string header, int priority, ImageSource icon = null, ICommand command = null,
KeyGesture gesture = null, bool isCheckable = false, bool hideDisabled = false,
IUnityContainer container = null)
: base(header, priority, icon, command, gesture, isCheckable, hideDisabled)
{
if (container != null)
{
IEventAggregator eventAggregator = container.Resolve<IEventAggregator>();
eventAggregator.GetEvent<ActiveContentChangedEvent>().Subscribe(SaveAs);
}
}
#endregion

private void SaveAs(ContentViewModel cvm)
{
if (cvm != null)
{
this.Header = "Save " + cvm.Title + " As...";
}
else
{ this.Header = "Save As..."; }
}
}
}
1 change: 1 addition & 0 deletions Src/Test/WideMD.Core/WideMD.Core.csproj
Expand Up @@ -90,6 +90,7 @@
<DependentUpon>MDView.xaml</DependentUpon>
</Compile>
<Compile Include="MDViewModel.cs" />
<Compile Include="SaveAsMenuItemViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
4 changes: 3 additions & 1 deletion Src/Wide/Interfaces/AbstractWorkspace.cs
Expand Up @@ -249,7 +249,9 @@ private void _menus_PropertyChanged(object sender, PropertyChangedEventArgs e)
if (e.Action == NotifyCollectionChangedAction.Remove)
{
if (_docs.Count == 0)
_activeDocument = null;
{
this.ActiveDocument = null;
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions Src/Wide/Interfaces/ContentViewModel.cs
Expand Up @@ -133,16 +133,6 @@ protected internal set
}
}

protected virtual void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
RaisePropertyChanged("Model");
RaisePropertyChanged("Title");
RaisePropertyChanged("ContentId");
RaisePropertyChanged("Tooltip");
RaisePropertyChanged("IsSelected");
RaisePropertyChanged("IsActive");
}

/// <summary>
/// The content view
/// </summary>
Expand Down Expand Up @@ -300,6 +290,16 @@ protected virtual void Close(object obj)
}
}


protected virtual void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
RaisePropertyChanged("Model");
RaisePropertyChanged("Title");
RaisePropertyChanged("ContentId");
RaisePropertyChanged("Tooltip");
RaisePropertyChanged("IsSelected");
RaisePropertyChanged("IsActive");
}
#endregion
}
}
8 changes: 4 additions & 4 deletions Src/Wide/Interfaces/Styles/VS2012/Menu.xaml
Expand Up @@ -97,9 +97,9 @@
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="35" Width="Auto" SharedSizeGroup="Icon" />
<ColumnDefinition MinWidth="70" Width="Auto" />
<ColumnDefinition MinWidth="90" Width="*" />
<ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Shortcut" />
<ColumnDefinition Width="13" />
<ColumnDefinition MinWidth="13" Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition MinHeight="20" Height="Auto " />
Expand Down Expand Up @@ -167,9 +167,9 @@
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="35" Width="Auto" SharedSizeGroup="Icon" />
<ColumnDefinition MinWidth="70" Width="Auto" />
<ColumnDefinition MinWidth="90" Width="*" />
<ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Shortcut" />
<ColumnDefinition Width="13" />
<ColumnDefinition MinWidth="13" Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition MinHeight="20" Height="Auto " />
Expand Down

0 comments on commit d3435ed

Please sign in to comment.