svn path=/branches/mono-1-1-9/mcs/; revision=50438
[mono.git] / mcs / class / System.Drawing / System.Drawing / SystemIcons.jvm.cs
1 //
2 // System.Drawing.SystemIcons.cs
3 //
4 // Authors:
5 //  Vladimir Krasnov (vladimirk@mainsoft.com)
6 //
7 // Copyright (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.IO;
33 using System.Configuration;
34 using System.Collections;
35 using System.Collections.Specialized;
36
37 namespace System.Drawing
38 {
39         public sealed class SystemIcons
40         {
41                 private static readonly string SYSTEM_ICONS = "IconsResource";
42
43                 private SystemIcons()
44                 {
45                 }
46
47                 public static Icon Application { get { return LoadIcon("Application");} }
48                 public static Icon Asterisk { get { return LoadIcon("Asterisk");} }
49                 public static Icon Error { get { return LoadIcon("Error");} }
50                 public static Icon Exclamation { get { return LoadIcon("Exclamation");} }
51                 public static Icon Hand { get { return LoadIcon("Hand");} }
52                 public static Icon Information { get { return LoadIcon("Information");} }
53                 public static Icon Question { get { return LoadIcon("Question");} }
54                 public static Icon Warning { get { return LoadIcon("Warning");} }
55                 public static Icon WinLogo { get { return LoadIcon("WinLogo");} }
56
57                 private static Icon LoadIcon (string iconName)
58                 {
59                         Hashtable systemIcons = (Hashtable)AppDomain.CurrentDomain.GetData("SYSTEM_ICONS");
60
61                         if (systemIcons == null)
62                         {
63                                 systemIcons = LoadSystemIconsFromResource();
64                                 AppDomain.CurrentDomain.SetData(SYSTEM_ICONS, systemIcons);
65                         }
66                 
67                         return (Icon)systemIcons[iconName];
68                 }
69
70                 private static Hashtable LoadSystemIconsFromResource()
71                 {
72                         string [] iconNames = {
73                                                                         "Application", "Asterisk", "Error", 
74                                                                         "Exclamation", "Hand", "Information", 
75                                                                         "Question", "Warning", "WinLogo"
76                                                                   };
77
78                         NameValueCollection config = (NameValueCollection) ConfigurationSettings.GetConfig ("system.drawing/icons");
79                         Hashtable icons = new Hashtable(9);
80
81                         for (int i = 0; i < iconNames.Length; i++)
82                                 icons.Add(iconNames[i], LoadIconFromResource( config[ iconNames[i] ] ));
83
84                         return icons;
85                 }
86
87                 private static Icon LoadIconFromResource(string iconName)
88                 {
89                         Stream s;
90                         try
91                         {
92                                 java.lang.ClassLoader cl = (java.lang.ClassLoader)
93                                         AppDomain.CurrentDomain.GetData("GH_ContextClassLoader");
94                                 if (cl == null)
95                                         return null;
96                                 java.io.InputStream inputStream = cl.getResourceAsStream(iconName);
97                                 s = (Stream)vmw.common.IOUtils.getStream(inputStream);
98                         }
99                         catch (Exception e)
100                         {
101                                 return null;
102                         }
103                         return new Icon(new Bitmap(s));
104                 }
105         }
106 }