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

PropertyGrid.cs « AspNetEdit.Editor.UI « AspNetEdit « Extras - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad6664810c4130c7ee4ce182c1631fcacc010d26 (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
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
 /* 
 * PropertyGrid.cs - PropertyGrid wrapper. Hooks into ISelectionService.
 * 
 * Authors: 
 *  Michael Hutchinson <m.j.hutchinson@gmail.com>
 *  
 * Copyright (C) 2005 Michael Hutchinson
 *
 * This sourcecode is licenced under The MIT License:
 * 
 * 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.ComponentModel.Design;
using Gtk;
using System.ComponentModel;
using System.Collections;
using MonoDevelop.DesignerSupport.PropertyGrid;

namespace AspNetEdit.Editor.UI
{
	public class PropertyGrid : Gtk.VBox
	{
		private ServiceContainer parentServices = null;
		private ISelectionService selectionService = null;
		private IExtenderListService extenderListService = null;
		private IComponentChangeService changeService = null;
		private ITypeDescriptorFilterService typeDescriptorFilterService = null;
		private MonoDevelop.DesignerSupport.PropertyGrid.PropertyGrid grid;
		private ListStore components;
		private ComboBox combo;
		
		private bool suppressChange = false;

		public PropertyGrid (ServiceContainer parentServices)
		{
			this.parentServices = parentServices;
			
			grid = new MonoDevelop.DesignerSupport.PropertyGrid.PropertyGrid ();
			this.PackEnd (grid, true, true, 0);


			components = new ListStore (typeof (string), typeof (IComponent));
			combo = new ComboBox (components);

			CellRenderer rdr = new CellRendererText ();
			combo.PackStart (rdr, true);
			combo.AddAttribute (rdr, "text", 0);

			this.PackStart (combo, false, false, 3);
			
			//for selecting nothing, i.e. deselect all
			components.AppendValues (new object[] { "", null} );

			combo.Changed += new EventHandler (combo_Changed);

			InitialiseServices();
		}

		void combo_Changed (object sender, EventArgs e)
		{
			if (suppressChange) return;
			TreeIter t;
			combo.GetActiveIter(out t);
			IComponent comp = (IComponent) components.GetValue(t, 1);

			//Tell everybody about the new selection. We'll hear about this too.
			selectionService.SetSelectedComponents ((comp == null)? null : new IComponent[] { comp });
		}
		
		// We need these services to be present, but we cache references for efficiency
		// Whenever new designer host loaded etc, must reinitialise the services
		public void InitialiseServices ()
		{
			//unregister old event handlers
			if (selectionService != null)
				selectionService.SelectionChanged -= new EventHandler(selectionService_SelectionChanged);

			//update references
			extenderListService = parentServices.GetService (typeof (IExtenderListService)) as IExtenderListService;
			selectionService = parentServices.GetService (typeof (ISelectionService)) as ISelectionService;
			changeService = parentServices.GetService (typeof (IComponentChangeService)) as IComponentChangeService;
			typeDescriptorFilterService = parentServices.GetService (typeof (ITypeDescriptorFilterService)) as ITypeDescriptorFilterService;

			//register event handlers
			if (selectionService != null)
				selectionService.SelectionChanged += new EventHandler (selectionService_SelectionChanged);
			if (changeService != null) {
				changeService.ComponentAdded += new ComponentEventHandler (changeService_ComponentAdded);
				changeService.ComponentRemoved += new ComponentEventHandler (changeService_ComponentRemoved);
				changeService.ComponentRename += new ComponentRenameEventHandler (changeService_ComponentRename);
				changeService.ComponentChanged += new ComponentChangedEventHandler (changeService_updateValues);
				/*TODO: should we also monitor these?
				changeService.ComponentAdding
				changeService.ComponentChanging
				changeService.ComponentRemoving
				*/
			}

			//get existing components for combo list
			IDesignerHost host = parentServices.GetService (typeof (IDesignerHost)) as IDesignerHost;
			if (host != null)
				foreach (IComponent comp in host.Container.Components)
					changeService_ComponentAdded(host.Container, new ComponentEventArgs (comp));
		}
		
		void changeService_updateValues (object sender, ComponentChangedEventArgs e)
		{
			grid.Refresh ();
		}

		
		void changeService_ComponentRename (object sender, ComponentRenameEventArgs e)
		{
			//We just need to rename the right component in the combobox list
			TreeIter t; 
			components.GetIterFirst (out t);

			do {
				if ((IComponent)components.GetValue (t, 1) == e.Component && (string) components.GetValue(t, 0) == e.OldName) {
					components.SetValue (t, 0, e.NewName);
					return;
				}
			} while (components.IterNext (ref t));
		}

		void changeService_ComponentRemoved (object sender, ComponentEventArgs e)
		{
			//remove component from combobox list
			//need a variable external to foreach so we can pass by ref
			TreeIter iter;
			components.GetIterFirst (out iter);

			do
			{
				if ((IComponent) components.GetValue (iter, 1) == e.Component)
				{
					components.Remove (ref iter);
					break;
				}
			}
			while (components.IterNext (ref iter));
		}

		void changeService_ComponentAdded (object sender, ComponentEventArgs e)
		{
			//simply add to the combobox list
			components.AppendValues (new object[] { e.Component.Site.Name, e.Component} );
		}

		private void selectionService_SelectionChanged (object sender, EventArgs e)
		{
			//stop combo change event from changing selection again!
			suppressChange = true;
			grid.CurrentObject = selectionService.PrimarySelection;
			
			TreeIter iter;
			components.GetIterFirst (out iter);
			
			do
			{
				if ((IComponent) components.GetValue (iter, 1) == selectionService.PrimarySelection)
				{
					combo.SetActiveIter (iter);
					break;
				}
			}
			while (components.IterNext (ref iter));		
			suppressChange = false;	
		}
	}
}