~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to samples/NAnt/NAnt.AddIn/Src/Gui/NAntPadTreeView.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// SharpDevelop samples
 
2
// Copyright (c) 2007, AlphaSierraPapa
 
3
// All rights reserved.
 
4
//
 
5
// Redistribution and use in source and binary forms, with or without modification, are
 
6
// permitted provided that the following conditions are met:
 
7
//
 
8
// - Redistributions of source code must retain the above copyright notice, this list
 
9
//   of conditions and the following disclaimer.
 
10
//
 
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.
 
14
//
 
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
 
17
//   permission.
 
18
//
 
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.
 
27
 
 
28
using System;
 
29
using System.Diagnostics;
 
30
using System.IO;
 
31
using System.Windows.Forms;
 
32
 
 
33
using ICSharpCode.Core;
 
34
using ICSharpCode.Core.WinForms;
 
35
using ICSharpCode.SharpDevelop;
 
36
using ICSharpCode.SharpDevelop.Project;
 
37
 
 
38
namespace ICSharpCode.NAnt.Gui
 
39
{
 
40
        /// <summary>
 
41
        /// NAnt pad's tree view.  Shows a high level view of build file in
 
42
        /// a set of projects.
 
43
        /// </summary>
 
44
        public class NAntPadTreeView : System.Windows.Forms.UserControl, IOwnerState
 
45
        {
 
46
                private System.Windows.Forms.TreeView treeView;
 
47
                const string ContextMenuAddInTreePath = "/SharpDevelop/Pads/NAntPad/ContextMenu";
 
48
                
 
49
                /// <summary>
 
50
                /// The possible states of the tree view.
 
51
                /// </summary>
 
52
                public enum NAntPadTreeViewState {
 
53
                        Nothing = 0,
 
54
                        BuildFileSelected = 1,
 
55
                        TargetSelected = 2,
 
56
                        ErrorSelected = 4
 
57
                }
 
58
                
 
59
                /// <summary>
 
60
                /// The current state of the tree view.
 
61
                /// </summary>
 
62
                NAntPadTreeViewState state = NAntPadTreeViewState.Nothing;
 
63
                
 
64
                delegate void AddSolutionInvoker(Solution solution);
 
65
                
 
66
                public NAntPadTreeView()
 
67
                {
 
68
                        //
 
69
                        // The InitializeComponent() call is required for Windows Forms designer support.
 
70
                        //
 
71
                        InitializeComponent();
 
72
                        
 
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);
 
78
                }
 
79
                
 
80
                /// <summary>
 
81
                /// Gets the "ownerstate" condition.
 
82
                /// </summary>
 
83
                public Enum InternalState {
 
84
                        get {
 
85
                                return state;
 
86
                        }
 
87
                }
 
88
                
 
89
                /// <summary>
 
90
                /// Clears all items from the tree view.
 
91
                /// </summary>
 
92
                public void Clear()
 
93
                {
 
94
                        if (InvokeRequired) {
 
95
                                MethodInvoker invoker = new MethodInvoker(Clear);
 
96
                                Invoke(invoker);
 
97
                        } else {
 
98
                                treeView.Nodes.Clear();
 
99
                        }
 
100
                }
 
101
                
 
102
                /// <summary>
 
103
                /// Adds items to the tree view for each build file that exists
 
104
                /// in all the solution's subprojects.
 
105
                /// </summary>
 
106
                /// <param name="solution">A solution containing projects.</param>
 
107
                public void AddSolution(Solution solution)
 
108
                {
 
109
                        if (InvokeRequired) {
 
110
                                AddSolutionInvoker invoker = new AddSolutionInvoker(AddSolution);
 
111
                                Invoke(invoker);
 
112
                        } else {
 
113
                                foreach (IProject project in solution.Projects) {
 
114
                                        AddProject(project);
 
115
                                }
 
116
                        }
 
117
                }
 
118
                
 
119
                /// <summary>
 
120
                /// Adds items to the tree view for each build file that exist
 
121
                /// in a project.
 
122
                /// </summary>
 
123
                /// <param name="project">A SharpDevelop project.</param>
 
124
                public void AddProject(IProject project)
 
125
                {
 
126
                        Debug.Assert(!InvokeRequired, "AddProject InvokeRequired");
 
127
                        
 
128
                        foreach (ProjectItem projectItem in project.Items) {
 
129
                                if (NAntBuildFile.IsBuildFile(projectItem.FileName)) {
 
130
                                        AddBuildFile(project.Name, projectItem.FileName);
 
131
                                }
 
132
                        }
 
133
                }
 
134
                
 
135
                /// <summary>
 
136
                /// Removes the specified build file from the
 
137
                /// tree view.</summary>
 
138
                public void RemoveBuildFile(string fileName)
 
139
                {
 
140
                        Debug.Assert(!InvokeRequired, "RemoveBuildFile InvokeRequired");
 
141
                        
 
142
                        NAntBuildFileTreeNode node = FindMatchingNode(fileName);
 
143
                        if (node != null) {
 
144
                                node.Remove();
 
145
                        }
 
146
                }
 
147
                
 
148
                /// <summary>
 
149
                /// Renames the build file.
 
150
                /// </summary>
 
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)
 
