~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet.Mvc/Gui/AddViewDialog.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
using System.Linq;
29
29
using System.IO;
30
30
using System.Collections.Generic;
 
31
using MonoDevelop.AspNet.StateEngine;
31
32
using PP = System.IO.Path;
32
33
 
33
34
using MonoDevelop.AspNet.Gui;
37
38
using MonoDevelop.Components;
38
39
using ICSharpCode.NRefactory.TypeSystem;
39
40
using MonoDevelop.Ide.TypeSystem;
 
41
using MonoDevelop.Xml.StateEngine;
40
42
 
41
43
namespace MonoDevelop.AspNet.Mvc.Gui
42
44
{
44
46
        
45
47
        public partial class AddViewDialog : Gtk.Dialog
46
48
        {       
47
 
                AspMvcProject project;
48
 
                IList<string> loadedTemplateList;
49
 
                string oldMaster;
50
 
                Gtk.ListStore primaryPlaceholderStore = new Gtk.ListStore (typeof (String));
51
 
                System.CodeDom.Compiler.CodeDomProvider provider;
52
 
                DropDownBox dataClassCombo;
 
49
                AspMvcProject project;
 
50
                IDictionary<string, IList<string>> loadedTemplateList;
 
51
                IDictionary<string, Gtk.ListStore> templateStore;
 
52
                Gtk.ListStore dataClassStore;
 
53
                string oldMaster;
 
54
                string oldEngine;
 
55
                Gtk.ListStore primaryPlaceholderStore = new Gtk.ListStore (typeof (String));
 
56
                System.CodeDom.Compiler.CodeDomProvider provider;
53
57
                TypeDataProvider classDataProvider;
54
58
                
55
59
                public AddViewDialog (AspMvcProject project)
57
61
                        this.project = project;
58
62
                        this.Build ();
59
63
                        
60
 
                        dataClassCombo = new DropDownBox ();
61
 
                        
62
 
                        int w, h;
63
 
                        Gtk.Icon.SizeLookup (Gtk.IconSize.Menu, out w, out h);
64
 
                        dataClassCombo.DefaultIconHeight = Math.Max (h, 16);
65
 
                        dataClassCombo.DefaultIconWidth = Math.Max (w, 16);
66
 
                        dataClassAlignment.Add (dataClassCombo);
67
 
                        dataClassAlignment.QueueResize ();
68
 
                        dataClassCombo.ShowAll ();
69
 
                        
70
 
                        provider = project.LanguageBinding.GetCodeDomProvider ();
 
64
                        provider = project.LanguageBinding.GetCodeDomProvider ();
 
65
 
 
66
                        var viewEngines = GetProperViewEngines ();
 
67
                        loadedTemplateList = new Dictionary<string, IList<string>> ();
 
68
                        foreach (var engine in viewEngines) {
 
69
                                viewEngineCombo.AppendText (engine);
 
70
                                loadedTemplateList[engine] = project.GetCodeTemplates ("AddView", engine);
 
71
                        }
 
72
 
 
73
                        viewEngineCombo.Active = 0;
 
74
                        InitializeTemplateStore (loadedTemplateList);
71
75
                        
72
76
                        ContentPlaceHolders = new List<string> ();
73
77
                        string siteMaster = project.VirtualToLocalPath ("~/Views/Shared/Site.master", null);
74
78
                        if (project.Files.GetFile (siteMaster) != null)
75
79
                                masterEntry.Text = "~/Views/Shared/Site.master";
76
80
                        
77
 
                        loadedTemplateList = project.GetCodeTemplates ("AddView");
78
 
                        bool foundEmptyTemplate = false;
79
 
                        int templateIndex = 0;
80
 
                        foreach (string file in loadedTemplateList) {
81
 
                                string name = PP.GetFileNameWithoutExtension (file);
82
 
                                templateCombo.AppendText (name);
83
 
                                if (!foundEmptyTemplate){
84
 
                                        if (name == "Empty") {
85
 
                                                templateCombo.Active = templateIndex;
86
 
                                                foundEmptyTemplate = true;
87
 
                                        } else
88
 
                                                templateIndex++;
89
 
                                }
90
 
                        }
91
 
                        
92
 
                        if (!foundEmptyTemplate)
93
 
                                throw new Exception ("The Empty.tt template is missing.");
94
 
                        
95
81
                        primaryPlaceholderCombo.Model = primaryPlaceholderStore;
96
82
                        
97
83
                        UpdateTypePanelSensitivity (null, null);
98
84
                        UpdateMasterPanelSensitivity (null, null);
99
85
                        Validate ();
 
86
                }
 
87
 
 
88
                IEnumerable<string> GetProperViewEngines ()
 
89
                {
 
90
                        yield return "Aspx";
 
91
                        if (MvcVersion.CompareTo ("2.0.0.0") > 0)
 
92
                                yield return "Razor";
 
93
                }
 
94
 
 
95
                void InitializeTemplateStore (IDictionary<string, IList<string>> tempList)
 
96
                {
 
97
                        templateStore = new Dictionary<string, Gtk.ListStore> ();
 
98
 
 
99
                        foreach (var engine in tempList.Keys) {
 
100
                                bool foundEmptyTemplate = false;
 
101
                                templateStore[engine] = new Gtk.ListStore (typeof (string));
 
102
 
 
103
                                foreach (string file in tempList[engine]) {
 
104
                                        string name = PP.GetFileNameWithoutExtension (file);
 
105
                                        if (!foundEmptyTemplate) {
 
106
                                                if (name == "Empty") {
 
107
                                                        templateStore[engine].InsertWithValues (0, name);
 
108
                                                        foundEmptyTemplate = true;
 
109
                                                } else
 
110
                                                        templateStore[engine].AppendValues (name);
 
111
                                        }
 
112
                                }
 
113
 
 
114
                                if (!foundEmptyTemplate)
 
115
                                        throw new Exception ("The Empty.tt template is missing.");
 
116
                        }
 
117
 
 
118
                        UpdateTemplateList ();
 
119
                }
 
120
 
 
121
                void UpdateTemplateList ()
 
122
                {
 
123
                        templateCombo.Model = templateStore[ActiveViewEngine];
 
124
                        oldEngine = ActiveViewEngine;
 
125
                        templateCombo.Active = 0;
100
126
                }
101
127
                
102
128
                protected virtual void Validate (object sender, EventArgs e)
113
139
                {
114
140
                        bool canHaveMaster = !IsPartialView;
115
141
                        masterCheck.Sensitive = canHaveMaster;
116
 
                        masterPanel.Sensitive = canHaveMaster && HasMaster;
 
142
                        masterPanel.Sensitive = canHaveMaster && HasMaster;
 
143
                        placeholderBox.Sensitive = masterPanel.Sensitive && ActiveViewEngine != "Razor";
117
144
                        MasterChanged (null, null);
118
145
                        Validate ();
119
146
                }
120
147
                
121
148
                protected virtual void UpdateTypePanelSensitivity (object sender, EventArgs e)
122
 
                {
123
 
                        //FIXME: need to fix the class list widget
124
 
                        bool enabled = typePanel.Sensitive = false; // stronglyTypedCheck.Active;
125
 
                        
126
 
                        if (enabled && classDataProvider == null) {
127
 
                                dataClassCombo.DataProvider = classDataProvider = new TypeDataProvider (project);
128
 
                                if (classDataProvider.List.Count > 0)
129
 
                                        dataClassCombo.SetItem (0);
130
 
                        }
131
 
                        
 
149
                {
 
150
                        bool enabled = typePanel.Sensitive = stronglyTypedCheck.Active;
 
151
 
 
152
                        if (enabled && classDataProvider == null) {
 
153
                                classDataProvider = new TypeDataProvider (project);
 
154
                                dataClassStore = new Gtk.ListStore (typeof (string));
 
155
                                foreach (var item in classDataProvider.TypeNamesList)
 
156
                                        dataClassStore.AppendValues (item);
 
157
                                dataClassCombo.Model = dataClassStore;
 
158
                                if (classDataProvider.TypeNamesList.Count > 0)
 
159
                                        dataClassCombo.Active = 0;
 
160
                        }
 
161
 
132
162
                        Validate ();
133
163
                }
134
164
                
141
171
                public bool IsValid ()
142
172
                {
143
173
                        if (!IsValidIdentifier (ViewName))
144
 
                                return false;
145
 
                        
146
 
                        if (!IsPartialView && HasMaster) {
147
 
                                if (String.IsNullOrEmpty (MasterFile) || !System.IO.File.Exists (project.VirtualToLocalPath (oldMaster, null)))
148
 
                                return false;
149
 
                                //PrimaryPlaceHolder can be empty
 
174
                                return false;
 
175
 
 
176
                        if (!IsPartialView && HasMaster && ActiveViewEngine != "Razor") {
 
177
                                if (String.IsNullOrEmpty (MasterFile) || !System.IO.File.Exists (project.VirtualToLocalPath (oldMaster, null)))
 
178
                                        return false;
 
179
                                //PrimaryPlaceHolder can be empty
 
180
                                //Layout Page can be empty in Razor Views - it's usually set in _ViewStart.cshtml file
150
181
                        }
151
182
                        
152
 
                        if (IsStronglyTyped && (ViewDataType == null))
 
183
                        if (IsStronglyTyped && String.IsNullOrEmpty(ViewDataTypeString))
153
184
                            return false;
154
185
                        
155
186
                        return true;
161
192
                }
162
193
        
163
194
                protected virtual void ShowMasterSelectionDialog (object sender, System.EventArgs e)
164
 
                {
165
 
                        var dialog = new MonoDevelop.Ide.Projects.ProjectFileSelectorDialog (project, null, "*.master") {
166
 
                                Title = MonoDevelop.Core.GettextCatalog.GetString ("Select a Master Page..."),
167
 
                                TransientFor = this,
 
195
                {
 
196
                        string pattern, title;
 
197
                        if (ActiveViewEngine == "Razor") {
 
198
                                pattern = "*.cshtml";
 
199
                                title = MonoDevelop.Core.GettextCatalog.GetString ("Select a Layout file...");
 
200
                        } else {
 
201
                                pattern = "*.master";
 
202
                                title = MonoDevelop.Core.GettextCatalog.GetString ("Select a Master Page...");
 
203
                        }
 
204
                        var dialog = new MonoDevelop.Ide.Projects.ProjectFileSelectorDialog (project, null, pattern)
 
205
                        {
 
206
                                Title = title,
 
207
                                TransientFor = this,
168
208
                        };
169
209
                        try {
170
210
                                if (MessageService.RunCustomDialog (dialog) == (int) Gtk.ResponseType.Ok)
195
235
                        
196
236
                        if (pd != null) {
197
237
                                try {
198
 
                                        var visitor = new ContentPlaceHolderVisitor ();
199
 
                                        pd.RootNode.AcceptVisit (visitor);
200
 
                                        ContentPlaceHolders.AddRange (visitor.PlaceHolders);
 
238
                                        ContentPlaceHolders.AddRange (pd.XDocument.GetAllPlaceholderIds ());
201
239
                                        
202
240
                                        for (int i = 0; i < ContentPlaceHolders.Count; i++) {
203
241
                                                string placeholder = ContentPlaceHolders[i];
204
242
                                                primaryPlaceholderStore.AppendValues (placeholder);
205
243
                                                
206
244
                                                if (placeholder.Contains ("main") || placeholder.Contains ("Main") 
207
 
                                                        || placeholder.Contains ("content") || placeholder.Contains ("Main"))
 
245
                                                        || placeholder.Contains ("content") || placeholder.Contains ("Content"))
208
246
                                                        primaryPlaceholderCombo.Active = i;
209
247
                                        }
210
248
                                } catch (Exception ex) {
213
251
                        }
214
252
                        
215
253
                        Validate ();
 
254
                }
 
255
 
 
256
                protected virtual void ViewEngineChanged (object sender, EventArgs e)
 
257
                {
 
258
                        if (String.IsNullOrEmpty (oldEngine))
 
259
                                return;
 
260
                        if (oldEngine != ActiveViewEngine) {
 
261
                                UpdateTemplateList ();
 
262
                                UpdateMasterPanelSensitivity (null, null);
 
263
                        }
 
264
                }
 
265
 
 
266
                protected virtual void DataClassChanged (object sender, EventArgs e)
 
267
                {
 
268
                        Validate ();
216
269
                }
217
270
                
218
271
                #region Public properties
219
272
                
220
 
                public IType ViewDataType {
 
273
                public Type ViewDataType {
 
274
                        get {
 
275
                                return dataClassCombo.Active >= 0 ? (Type)classDataProvider.TypesList[dataClassCombo.Active] : System.Type.GetType(dataClassCombo.ActiveText, false);
 
276
                        }
 
277
                }
 
278
 
 
279
                public string ViewDataTypeString {
221
280
                        get {
222
 
                                return (IType)dataClassCombo.CurrentItem;
 
281
                                return dataClassCombo.ActiveText;
223
282
                        }
224
283
                }
225
284
                
246
305
                }
247
306
                
248
307
                public string TemplateFile {
249
 
                        get {
250
 
                                return loadedTemplateList[templateCombo.Active];
 
308
                        get {
 
309
                                return loadedTemplateList[ActiveViewEngine][templateCombo.Active];
251
310
                        }
252
311
                }
253
312
                
267
326
                public bool IsStronglyTyped {
268
327
                        get { return stronglyTypedCheck.Active; }
269
328
                }
270
 
                
271
 
                #endregion
272
 
                
273
 
                class TypeDataProvider : DropDownBoxListWindow.IListDataProvider
274
 
                {
275
 
                        Ambience ambience;
276
 
                        
277
 
                        public List<ITypeDefinition> List { get; private set; }
278
 
                        
279
 
                        public TypeDataProvider (MonoDevelop.Projects.DotNetProject project)
280
 
                        {
281
 
                                var ctx = TypeSystemService.GetCompilation (project);
282
 
                                List = new List<ITypeDefinition> (ctx.MainAssembly.GetAllTypeDefinitions ());
283
 
                                this.ambience = AmbienceService.GetAmbience (project.LanguageName);
284
 
                        }
285
 
                        
286
 
                        public int IconCount { get { return List.Count; } }
287
 
                        
288
 
                        public void Reset ()
289
 
                        {
290
 
                                //called when the list is shown
291
 
                        }
292
 
                        
293
 
                        public string GetMarkup (int n)
294
 
                        {
295
 
                                return ambience.GetString ((IEntity)List[n], OutputFlags.IncludeGenerics | OutputFlags.UseFullName | OutputFlags.IncludeMarkup);
296
 
                        }
297
 
                        
298
 
                        public Gdk.Pixbuf GetIcon (int n)
299
 
                        {
300
 
                                return ImageService.GetPixbuf (List[n].GetStockIcon (),Gtk.IconSize.Menu);
301
 
                        }
302
 
                        
303
 
                        public object GetTag (int n)
304
 
                        {
305
 
                                return List[n];
306
 
                        }
307
 
                        
308
 
                        public void ActivateItem (int n)
309
 
                        {
310
 
                                // nothing
311
 
                        }
 
329
 
 
330
                public string ActiveViewEngine {
 
331
                        get { return viewEngineCombo.ActiveText; }
 
332
                }
 
333
 
 
334
                public string MvcVersion {
 
335
                        get {
 
336
                                return project.GetAspNetMvcVersion ();
 
337
                        }
 
338
                }
 
339
                
 
340
                #endregion
 
341
 
 
342
                class TypeDataProvider
 
343
                {
 
344
                        public List<ITypeDefinition> TypesList { get; private set; }
 
345
                        public List<string> TypeNamesList { get; private set; }
 
346
                        Ambience ambience;
 
347
 
 
348
                        public TypeDataProvider (MonoDevelop.Projects.DotNetProject project)
 
349
                        {
 
350
                                TypeNamesList = new List<string> ();
 
351
                                var ctx = TypeSystemService.GetCompilation (project);
 
352
                                TypesList = new List<ITypeDefinition> (ctx.MainAssembly.GetAllTypeDefinitions ());
 
353
                                this.ambience = AmbienceService.GetAmbience (project.LanguageName);
 
354
                                foreach (var typeDef in TypesList) {
 
355
                                        TypeNamesList.Add (ambience.GetString ((IEntity)typeDef, OutputFlags.IncludeGenerics | OutputFlags.UseFullName | OutputFlags.IncludeMarkup));
 
356
                                }
 
357
                        }
312
358
                }
313
359
        }
314
360
}