Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NameConventionPanelWidget.cs « NamingInspection « MonoDevelop.CSharp.Refactoring.CodeIssues « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bcdc528d6d0cefdffe9448a14354e9ad66f970ef (plain)
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
// 
// NamingConventionPanelWidget.cs
//  
// Author:
//       Mike Krüger <mkrueger@novell.com>
// 
// Copyright (c) 2011 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 Gtk;
using ICSharpCode.NRefactory.CSharp.Refactoring;
using MonoDevelop.Core;
using MonoDevelop.Ide;
using System.Collections.Generic;

namespace MonoDevelop.CSharp.Refactoring.CodeIssues
{
	[System.ComponentModel.ToolboxItem(true)]
	partial class NameConventionPanelWidget : Gtk.Bin
	{
		TreeStore treeStore = new TreeStore (typeof(NameConventionRule));
		NameConventionPolicy policy;

		internal NameConventionPolicy Policy {
			get {
				return policy;
			}
			set {
				policy = value;
				FillRules (policy.Rules);
			}
		}

		public NameConventionPanelWidget ()
		{
			Build ();	
			Show ();

			var ct1 = new CellRendererText ();
			var col1 = treeviewConventions.AppendColumn (GettextCatalog.GetString ("Rule"), ct1);
			col1.Expand = true;
			col1.SetCellDataFunc (ct1, delegate (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) {
				var rule = (NameConventionRule)model.GetValue (iter, 0);
				ct1.Text = rule.Name;
			});
			
			
			var ct2 = new CellRendererText ();
			var col2 = treeviewConventions.AppendColumn (GettextCatalog.GetString ("Example"), ct2);
			col2.Expand = true;
			col2.SetCellDataFunc (ct2, delegate (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) {
				var rule = (NameConventionRule)model.GetValue (iter, 0);
				ct2.Text = rule.GetPreview ();
			});
			
			treeviewConventions.Model = treeStore;
			treeviewConventions.Selection.Changed += HandleSelectionChanged;
			treeviewConventions.RowActivated += (o, args) => EditSelectedEntry ();
			buttonEdit.Clicked += (o, s) => EditSelectedEntry ();
			buttonRemove.Clicked += (o, s) => RemoveSelectedEntry ();
			buttonAdd.Clicked += (o, s) => AddEntry ();

			HandleSelectionChanged (null, null);
		}

		void HandleSelectionChanged (object sender, EventArgs e)
		{
			TreeIter iter;
			buttonEdit.Sensitive = treeviewConventions.Selection.GetSelected (out iter);
		}

		public void ApplyChanges ()
		{
			var rules = new List<NameConventionRule> ();
			TreeIter iter;
			if (treeStore.GetIterFirst (out iter)) {
				do {
					var rule = (NameConventionRule)treeStore.GetValue (iter, 0);
					rules.Add (rule);
				} while (treeStore.IterNext (ref iter));
			}
			policy.Rules = rules.ToArray ();
			if (IdeApp.Workbench.ActiveDocument != null)
				IdeApp.Workbench.ActiveDocument.UpdateParseDocument ();
		}

		void AddEntry ()
		{
			var newRule = new NameConventionRule ();
			newRule.Name = "New Rule";
			var diag = new NameConventionEditRuleDialog (newRule);
			var result = MessageService.ShowCustomDialog (diag);
			if (result == (int)ResponseType.Ok)
				treeStore.AppendValues (newRule);
			OnPolicyChanged (EventArgs.Empty);
		}

		void EditSelectedEntry ()
		{
			TreeIter iter;
			if (!treeviewConventions.Selection.GetSelected (out iter))
				return;
			var rule = treeStore.GetValue (iter, 0) as NameConventionRule;
			var diag = new NameConventionEditRuleDialog (rule);
			int result = MessageService.ShowCustomDialog (diag);
			treeviewConventions.QueueResize ();
			if (result == (int)Gtk.ResponseType.Ok) 
				OnPolicyChanged (EventArgs.Empty);
		}
		
		void RemoveSelectedEntry ()
		{
			TreeIter iter;
			if (!treeviewConventions.Selection.GetSelected (out iter))
				return;
			treeStore.Remove (ref iter);
			OnPolicyChanged (EventArgs.Empty);
		}

		void FillRules (IEnumerable<NameConventionRule> rules)
		{
			treeStore.Clear ();
			foreach (var rule in rules) {
				treeStore.AppendValues (rule);
			}
		}

		protected virtual void OnPolicyChanged (EventArgs e)
		{
			var handler = PolicyChanged;
			if (handler != null)
				handler (this, e);
		}

		public event EventHandler PolicyChanged;
	}
}