154
                {
 
155
                        Debug.Assert(!InvokeRequired, "RenameBuildFile InvokeRequired");
 
156
 
 
157
                        NAntBuildFileTreeNode node = FindMatchingNode(oldFileName);
 
158
                        
 
159
                        if (node != null) {
 
160
                                node.FileName = Path.GetFileName(newFileName);
 
161
                        }
 
162
                }
 
163
                
 
164
                /// <summary>
 
165
                /// Updates the build file in the tree view.
 
166
                /// </summary>
 
167
                /// <param name="fileName">The build file name.</param>
 
168
                public void UpdateBuildFile(string fileName)
 
169
                {
 
170
                        Debug.Assert(!InvokeRequired, "UpdateBuildFile InvokeRequired");
 
171
                        
 
172
                        NAntBuildFileTreeNode node = FindMatchingNode(fileName);
 
173
                        
 
174
                        if (node != null) {
 
175
                                NAntBuildFile buildFile = new NAntBuildFile(fileName);
 
176
                                node.BuildFile = buildFile;
 
177
                        } else {
 
178
                                AddBuildFile(String.Empty, fileName);
 
179
                        }
 
180
                }
 
181
                
 
182
                /// <summary>
 
183
                /// Adds a build file to the tree.
 
184
                /// </summary>
 
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)
 
191
                {
 
192
                        Debug.Assert(!InvokeRequired, "AddBuildFile InvokeRequired");
 
193
 
 
194
                        if (File.Exists(fileName)) {
 
195
                                NAntBuildFile buildFile = new NAntBuildFile(fileName);
 
196
                                NAntBuildFileTreeNode node = new NAntBuildFileTreeNode(projectName, buildFile);
 
197
                                treeView.Nodes.Add(node);
 
198
                        }
 
199
                }
 
200
                
 
201
                /// <summary>
 
202
                /// Gets the currently selected <see cref="NAntBuildFile"/>.
 
203
                /// </summary>
 
204
                /// <remarks>This will return a NAntBuildFile if
 
205
                /// a target node is selected.</remarks>
 
206
                public NAntBuildFile SelectedBuildFile {
 
207
                        get {
 
208
                                NAntBuildFile buildFile = null;
 
209
                                
 
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;
 
222
                                }
 
223
                                
 
224
                                return buildFile;
 
225
                        }
 
226
                }
 
227
                
 
228
                /// <summary>
 
229
                /// Gets the current selected <see cref="NAntBuildTarget"/>
 
230
                /// </summary>
 
231
                public NAntBuildTarget SelectedTarget {
 
232
                        get {
 
233
                                NAntBuildTarget target = null;
 
234
                                
 
235
                                NAntBuildTargetTreeNode targetNode = treeView.SelectedNode as NAntBuildTargetTreeNode;
 
236
                                
 
237
                                if (targetNode != null) {
 
238
                                        target = targetNode.Target;
 
239
                                }
 
240
                                
 
241
                                return target;
 
242
                        }
 
243
                }
 
244
                
 
245
                /// <summary>
 
246
                /// Gets the current selected <see cref="NAntBuildFileError"/>
 
247
                /// </summary>
 
248
                public NAntBuildFileError SelectedError {
 
249
                        get {
 
250
                                NAntBuildFileError error = null;
 
251
                                
 
252
                                NAntBuildFileErrorTreeNode errorNode = treeView.SelectedNode as NAntBuildFileErrorTreeNode;
 
253
                                
 
254
                                if (errorNode != null) {
 
255
                                        error = errorNode.Error;
 
256
                                }
 
257
                                
 
258
                                return error;
 
259
                        }
 
260
                }
 
261
                
 
262
                /// <summary>
 
263
                /// Gets whether a target is selected.
 
264
                /// </summary>
 
