1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Generator
.Actions
19 using System
.Windows
.Forms
;
20 using System
.Runtime
.Serialization
;
21 using System
.Runtime
.Serialization
.Formatters
.Binary
;
24 public class ProjectSaveAction
: AbstractAction
26 private MenuItem _item
;
27 private ToolBarButton _button
;
29 public ProjectSaveAction()
33 public override void Install(IWorkspace workspace
, object parentMenu
, object parentGroup
)
35 base.Install(workspace
, parentMenu
, parentGroup
);
37 _item
= new MenuItem("&Save");
38 _item
.Click
+= new EventHandler(OnSave
);
40 (parentMenu
as MenuItem
).MenuItems
.Add(_item
);
42 _button
= new ToolBarButton();
43 _button
.ToolTipText
= "Save";
44 _button
.ImageIndex
= 2;
46 (parentGroup
as ToolBar
).Buttons
.Add( _button
);
47 (parentGroup
as ToolBar
).ButtonClick
+= new ToolBarButtonClickEventHandler(OnButtonClick
);
50 private void OnSave(object sender
, EventArgs e
)
52 if (Model
.Filename
== null)
54 using(SaveFileDialog dlg
= new SaveFileDialog())
56 dlg
.CheckPathExists
= true;
57 dlg
.OverwritePrompt
= true;
58 dlg
.CreatePrompt
= false;
59 dlg
.DefaultExt
= ".arproj";
61 if (dlg
.ShowDialog(Workspace
.ActiveWindow
) == DialogResult
.OK
)
63 Model
.Filename
= dlg
.FileName
;
75 Model
.CurrentProject
.Name
= new FileInfo(Model
.Filename
).Name
;
77 using(FileStream fs
= new FileStream(Model
.Filename
, FileMode
.Create
, FileAccess
.Write
, FileShare
.Write
))
79 BinaryFormatter formatter
= new BinaryFormatter();
81 formatter
.Serialize( fs
, Model
.CurrentProject
);
88 MessageBox
.Show(Workspace
.ActiveWindow
, ex
.Message
, "Error saving project");
92 private void OnButtonClick(object sender
, ToolBarButtonClickEventArgs e
)
94 if (e
.Button
== _button
)