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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
//
// CodeFormattingPanelWidget.cs
//
// Author:
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using Mono.Addins;
using MonoDevelop.Ide.Gui.Dialogs;
using MonoDevelop.Ide.Extensions;
using MonoDevelop.Projects;
using MonoDevelop.Core;
using MonoDevelop.Projects.Policies;
using MonoDevelop.Components;
namespace MonoDevelop.Ide.Projects.OptionPanels
{
class CodeFormattingPanel: OptionsPanel
{
PolicyContainer policyContainer;
Dictionary<string,MimeTypePanelData> typeSections = new Dictionary<string, MimeTypePanelData> ();
List<string> globalMimeTypes;
public override void Initialize (MonoDevelop.Ide.Gui.Dialogs.OptionsDialog dialog, object dataObject)
{
base.Initialize (dialog, dataObject);
if (dataObject is SolutionItem) {
policyContainer = ((SolutionItem)dataObject).Policies;
} else if (dataObject is Solution) {
policyContainer = ((Solution)dataObject).Policies;
} else if (dataObject is PolicySet) {
policyContainer = ((PolicySet)dataObject);
globalMimeTypes = new List<string> ();
string userTypes = PropertyService.Get<string> ("MonoDevelop.Projects.GlobalPolicyMimeTypes", "");
globalMimeTypes.AddRange (userTypes.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
}
foreach (string mt in GetItemMimeTypes ())
AddPanel (mt);
}
MimeTypePanelData AddPanel (string mt)
{
MimeTypePanelData data = new MimeTypePanelData ();
OptionsDialogSection sec = new OptionsDialogSection (typeof(MimeTypePolicyOptionsSection));
sec.Fill = true;
Gdk.Pixbuf icon = DesktopService.GetPixbufForType (mt, Gtk.IconSize.Menu);
sec.Icon = ImageService.GetStockId (icon, Gtk.IconSize.Menu);
data.Section = sec;
data.MimeType = mt;
data.TypeDescription = DesktopService.GetMimeTypeDescription (mt);
if (string.IsNullOrEmpty (data.TypeDescription))
data.TypeDescription = mt;
data.DataObject = DataObject;
data.PolicyContainer = policyContainer;
sec.Label = data.TypeDescription;
LoadPolicyTypeData (data, mt);
typeSections [mt] = data;
ParentDialog.AddChildSection (this, sec, data);
return data;
}
void RemovePanel (string mt)
{
MimeTypePanelData data = typeSections [mt];
typeSections.Remove (mt);
ParentDialog.RemoveSection (data.Section);
}
internal MimeTypePanelData AddGlobalMimeType (string mt)
{
if (!globalMimeTypes.Contains (mt)) {
globalMimeTypes.Add (mt);
return AddPanel (mt);
}
return null;
}
internal void RemoveGlobalMimeType (string mt)
{
if (globalMimeTypes.Remove (mt))
RemovePanel (mt);
}
public IEnumerable<MimeTypePanelData> GetMimeTypeData ()
{
return typeSections.Values;
}
public PolicyContainer PolicyContainer {
get { return policyContainer; }
}
void LoadPolicyTypeData (MimeTypePanelData data, string mimeType)
{
List<string> types = new List<string> ();
types.AddRange (DesktopService.GetMimeTypeInheritanceChain (mimeType));
List<IMimeTypePolicyOptionsPanel> panels = new List<IMimeTypePolicyOptionsPanel> ();
bool useParentPolicy = false;
foreach (MimeTypeOptionsPanelNode node in AddinManager.GetExtensionNodes ("/MonoDevelop/ProjectModel/Gui/MimeTypePolicyPanels")) {
if (!types.Contains (node.MimeType))
continue;
IMimeTypePolicyOptionsPanel panel = (IMimeTypePolicyOptionsPanel) node.CreateInstance (typeof(IMimeTypePolicyOptionsPanel));
panel.Initialize (ParentDialog, DataObject);
panel.InitializePolicy (policyContainer, mimeType, mimeType == node.MimeType);
panel.Label = GettextCatalog.GetString (node.Label);
if (!panel.IsVisible ())
continue;
if (!panel.HasCustomPolicy)
useParentPolicy = true;
panels.Add (panel);
}
data.Panels = panels;
if (!policyContainer.IsRoot)
data.UseParentPolicy = useParentPolicy;
}
public override Gtk.Widget CreatePanelWidget ()
{
return new CodeFormattingPanelWidget (this, ParentDialog);
}
public override void ApplyChanges ()
{
if (globalMimeTypes != null) {
string types = string.Join (";", globalMimeTypes.ToArray ());
PropertyService.Set ("MonoDevelop.Projects.GlobalPolicyMimeTypes", types);
}
// If a section is already loaded, changes will be committed in the panel
foreach (MimeTypePanelData pd in typeSections.Values) {
if (!pd.SectionLoaded)
pd.ApplyChanges ();
}
}
public IEnumerable<string> GetItemMimeTypes ()
{
HashSet<string> types = new HashSet<string> ();
if (DataObject is Solution)
GetItemMimeTypes (types, ((Solution)DataObject).RootFolder);
else if (DataObject is SolutionItem)
GetItemMimeTypes (types, (SolutionItem)DataObject);
else {
types.Add ("application/xml");
foreach (MimeTypeOptionsPanelNode node in AddinManager.GetExtensionNodes ("/MonoDevelop/ProjectModel/Gui/MimeTypePolicyPanels")) {
types.Add (node.MimeType);
globalMimeTypes.Remove (node.MimeType);
}
types.UnionWith (globalMimeTypes);
}
return types;
}
public bool IsUserMimeType (string type)
{
return globalMimeTypes != null && globalMimeTypes.Contains (type);
}
void GetItemMimeTypes (HashSet<string> types, SolutionItem item)
{
if (item is SolutionFolder) {
foreach (SolutionItem it in ((SolutionFolder)item).Items)
GetItemMimeTypes (types, it);
}
else if (item is Project) {
foreach (ProjectFile pf in ((Project)item).Files) {
string mt = DesktopService.GetMimeTypeForUri (pf.FilePath);
foreach (string mth in DesktopService.GetMimeTypeInheritanceChain (mt))
types.Add (mth);
}
}
}
}
[System.ComponentModel.ToolboxItem(true)]
partial class CodeFormattingPanelWidget : Gtk.Bin
{
CodeFormattingPanel panel;
Gtk.ListStore store;
OptionsDialog dialog;
public CodeFormattingPanelWidget (CodeFormattingPanel panel, OptionsDialog dialog)
{
this.Build();
this.panel = panel;
this.dialog = dialog;
store = new Gtk.ListStore (typeof(MimeTypePanelData), typeof(Gdk.Pixbuf), typeof(string));
tree.Model = store;
boxButtons.Visible = panel.DataObject is PolicySet;
Gtk.CellRendererText crt = new Gtk.CellRendererText ();
Gtk.CellRendererPixbuf crp = new Gtk.CellRendererPixbuf ();
Gtk.TreeViewColumn col = new Gtk.TreeViewColumn ();
col.Title = GettextCatalog.GetString ("File Type");
col.PackStart (crp, false);
col.PackStart (crt, true);
col.AddAttribute (crp, "pixbuf", 1);
col.AddAttribute (crt, "text", 2);
tree.AppendColumn (col);
store.SetSortColumnId (2, Gtk.SortType.Ascending);
CellRendererComboBox comboCell = new CellRendererComboBox ();
comboCell.Changed += OnPolicySelectionChanged;
Gtk.TreeViewColumn polCol = tree.AppendColumn (GettextCatalog.GetString ("Policy"), comboCell, new Gtk.TreeCellDataFunc (OnSetPolicyData));
tree.Selection.Changed += delegate {
Gtk.TreeIter it;
tree.Selection.GetSelected (out it);
Gtk.TreeViewColumn ccol;
Gtk.TreePath path;
tree.GetCursor (out path, out ccol);
if (ccol == polCol)
tree.SetCursor (path, ccol, true);
};
Fill ();
UpdateButtons ();
tree.Selection.Changed += delegate {
UpdateButtons ();
};
}
static readonly string parentPolicyText = GettextCatalog.GetString ("(Inherited Policy)");
static readonly string customPolicyText = GettextCatalog.GetString ("(Custom)");
void OnSetPolicyData (Gtk.TreeViewColumn treeColumn, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
{
MimeTypePanelData mt = (MimeTypePanelData) store.GetValue (iter, 0);
string selection;
if (mt.UseParentPolicy)
selection = parentPolicyText;
else {
PolicySet matchingSet = mt.GetMatchingSet ();
if (matchingSet != null)
selection = matchingSet.Name;
else
selection = customPolicyText;
}
CellRendererComboBox comboCell = (CellRendererComboBox) cell;
comboCell.Values = GetComboOptions (mt);
comboCell.Text = selection;
}
string[] GetComboOptions (MimeTypePanelData mt)
{
List<string> values = new List<string> ();
if (!this.panel.PolicyContainer.IsRoot)
values.Add (parentPolicyText);
foreach (PolicySet set in mt.GetSupportedPolicySets ())
values.Add (set.Name);
values.Add (customPolicyText);
return values.ToArray ();
}
void OnPolicySelectionChanged (object s, ComboSelectionChangedArgs args)
{
Gtk.TreeIter iter;
if (store.GetIter (out iter, new Gtk.TreePath (args.Path))) {
MimeTypePanelData mt = (MimeTypePanelData) store.GetValue (iter, 0);
if (args.Active != -1) {
string sel = args.ActiveText;
if (sel == parentPolicyText)
mt.UseParentPolicy = true;
else if (sel != customPolicyText) {
PolicySet pset = PolicyService.GetPolicySet (sel);
mt.AssignPolicies (pset);
}
}
}
}
void Fill ()
{
foreach (MimeTypePanelData mt in panel.GetMimeTypeData ()) {
store.AppendValues (mt, DesktopService.GetPixbufForType (mt.MimeType, Gtk.IconSize.Menu), mt.TypeDescription);
}
}
protected void OnButtonEditClicked (object sender, System.EventArgs e)
{
Gtk.TreeIter iter;
if (tree.Selection.GetSelected (out iter)) {
MimeTypePanelData mt = (MimeTypePanelData) store.GetValue (iter, 0);
dialog.ShowPage (mt.Section);
}
}
protected virtual void OnButtonAddClicked (object sender, System.EventArgs e)
{
AddMimeTypeDialog dlg = new AddMimeTypeDialog (panel.GetItemMimeTypes ());
try {
if (MessageService.RunCustomDialog (dlg, this.Toplevel as Gtk.Window) == (int) Gtk.ResponseType.Ok) {
MimeTypePanelData mt = panel.AddGlobalMimeType (dlg.MimeType);
store.AppendValues (mt, DesktopService.GetPixbufForType (mt.MimeType, Gtk.IconSize.Menu), mt.TypeDescription);
}
} finally {
dlg.Destroy ();
}
}
protected virtual void OnButtonRemoveClicked (object sender, System.EventArgs e)
{
Gtk.TreeIter iter;
if (tree.Selection.GetSelected (out iter)) {
MimeTypePanelData mt = (MimeTypePanelData) store.GetValue (iter, 0);
if (MessageService.Confirm (GettextCatalog.GetString ("Are you sure you want to remove the formatting policy for the type '{0}'?", mt.TypeDescription), AlertButton.Delete)) {
panel.RemoveGlobalMimeType (mt.MimeType);
store.Remove (ref iter);
}
}
}
void UpdateButtons ()
{
Gtk.TreeIter iter;
if (tree.Selection.GetSelected (out iter)) {
MimeTypePanelData mt = (MimeTypePanelData) store.GetValue (iter, 0);
if (panel.IsUserMimeType (mt.MimeType)) {
buttonRemove.Sensitive = buttonEdit.Sensitive = true;
return;
}
}
buttonRemove.Sensitive = buttonEdit.Sensitive = false;
}
}
}
|