Initial implementation of the new Debug Plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / XDebugUIPlugin.java
1 package net.sourceforge.phpeclipse.xdebug.ui;
2
3 import java.util.MissingResourceException;
4 import java.util.ResourceBundle;
5
6 import org.eclipse.swt.widgets.Display;
7 import org.eclipse.swt.widgets.Shell;
8 import org.eclipse.ui.plugin.*;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.Status;
12 import org.eclipse.jface.dialogs.ErrorDialog;
13 import org.eclipse.jface.resource.ImageDescriptor;
14 import org.osgi.framework.BundleContext;
15
16 /**
17  * The main plugin class to be used in the desktop.
18  */
19 public class XDebugUIPlugin extends AbstractUIPlugin {
20
21         private static final String BUNDLE_NAME = "net.sourceforge.phpeclipse.xdebug.ui.XDebugUIMessages"; //$NON-NLS-1$
22
23         private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
24                         .getBundle(BUNDLE_NAME);
25         
26         private static final String PLUGIN_ID ="net.sourceforge.phpeclipse.xdebug.ui"; 
27
28         
29         //The shared instance.
30         private static XDebugUIPlugin plugin;
31         
32         /**
33          * The constructor.
34          */
35         public XDebugUIPlugin() {
36                 plugin = this;
37         }
38
39         /**
40          * This method is called upon plug-in activation
41          */
42         public void start(BundleContext context) throws Exception {
43                 super.start(context);
44         }
45
46         /**
47          * This method is called when the plug-in is stopped
48          */
49         public void stop(BundleContext context) throws Exception {
50                 super.stop(context);
51                 plugin = null;
52         }
53
54         /**
55          * Returns the shared instance.
56          */
57         public static XDebugUIPlugin getDefault() {
58                 return plugin;
59         }
60
61         /**
62          * Returns an image descriptor for the image file at the given
63          * plug-in relative path.
64          *
65          * @param path the path
66          * @return the image descriptor
67          */
68         public static ImageDescriptor getImageDescriptor(String path) {
69                 return AbstractUIPlugin.imageDescriptorFromPlugin("net.sourceforge.phpeclipse.xdebug.ui", path);
70         }
71         
72         /**
73          * Convenience method which returns the unique identifier of this plugin.
74          */
75         public static String getUniqueIdentifier() {
76                 return PLUGIN_ID;
77         }
78         
79         /**
80          * Utility method with conventions
81          */
82         public static void errorDialog(Shell shell, String title, String message, IStatus s) {
83                 // if the 'message' resource string and the IStatus' message are the same,
84                 // don't show both in the dialog
85                 if (s != null && message.equals(s.getMessage())) {
86                         message= null;
87                 }
88                 ErrorDialog.openError(shell, title, message, s);
89         }
90                 
91
92         /**
93          * Utility method with conventions
94          */
95         public static void errorDialog(Shell shell, String title, String message, Throwable t) {
96                 IStatus status;
97                 if (t instanceof CoreException) {
98                         status= ((CoreException)t).getStatus();
99                         // if the 'message' resource string and the IStatus' message are the same,
100                         // don't show both in the dialog
101                         if (status != null && message.equals(status.getMessage())) {
102                                 message= null;
103                         }
104                 } else {
105                         status= new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, "Error within Debug UI: ", t); //$NON-NLS-1$
106                         log(status);    
107                 }
108                 ErrorDialog.openError(shell, title, message, status);
109         }
110
111         /**
112          * Logs the specified status with this plug-in's log.
113          * 
114          * @param status status to log
115          */
116         public static void log(IStatus status) {
117                 getDefault().getLog().log(status);
118         }
119         
120         /**
121          * Logs an internal error with the specified throwable
122          * 
123          * @param e the exception to be logged
124          */     
125         public static void log(Throwable e) {
126                 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, "Internal Error", e));  
127         }
128         
129         /**
130          * Returns the standard display to be used. The method first checks, if
131          * the thread calling this method has an associated display. If so, this
132          * display is returned. Otherwise the method returns the default display.
133          */
134         public static Display getStandardDisplay() {
135                 Display display;
136                 display= Display.getCurrent();
137                 if (display == null)
138                         display= Display.getDefault();
139                 return display;         
140         }
141
142         public static String getString(String key) {
143                 try {
144                         return RESOURCE_BUNDLE.getString(key);
145                 } catch (MissingResourceException e) {
146                         return '!' + key + '!';
147                 }
148         }
149         
150 }