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
.Formatters
.Binary
;
22 using Castle
.ActiveRecord
.Generator
.Components
;
25 public class ProjectOpenAction
: AbstractAction
27 private ToolBarButton _button
;
29 public ProjectOpenAction()
33 #region IAction Members
35 public override void Install(IWorkspace workspace
, object parentMenu
, object parentGroup
)
37 base.Install(workspace
, parentMenu
, parentGroup
);
39 MenuItem item
= new MenuItem("&Open...");
41 item
.Click
+= new EventHandler(OnOpen
);
43 (parentMenu
as MenuItem
).MenuItems
.Add(item
);
45 _button
= new ToolBarButton();
46 _button
.ToolTipText
= "Open";
47 _button
.ImageIndex
= 1;
49 (parentGroup
as ToolBar
).Buttons
.Add( _button
);
50 (parentGroup
as ToolBar
).ButtonClick
+= new ToolBarButtonClickEventHandler(OnButtonClick
);
55 private void OnOpen(object sender
, EventArgs e
)
57 String filename
= null;
59 using (OpenFileDialog dlg
= new OpenFileDialog())
61 dlg
.CheckFileExists
= true;
62 dlg
.CheckPathExists
= true;
63 dlg
.Multiselect
= false;
64 dlg
.ShowReadOnly
= false;
65 dlg
.DefaultExt
= ".arproj";
67 if (dlg
.ShowDialog(Workspace
.ActiveWindow
) == DialogResult
.OK
)
69 filename
= dlg
.FileName
;
75 using(FileStream fs
= new FileStream(filename
, FileMode
.Open
, FileAccess
.Read
, FileShare
.Read
))
77 BinaryFormatter formatter
= new BinaryFormatter();
79 Model
.CurrentProject
= formatter
.Deserialize( fs
) as Project
;
82 Model
.Filename
= filename
;
86 Model
.CurrentProject
= new Project();
88 MessageBox
.Show(Workspace
.ActiveWindow
, ex
.Message
, "Error opening project");
92 private void OnButtonClick(object sender
, ToolBarButtonClickEventArgs e
)
94 if (e
.Button
== _button
)