001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.impl.util;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.osgi.util.NLS;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Display;
020: import org.eclipse.swt.widgets.Event;
021: import org.eclipse.swt.widgets.Listener;
022: import org.eclipse.swt.widgets.Shell;
023: import org.eclipse.ui.PartInitException;
024: import org.eclipse.ui.PlatformUI;
025: import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
026:
027: public class Util {
028:
029: /**
030: * Handle the exception by logging to the Log. <br>
031: * Variables are subsituted in the message.
032: */
033: public static void handleException(String msg, Exception e,
034: Object[] variables) {
035: if (msg == null)
036: return;
037: if (variables != null) {
038: // if variables is not null, errorId will never be null.
039: msg = NLS.bind(msg, variables);
040: }
041: Log.error(msg, e);
042: }
043:
044: /**
045: * Handle the exception by displaying an Error Dialog. <br>
046: * Also, the error is logged by the Log.
047: */
048: public static void handleExceptionWithPopUp(Shell parent,
049: String msg, Exception e) {
050: // if it is a core exception, use ErrorDialog. If the error id is null
051: // this translates to giving null to this dialog which is handled by
052: // Eclipse by displaying the detyailed message directly.
053: if (e instanceof CoreException) {
054: if (parent == null)
055: parent = DialogUtil.getActiveShell();
056: DialogUtil.displayCoreErrorDialog(parent, msg,
057: (CoreException) e);
058:
059: return;
060: }
061:
062: // any other exception, use MessageDialog.
063: // if errorID is null, use error message.
064: if (msg == null)
065: msg = e.getMessage();
066: if (parent == null)
067: parent = DialogUtil.getActiveShell();
068: DialogUtil.displayErrorMessage(parent, msg, e);
069: }
070:
071: /**
072: * Utility method that will add a debug listener to the given control. All
073: * common events are added.
074: *
075: * @param control
076: * @return
077: */
078: public static Listener addDebugListener(Control control) {
079: Listener listener = new Listener() {
080:
081: public void handleEvent(Event e) {
082: switch (e.type) {
083: case SWT.Selection:
084: System.out
085: .println("Selection EVENT: " + e.toString()); //$NON-NLS-1$
086: break;
087: case SWT.Dispose:
088: System.out
089: .println("Dispose EVENT: " + e.toString()); //$NON-NLS-1$
090: break;
091: case SWT.Paint:
092: System.out.println("Paint EVENT: " + e.toString()); //$NON-NLS-1$
093: break;
094: case SWT.Resize:
095: System.out.println("Resize EVENT: " + e.toString()); //$NON-NLS-1$
096: break;
097: case SWT.MouseDoubleClick:
098: System.out.println("MouseDoubleClick EVENT: " //$NON-NLS-1$
099: + e.toString());
100: break;
101: case SWT.MouseDown:
102: System.out
103: .println("MouseDown EVENT: " + e.toString()); //$NON-NLS-1$
104: break;
105: case SWT.MouseUp:
106: System.out
107: .println("MouseUp EVENT: " + e.toString()); //$NON-NLS-1$
108: break;
109: case SWT.MouseMove:
110: System.out
111: .println("MouseMove EVENT: " + e.toString()); //$NON-NLS-1$
112: break;
113: case SWT.MouseEnter:
114: System.out
115: .println("MouseEnter EVENT: " + e.toString()); //$NON-NLS-1$
116: break;
117: case SWT.MouseExit:
118: System.out
119: .println("MouseExit EVENT: " + e.toString()); //$NON-NLS-1$
120: break;
121: case SWT.MouseHover:
122: System.out
123: .println("MouseHover EVENT: " + e.toString()); //$NON-NLS-1$
124: break;
125: case SWT.FocusIn:
126: System.out
127: .println("FocusIn EVENT: " + e.toString()); //$NON-NLS-1$
128: break;
129: case SWT.FocusOut:
130: System.out
131: .println("FocusOut EVENT: " + e.toString()); //$NON-NLS-1$
132: break;
133: case SWT.KeyDown:
134: System.out
135: .println("KeyDown EVENT: " + e.toString()); //$NON-NLS-1$
136: break;
137: case SWT.KeyUp:
138: System.out.println("KeyUp EVENT: " + e.toString()); //$NON-NLS-1$
139: break;
140: case SWT.Traverse:
141: System.out
142: .println("Traverse EVENT: " + e.toString()); //$NON-NLS-1$
143: break;
144: case SWT.Show:
145: System.out.println("Show EVENT: " + e.toString()); //$NON-NLS-1$
146: break;
147: case SWT.Hide:
148: System.out.println("Hide EVENT: " + e.toString()); //$NON-NLS-1$
149: break;
150: default:
151: System.out.println(e.toString());
152: }
153: }
154: };
155: int[] allEvents = new int[] { SWT.Selection, SWT.Dispose,
156: SWT.Paint, SWT.Resize, SWT.MouseDoubleClick,
157: SWT.MouseDown,
158: SWT.MouseUp,
159: // SWT.MouseMove,
160: SWT.MouseEnter, SWT.MouseExit, SWT.MouseHover,
161: SWT.FocusIn, SWT.FocusOut, SWT.KeyDown, SWT.KeyUp,
162: SWT.Traverse, SWT.Show, SWT.Hide };
163: for (int i = 0; i < allEvents.length; i++) {
164: control.addListener(allEvents[i], listener);
165: }
166: return listener;
167: }
168:
169: public static void sleep(int delay) {
170: try {
171: Thread.sleep(delay);
172: } catch (InterruptedException e) {
173: // no-op
174: }
175: }
176:
177: public static void highlight(Control control, int color) {
178: control.setBackground(control.getDisplay()
179: .getSystemColor(color));
180: }
181:
182: public static void highlightFocusControl() {
183: Control control = Display.getCurrent().getFocusControl();
184: if (control != null)
185: control.setBackground(Display.getCurrent().getSystemColor(
186: SWT.COLOR_DARK_RED));
187: }
188:
189: /**
190: * Launch an external brwoser on the given url.
191: */
192: public static boolean openBrowser(String href) {
193: try {
194: URL url = new URL(href);
195: IWorkbenchBrowserSupport support = PlatformUI
196: .getWorkbench().getBrowserSupport();
197: support.getExternalBrowser().openURL(url);
198: return true;
199: } catch (PartInitException e) {
200: Log.error("Intro failed to get Browser support.", e); //$NON-NLS-1$
201: return false;
202: } catch (MalformedURLException e) {
203: Log.error("Intro failed to display: " + href, e); //$NON-NLS-1$
204: return false;
205: }
206: }
207:
208: public static void logPerformanceTime(String message, long startTime) {
209: long endTime = System.currentTimeMillis();
210: Log
211: .forcedInfo("Intro Performance - " + message + (endTime - startTime) //$NON-NLS-1$
212: + "ms"); //$NON-NLS-1$
213: }
214:
215: public static void logPerformanceMessage(String message, long time) {
216: Log
217: .forcedInfo("Intro Performance - " + message + " " + time + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
218: }
219:
220: }
|