Skip to content
This repository

Save As function #36

Merged
merged 1 commit into from 10 months ago

1 participant

Chandramouleswaran
Chandramouleswaran

No description provided.

Chandramouleswaran chandramouleswaran merged commit 66a94d2 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 09, 2013
Chandramouleswaran Save As d3435ed
This page is out of date. Refresh to see the latest.
24 Src/Test/WideMD.Core/CoreModule.cs
@@ -117,12 +117,14 @@ private void LoadCommands()
117 117 var openCommand = new DelegateCommand(OpenModule);
118 118 var exitCommand = new DelegateCommand(CloseCommandExecute);
119 119 var saveCommand = new DelegateCommand(SaveDocument, CanExecuteSaveDocument);
  120 + var saveAsCommand = new DelegateCommand(SaveAsDocument, CanExecuteSaveAsDocument);
120 121 var themeCommand = new DelegateCommand<string>(ThemeChangeCommand);
121 122 var loggerCommand = new DelegateCommand(ToggleLogger);
122 123
123 124
124 125 manager.RegisterCommand("OPEN", openCommand);
125 126 manager.RegisterCommand("SAVE", saveCommand);
  127 + manager.RegisterCommand("SAVEAS", saveAsCommand);
126 128 manager.RegisterCommand("EXIT", exitCommand);
127 129 manager.RegisterCommand("LOGSHOW", loggerCommand);
128 130 manager.RegisterCommand("THEMECHANGE", themeCommand);
@@ -169,6 +171,12 @@ private void LoadMenus()
169 171 @"pack://application:,,,/WideMD.Core;component/Icons/Save_6530.png")),
170 172 manager.GetCommand("SAVE"),
171 173 new KeyGesture(Key.S, ModifierKeys.Control, "Ctrl + S")));
  174 + menuService.Get("_File").Add(new SaveAsMenuItemViewModel("Save As..", 6,
  175 + new BitmapImage(
  176 + new Uri(
  177 + @"pack://application:,,,/WideMD.Core;component/Icons/Save_6530.png")),
  178 + manager.GetCommand("SAVEAS"),null,false,false,_container));
  179 +
172 180 menuService.Get("_File").Add(new MenuItemViewModel("Close", 8, null, manager.GetCommand("CLOSE"),
173 181 new KeyGesture(Key.F4, ModifierKeys.Control, "Ctrl + F4")));
174 182
@@ -264,6 +272,12 @@ private bool CanExecuteSaveDocument()
264 272 return false;
265 273 }
266 274
  275 + private bool CanExecuteSaveAsDocument()
  276 + {
  277 + IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
  278 + return (workspace.ActiveDocument != null) ;
  279 + }
  280 +
267 281 private void SaveDocument()
268 282 {
269 283 IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
@@ -272,6 +286,16 @@ private void SaveDocument()
272 286 manager.Refresh();
273 287 }
274 288
  289 + private void SaveAsDocument()
  290 + {
  291 + IWorkspace workspace = _container.Resolve<AbstractWorkspace>();
  292 + ICommandManager manager = _container.Resolve<ICommandManager>();
  293 + if (workspace.ActiveDocument != null)
  294 + {
  295 + workspace.ActiveDocument.Handler.SaveContent(workspace.ActiveDocument, true);
  296 + manager.Refresh();
  297 + }
  298 + }
275 299 #endregion
276 300
277 301 #region Theme
31 Src/Test/WideMD.Core/MDHandler.cs
@@ -18,6 +18,7 @@
18 18 using Wide.Core.Attributes;
19 19 using Wide.Interfaces;
20 20 using Wide.Interfaces.Services;
  21 +using Microsoft.Win32;
