aboutsummaryrefslogtreecommitdiffstatsci
path: root/CrowIDE/src/MembersView.cs
blob: f0c3a13a584f5dbab8fd9c4746851c8127d3c92d (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
//
//  MembersView.cs
//
//  Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
//  Copyright (c) 2016 jp
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using Crow;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Cairo;

namespace Crow.Coding
{	
	public class MembersView : ListBox
	{		
		object instance;
		ImlProjectItem projFile;

		public MembersView () : base() {}

		//cache property containers per type
		Dictionary<string,PropertyContainer[]> propContainersCache = new Dictionary<string, PropertyContainer[]>();

		[XmlAttributeAttribute][DefaultValue(null)]
		public virtual object Instance {
			get { return instance; }
			set {
				if (instance == value)
					return;
				object lastInst = instance;

				instance = value;
				NotifyValueChanged ("Instance", instance);

				if (Instance is GraphicObject) {
					NotifyValueChanged ("SelectedItemName", Instance.GetType().Name + (Instance as GraphicObject).design_id
						+ ":" + (Instance as GraphicObject).design_imlPath );
				}else
					NotifyValueChanged ("SelectedItemName", "");

				if (instance == null) { 
					Data = null;
					return;
				}	

				Type it = instance.GetType ();
				if (!propContainersCache.ContainsKey (it.FullName)) {
					MemberInfo[] members = it.GetMembers (BindingFlags.Public | BindingFlags.Instance);
					List<PropertyContainer> props = new List<PropertyContainer> ();
					foreach (MemberInfo m in members) {
						if (m.MemberType == MemberTypes.Property) {
							PropertyInfo pi = m as PropertyInfo;
							if (!pi.CanWrite)
								continue;
							if (pi.GetCustomAttribute (typeof(XmlIgnoreAttribute)) != null)
								continue;
							props.Add (new PropertyContainer (this, pi));
						}
					}
					propContainersCache.Add (it.FullName, props.OrderBy (p => p.Name).ToArray ());
				}

				Data = propContainersCache [it.FullName];

				if (lastInst != instance) {
					foreach (PropertyContainer pc in propContainersCache [it.FullName]) {
						pc.NotifyValueChanged ("Value", pc.Value);
					}
				}
			}
		}
		public ImlProjectItem ProjectNode {
			get { return projFile; }
			set {
				if (projFile == value)
					return;
				
//				if (projFile != null)
//					projFile.UnregisterEditor (this);
				
				projFile = value;

//				if (projFile != null)
//					projFile.RegisterEditor (this);

				NotifyValueChanged ("ProjectNode", projFile);
			}
		}

		public void updateSource () {
			if (projFile == null)
				return;
			projFile.UpdateSource (this, (Instance as GraphicObject).GetIML ());
		}

//		public override void Paint (ref Context ctx)
//		{
//			base.Paint (ref ctx);
//
//			if (SelectedIndex < 0)
//				return;
//
//			Rectangle r =  Parent.ContextCoordinates(Items [SelectedIndex].Slot);
//			ctx.SetSourceRGB (0, 0, 1);
//			ctx.Rectangle (r);
//			ctx.LineWidth = 2;
//			ctx.Stroke ();
//		}

	}
}