Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Labeling/ILTextRendererManager.cs @ 12448

Last change on this file since 12448 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 10.9 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Reflection;
44using ILNumerics.Drawing.Interfaces;
45using ILNumerics.Exceptions;
46using ILNumerics.Drawing;
47
48
49namespace ILNumerics.Drawing.Labeling {
50    /// <summary>
51    /// Provides access to all (device dependent) ILRenderer available
52    /// </summary>
53    public class ILRendererManager {
54
55        #region members / properties
56       
57        Dictionary<string,string> m_rendererCollection;
58        Dictionary<string,IILTextRenderer> m_rendererCache;
59        GraphicDeviceType m_graphicsDevice;
60        string m_defaultRendererScreen;
61        string m_defaultRendererWorld;
62        ILPanel m_panel;
63        #endregion
64       
65        #region constructor
66        /// <summary>
67        /// create manager instance, device dependent
68        /// </summary>
69        /// <param name="panel">the hosting panel, will give access to this manager instance</param>
70        /// <remarks>While creating ILTextRendereManager instances, the executing assembly will be queried for
71        /// available classes matching the device. Corresponding types are than provided by calling the GetRenderer() method.</remarks>
72        public ILRendererManager (ILPanel panel)
73            : this(panel,new Assembly[] { Assembly.GetExecutingAssembly()} ) {
74        }
75        /// <summary>
76        /// create manager instance, device dependent
77        /// </summary>
78        /// <param name="panel">The hosting panel, giving access to his manager instance</param>
79        /// <param name="assemblies">assemblies to query for matching ILRenderer types</param>
80        /// <remarks>While creating ILRendererManager instances, the given assemblies will be queried for
81        /// available classes matching the device. Corresponding types are than provided by calling the
82        /// GetRenderer() method.</remarks>
83        public ILRendererManager (ILPanel panel, Assembly[] assemblies) {
84            m_graphicsDevice = panel.GraphicDeviceType;
85            m_panel = panel;
86            m_rendererCollection = new Dictionary<string,string>();
87            m_rendererCache = new Dictionary<string,IILTextRenderer>();
88            foreach (Assembly ass in assemblies) {
89                AddAssemblyTypes(ass);
90            }
91            if (m_rendererCollection.Count == 0)
92                throw new InvalidOperationException("No valid ILRenderer found!");
93        }
94        #endregion
95
96        #region public interface
97
98        /// <summary>
99        /// Create a new ILRenderer instance, matching the current graphics device
100        /// </summary>
101        /// <param name="typeName">full class name of the new text renderer</param>
102        /// <param name="assembly">assembly hosting the textrenderer, null for executing assembly</param>
103        /// <returns>newly created ILRenderer instance from assembly</returns>
104        public IILTextRenderer CreateInstance (string typeName, Assembly assembly) {
105            if (typeName == null)
106                throw new ILArgumentException ("The type must be a valid ILTextRenderer!");
107            string key = (assembly==null)
108                ? typeName : assembly.FullName +  "|" + typeName;
109            if (m_rendererCache.ContainsKey(key))
110                return m_rendererCache[key];
111            if (assembly == null)
112                assembly = Assembly.GetExecutingAssembly();
113            Type type = assembly.GetType(typeName);
114            if (type == null  || type.GetInterface("IILTextRenderer") == null)
115                throw new ILArgumentException (String.Format(
116                            "The type '{0}' was not found in assembly {1} or is not a valid IILTextRenderer!"
117                            ,typeName,assembly));
118            queryAddRenderer(type);
119            if (!m_rendererCollection.ContainsKey(key))
120                throw new ILArgumentException("The renderer could not be loaded. Make sure, to specify the correct assembly and the type is properly decorated by the ILRendererAttribute!");
121            IILTextRenderer ret = (IILTextRenderer)type.InvokeMember(
122                          typeName, BindingFlags.DeclaredOnly |
123                          BindingFlags.Public | BindingFlags.NonPublic |
124                          BindingFlags.Instance | BindingFlags.CreateInstance, null, null,
125                          new object[] {m_panel});
126            if (!m_rendererCache.ContainsKey(typeName)) {
127                m_rendererCache[typeName] = ret; 
128            }
129            return ret;
130        }
131        /// <summary>
132        /// Create the default instance of IILTextRenderer for
133        /// the current graphics device and a coord system
134        /// </summary>
135        /// <param name="coords">coord system, the renderer is
136        /// specialized and the default renderer for</param>
137        /// <returns>default IILTextRenderer object</returns>
138        public IILTextRenderer GetDefault(CoordSystem coords) {
139            string key = (coords == CoordSystem.World3D) 
140                 ? m_defaultRendererWorld : m_defaultRendererScreen;
141            if (m_rendererCache.ContainsKey(key) && m_rendererCache[key] != null)
142                return m_rendererCache[key];
143            // must create a new instance
144            IILTextRenderer ret;
145            switch (coords) {
146                case CoordSystem.World3D:
147                    ret = CreateInstance(m_defaultRendererWorld,null);
148                    break;
149                default:
150                    ret = CreateInstance(m_defaultRendererScreen,null);
151                    break;
152            }
153            m_rendererCache[key] = ret;
154            return ret;
155        }
156        /// <summary>
157        /// Get a collection of all IILTextRenderer types and corresponding displayNames available
158        /// </summary>
159        public IDictionary<string,string> RendererNames {
160            get {
161                IDictionary<string,string> ret = new Dictionary<string,string>();
162                foreach (KeyValuePair<string,string> v in m_rendererCollection) {
163                    ret.Add(v);
164                }
165                return ret;
166            }
167        }
168        #endregion
169
170        #region private helper
171        /// <summary>
172        /// iterate over types attribute, select IILTextRenderer attribute,
173        /// register for later reference, add/overwrite default renderers
174        /// </summary>
175        /// <param name="t">IILTextRenderer type</param>
176        private void queryAddRenderer(Type t)
177        {
178            foreach (object att in t.GetCustomAttributes(false)) {
179                if (att is ILRendererAttribute) {
180                    ILRendererAttribute trAttr = (ILRendererAttribute)att;
181                    if (trAttr.GraphicDeviceType == m_graphicsDevice ||
182                        trAttr.GraphicDeviceType == GraphicDeviceType.GDI) {
183                        if (t.GetInterface("IILTextRenderer") == null)
184                            continue;
185                        if (m_rendererCollection.ContainsKey(t.FullName))
186                            m_rendererCollection[t.FullName] = trAttr.Name;
187                        else
188                            m_rendererCollection.Add(t.FullName,trAttr.Name);
189                        if (trAttr.IsDefault) {
190                            if (trAttr.Coords == CoordSystem.Screen)
191                                m_defaultRendererScreen = t.FullName;
192                            else if (trAttr.Coords == CoordSystem.World3D)
193                                m_defaultRendererWorld = t.FullName;
194                        }
195                        break;
196                    }
197                }
198            }
199        }
200
201        private void AddAssemblyTypes (Assembly assembly) {
202            System.Diagnostics.Trace.Write("Entering ILTextRendererManager.AddAssemblyTypes...");
203            m_rendererCollection.Add("ILNumerics.Drawing.Platform.OpenGL.ILOGLRenderer", "OpenGL Renderer");
204            m_defaultRendererScreen = "ILNumerics.Drawing.Platform.OpenGL.ILOGLRenderer";
205            m_defaultRendererWorld = "ILNumerics.Drawing.Platform.OpenGL.ILOGLRenderer";
206            try {
207                Type[] types = assembly.GetTypes();
208                foreach (Type t in types) {
209                    queryAddRenderer(t);
210                }
211                System.Diagnostics.Trace.WriteLine("success. " + m_rendererCollection.Count.ToString() + " renderer found.");
212                System.Diagnostics.Trace.Indent();
213                foreach (var i in m_rendererCollection) {
214                    System.Diagnostics.Trace.WriteLine(i.Key.ToString() + " -- " + i.Value);
215                }
216                System.Diagnostics.Trace.Unindent();
217            } catch (Exception exc) {
218                System.Diagnostics.Trace.WriteLine("failure. Exception information:");
219                System.Diagnostics.Trace.WriteLine(exc.ToString());
220            }
221        }
222
223        #endregion
224    }
225}
Note: See TracBrowser for help on using the repository browser.