예제 #1
0
  /**
   * 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);
  }
 /**
  * Returns a {@link Font} based on its name, height and style. Windows-specific strikeout and
  * underline flags are also supported.
  *
  * @param name the name of the font
  * @param size the size of the font
  * @param style the style of the font
  * @param strikeout the strikeout flag (warning: Windows only)
  * @param underline the underline flag (warning: Windows only)
  * @return {@link Font} The font matching the name, height, style, strikeout and underline
  */
 public static Font getFont(
     String name, int size, int style, boolean strikeout, boolean underline) {
   String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline;
   Font font = m_fontMap.get(fontName);
   if (font == null) {
     FontData fontData = new FontData(name, size, style);
     if (strikeout || underline) {
       try {
         Class<?> logFontClass =
             Class.forName("org.eclipse.swt.internal.win32.LOGFONT"); // $NON-NLS-1$
         Object logFont = FontData.class.getField("data").get(fontData); // $NON-NLS-1$
         if (logFont != null && logFontClass != null) {
           if (strikeout) {
             logFontClass
                 .getField("lfStrikeOut")
                 .set(logFont, Byte.valueOf((byte) 1)); // $NON-NLS-1$
           }
           if (underline) {
             logFontClass
                 .getField("lfUnderline")
                 .set(logFont, Byte.valueOf((byte) 1)); // $NON-NLS-1$
           }
         }
       } catch (Throwable e) {
         System.err.println(
             "Unable to set underline or strikeout"
                 + " (probably on a non-Windows platform). "
                 + e); //$NON-NLS-1$ //$NON-NLS-2$
       }
     }
     font = new Font(Display.getCurrent(), fontData);
     m_fontMap.put(fontName, font);
   }
   return font;
 }
예제 #3
0
 /**
  * Retuns an URL based on a plugin and file path
  *
  * @param plugin Object The plugin containing the file path
  * @param name String The file path
  * @return URL The URL representing the file at the specified path
  * @throws Exception
  */
 @SuppressWarnings("unchecked") // $NON-NLS-1$
 private static URL getPluginImageURL(Object plugin, String name) throws Exception {
   // try to work with 'plugin' as with OSGI BundleContext
   try {
     Class bundleClass = Class.forName("org.osgi.framework.Bundle"); // $NON-NLS-1$
     Class bundleContextClass = Class.forName("org.osgi.framework.BundleContext"); // $NON-NLS-1$
     if (bundleContextClass.isAssignableFrom(plugin.getClass())) {
       Method getBundleMethod =
           bundleContextClass.getMethod("getBundle", new Class[] {}); // $NON-NLS-1$
       Object bundle = getBundleMethod.invoke(plugin, new Object[] {});
       //
       Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath"); // $NON-NLS-1$
       Class pathClass = Class.forName("org.eclipse.core.runtime.Path"); // $NON-NLS-1$
       Constructor pathConstructor = pathClass.getConstructor(new Class[] {String.class});
       Object path = pathConstructor.newInstance(new Object[] {name});
       //
       Class platformClass = Class.forName("org.eclipse.core.runtime.Platform"); // $NON-NLS-1$
       Method findMethod =
           platformClass.getMethod("find", new Class[] {bundleClass, ipathClass}); // $NON-NLS-1$
       return (URL) findMethod.invoke(null, new Object[] {bundle, path});
     }
   } catch (Throwable e) {
     // Ignore any exceptions
   }
   // else work with 'plugin' as with usual Eclipse plugin
   {
     Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); // $NON-NLS-1$
     if (pluginClass.isAssignableFrom(plugin.getClass())) {
       //
       Class ipathClass = Class.forName("org.eclipse.core.runtime.IPath"); // $NON-NLS-1$
       Class pathClass = Class.forName("org.eclipse.core.runtime.Path"); // $NON-NLS-1$
       Constructor pathConstructor = pathClass.getConstructor(new Class[] {String.class});
       Object path = pathConstructor.newInstance(new Object[] {name});
       //
       Method findMethod = pluginClass.getMethod("find", new Class[] {ipathClass}); // $NON-NLS-1$
       return (URL) findMethod.invoke(plugin, new Object[] {path});
     }
   }
   return null;
 }
예제 #4
0
 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;
 }