21 22
22 23 namespace WideMD.Core
23 24 {
@@ -33,6 +34,8 @@ internal class MDHandler : IContentHandler
33 34 /// The injected logger service
34 35 /// </summary>
35 36 private readonly ILoggerService _loggerService;
  37 +
  38 + private SaveFileDialog _dialog;
36 39
37 40 /// <summary>
38 41 /// Constructor of MDHandler - all parameters are injected
@@ -43,6 +46,7 @@ public MDHandler(IUnityContainer container, ILoggerService loggerService)
43 46 {
44 47 _container = container;
45 48 _loggerService = loggerService;
  49 + _dialog = new SaveFileDialog();
46 50 }
47 51
48 52 #region IContentHandler Members
@@ -182,8 +186,31 @@ public virtual bool SaveContent(ContentViewModel contentViewModel, bool saveAs =
182 186
183 187 if (saveAs)
184 188 {
185   - //TODO: Save as...?
186   - //contentViewModel.Model.Location = "tesT";
  189 + if (location != null)
  190 + _dialog.InitialDirectory = Path.GetDirectoryName(location);
  191 +
  192 + _dialog.CheckPathExists = true;
  193 + _dialog.DefaultExt = "md";
  194 + _dialog.Filter = "Markdown files (*.md)|*.md";
  195 +
  196 + if (_dialog.ShowDialog() == true)
  197 + {
  198 + location = _dialog.FileName;
  199 + mdModel.SetLocation(location);
  200 + mdViewModel.Title = Path.GetFileName(location);
  201 + try
  202 + {
  203 + File.WriteAllText(location, mdModel.Document.Text);
  204 + mdModel.SetDirty(false);
  205 + return true;
  206 + }
  207 + catch (Exception exception)
  208 + {
  209 + _loggerService.Log(exception.Message, LogCategory.Exception, LogPriority.High);
  210 + _loggerService.Log(exception.StackTrace, LogCategory.Exception, LogPriority.High);
  211 + return false;
  212 + }
  213 + }
187 214 }
188 215 else
189 216 {
62 Src/Test/WideMD.Core/SaveAsMenuItemViewModel.cs
... ... @@ -0,0 +1,62 @@
  1 +#region License
  2 +
  3 +// Copyright (c) 2013 Chandramouleswaran Ravichandran
  4 +//
  5 +// 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:
  6 +//
  7 +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8 +//
  9 +// 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.
  10 +
  11 +#endregion
  12 +
  13 +using System.Windows.Input;
  14 +using System.Windows.Media;
  15 +using Microsoft.Practices.Unity;
  16 +using Microsoft.Practices.Prism.Events;
  17 +using Wide.Interfaces.Events;
  18 +
  19 +namespace Wide.Interfaces
  20 +{
  21 + /// <summary>
  22 + /// Class SaveAsMenuItemViewModel - simple menu implementation with events
  23 + /// </summary>
  24 + public sealed class SaveAsMenuItemViewModel : AbstractMenuItem
  25 + {
  26 + #region CTOR
  27 +
  28 + /// <summary>
  29 + /// Initializes a new instance of the <see cref="MenuItemViewModel"/> class.
  30 + /// </summary>
  31 + /// <param name="header">The header.</param>
  32 + /// <param name="priority">The priority.</param>
  33 + /// <param name="icon">The icon.</param>
  34 + /// <param name="command">The command.</param>
  35 + /// <param name="gesture">The gesture.</param>
  36 + /// <param name="isCheckable">if set to <c>true</c> this menu acts as a checkable menu.</param>
  37 + /// <param name="hideDisabled">if set to <c>true</c> this menu is not visible when disabled.</param>
  38 + /// <param name="container">The container.</param>
  39 + public SaveAsMenuItemViewModel(string header, int priority, ImageSource icon = null, ICommand command = null,
  40 + KeyGesture gesture = null, bool isCheckable = false, bool hideDisabled = false,
  41 + IUnityContainer container = null)
  42 + : base(header, priority, icon, command, gesture, isCheckable, hideDisabled)
  43 + {
  44 + if (container != null)
  45 + {
  46 + IEventAggregator eventAggregator = container.Resolve<IEventAggregator>();
  47 + eventAggregator.GetEvent<ActiveContentChangedEvent>().Subscribe(SaveAs);
  48 + }
  49 + }
  50 + #endregion
  51 +
  52 + private void SaveAs(ContentViewModel cvm)
  53 + {
  54 + if (cvm != null)
  55 + {
  56 + this.Header = "Save " + cvm.Title + " As...";
  57 + }
  58 + else
  59 + { this.Header = "Save As..."; }
  60 + }
  61 + }
  62 +}
1  Src/Test/WideMD.Core/WideMD.Core.csproj
@@ -90,6 +90,7 @@
90 90 <DependentUpon>MDView.xaml</DependentUpon>
91 91 </Compile>
92 92 <Compile Include="MDViewModel.cs" />
  93 + <Compile Include="SaveAsMenuItemViewModel.cs" />
93 94 <Compile Include="Properties\AssemblyInfo.cs">
94 95 <SubType>Code</SubType>
95 96 </Compile>
4 Src/Wide/Interfaces/AbstractWorkspace.cs
@@ -249,7 +249,9 @@ private void _menus_PropertyChanged(object sender, PropertyChangedEventArgs e)
249 249 if (e.Action == NotifyCollectionChangedAction.Remove)
250 250 {
251 251 if (_docs.Count == 0)
252   - _activeDocument = null;
  252 + {
  253 + this.ActiveDocument = null;
  254 + }
253 255 }
254 256 }
255 257
20 Src/Wide/Interfaces/ContentViewModel.cs
@@ -133,16 +133,6 @@ protected internal set
133 133 }
134 134 }
135 135
136   - protected virtual void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
137   - {
138   - RaisePropertyChanged("Model");
139   - RaisePropertyChanged("Title");
140   - RaisePropertyChanged("ContentId");
141   - RaisePropertyChanged("Tooltip");
142   - RaisePropertyChanged("IsSelected");
143   - RaisePropertyChanged("IsActive");
144   - }
145   -
146 136 /// <summary>
147 137 /// The content view
148 138 /// </summary>
@@ -300,6 +290,16 @@ protected virtual void Close(object obj)
300 290 }
301 291 }
302 292
  293 +
  294 + protected virtual void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
  295 + {
  296 + RaisePropertyChanged("Model");
  297 + RaisePropertyChanged("Title");
  298 + RaisePropertyChanged("ContentId");
  299 + RaisePropertyChanged("Tooltip");
  300 + RaisePropertyChanged("IsSelected");
  301 + RaisePropertyChanged("IsActive");
  302 + }
