~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Internal/Doozers/PadDescriptor.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
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using ICSharpCode.Core;
 
6
using ICSharpCode.SharpDevelop.Gui;
 
7
 
 
8
namespace ICSharpCode.SharpDevelop
 
9
{
 
10
        /// <summary>
 
11
        /// Indicates the default position for a pad.
 
12
        /// This is a bit-flag enum, Hidden can be combined with the directions.
 
13
        /// </summary>
 
14
        [Flags]
 
15
        public enum DefaultPadPositions
 
16
        {
 
17
                None = 0,
 
18
                Right = 1,
 
19
                Left = 2,
 
20
                Bottom = 4,
 
21
                Top = 8,
 
22
                Hidden = 16
 
23
        }
 
24
        
 
25
        /// <summary>
 
26
        /// Describes a pad.
 
27
        /// </summary>
 
28
        public class PadDescriptor : IDisposable
 
29
        {
 
30
                string @class;
 
31
                string title;
 
32
                string icon;
 
33
                string category;
 
34
                string shortcut;
 
35
                
 
36
                AddIn addIn;
 
37
                Type padType;
 
38
                
 
39
                IPadContent padContent;
 
40
                bool        padContentCreated;
 
41
                
 
42
                /// <summary>
 
43
                /// Creates a new pad descriptor from the AddIn tree.
 
44
                /// </summary>
 
45
                public PadDescriptor(Codon codon)
 
46
                {
 
47
                        if (codon == null)
 
48
                                throw new ArgumentNullException("codon");
 
49
                        addIn = codon.AddIn;
 
50
                        shortcut = codon.Properties["shortcut"];
 
51
                        category = codon.Properties["category"];
 
52
                        icon = codon.Properties["icon"];
 
53
                        title = codon.Properties["title"];
 
54
                        @class = codon.Properties["class"];
 
55
                        if (!string.IsNullOrEmpty(codon.Properties["defaultPosition"])) {
 
56
                                DefaultPosition = (DefaultPadPositions)Enum.Parse(typeof(DefaultPadPositions), codon.Properties["defaultPosition"]);
 
57
                        }
 
58
                }
 
59
                
 
60
                /// <summary>
 
61
                /// Creates a pad descriptor for the specified pad type.
 
62
                /// </summary>
 
63
                public PadDescriptor(Type padType, string title, string icon)
 
64
                {
 
65
                        if (padType == null)
 
66
                                throw new ArgumentNullException("padType");
 
67
                        if (title == null)
 
68
                                throw new ArgumentNullException("title");
 
69
                        if (icon == null)
 
70
                                throw new ArgumentNullException("icon");
 
71
                        this.padType = padType;
 
72
                        this.@class = padType.FullName;
 
73
                        this.title = title;
 
74
                        this.icon = icon;
 
75
                        this.category = "none";
 
76
                        this.shortcut = "";
 
77
                }
 
78
                
 
79
                /// <summary>
 
80
                /// Returns the title of the pad.
 
81
                /// </summary>
 
82
                public string Title {
 
83
                        get {
 
84
                                return title;
 
85
                        }
 
86
                }
 
87
                
 
88
                /// <summary>
 
89
                /// Returns the icon bitmap resource name of the pad. May be an empty string
 
90
                /// if the pad has no icon defined.
 
91
                /// </summary>
 
92
                public string Icon {
 
93
                        get {
 
94
                                return icon;
 
95
                        }
 
96
                }
 
97
                
 
98
                /// <summary>
 
99
                /// Returns the category (this is used for defining where the menu item to
 
100
                /// this pad goes)
 
101
                /// </summary>
 
102
                public string Category {
 
103
                        get {
 
104
                                return category;
 
105
                        }
 
106
                        set {
 
107
                                if (value == null)
 
108
                                        throw new ArgumentNullException("value");
 
109
                                category = value;
 
110
                        }
 
111
                }
 
112
                
 
113
                /// <summary>
 
114
                /// Returns the menu shortcut for the view menu item.
 
115
                /// </summary>
 
116
                public string Shortcut {
 
117
                        get {
 
118
                                return shortcut;
 
119
                        }
 
120
                        set {
 
121
                                if (value == null)
 
122
                                        throw new ArgumentNullException("value");
 
123
                                shortcut = value;
 
124
                        }
 
125
                }
 
126
                
 
127
                /// <summary>
 
128
                /// Gets the name of the pad class.
 
129
                /// </summary>
 
130
                public string Class {
 
131
                        get {
 
132
                                return @class;
 
133
                        }
 
134
                }
 
135
                
 
136
                /// <summary>
 
137
                /// Gets/sets the default position of the pad.
 
138
                /// </summary>
 
139
                public DefaultPadPositions DefaultPosition { get; set; }
 
140
                
 
141
                public IPadContent PadContent {
 
142
                        get {
 
143
                                CreatePad();
 
144
                                return padContent;
 
145
                        }
 
146
                }
 
147
                
 
148
                public void Dispose()
 
149
                {
 
150
                        if (padContent != null) {
 
151
                                padContent.Dispose();
 
152
                                padContent = null;
 
153
                        }
 
154
                }
 
155
                
 
156
                public void CreatePad()
 
157
                {
 
158
                        if (WorkbenchSingleton.InvokeRequired)
 
159
                                throw new InvalidOperationException("This action could trigger pad creation and is only valid on the main thread!");
 
160
                        if (!padContentCreated) {
 
161
                                padContentCreated = true;
 
162
                                try {
 
163
                                        if (addIn != null) {
 
164
                                                LoggingService.Debug("Creating pad " + Class + "...");
 
165
                                                padContent = (IPadContent)addIn.CreateObject(Class);
 
166
                                        } else {
 
167
                                                padContent = (IPadContent)Activator.CreateInstance(padType);
 
168
                                        }
 
169
                                } catch (Exception ex) {
 
170
                                        MessageService.ShowException(ex, "Error creating pad instance");
 
171
                                }
 
172
                        }
 
173
                }
 
174
                
 
175
                public void BringPadToFront()
 
176
                {
 
177
                        CreatePad();
 
178
                        if (padContent == null) return;
 
179
                        WorkbenchSingleton.Workbench.WorkbenchLayout.ActivatePad(this);
 
180
                }
 
181
                
 
182
                public override string ToString()
 
183
                {
 
184
                        return "[PadDescriptor " + this.Class + "]";
 
185
                }
 
186
        }
 
187
}