public void openUrl(String url) { String os = System.getProperty("os.name"); Runtime runtime = Runtime.getRuntime(); try { // Block for Windows Platform if (os.startsWith("Windows")) { String cmd = "rundll32 url.dll,FileProtocolHandler " + url; Process p = runtime.exec(cmd); // Block for Mac OS } else if (os.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class}); openURL.invoke(null, new Object[] {url}); // Block for UNIX Platform } else { String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"}; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) if (runtime.exec(new String[] {"which", browsers[count]}).waitFor() == 0) browser = browsers[count]; if (browser == null) throw new Exception("Could not find web browser"); else runtime.exec(new String[] {browser, url}); } } catch (Exception x) { System.err.println("Exception occurd while invoking Browser!"); x.printStackTrace(); } }
/** * Write the device independent image array stored in the specified loader to the specified output * stream using the specified file format. */ public static void save(OutputStream os, int format, ImageLoader loader) { if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); if (FORMATS[format] == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); if (loader.data == null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); LEDataOutputStream stream = new LEDataOutputStream(os); FileFormat fileFormat = null; try { Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX); fileFormat = (FileFormat) clazz.newInstance(); } catch (Exception e) { SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); } if (format == SWT.IMAGE_BMP_RLE) { switch (loader.data[0].depth) { case 8: fileFormat.compression = 1; break; case 4: fileFormat.compression = 2; break; } } fileFormat.unloadIntoStream(loader, stream); }
/** Loads the resources */ void initResources() { final Class<ControlExample> clazz = ControlExample.class; if (resourceBundle != null) { try { if (images == null) { images = new Image[imageLocations.length]; for (int i = 0; i < imageLocations.length; ++i) { InputStream sourceStream = clazz.getResourceAsStream(imageLocations[i]); ImageData source = new ImageData(sourceStream); if (imageTypes[i] == SWT.ICON) { ImageData mask = source.getTransparencyMask(); images[i] = new Image(null, source, mask); } else { images[i] = new Image(null, source); } try { sourceStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return; } catch (Throwable t) { } } String error = (resourceBundle != null) ? getResourceString("error.CouldNotLoadResources") : "Unable to load resources"; //$NON-NLS-1$ freeResources(); throw new RuntimeException(error); }
static synchronized void initializeSwing() { if (swingInitialized) return; swingInitialized = true; try { /* Initialize the default focus traversal policy */ Class[] emptyClass = new Class[0]; Object[] emptyObject = new Object[0]; Class clazz = Class.forName("javax.swing.UIManager"); Method method = clazz.getMethod("getDefaults", emptyClass); if (method != null) method.invoke(clazz, emptyObject); } catch (Throwable e) { } }
static synchronized void initializeSwing() { if (swingInitialized) return; swingInitialized = true; /* * Feature in GTK. The default X error handler * for GTK calls exit() after printing the X error. * Normally, this isn't that big a problem for SWT * applications because they don't cause X errors. * However, sometimes X errors are generated by AWT * that make SWT exit. The fix is to hide all X * errors when AWT is running. */ OS.gdk_error_trap_push(); try { /* Initialize the default focus traversal policy */ Class[] emptyClass = new Class[0]; Object[] emptyObject = new Object[0]; Class clazz = Class.forName("javax.swing.UIManager"); Method method = clazz.getMethod("getDefaults", emptyClass); if (method != null) method.invoke(clazz, emptyObject); } catch (Throwable e) { } }
/** * Creates a new <code>java.awt.Frame</code>. This frame is the root for the AWT components that * will be embedded within the composite. In order for the embedding to succeed, the composite * must have been created with the SWT.EMBEDDED style. * * <p>IMPORTANT: As of JDK1.5, the embedded frame does not receive mouse events. When a * lightweight component is added as a child of the embedded frame, the cursor does not change. In * order to work around both these problems, it is strongly recommended that a heavyweight * component such as <code>java.awt.Panel</code> be added to the frame as the root of all * components. * * @param parent the parent <code>Composite</code> of the new <code>java.awt.Frame</code> * @return a <code>java.awt.Frame</code> to be the parent of the embedded AWT components * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null * <li>ERROR_INVALID_ARGUMENT - if the parent Composite does not have the SWT.EMBEDDED style * </ul> * * @since 3.0 */ public static Frame new_Frame(final Composite parent) { if (parent == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if ((parent.getStyle() & SWT.EMBEDDED) == 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } final int /*long*/ handle = parent.view.id; Class clazz = null; try { String className = embeddedFrameClass != null ? embeddedFrameClass : "apple.awt.CEmbeddedFrame"; if (embeddedFrameClass == null) { clazz = Class.forName(className, true, ClassLoader.getSystemClassLoader()); } else { clazz = Class.forName(className); } } catch (ClassNotFoundException cne) { SWT.error(SWT.ERROR_NOT_IMPLEMENTED, cne); } catch (Throwable e) { SWT.error(SWT.ERROR_UNSPECIFIED, e, " [Error while starting AWT]"); } initializeSwing(); Object value = null; Constructor constructor = null; try { constructor = clazz.getConstructor(new Class[] {long.class}); value = constructor.newInstance(new Object[] {new Long(handle)}); } catch (Throwable e) { SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e); } final Frame frame = (Frame) value; frame.addNotify(); parent.setData(EMBEDDED_FRAME_KEY, frame); /* Forward the iconify and deiconify events */ final Listener shellListener = new Listener() { public void handleEvent(Event e) { switch (e.type) { case SWT.Deiconify: EventQueue.invokeLater( new Runnable() { public void run() { frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_DEICONIFIED)); } }); break; case SWT.Iconify: EventQueue.invokeLater( new Runnable() { public void run() { frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED)); } }); break; } } }; Shell shell = parent.getShell(); shell.addListener(SWT.Deiconify, shellListener); shell.addListener(SWT.Iconify, shellListener); /* * Generate the appropriate events to activate and deactivate * the embedded frame. This is needed in order to make keyboard * focus work properly for lightweights. */ Listener listener = new Listener() { public void handleEvent(Event e) { switch (e.type) { case SWT.Dispose: Shell shell = parent.getShell(); shell.removeListener(SWT.Deiconify, shellListener); shell.removeListener(SWT.Iconify, shellListener); parent.setVisible(false); EventQueue.invokeLater( new Runnable() { public void run() { try { frame.dispose(); } catch (Throwable e) { } } }); break; case SWT.FocusIn: EventQueue.invokeLater( new Runnable() { public void run() { if (frame.isActive()) return; try { Class clazz = frame.getClass(); Method method = clazz.getMethod( "synthesizeWindowActivation", new Class[] {boolean.class}); if (method != null) method.invoke(frame, new Object[] {new Boolean(true)}); } catch (Throwable e) { e.printStackTrace(); } } }); break; case SWT.Deactivate: EventQueue.invokeLater( new Runnable() { public void run() { if (!frame.isActive()) return; try { Class clazz = frame.getClass(); Method method = clazz.getMethod( "synthesizeWindowActivation", new Class[] {boolean.class}); if (method != null) method.invoke(frame, new Object[] {new Boolean(false)}); } catch (Throwable e) { e.printStackTrace(); } } }); break; } } }; parent.addListener(SWT.FocusIn, listener); parent.addListener(SWT.Deactivate, listener); parent.addListener(SWT.Dispose, listener); parent .getDisplay() .asyncExec( new Runnable() { public void run() { if (parent.isDisposed()) return; final Rectangle clientArea = parent.getClientArea(); EventQueue.invokeLater( new Runnable() { public void run() { frame.setSize(clientArea.width, clientArea.height); frame.validate(); // Bug in Cocoa AWT? For some reason the frame isn't showing up on first // draw. // Toggling visibility seems to be the only thing that works. frame.setVisible(false); frame.setVisible(true); } }); } }); return frame; }
/** * Creates a new <code>java.awt.Frame</code>. This frame is the root for the AWT components that * will be embedded within the composite. In order for the embedding to succeed, the composite * must have been created with the SWT.EMBEDDED style. * * <p>IMPORTANT: As of JDK1.5, the embedded frame does not receive mouse events. When a * lightweight component is added as a child of the embedded frame, the cursor does not change. In * order to work around both these problems, it is strongly recommended that a heavyweight * component such as <code>java.awt.Panel</code> be added to the frame as the root of all * components. * * @param parent the parent <code>Composite</code> of the new <code>java.awt.Frame</code> * @return a <code>java.awt.Frame</code> to be the parent of the embedded AWT components * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null * <li>ERROR_INVALID_ARGUMENT - if the parent Composite does not have the SWT.EMBEDDED style * </ul> * * @since 3.0 */ public static Frame new_Frame(final Composite parent) { if (parent == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if ((parent.getStyle() & SWT.EMBEDDED) == 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } int /*long*/ handle = parent.embeddedHandle; /* * Some JREs have implemented the embedded frame constructor to take an integer * and other JREs take a long. To handle this binary incompatibility, use * reflection to create the embedded frame. */ Class clazz = null; try { String className = embeddedFrameClass != null ? embeddedFrameClass : "sun.awt.X11.XEmbeddedFrame"; clazz = Class.forName(className); } catch (Throwable e) { SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e, " [need JDK 1.5 or greater]"); } initializeSwing(); Object value = null; Constructor constructor = null; try { constructor = clazz.getConstructor(new Class[] {int.class, boolean.class}); value = constructor.newInstance(new Object[] {new Integer((int) /*64*/ handle), Boolean.TRUE}); } catch (Throwable e1) { try { constructor = clazz.getConstructor(new Class[] {long.class, boolean.class}); value = constructor.newInstance(new Object[] {new Long(handle), Boolean.TRUE}); } catch (Throwable e2) { SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e2); } } final Frame frame = (Frame) value; parent.setData(EMBEDDED_FRAME_KEY, frame); if (Device.DEBUG) { loadLibrary(); setDebug(frame, true); } try { /* Call registerListeners() to make XEmbed focus traversal work */ Method method = clazz.getMethod("registerListeners", null); if (method != null) method.invoke(value, null); } catch (Throwable e) { } final AWTEventListener awtListener = new AWTEventListener() { public void eventDispatched(AWTEvent event) { if (event.getID() == WindowEvent.WINDOW_OPENED) { final Window window = (Window) event.getSource(); if (window.getParent() == frame) { parent .getDisplay() .asyncExec( new Runnable() { public void run() { if (parent.isDisposed()) return; Shell shell = parent.getShell(); loadLibrary(); int /*long*/ awtHandle = getAWTHandle(window); if (awtHandle == 0) return; int /*long*/ xWindow = OS.gdk_x11_drawable_get_xid( OS.GTK_WIDGET_WINDOW(OS.gtk_widget_get_toplevel(shell.handle))); OS.XSetTransientForHint( OS.gdk_x11_display_get_xdisplay(OS.gdk_display_get_default()), awtHandle, xWindow); } }); } } } }; frame.getToolkit().addAWTEventListener(awtListener, AWTEvent.WINDOW_EVENT_MASK); final Listener shellListener = new Listener() { public void handleEvent(Event e) { switch (e.type) { case SWT.Deiconify: EventQueue.invokeLater( new Runnable() { public void run() { frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_DEICONIFIED)); } }); break; case SWT.Iconify: EventQueue.invokeLater( new Runnable() { public void run() { frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED)); } }); break; } } }; Shell shell = parent.getShell(); shell.addListener(SWT.Deiconify, shellListener); shell.addListener(SWT.Iconify, shellListener); Listener listener = new Listener() { public void handleEvent(Event e) { switch (e.type) { case SWT.Dispose: Shell shell = parent.getShell(); shell.removeListener(SWT.Deiconify, shellListener); shell.removeListener(SWT.Iconify, shellListener); parent.setVisible(false); EventQueue.invokeLater( new Runnable() { public void run() { frame.getToolkit().removeAWTEventListener(awtListener); frame.dispose(); } }); break; case SWT.Resize: if (Library.JAVA_VERSION >= Library.JAVA_VERSION(1, 6, 0)) { final Rectangle clientArea = parent.getClientArea(); EventQueue.invokeLater( new Runnable() { public void run() { frame.setSize(clientArea.width, clientArea.height); } }); } break; } } }; parent.addListener(SWT.Dispose, listener); parent.addListener(SWT.Resize, listener); parent .getDisplay() .asyncExec( new Runnable() { public void run() { if (parent.isDisposed()) return; final Rectangle clientArea = parent.getClientArea(); EventQueue.invokeLater( new Runnable() { public void run() { frame.setSize(clientArea.width, clientArea.height); frame.validate(); } }); } }); return frame; }
static FileFormat getFileFormat(LEDataInputStream stream, String format) throws Exception { Class clazz = Class.forName(FORMAT_PACKAGE + '.' + format + FORMAT_SUFFIX); FileFormat fileFormat = (FileFormat) clazz.newInstance(); if (fileFormat.isFileFormat(stream)) return fileFormat; return null; }
static { try { Class.forName("org.eclipse.swt.widgets.Display"); // $NON-NLS-1$ } catch (Throwable e) { } }