Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DirectorySelectorForm.cs « Gui « src « doctools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e51e61515c2386a3a88912d67acda0e83d604b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// This file is part of Monodoc, a multilingual API documentation tool.
//
// Monodoc is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Monodoc is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Monodoc; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace Mono.Doc.Gui
{
	/// <summary>
	/// Summary description for DirectorySelectorForm.
	/// </summary>
	public class DirectorySelectorForm : System.Windows.Forms.Form
	{
		private DirectoryInfo currentDirectory;
		private string        selectedDirectoryName;

		private System.Windows.Forms.Label labelPath;
		private System.Windows.Forms.TextBox textBoxPath;
		private System.Windows.Forms.TreeView tree;
		private System.Windows.Forms.Button buttonOK;
		private System.Windows.Forms.Button buttonCancel;
		private System.Windows.Forms.Button buttonParentDir;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public DirectorySelectorForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

			this.buttonParentDir.ImageList  = AssemblyTreeImages.List;
			this.buttonParentDir.ImageIndex = AssemblyTreeImages.Shortcuts; // TODO: need prev. dir icon
			this.tree.ImageList             = AssemblyTreeImages.List;
			this.tree.ImageIndex            = AssemblyTreeImages.Shortcuts; // TODO: need folder icon
			this.tree.SelectedImageIndex    = AssemblyTreeImages.Shortcuts; // TODO: need folder icon

			if (SetCurrentDirectory(Directory.GetCurrentDirectory()))
			{
				this.textBoxPath.Text      = this.currentDirectory.FullName;
				this.selectedDirectoryName = this.currentDirectory.FullName;
				
				InitializeTree();
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Private Instance Methods

		private void InitializeTree()
		{
			this.tree.Nodes.Clear();

			foreach (DirectoryInfo dirInfo in this.currentDirectory.GetDirectories())
			{
				TreeNode dirNode = new TreeNode(dirInfo.Name);
				dirNode.Tag      = dirInfo.FullName;

				CreateSubdirectoryNodes(dirNode);
				this.tree.Nodes.Add(dirNode);
			}
		}

		private void CreateSubdirectoryNodes(TreeNode dirNode)
		{
			DirectoryInfo dirInfo = new DirectoryInfo(dirNode.Tag as string);

			foreach (DirectoryInfo childDirInfo in dirInfo.GetDirectories())
			{
				TreeNode childDirNode = new TreeNode(childDirInfo.Name);
				childDirNode.Tag      = childDirInfo.FullName;

				dirNode.Nodes.Add(childDirNode);
			}
		}

		private bool SetCurrentDirectory(string dirName)
		{
			string        errorMessage = null;
			DirectoryInfo newDirectory = null;

			try
			{
				newDirectory = new DirectoryInfo(dirName);
			}
			catch (ArgumentNullException)
			{
				errorMessage = "cannot be null.";
			}
			catch (DirectoryNotFoundException)
			{
				errorMessage = "was not found.";
			}
			catch (ArgumentException)
			{
				errorMessage = "contains invalid characters.";
			}
			catch (PathTooLongException)
			{
				errorMessage = "is too long.";
			}

			if (errorMessage != null)
			{
				MessageBox.Show("The selected path " + errorMessage,
					"Problem Opening Directory",
					MessageBoxButtons.OK,
					MessageBoxIcon.Error
					);

				return false;
			}
			else
			{
				this.currentDirectory = newDirectory;
				
				return true;
			}
		}

		#endregion // Private Instance Methods

		#region Public Instance Properties

		public string DirectoryName
		{
			get { return selectedDirectoryName; }
		}

		#endregion // Public Instance Properties

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.labelPath = new System.Windows.Forms.Label();
			this.textBoxPath = new System.Windows.Forms.TextBox();
			this.tree = new System.Windows.Forms.TreeView();
			this.buttonOK = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.buttonParentDir = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// labelPath
			// 
			this.labelPath.Location = new System.Drawing.Point(8, 8);
			this.labelPath.Name = "labelPath";
			this.labelPath.Size = new System.Drawing.Size(40, 16);
			this.labelPath.TabIndex = 0;
			this.labelPath.Text = "Path:";
			this.labelPath.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBoxPath
			// 
			this.textBoxPath.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBoxPath.Location = new System.Drawing.Point(48, 6);
			this.textBoxPath.Name = "textBoxPath";
			this.textBoxPath.Size = new System.Drawing.Size(232, 20);
			this.textBoxPath.TabIndex = 1;
			this.textBoxPath.Text = "";
			this.textBoxPath.Leave += new System.EventHandler(this.textBoxPath_Leave);
			// 
			// tree
			// 
			this.tree.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.tree.ImageIndex = -1;
			this.tree.Location = new System.Drawing.Point(8, 32);
			this.tree.Name = "tree";
			this.tree.SelectedImageIndex = -1;
			this.tree.Size = new System.Drawing.Size(304, 288);
			this.tree.TabIndex = 3;
			this.tree.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.tree_BeforeSelect);
			this.tree.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tree_BeforeExpand);
			// 
			// buttonOK
			// 
			this.buttonOK.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonOK.Location = new System.Drawing.Point(144, 328);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 4;
			this.buttonOK.Text = "OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// buttonCancel
			// 
			this.buttonCancel.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonCancel.Location = new System.Drawing.Point(232, 328);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 5;
			this.buttonCancel.Text = "Cancel";
			this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
			// 
			// buttonParentDir
			// 
			this.buttonParentDir.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.buttonParentDir.Location = new System.Drawing.Point(288, 5);
			this.buttonParentDir.Name = "buttonParentDir";
			this.buttonParentDir.Size = new System.Drawing.Size(24, 23);
			this.buttonParentDir.TabIndex = 6;
			this.buttonParentDir.Text = "..";
			this.buttonParentDir.Click += new System.EventHandler(this.buttonParentDir_Click);
			// 
			// DirectorySelectorForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(320, 358);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.buttonParentDir,
																		  this.buttonCancel,
																		  this.buttonOK,
																		  this.tree,
																		  this.textBoxPath,
																		  this.labelPath});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
			this.Name = "DirectorySelectorForm";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.Text = "Select Directory";
			this.ResumeLayout(false);

		}
		#endregion

		private void buttonCancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void textBoxPath_Leave(object sender, System.EventArgs e)
		{
			if (SetCurrentDirectory(textBoxPath.Text))
			{
				InitializeTree();
			}
		}

		private void tree_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
		{
			foreach (TreeNode childDirNode in e.Node.Nodes)
			{
				if (childDirNode.Nodes.Count == 0)
				{
					CreateSubdirectoryNodes(childDirNode);
				}
			}
		}

		private void tree_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
		{
			string path                = e.Node.Tag as string;
			this.textBoxPath.Text      = path;
			this.selectedDirectoryName = path;
		}

		private void buttonParentDir_Click(object sender, System.EventArgs e)
		{
			DirectoryInfo parent = this.currentDirectory.Parent;

			if (parent != null && SetCurrentDirectory(parent.FullName))
			{
				textBoxPath.Text = parent.FullName;
				InitializeTree();
			}
		}
	}
}