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