303 303 #endregion
304 304 }
305 305 }
8 Src/Wide/Interfaces/Styles/VS2012/Menu.xaml
@@ -97,9 +97,9 @@
97 97 <Grid>
98 98 <Grid.ColumnDefinitions>
99 99 <ColumnDefinition MinWidth="35" Width="Auto" SharedSizeGroup="Icon" />
100   - <ColumnDefinition MinWidth="70" Width="Auto" />
  100 + <ColumnDefinition MinWidth="90" Width="*" />
101 101 <ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Shortcut" />
102   - <ColumnDefinition Width="13" />
  102 + <ColumnDefinition MinWidth="13" Width="Auto" />
103 103 </Grid.ColumnDefinitions>
104 104 <Grid.RowDefinitions>
105 105 <RowDefinition MinHeight="20" Height="Auto " />
@@ -167,9 +167,9 @@
167 167 <Grid>
168 168 <Grid.ColumnDefinitions>
169 169 <ColumnDefinition MinWidth="35" Width="Auto" SharedSizeGroup="Icon" />
170   - <ColumnDefinition MinWidth="70" Width="Auto" />
  170 + <ColumnDefinition MinWidth="90" Width="*" />
171 171 <ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Shortcut" />
172   - <ColumnDefinition Width="13" />
  172 + <ColumnDefinition MinWidth="13" Width="Auto" />
173 173 </Grid.ColumnDefinitions>
174 174 <Grid.RowDefinitions>
175 175 <RowDefinition MinHeight="20" Height="Auto " />

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.