265
                public bool IsTargetSelected {
 
266
                        get {
 
267
                                bool isSelected = false;
 
268
                                
 
269
                                if (SelectedTarget != null) {
 
270
                                        isSelected = true;
 
271
                                }
 
272
                                
 
273
                                return isSelected;
 
274
                        }
 
275
                }
 
276
                
 
277
                #region Windows Forms Designer generated code
 
278
                /// <summary>
 
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.
 
282
                /// </summary>
 
283
                private void InitializeComponent() {
 
284
                        this.treeView = new System.Windows.Forms.TreeView();
 
285
                        this.SuspendLayout();
 
286
                        // 
 
287
                        // treeView
 
288
                        // 
 
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);
 
297
                        // 
 
298
                        // NAntPadTreeView
 
299
                        // 
 
300
                        this.Controls.Add(this.treeView);
 
301
                        this.Name = "NAntPadTreeView";
 
302
                        this.Size = new System.Drawing.Size(292, 266);
 
303
                        this.ResumeLayout(false);
 
304
                }
 
305
                #endregion
 
306
                
 
307
                /// <summary>
 
308
                /// User clicked the tree view.
 
309
                /// </summary>
 
310
                /// <param name="sender">The event source.</param>
 
311
                /// <param name="e">The event arguments.</param>
 
312
                void TreeViewMouseDown(object sender, MouseEventArgs e)
 
313
                {
 
314
                        TreeNode node = treeView.GetNodeAt(e.X, e.Y);
 
315
 
 
316
                        treeView.SelectedNode = node;
 
317
                        
 
318
                        state = NAntPadTreeViewState.Nothing;
 
319
                        if (IsBuildFileNodeSelected) {
 
320
                                state = NAntPadTreeViewState.BuildFileSelected;
 
321
                        }
 
322
                        
 
323
                        if (IsBuildTargetNodeSelected) {
 
324
                                state = NAntPadTreeViewState.TargetSelected;
 
325
                        }
 
326
                        
 
327
                        if (IsBuildFileErrorNodeSelected) {
 
328
                                state = NAntPadTreeViewState.ErrorSelected;
 
329
                        }
 
330
                }
 
331
                
 
332
                /// <summary>
 
333
                /// Gets whether a build file is selected.
 
334
                /// </summary>
 
335
                bool IsBuildFileNodeSelected {
 
336
                        get {
 
337
                                return treeView.SelectedNode is NAntBuildFileTreeNode;
 
338
                        }
 
339
                }
 
340
                
 
341
                /// <summary>
 
342
                /// Gets whether a target is selected.
 
343
                /// </summary>
 
344
                bool IsBuildTargetNodeSelected {
 
345
                        get {
 
346
                                return treeView.SelectedNode is NAntBuildTargetTreeNode;
 
347
                        }
 
348
                }
 
349
                
 
350
                /// <summary>
 
351
                /// Gets whether a build file error is selected.
 
352
                /// </summary>
 
353
                bool IsBuildFileErrorNodeSelected {
 
354
                        get {
 
355
                                return treeView.SelectedNode is NAntBuildFileErrorTreeNode;
 
356
                        }
 
357
                }
 
358
                
 
359
                /// <summary>
 
360
                /// Double clicking a node on the tree view opens the corresponding
 
361
                /// file.
 
362
                /// </summary>
 
363
                /// <param name="sender">The event source.</param>
 
364
                /// <param name="e">The event arguments.</param>
 
365
                void TreeViewDoubleClick(object sender, EventArgs e)
 
366
                {
 
367
                        NAntBuildFile buildFile = SelectedBuildFile;
 
368
                        if (buildFile != null) {
 
369
                                
 
370
                                string fileName = Path.Combine(buildFile.Directory, buildFile.FileName);
 
371
                                
 
372
                                if (IsBuildTargetNodeSelected) {
 
373
                                        FileService.JumpToFilePosition(fileName, SelectedTarget.Line, SelectedTarget.Column);
 
374
                                } else if (IsBuildFileErrorNodeSelected) {
 
375
                                        FileService.JumpToFilePosition(fileName, SelectedError.Line, SelectedError.Column);
 
376
                                } else {
 
377
                                        FileService.OpenFile(fileName);
 
378
                                }
 
379
                        }
 
380
                }
 
381
                
 
382
                /// <summary>
 
383
                /// Looks for the tree node that is displaying the specified
 
384
                /// build file.
 
385
                /// </summary>
 
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)
 
390
                {
 
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) {
 
394
                                        return node;
 
395
                                }
 
396
                        }
 
397
                        return null;
 
398
                }
 
399
        }
 
400
}