da01781022c731590fb606862c152fc2888f2fa1
[public/netxms.git] / src / java / netxms-eclipse / NetworkMaps / src / org / netxms / ui / eclipse / networkmaps / MapImageProvidersManager.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.networkmaps;
20
21 import java.util.Map;
22 import java.util.TreeMap;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExtensionRegistry;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.swt.graphics.Image;
28 import org.netxms.client.objects.AbstractObject;
29 import org.netxms.ui.eclipse.networkmaps.api.NetworkMapImageProvider;
30
31 /**
32 * Manager of network map image providers. There should be only one instance of it,
33 * created early during console startup.
34 */
35 public class MapImageProvidersManager
36 {
37 private static MapImageProvidersManager instance = null;
38
39 /**
40 * Get color manager instance.
41 *
42 * @return
43 */
44 public static MapImageProvidersManager getInstance()
45 {
46 return instance;
47 }
48
49 /**
50 * Create color manager instance
51 */
52 protected static void create()
53 {
54 if (instance == null)
55 instance = new MapImageProvidersManager();
56 }
57
58 private Map<Integer, NetworkMapImageProvider> providers = new TreeMap<Integer, NetworkMapImageProvider>();
59
60 /**
61 * Constructor
62 */
63 private MapImageProvidersManager()
64 {
65 // Read all registered extensions and create image providers
66 final IExtensionRegistry reg = Platform.getExtensionRegistry();
67 IConfigurationElement[] elements = reg.getConfigurationElementsFor("org.netxms.ui.eclipse.networkmaps.imageproviders"); //$NON-NLS-1$
68 for(int i = 0; i < elements.length; i++)
69 {
70 try
71 {
72 final NetworkMapImageProvider p = (NetworkMapImageProvider)elements[i].createExecutableExtension("class"); //$NON-NLS-1$
73 int priority = 65535;
74 String value = elements[i].getAttribute("priority"); //$NON-NLS-1$
75 if (value != null)
76 {
77 try
78 {
79 priority = Integer.parseInt(value);
80 if (priority < 0)
81 priority = 65535;
82 }
83 catch(NumberFormatException e)
84 {
85 }
86 }
87 providers.put(priority, p);
88 }
89 catch(CoreException e)
90 {
91 // TODO Auto-generated catch block
92 e.printStackTrace();
93 }
94 }
95 }
96
97 /**
98 * Get image for object.
99 *
100 * @return image for given object or null.
101 */
102 public Image getMapImage(AbstractObject object)
103 {
104 for(NetworkMapImageProvider p : providers.values())
105 {
106 Image i = p.getMapImage(object);
107 if (i != null)
108 return i;
109 }
110 return null;
111 }
112 }