Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.ui / src / net / sourceforge / phpdt / monitor / ui / internal / MonitorUIPlugin.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.ui.internal;
12
13 import java.net.URL;
14 import java.text.MessageFormat;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.resource.ImageRegistry;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.ui.plugin.AbstractUIPlugin;
23 import org.osgi.framework.BundleContext;
24 /**
25  * The TCP/IP monitor UI plugin.
26  */
27 public class MonitorUIPlugin extends AbstractUIPlugin {
28         private static MonitorUIPlugin singleton;
29
30         protected Map imageDescriptors = new HashMap();
31
32         private static URL ICON_BASE_URL;
33         
34         private static final String URL_CLCL = "clcl16/";
35         private static final String URL_ELCL = "elcl16/";
36         private static final String URL_DLCL = "dlcl16/";
37         private static final String URL_OBJ = "obj16/";
38
39         public static final String PLUGIN_ID = "net.sourceforge.phpeclipse.monitor.ui";
40
41         public static final String IMG_ELCL_SORT_RESPONSE_TIME = "IMG_ELCL_SORT_RESPONSE_TIME";
42         public static final String IMG_ELCL_CLEAR = "IMG_ELCL_CLEAR";
43         public static final String IMG_ELCL_HTTP_HEADER = "IMG_ELCL_HTTP_HEADER";
44         public static final String IMG_CLCL_SORT_RESPONSE_TIME = "IMG_CLCL_SORT_RESPONSE_TIME";
45         public static final String IMG_CLCL_CLEAR = "IMG_CLCL_CLEAR";
46         public static final String IMG_CLCL_HTTP_HEADER = "IMG_CLCL_HTTP_HEADER";
47         public static final String IMG_DLCL_SORT_RESPONSE_TIME = "IMG_DLCL_SORT_RESPONSE_TIME";
48         public static final String IMG_DLCL_CLEAR = "IMG_DLCL_CLEAR";
49         public static final String IMG_DLCL_HTTP_HEADER = "IMG_DLCL_HTTP_HEADER";
50                 
51         public static final String IMG_REQUEST_RESPONSE = "requestResponse";
52         public static final String IMG_HOST = "host";
53         public static final String IMG_MONITOR_ON = "monitorOn";
54         public static final String IMG_MONITOR_OFF = "monitorOff";
55         
56         private static final String SHOW_VIEW_ON_ACTIVITY = "show-view";
57         private static final String SHOW_HEADER = "show-header";
58
59         /**
60          * MonitorUIPlugin constructor comment.
61          */
62         public MonitorUIPlugin() {
63                 super();
64                 singleton = this;
65         }
66         
67         /**
68          * Creates and pre-loads the image registry.
69          *
70          * @return ImageRegistry
71          */
72         protected ImageRegistry createImageRegistry() {
73                 ImageRegistry registry = super.createImageRegistry();
74         
75                 registerImage(registry, IMG_REQUEST_RESPONSE, URL_OBJ + "tcp.gif");
76                 registerImage(registry, IMG_HOST, URL_OBJ + "host.gif");
77                 registerImage(registry, IMG_MONITOR_ON, URL_OBJ + "monitorOn.gif");
78                 registerImage(registry, IMG_MONITOR_OFF, URL_OBJ + "monitorOff.gif");
79         
80                 registerImage(registry, IMG_CLCL_CLEAR, URL_CLCL + "clear.gif");
81                 registerImage(registry, IMG_CLCL_SORT_RESPONSE_TIME, URL_CLCL + "sortResponseTime.gif");
82                 registerImage(registry, IMG_CLCL_HTTP_HEADER, URL_CLCL + "httpHeader.gif");
83                 
84                 registerImage(registry, IMG_ELCL_CLEAR, URL_ELCL + "clear.gif");
85                 registerImage(registry, IMG_ELCL_SORT_RESPONSE_TIME, URL_ELCL + "sortResponseTime.gif");
86                 registerImage(registry, IMG_ELCL_HTTP_HEADER, URL_ELCL + "httpHeader.gif");
87                 
88                 registerImage(registry, IMG_DLCL_CLEAR, URL_DLCL + "clear.gif");
89                 registerImage(registry, IMG_DLCL_SORT_RESPONSE_TIME, URL_DLCL + "sortResponseTime.gif");
90                 registerImage(registry, IMG_DLCL_HTTP_HEADER, URL_DLCL + "httpHeader.gif");
91                 
92                 return registry;
93         }
94
95         /**
96          * Return the image with the given key from the image registry.
97          *
98          * @param key java.lang.String
99          * @return org.eclipse.jface.parts.IImage
100          */
101         public static Image getImage(String key) {
102                 return getInstance().getImageRegistry().get(key);
103         }
104
105         /**
106          * Return the image with the given key from the image registry.
107          *
108          * @param key java.lang.String
109          * @return org.eclipse.jface.parts.IImage
110          */
111         public static ImageDescriptor getImageDescriptor(String key) {
112                 try {
113                         getInstance().getImageRegistry();
114                         return (ImageDescriptor) getInstance().imageDescriptors.get(key);
115                 } catch (Exception e) {
116                         return null;
117                 }
118         }
119
120         /**
121          * Returns the singleton instance of this plugin.
122          *
123          * @return org.eclipse.tcpip.monitor.MonitorServerPlugin
124          */
125         public static MonitorUIPlugin getInstance() {
126                 return singleton;
127         }
128
129         /**
130          * Returns the translated String found with the given key.
131          *
132          * @return java.lang.String
133          * @param key java.lang.String
134          */
135         public static String getResource(String key) {
136                 try {
137                         return Platform.getResourceString(getInstance().getBundle(), key);
138                 } catch (Exception e) {
139                         return key;
140                 }
141         }
142
143         /**
144          * Returns the translated String found with the given key,
145          * and formatted with the given object.
146          *
147          * @param key java.lang.String
148          * @param obj java.lang.Object[]
149          * @return java.lang.String
150          */
151         public static String getResource(String key, Object[] obj) {
152                 try {
153                         return MessageFormat.format(getResource(key), obj);
154                 } catch (Exception e) {
155                         return key;
156                 }
157         }
158
159         /**
160          * Returns the translated String found with the given key,
161          * and formatted with the given object.
162          *
163          * @param key java.lang.String
164          * @param s java.lang.String
165          * @return java.lang.String
166          */
167         public static String getResource(String key, String s) {
168                 try {
169                         return MessageFormat.format(getResource(key), new String[] {s});
170                 } catch (Exception e) {
171                         return key;
172                 }
173         }
174
175         /**
176          * Register an image with the registry.
177          *
178          * @param key java.lang.String
179          * @param partialURL java.lang.String
180          */
181         private void registerImage(ImageRegistry registry, String key, String partialURL) {
182                 if (ICON_BASE_URL == null) {
183                         String pathSuffix = "icons/";
184                         ICON_BASE_URL = singleton.getBundle().getEntry(pathSuffix);
185                 }
186                 
187                 try {
188                         ImageDescriptor id = ImageDescriptor.createFromURL(new URL(ICON_BASE_URL, partialURL));
189                         registry.put(key, id);
190                         imageDescriptors.put(key, id);
191                 } catch (Exception e) {
192                         Trace.trace(Trace.SEVERE, "Error registering image", e);
193                 }
194         }
195
196         /**
197          * Start this plug-in.
198          */
199         public void start(BundleContext context) throws Exception {
200                 super.start(context);
201                 
202                 getPreferenceStore().setDefault(MonitorUIPlugin.SHOW_VIEW_ON_ACTIVITY, true);
203         }
204         
205         public static boolean getDefaultShowOnActivityPreference() {
206                 return getInstance().getPreferenceStore().getDefaultBoolean(SHOW_VIEW_ON_ACTIVITY);
207         }
208         
209         public static boolean getShowOnActivityPreference() {
210                 return getInstance().getPreferenceStore().getBoolean(SHOW_VIEW_ON_ACTIVITY);
211         }
212         
213         public static void setShowOnActivityPreference(boolean b) {
214                 getInstance().getPreferenceStore().setValue(SHOW_VIEW_ON_ACTIVITY, b);
215                 getInstance().savePluginPreferences();
216         }
217         
218         public static boolean getShowHeaderPreference() {
219                 return getInstance().getPreferenceStore().getBoolean(SHOW_HEADER);
220         }
221         
222         public static void setShowHeaderPreference(boolean b) {
223                 getInstance().getPreferenceStore().setValue(SHOW_HEADER, b);
224                 getInstance().savePluginPreferences();
225         }
226 }