1
// SharpDevelop samples
2
// Copyright (c) 2007, AlphaSierraPapa
3
// All rights reserved.
5
// Redistribution and use in source and binary forms, with or without modification, are
6
// permitted provided that the following conditions are met:
8
// - Redistributions of source code must retain the above copyright notice, this list
9
// of conditions and the following disclaimer.
11
// - Redistributions in binary form must reproduce the above copyright notice, this list
12
// of conditions and the following disclaimer in the documentation and/or other materials
13
// provided with the distribution.
15
// - Neither the name of the SharpDevelop team nor the names of its contributors may be used to
16
// endorse or promote products derived from this software without specific prior written
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
20
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
using System.Diagnostics;
31
using System.Windows.Forms;
33
using ICSharpCode.Core;
34
using ICSharpCode.Core.WinForms;
35
using ICSharpCode.SharpDevelop;
36
using ICSharpCode.SharpDevelop.Project;
38
namespace ICSharpCode.NAnt.Gui
41
/// NAnt pad's tree view. Shows a high level view of build file in
42
/// a set of projects.
44
public class NAntPadTreeView : System.Windows.Forms.UserControl, IOwnerState
46
private System.Windows.Forms.TreeView treeView;
47
const string ContextMenuAddInTreePath = "/SharpDevelop/Pads/NAntPad/ContextMenu";
50
/// The possible states of the tree view.
52
public enum NAntPadTreeViewState {
54
BuildFileSelected = 1,
60
/// The current state of the tree view.
62
NAntPadTreeViewState state = NAntPadTreeViewState.Nothing;
64
delegate void AddSolutionInvoker(Solution solution);
66
public NAntPadTreeView()
69
// The InitializeComponent() call is required for Windows Forms designer support.
71
InitializeComponent();
73
treeView.Sorted = true;
74
treeView.HideSelection = false;
75
treeView.ImageList = NAntPadTreeViewImageList.GetImageList();
76
treeView.ContextMenuStrip = MenuService.CreateContextMenu(this, ContextMenuAddInTreePath);
77
treeView.DoubleClick += new EventHandler(TreeViewDoubleClick);
81
/// Gets the "ownerstate" condition.
83
public Enum InternalState {
90
/// Clears all items from the tree view.
95
MethodInvoker invoker = new MethodInvoker(Clear);
98
treeView.Nodes.Clear();
103
/// Adds items to the tree view for each build file that exists
104
/// in all the solution's subprojects.
106
/// <param name="solution">A solution containing projects.</param>
107
public void AddSolution(Solution solution)
109
if (InvokeRequired) {
110
AddSolutionInvoker invoker = new AddSolutionInvoker(AddSolution);
113
foreach (IProject project in solution.Projects) {
120
/// Adds items to the tree view for each build file that exist
123
/// <param name="project">A SharpDevelop project.</param>
124
public void AddProject(IProject project)
126
Debug.Assert(!InvokeRequired, "AddProject InvokeRequired");
128
foreach (ProjectItem projectItem in project.Items) {
129
if (NAntBuildFile.IsBuildFile(projectItem.FileName)) {
130
AddBuildFile(project.Name, projectItem.FileName);
136
/// Removes the specified build file from the
137
/// tree view.</summary>
138
public void RemoveBuildFile(string fileName)
140
Debug.Assert(!InvokeRequired, "RemoveBuildFile InvokeRequired");
142
NAntBuildFileTreeNode node = FindMatchingNode(fileName);
149
/// Renames the build file.
151
/// <param name="oldFileName">The filename to update.</param>
152
/// <param name="newFileName">The updated filename.</param>
153
public void RenameBuildFile(string oldFileName, string newFileName)
155
Debug.Assert(!InvokeRequired, "RenameBuildFile InvokeRequired");
157
NAntBuildFileTreeNode node = FindMatchingNode(oldFileName);
160
node.FileName = Path.GetFileName(newFileName);
165
/// Updates the build file in the tree view.
167
/// <param name="fileName">The build file name.</param>
168
public void UpdateBuildFile(string fileName)
170
Debug.Assert(!InvokeRequired, "UpdateBuildFile InvokeRequired");
172
NAntBuildFileTreeNode node = FindMatchingNode(fileName);
175
NAntBuildFile buildFile = new NAntBuildFile(fileName);
176
node.BuildFile = buildFile;
178
AddBuildFile(String.Empty, fileName);
183
/// Adds a build file to the tree.
185
/// <param name="projectName">The name of the project.</param>
186
/// <param name="fileName">The build file name.</param>
187
/// <param name="debug"><see langword="true"/> if the project's
188
/// active configuration is debug; <see langword="false"/>
189
/// otherwise.</param>
190
public void AddBuildFile(string projectName, string fileName)
192
Debug.Assert(!InvokeRequired, "AddBuildFile InvokeRequired");
194
if (File.Exists(fileName)) {
195
NAntBuildFile buildFile = new NAntBuildFile(fileName);
196
NAntBuildFileTreeNode node = new NAntBuildFileTreeNode(projectName, buildFile);
197
treeView.Nodes.Add(node);
202
/// Gets the currently selected <see cref="NAntBuildFile"/>.
204
/// <remarks>This will return a NAntBuildFile if
205
/// a target node is selected.</remarks>
206
public NAntBuildFile SelectedBuildFile {
208
NAntBuildFile buildFile = null;
210
TreeNode selectedNode = treeView.SelectedNode;
211
if (selectedNode is NAntBuildFileTreeNode) {
212
NAntBuildFileTreeNode buildNode = (NAntBuildFileTreeNode)selectedNode;
213
buildFile = buildNode.BuildFile;
214
} else if(selectedNode is NAntBuildTargetTreeNode) {
215
NAntBuildTargetTreeNode targetNode = (NAntBuildTargetTreeNode)selectedNode;
216
NAntBuildFileTreeNode buildNode = (NAntBuildFileTreeNode)targetNode.Parent;
217
buildFile = buildNode.BuildFile;
218
} else if(selectedNode is NAntBuildFileErrorTreeNode) {
219
NAntBuildFileErrorTreeNode errorNode = (NAntBuildFileErrorTreeNode)selectedNode;
220
NAntBuildFileTreeNode buildNode = (NAntBuildFileTreeNode)errorNode.Parent;
221
buildFile = buildNode.BuildFile;
229
/// Gets the current selected <see cref="NAntBuildTarget"/>
231
public NAntBuildTarget SelectedTarget {
233
NAntBuildTarget target = null;
235
NAntBuildTargetTreeNode targetNode = treeView.SelectedNode as NAntBuildTargetTreeNode;
237
if (targetNode != null) {
238
target = targetNode.Target;
246
/// Gets the current selected <see cref="NAntBuildFileError"/>
248
public NAntBuildFileError SelectedError {
250
NAntBuildFileError error = null;
252
NAntBuildFileErrorTreeNode errorNode = treeView.SelectedNode as NAntBuildFileErrorTreeNode;
254
if (errorNode != null) {
255
error = errorNode.Error;
263
/// Gets whether a target is selected.
265
public bool IsTargetSelected {
267
bool isSelected = false;
269
if (SelectedTarget != null) {
277
#region Windows Forms Designer generated code
279
/// This method is required for Windows Forms designer support.
280
/// Do not change the method contents inside the source code editor. The Forms designer might
281
/// not be able to load this method if it was changed manually.
283
private void InitializeComponent() {
284
this.treeView = new System.Windows.Forms.TreeView();
285
this.SuspendLayout();
289
this.treeView.Dock = System.Windows.Forms.DockStyle.Fill;
290
this.treeView.ImageIndex = -1;
291
this.treeView.Location = new System.Drawing.Point(0, 0);
292
this.treeView.Name = "treeView";
293
this.treeView.SelectedImageIndex = -1;
294
this.treeView.Size = new System.Drawing.Size(292, 266);
295
this.treeView.TabIndex = 0;
296
this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TreeViewMouseDown);
300
this.Controls.Add(this.treeView);
301
this.Name = "NAntPadTreeView";
302
this.Size = new System.Drawing.Size(292, 266);
303
this.ResumeLayout(false);
308
/// User clicked the tree view.
310
/// <param name="sender">The event source.</param>
311
/// <param name="e">The event arguments.</param>
312
void TreeViewMouseDown(object sender, MouseEventArgs e)
314
TreeNode node = treeView.GetNodeAt(e.X, e.Y);
316
treeView.SelectedNode = node;
318
state = NAntPadTreeViewState.Nothing;
319
if (IsBuildFileNodeSelected) {
320
state = NAntPadTreeViewState.BuildFileSelected;
323
if (IsBuildTargetNodeSelected) {
324
state = NAntPadTreeViewState.TargetSelected;
327
if (IsBuildFileErrorNodeSelected) {
328
state = NAntPadTreeViewState.ErrorSelected;
333
/// Gets whether a build file is selected.
335
bool IsBuildFileNodeSelected {
337
return treeView.SelectedNode is NAntBuildFileTreeNode;
342
/// Gets whether a target is selected.
344
bool IsBuildTargetNodeSelected {
346
return treeView.SelectedNode is NAntBuildTargetTreeNode;
351
/// Gets whether a build file error is selected.
353
bool IsBuildFileErrorNodeSelected {
355
return treeView.SelectedNode is NAntBuildFileErrorTreeNode;
360
/// Double clicking a node on the tree view opens the corresponding
363
/// <param name="sender">The event source.</param>
364
/// <param name="e">The event arguments.</param>
365
void TreeViewDoubleClick(object sender, EventArgs e)
367
NAntBuildFile buildFile = SelectedBuildFile;
368
if (buildFile != null) {
370
string fileName = Path.Combine(buildFile.Directory, buildFile.FileName);
372
if (IsBuildTargetNodeSelected) {
373
FileService.JumpToFilePosition(fileName, SelectedTarget.Line, SelectedTarget.Column);
374
} else if (IsBuildFileErrorNodeSelected) {
375
FileService.JumpToFilePosition(fileName, SelectedError.Line, SelectedError.Column);
377
FileService.OpenFile(fileName);
383
/// Looks for the tree node that is displaying the specified
386
/// <param name="fileName">The build file to look for.</param>
387
/// <returns>The matching tree node if the build file exists
388
/// in the tree; otherwise <see langword="null"/>.</returns>
389
NAntBuildFileTreeNode FindMatchingNode(string fileName)
391
foreach (NAntBuildFileTreeNode node in treeView.Nodes) {
392
string nodeFileName = Path.Combine(node.BuildFile.Directory, node.BuildFile.FileName);
393
if (String.Compare(Path.GetFullPath(fileName), Path.GetFullPath(nodeFileName), true) == 0) {