1 11 package org.eclipse.ui.internal.intro.impl.util; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 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 36 public static void handleException(String msg, Exception e, 37 Object [] variables) { 38 if (msg == null) 39 return; 40 if (variables != null) { 41 msg = NLS.bind(msg, variables); 43 } 44 Log.error(msg, e); 45 } 46 47 51 public static void handleExceptionWithPopUp(Shell parent, String msg, 52 Exception e) { 53 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 if (msg == null) 67 msg = e.getMessage(); 68 if (parent == null) 69 parent = DialogUtil.getActiveShell(); 70 DialogUtil.displayErrorMessage(parent, msg, e); 71 } 72 73 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()); break; 88 case SWT.Dispose: 89 System.out.println("Dispose EVENT: " + e.toString()); break; 91 case SWT.Paint: 92 System.out.println("Paint EVENT: " + e.toString()); break; 94 case SWT.Resize: 95 System.out.println("Resize EVENT: " + e.toString()); break; 97 case SWT.MouseDoubleClick: 98 System.out.println("MouseDoubleClick EVENT: " + e.toString()); 100 break; 101 case SWT.MouseDown: 102 System.out.println("MouseDown EVENT: " + e.toString()); break; 104 case SWT.MouseUp: 105 System.out.println("MouseUp EVENT: " + e.toString()); break; 107 case SWT.MouseMove: 108 System.out.println("MouseMove EVENT: " + e.toString()); break; 110 case SWT.MouseEnter: 111 System.out.println("MouseEnter EVENT: " + e.toString()); break; 113 case SWT.MouseExit: 114 System.out.println("MouseExit EVENT: " + e.toString()); break; 116 case SWT.MouseHover: 117 System.out.println("MouseHover EVENT: " + e.toString()); break; 119 case SWT.FocusIn: 120 System.out.println("FocusIn EVENT: " + e.toString()); break; 122 case SWT.FocusOut: 123 System.out.println("FocusOut EVENT: " + e.toString()); break; 125 case SWT.KeyDown: 126 System.out.println("KeyDown EVENT: " + e.toString()); break; 128 case SWT.KeyUp: 129 System.out.println("KeyUp EVENT: " + e.toString()); break; 131 case SWT.Traverse: 132 System.out.println("Traverse EVENT: " + e.toString()); break; 134 case SWT.Show: 135 System.out.println("Show EVENT: " + e.toString()); break; 137 case SWT.Hide: 138 System.out.println("Hide EVENT: " + e.toString()); 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.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 e) { 161 } 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 179 public static boolean openBrowser(String href) { 180 try { 181 URL url = new URL (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); return false; 189 } catch (MalformedURLException e) { 190 Log.error("Intro failed to display: " + href, e); return false; 192 } 193 } 194 195 public static void logPerformanceTime(String message, long startTime) { 196 long endTime = System.currentTimeMillis(); 197 Log.forcedInfo("Intro Performance - " + message + (endTime - startTime) + "ms"); } 200 201 public static void logPerformanceMessage(String message, long time) { 202 Log.forcedInfo("Intro Performance - " + message + " " + time + "ms"); } 204 205 } 206 | Popular Tags |