Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/Somali/EnvControl/LayoutSecretary.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 95 - (show annotations) (download)
Sun Nov 28 06:24:47 2010 UTC (13 years, 5 months ago) by juan
File size: 12326 byte(s)
mod ExplorerForm with able to open dirTree
1 using System;
2 using System.Windows.Forms;
3 using System.IO;
4 using System.Xml.Linq;
5 using WeifenLuo.WinFormsUI.Docking;
6 using Somali.Base;
7
8 namespace Somali.EnvControl
9 {
10 class LayoutSecretary : Somali.Base.BaseSecretary
11 {
12 // レイアウトファイル名
13 private readonly string layoutFileName = global::Somali.Properties.Resources.LAYOUT_FILE;
14 // Window情報ファイル名
15 private readonly string windStateFileName = global::Somali.Properties.Resources.WINDOW_STATE_FILE;
16
17 private IWindowPreservable _wsp;
18 private DockPanel _dockPanel;
19
20 public LayoutSecretary( IWindowPreservable wndStPrObj, DockPanel dockPanel )
21 {
22 if ( wndStPrObj.IsContainDockPanel( dockPanel ) == true )
23 {
24 this._wsp = wndStPrObj;
25 this._dockPanel = dockPanel;
26 }
27 else
28 {
29 throw new ArgumentException( "指定されたDockPanelはメンバではありません。", dockPanel.Name );
30 }
31 }
32
33 /// <summary>
34 /// レイアウトの初期化
35 /// </summary>
36 public void InitializeLayout()
37 {
38 string dirPath = Util.GetAppSettingDir();
39
40 // Window状態復元
41 string filePath = String.Format( @"{0}\{1}", dirPath, windStateFileName );
42 if ( File.Exists( filePath ) )
43 {
44 LoadWindowState( filePath );
45 }
46
47 // レイアウトファイル読み込み
48 filePath = String.Format( @"{0}\{1}", dirPath, layoutFileName );
49 if ( File.Exists( filePath ) )
50 {
51 LoadDockLayout( filePath );
52 }
53 else
54 {
55 // TODO:ファイルが存在しない場合、デフォルトファイルを生成
56
57 }
58 }
59
60 /// <summary>
61 /// レイアウトの保存
62 /// </summary>
63 public void PreserveLayout()
64 {
65 string dirPath = Util.GetDirPathWithCreate( new Util.FolderSelector( Util.GetAppSettingDir ) );
66
67 // Window状態保存
68 string filePath = String.Format( @"{0}\{1}", dirPath, windStateFileName );
69 SaveWindowState( filePath );
70
71 // DockPanelレイアウト保存
72 filePath = String.Format( @"{0}\{1}", dirPath, layoutFileName );
73 SaveDockLayout( filePath );
74 }
75
76 #region レイアウト保存
77 /// <summary>
78 /// Window状態の保存
79 /// </summary>
80 /// <param name="filePath"></param>
81 private void SaveWindowState( string filePath )
82 {
83 XElement windXml = new XElement( "SomaliWindow",
84 new XElement( "X",
85 this._wsp.Location.X, new XAttribute( "Type", this._wsp.Location.X.GetType() ) ),
86 new XElement( "Y",
87 this._wsp.Location.Y, new XAttribute( "Type", this._wsp.Location.Y.GetType() ) ),
88 new XElement( "WindowState",
89 this._wsp.WindowState, new XAttribute( "Type", this._wsp.WindowState.GetType() ) ),
90 new XElement( "Height",
91 this._wsp.Height, new XAttribute( "Type", this._wsp.Height.GetType() ) ),
92 new XElement( "Width",
93 this._wsp.Width, new XAttribute( "Type", this._wsp.Width.GetType() ) ),
94 new XElement( "NormalizeX",
95 this._wsp.NormalizeLocation.X, new XAttribute( "Type", this._wsp.NormalizeLocation.X.GetType() ) ),
96 new XElement( "NormalizeY",
97 this._wsp.NormalizeLocation.Y, new XAttribute( "Type", this._wsp.NormalizeLocation.Y.GetType() ) ),
98 new XElement( "NormalizeHeight",
99 this._wsp.NormalizeSize.Height, new XAttribute( "Type", this._wsp.NormalizeSize.Height.GetType() ) ),
100 new XElement( "NormalizeWidth",
101 this._wsp.NormalizeSize.Width, new XAttribute( "Type", this._wsp.NormalizeSize.Width.GetType() ) )
102 );
103
104 SaveXmlFile( filePath, windXml );
105
106 //IDictionary<string, object> valueMap = new Dictionary<string, object>();
107
108 //valueMap.Add( "X", this._form.Location.X );
109 //valueMap.Add( "Y", this._form.Location.Y );
110 //valueMap.Add( "WindowState", this._form.WindowState );
111 //valueMap.Add( "Height", this._form.Height );
112 //valueMap.Add( "Width", this._form.Width );
113
114 //SaveXmlFileByTag( filePath, valueMap );
115 }
116
117 /// <summary>
118 /// レイアウトをXMLファイルに保存
119 /// </summary>
120 /// <remarks>User/%username%/AppData/Roaming/Somali配下に設定ファイルを書きだします。</remarks>
121 private void SaveDockLayout( string filePath )
122 {
123 this._dockPanel.SaveAsXml( filePath );
124 }
125 #endregion
126
127 #region レイアウト復元
128 private void LoadWindowState( string filePath )
129 {
130 XElement windXml = LoadXmlFile( filePath );
131 int x, y, h, w;
132
133 Type t = typeof( FormWindowState );
134 FormWindowState fws = (FormWindowState)Enum.Parse( t, GetValueByTag( windXml, "WindowState" ) );
135 if ( fws.Equals( FormWindowState.Normal ) )
136 {
137 x = Int32.Parse( GetValueByTag( windXml, "X" ) );
138 y = Int32.Parse( GetValueByTag( windXml, "Y" ) );
139
140 h = Int32.Parse( GetValueByTag( windXml, "Height" ) );
141 w = Int32.Parse( GetValueByTag( windXml, "Width" ) );
142
143 }
144 else
145 {
146 x = Int32.Parse( GetValueByTag( windXml, "NormalizeX" ) );
147 y = Int32.Parse( GetValueByTag( windXml, "NormalizeY" ) );
148
149 h = Int32.Parse( GetValueByTag( windXml, "NormalizeHeight" ) );
150 w = Int32.Parse( GetValueByTag( windXml, "NormalizeWidth" ) );
151
152 }
153 this._wsp.Location = new System.Drawing.Point( x, y );
154 this._wsp.Size = new System.Drawing.Size( w, h );
155 this._wsp.NormalizeLocation = new System.Drawing.Point( x, y );
156 this._wsp.NormalizeSize = new System.Drawing.Size( w, h );
157 this._wsp.WindowState = fws;
158
159 }
160
161 /// <summary>
162 /// XMLファイルからレイアウトを復元
163 /// </summary>
164 /// <remarks>User/%username%/AppData/Roaming/Somali配下にある設定ファイルから読みだします。</remarks>
165 private void LoadDockLayout( string filePath )
166 {
167 //デリゲート生成
168 DeserializeDockContent deserializeDockContent
169 = new DeserializeDockContent( GetDockContentFromPersistString );
170
171 this._dockPanel.LoadFromXml( filePath, deserializeDockContent );
172 }
173
174 /// <summary>
175 /// PersistStringからDockContentを返却
176 /// </summary>
177 /// <param name="persistString">DockContentの名前</param>
178 /// <returns></returns>
179 private IDockContent GetDockContentFromPersistString( string persistString )
180 {
181 //persistStringに応じて、対応するDockContentを生成し、それを返却
182 if ( persistString.Equals( typeof( OutlineForm ).ToString() ) )
183 {
184 return new OutlineForm();
185 }
186 else if ( persistString.Equals( typeof( ExplorerForm ).ToString() ) )
187 {
188 return new ExplorerForm();
189 }
190 else
191 {
192 //ファイルパス取得
193 string[] parsedStrings = persistString.Split( new char[] { '|' } );
194 if ( parsedStrings.Length != 2 )
195 return null;
196
197 if ( parsedStrings[0] != typeof( EditorForm ).ToString() )
198 return null;
199
200 EditorForm frmEditor = new EditorForm();
201 if ( parsedStrings[1] != string.Empty )
202 frmEditor.FilePath = parsedStrings[1];
203
204 return frmEditor;
205
206 }
207 }
208 #endregion
209
210 /// <summary>
211 /// 指定したTypeのDockContentを取得
212 /// </summary>
213 /// <param name="formType">DockContentを継承するオブジェクトのType</param>
214 /// <returns>DockPanelに存在すればDockContentを返却します。存在しない場合は、nullを返却します。</returns>
215 public IDockContent GetDockFormIfExists( Type formType )
216 {
217 DockContentCollection dc = this._dockPanel.Contents;
218 foreach ( IDockContent d in dc )
219 {
220 if ( d.GetType().Equals( formType ) )
221 {
222 return d;
223 }
224 }
225
226 return null;
227 }
228
229 /// <summary>
230 /// 指定したTypeのDockContentを取得
231 /// </summary>
232 /// <typeparam name="T"></typeparam>
233 /// <returns>DockPanelに存在すればDockContentを返却します。存在しない場合は、nullを返却します。</returns>
234 public T GetDockFormIfExists<T>()
235 where T : class, IDockContent
236 {
237 DockContentCollection dc = this._dockPanel.Contents;
238 foreach ( IDockContent d in dc )
239 {
240 if ( d.GetType().Equals( typeof( T ) ) )
241 {
242 return (T)d;
243 }
244 }
245 return null;
246 //return default( T );
247 }
248
249 /// <summary>
250 /// IDockContentを継承するFormを取得
251 /// </summary>
252 /// <typeparam name="T"></typeparam>
253 /// <param name="dockForm"></param>
254 /// <returns>true:取得成功<br/>false:取得失敗</returns>
255 /// <remarks>DockPanelに存在しないFormの場合、outにはnullを設定します。</remarks>
256 public bool TryGetDockForm<T>( out T dockForm )
257 where T : class, IDockContent
258 {
259 try
260 {
261 dockForm = GetDockFormIfExists<T>();
262 //dockForm = (T)GetDockFormIfExists( typeof( T ) );
263 if ( dockForm != null )
264 {
265 return true;
266 }
267 else
268 {
269 return false;
270 }
271 }
272 catch ( Exception )
273 {
274 dockForm = default( T );
275 return false;
276 }
277 }
278
279 /// <summary>
280 /// IDockContentを継承するFormの実装インスタンスを取得
281 /// </summary>
282 /// <typeparam name="T"></typeparam>
283 /// <param name="dockForm"></param>
284 /// <returns>true:取得成功<br/>false:取得失敗</returns>
285 /// <remarks>DockPanelに存在しないFormの場合、outにはdockFormの新しいインスタンスを設定します。</remarks>
286 public bool GetDockFormImpl<T>( out T dockForm )
287 where T : class, IDockContent, new()
288 {
289 try
290 {
291 if ( TryGetDockForm( out dockForm ) )
292 {
293 return true;
294 }
295 else
296 {
297 dockForm = new T();
298 return true;
299 }
300 }
301 catch ( Exception )
302 {
303 dockForm = default( T );
304 return false;
305 }
306 }
307
308 }
309 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26