KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > util > Util


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.util;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
27
28 public class Util {
29
30
31
32     /**
33      * Handle the exception by logging to the Log. <br>
34      * Variables are subsituted in the message.
35      */

36     public static void handleException(String JavaDoc msg, Exception JavaDoc e,
37             Object JavaDoc[] variables) {
38         if (msg == null)
39             return;
40         if (variables != null) {
41             // if variables is not null, errorId will never be null.
42
msg = NLS.bind(msg, variables);
43         }
44         Log.error(msg, e);
45     }
46
47     /**
48      * Handle the exception by displaying an Error Dialog. <br>
49      * Also, the error is logged by the Log.
50      */

51     public static void handleExceptionWithPopUp(Shell parent, String JavaDoc msg,
52             Exception JavaDoc e) {
53         // if it is a core exception, use ErrorDialog. If the error id is null
54
// this translates to giving null to this dialog which is handled by
55
// Eclipse by displaying the detyailed message directly.
56
if (e instanceof CoreException) {
57             if (parent == null)
58                 parent = DialogUtil.getActiveShell();
59             DialogUtil.displayCoreErrorDialog(parent, msg, (CoreException) e);
60
61             return;
62         }
63
64         // any other exception, use MessageDialog.
65
// if errorID is null, use error message.
66
if (msg == null)
67             msg = e.getMessage();
68         if (parent == null)
69             parent = DialogUtil.getActiveShell();
70         DialogUtil.displayErrorMessage(parent, msg, e);
71     }
72
73     /**
74      * Utility method that will add a debug listener to the given control. All
75      * common events are added.
76      *
77      * @param control
78      * @return
79      */

80     public static Listener addDebugListener(Control control) {
81         Listener listener = new Listener() {
82
83             public void handleEvent(Event e) {
84                 switch (e.type) {
85                 case SWT.Selection:
86                     System.out.println("Selection EVENT: " + e.toString()); //$NON-NLS-1$
87
break;
88                 case SWT.Dispose:
89                     System.out.println("Dispose EVENT: " + e.toString()); //$NON-NLS-1$
90
break;
91                 case SWT.Paint:
92                     System.out.println("Paint EVENT: " + e.toString()); //$NON-NLS-1$
93
break;
94                 case SWT.Resize:
95                     System.out.println("Resize EVENT: " + e.toString()); //$NON-NLS-1$
96
break;
97                 case SWT.MouseDoubleClick:
98                     System.out.println("MouseDoubleClick EVENT: " //$NON-NLS-1$
99
+ e.toString());
100                     break;
101                 case SWT.MouseDown:
102                     System.out.println("MouseDown EVENT: " + e.toString()); //$NON-NLS-1$
103
break;
104                 case SWT.MouseUp:
105                     System.out.println("MouseUp EVENT: " + e.toString()); //$NON-NLS-1$
106
break;
107                 case SWT.MouseMove:
108                     System.out.println("MouseMove EVENT: " + e.toString()); //$NON-NLS-1$
109
break;
110                 case SWT.MouseEnter:
111                     System.out.println("MouseEnter EVENT: " + e.toString()); //$NON-NLS-1$
112
break;
113                 case SWT.MouseExit:
114                     System.out.println("MouseExit EVENT: " + e.toString()); //$NON-NLS-1$
115
break;
116                 case SWT.MouseHover:
117                     System.out.println("MouseHover EVENT: " + e.toString()); //$NON-NLS-1$
118
break;
119                 case SWT.FocusIn:
120                     System.out.println("FocusIn EVENT: " + e.toString()); //$NON-NLS-1$
121
break;
122                 case SWT.FocusOut:
123                     System.out.println("FocusOut EVENT: " + e.toString()); //$NON-NLS-1$
124
break;
125                 case SWT.KeyDown:
126                     System.out.println("KeyDown EVENT: " + e.toString()); //$NON-NLS-1$
127
break;
128                 case SWT.KeyUp:
129                     System.out.println("KeyUp EVENT: " + e.toString()); //$NON-NLS-1$
130
break;
131                 case SWT.Traverse:
132                     System.out.println("Traverse EVENT: " + e.toString()); //$NON-NLS-1$
133
break;
134                 case SWT.Show:
135                     System.out.println("Show EVENT: " + e.toString()); //$NON-NLS-1$
136
break;
137                 case SWT.Hide:
138                     System.out.println("Hide EVENT: " + e.toString()); //$NON-NLS-1$
139
break;
140                 default:
141                     System.out.println(e.toString());
142                 }
143             }
144         };
145         int[] allEvents = new int[] { SWT.Selection, SWT.Dispose, SWT.Paint,
146                 SWT.Resize, SWT.MouseDoubleClick, SWT.MouseDown, SWT.MouseUp,
147                 // SWT.MouseMove,
148
SWT.MouseEnter, SWT.MouseExit, SWT.MouseHover, SWT.FocusIn,
149                 SWT.FocusOut, SWT.KeyDown, SWT.KeyUp, SWT.Traverse, SWT.Show,
150                 SWT.Hide };
151         for (int i = 0; i < allEvents.length; i++) {
152             control.addListener(allEvents[i], listener);
153         }
154         return listener;
155     }
156
157     public static void sleep(int delay) {
158         try {
159             Thread.sleep(delay);
160         } catch (InterruptedException JavaDoc e) {
161             // no-op
162
}
163     }
164
165     public static void highlight(Control control, int color) {
166         control.setBackground(control.getDisplay().getSystemColor(color));
167     }
168
169     public static void highlightFocusControl() {
170         Control control = Display.getCurrent().getFocusControl();
171         if (control != null)
172             control.setBackground(Display.getCurrent().getSystemColor(
173                 SWT.COLOR_DARK_RED));
174     }
175
176     /**
177      * Launch an external brwoser on the given url.
178      */

179     public static boolean openBrowser(String JavaDoc href) {
180         try {
181             URL JavaDoc url = new URL JavaDoc(href);
182             IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
183                 .getBrowserSupport();
184             support.getExternalBrowser().openURL(url);
185             return true;
186         } catch (PartInitException e) {
187             Log.error("Intro failed to get Browser support.", e); //$NON-NLS-1$
188
return false;
189         } catch (MalformedURLException JavaDoc e) {
190             Log.error("Intro failed to display: " + href, e); //$NON-NLS-1$
191
return false;
192         }
193     }
194
195     public static void logPerformanceTime(String JavaDoc message, long startTime) {
196         long endTime = System.currentTimeMillis();
197         Log.forcedInfo("Intro Performance - " + message + (endTime - startTime) //$NON-NLS-1$
198
+ "ms"); //$NON-NLS-1$
199
}
200
201     public static void logPerformanceMessage(String JavaDoc message, long time) {
202         Log.forcedInfo("Intro Performance - " + message + " " + time + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
203
}
204
205 }
206
Popular Tags