예제 #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);
  }
  /** 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);
  }
 /**
  * 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;
 }
예제 #4
0
파일: UIUtils.java 프로젝트: ralic/dbeaver
 @Nullable
 public static <T> T findView(IWorkbenchWindow workbenchWindow, Class<T> viewClass) {
   IViewReference[] references = workbenchWindow.getActivePage().getViewReferences();
   for (IViewReference ref : references) {
     IViewPart view = ref.getView(false);
     if (view != null && viewClass.isAssignableFrom(view.getClass())) {
       return viewClass.cast(view);
     }
   }
   return null;
 }
 /**
  * Returns an {@link Image} stored in the file at the specified path relative to the specified
  * class.
  *
  * @param clazz the {@link Class} relative to which to find the image
  * @param path the path to the image file, if starts with <code>'/'</code>
  * @return the {@link Image} stored in the file at the specified path
  */
 public static Image getImage(Class<?> clazz, String path) {
   String key = clazz.getName() + '|' + path;
   Image image = m_imageMap.get(key);
   if (image == null) {
     try {
       image = getImage(clazz.getResourceAsStream(path));
       m_imageMap.put(key, image);
     } catch (Exception e) {
       image = getMissingImage();
       m_imageMap.put(key, image);
     }
   }
   return image;
 }
예제 #6
0
 private void updateCursor(final String selection) {
   Cursor cursor = null;
   Class swtClass = SWT.class;
   if (selection != null) {
     try {
       Field field = swtClass.getField(selection);
       int cursorStyle = field.getInt(swtClass);
       cursor = Display.getCurrent().getSystemCursor(cursorStyle);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   Iterator iter = controls.iterator();
   while (iter.hasNext()) {
     Control control = (Control) iter.next();
     control.setCursor(cursor);
   }
 }
예제 #7
0
파일: UIUtils.java 프로젝트: ralic/dbeaver
 public static CellEditor createCellEditor(
     Composite parent, Object object, DBPPropertyDescriptor property) {
   // List
   if (property instanceof IPropertyValueListProvider) {
     final IPropertyValueListProvider listProvider = (IPropertyValueListProvider) property;
     final Object[] items = listProvider.getPossibleValues(object);
     if (!ArrayUtils.isEmpty(items)) {
       final String[] strings = new String[items.length];
       for (int i = 0, itemsLength = items.length; i < itemsLength; i++) {
         strings[i] =
             items[i] instanceof DBPNamedObject
                 ? ((DBPNamedObject) items[i]).getName()
                 : CommonUtils.toString(items[i]);
       }
       final CustomComboBoxCellEditor editor =
           new CustomComboBoxCellEditor(
               parent,
               strings,
               SWT.DROP_DOWN | (listProvider.allowCustomValue() ? SWT.NONE : SWT.READ_ONLY));
       return editor;
     }
   }
   Class<?> propertyType = property.getDataType();
   if (propertyType == null || CharSequence.class.isAssignableFrom(propertyType)) {
     return new CustomTextCellEditor(parent);
   } else if (BeanUtils.isNumericType(propertyType)) {
     return new CustomNumberCellEditor(parent, propertyType);
   } else if (BeanUtils.isBooleanType(propertyType)) {
     return new CustomCheckboxCellEditor(parent);
     // return new CheckboxCellEditor(parent);
   } else if (propertyType.isEnum()) {
     final Object[] enumConstants = propertyType.getEnumConstants();
     final String[] strings = new String[enumConstants.length];
     for (int i = 0, itemsLength = enumConstants.length; i < itemsLength; i++) {
       strings[i] = ((Enum) enumConstants[i]).name();
     }
     return new CustomComboBoxCellEditor(parent, strings, SWT.DROP_DOWN | SWT.READ_ONLY);
   } else {
     log.warn("Unsupported property type: " + propertyType.getName());
     return null;
   }
 }
예제 #8
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;
 }
예제 #9
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;
 }