Example usage for org.eclipse.jface.resource ColorDescriptor createColor

List of usage examples for org.eclipse.jface.resource ColorDescriptor createColor

Introduction

In this page you can find the example usage for org.eclipse.jface.resource ColorDescriptor createColor.

Prototype

public abstract Color createColor(Device device) throws DeviceResourceException;

Source Link

Document

Returns the Color described by this descriptor.

Usage

From source file:net.enilink.komma.edit.ui.provider.ExtendedColorRegistry.java

License:Open Source License

public Color getColor(Color foregroundColor, Color backgroundColor, Object object) {
    if (object instanceof Color) {
        return (Color) object;
    } else {/*  w  ww  .  j a  v  a2 s .  c  om*/
        Collection<Object> key = new ArrayList<Object>(2);
        key.add(foregroundColor);
        key.add(backgroundColor);
        key.add(object);

        Display display = Display.getCurrent();
        Color result = null;
        Map<Collection<?>, Color> cache = null;
        if (display != null) {
            synchronized (display) {
                cache = colors.get(display);
                if (cache == null) {
                    hookDisplayDispose(display);
                    cache = new HashMap<Collection<?>, Color>();
                    colors.put(display, cache);
                }
                result = cache.get(object);
            }
        }
        if (result == null) {
            if (object instanceof ColorDescriptor) {
                ColorDescriptor colorDescriptor = (ColorDescriptor) object;
                try {
                    result = colorDescriptor.createColor(display);
                } catch (DeviceResourceException exception) {
                    KommaEditUIPlugin.INSTANCE.log(exception);
                }
            } else if (object instanceof URI) {
                URI colorURI = (URI) object;
                if (!"color".equals(colorURI.scheme())) {
                    throw new IllegalArgumentException("Only 'color' scheme is recognized" + colorURI);
                }

                RGB colorData;
                if ("rgb".equals(colorURI.authority())) {
                    int red = Integer.parseInt(colorURI.segment(0));
                    int green = Integer.parseInt(colorURI.segment(1));
                    int blue = Integer.parseInt(colorURI.segment(2));
                    colorData = new RGB(red, green, blue);
                } else if ("hsb".equals(colorURI.authority())) {
                    float[] hsb = new float[3];
                    for (int i = 0; i < 3; ++i) {
                        String segment = colorURI.segment(i);
                        hsb[i] = "".equals(segment) || "foreground".equals(segment)
                                ? foregroundColor.getRGB().getHSB()[i]
                                : "background".equals(segment) ? backgroundColor.getRGB().getHSB()[i]
                                        : Float.parseFloat(segment);
                    }
                    colorData = new RGB(hsb[0], hsb[1], hsb[2]);
                } else {
                    throw new IllegalArgumentException(
                            "Only 'rgb' and 'hsb' authority are recognized" + colorURI);
                }

                try {
                    result = ColorDescriptor.createFrom(colorData).createColor(display);
                } catch (DeviceResourceException exception) {
                    KommaEditUIPlugin.INSTANCE.log(exception);
                }
            }

            if (result != null && display != null) {
                synchronized (display) {
                    cache.put(key, result);
                }
            }
        }
        return result;
    }
}

From source file:org.eclipse.e4mf.edit.ui.provider.ExtendedColorRegistry.java

License:Open Source License

public Color getColor(Color foregroundColor, Color backgroundColor, Object object) {
    if (object instanceof Color) {
        return (Color) object;
    } else {/*w  ww.  jav a 2  s .co m*/
        Collection<Object> key = new ArrayList<Object>(2);
        key.add(foregroundColor);
        key.add(backgroundColor);
        key.add(object);

        Color result = table.get(key);
        if (result == null) {
            if (object instanceof ColorDescriptor) {
                ColorDescriptor colorDescriptor = (ColorDescriptor) object;
                try {
                    result = colorDescriptor.createColor(display);
                } catch (DeviceResourceException exception) {
                    EMFEditUIPlugin.INSTANCE.log(exception);
                }
            } else if (object instanceof URI) {
                URI colorURI = (URI) object;
                if (!"color".equals(colorURI.scheme())) {
                    throw new IllegalArgumentException("Only 'color' scheme is recognized" + colorURI);
                }

                RGB colorData;
                if ("rgb".equals(colorURI.authority())) {
                    int red = Integer.parseInt(colorURI.segment(0));
                    int green = Integer.parseInt(colorURI.segment(1));
                    int blue = Integer.parseInt(colorURI.segment(2));
                    colorData = new RGB(red, green, blue);
                } else if ("hsb".equals(colorURI.authority())) {
                    float[] hsb = new float[3];
                    for (int i = 0; i < 3; ++i) {
                        String segment = colorURI.segment(i);
                        hsb[i] = "".equals(segment) || "foreground".equals(segment)
                                ? foregroundColor.getRGB().getHSB()[i]
                                : "background".equals(segment) ? backgroundColor.getRGB().getHSB()[i]
                                        : Float.parseFloat(segment);
                    }
                    colorData = new RGB(hsb[0], hsb[1], hsb[2]);
                } else {
                    throw new IllegalArgumentException(
                            "Only 'rgb' and 'hsb' authority are recognized" + colorURI);
                }

                try {
                    result = ColorDescriptor.createFrom(colorData).createColor(display);
                } catch (DeviceResourceException exception) {
                    EMFEditUIPlugin.INSTANCE.log(exception);
                }
            }

            if (result != null) {
                table.put(key, result);
            }
        }
        return result;
    }
}

From source file:org.eclipse.emf.ecp.internal.ui.view.emf.ExtendedColorRegistry.java

License:Open Source License

public Color getColor(Color foregroundColor, Color backgroundColor, Object object) {
    if (object instanceof Color) {
        return (Color) object;
    } else {//w w  w .  j  av  a 2 s .c om
        final Collection<Object> key = new ArrayList<Object>(2);
        key.add(foregroundColor);
        key.add(backgroundColor);
        key.add(object);

        Color result = table.get(key);
        if (result == null) {
            if (object instanceof ColorDescriptor) {
                final ColorDescriptor colorDescriptor = (ColorDescriptor) object;
                try {
                    result = colorDescriptor.createColor(display);
                } catch (final DeviceResourceException exception) {
                    Activator.log(exception);
                }
            } else if (object instanceof URI) {
                final URI colorURI = (URI) object;
                if (!"color".equals(colorURI.scheme())) {
                    throw new IllegalArgumentException("Only 'color' scheme is recognized" + colorURI);
                }

                RGB colorData;
                if ("rgb".equals(colorURI.authority())) {
                    final int red = Integer.parseInt(colorURI.segment(0));
                    final int green = Integer.parseInt(colorURI.segment(1));
                    final int blue = Integer.parseInt(colorURI.segment(2));
                    colorData = new RGB(red, green, blue);
                } else if ("hsb".equals(colorURI.authority())) {
                    final float[] hsb = new float[3];
                    for (int i = 0; i < 3; ++i) {
                        final String segment = colorURI.segment(i);
                        hsb[i] = "".equals(segment) || "foreground".equals(segment)
                                ? foregroundColor.getRGB().getHSB()[i]
                                : "background".equals(segment) ? backgroundColor.getRGB().getHSB()[i]
                                        : Float.parseFloat(segment);
                    }
                    colorData = new RGB(hsb[0], hsb[1], hsb[2]);
                } else {
                    throw new IllegalArgumentException(
                            "Only 'rgb' and 'hsb' authority are recognized" + colorURI);
                }

                try {
                    result = ColorDescriptor.createFrom(colorData).createColor(display);
                } catch (final DeviceResourceException exception) {
                    Activator.log(exception);
                }
            }

            if (result != null) {
                table.put(key, result);
            }
        }
        return result;
    }
}

From source file:org.eclipse.ui.tests.themes.JFaceThemeTest.java

License:Open Source License

/**
* Tests to ensure correct behavior of getColorDescriptor methods.
*///from w w  w  .j  av  a2 s  .com
public void testDefaultColorDescriptor() {
    ColorDescriptor desc = getDefaultTheme().getColorRegistry().getColorDescriptor("somegarbage");
    assertNotNull(desc);
    Color color = desc.createColor(getWorkbench().getDisplay());
    assertNotNull(color);
    color.dispose();

    desc = getDefaultTheme().getColorRegistry().getColorDescriptor("somegarbage", null);
    assertNull(desc);

    desc = getDefaultTheme().getColorRegistry().getColorDescriptor("somegarbage",
            ColorDescriptor.createFrom(new RGB(0, 0, 0)));
    assertNotNull(desc);
    color = desc.createColor(getWorkbench().getDisplay());
    assertNotNull(color);
    color.dispose();
}

From source file:org.xmind.ui.properties.PropertyEditingEntry.java

License:Open Source License

private void updateLabels() {
    Object value = source.getPropertyValue(descriptor.getId());
    if (nameLayout != null && !nameLayout.isDisposed()) {
        nameLayout.setText(descriptor.getDisplayName());
        nameLayout.setStyle(new TextStyle(canvas.getFont(), canvas.getForeground(), null), 0,
                nameLayout.getText().length());
    }/*from w w w.  j  a  va2s  .co  m*/
    ILabelDescriptor labelDescriptor = descriptor.getLabelDescriptor();
    ImageDescriptor image = labelDescriptor == null ? null : labelDescriptor.getImage(value);
    if (valueImage != null) {
        valueImage.dispose();
    }
    valueImage = image == null ? null : image.createImage(false, Display.getCurrent());

    ColorDescriptor color = labelDescriptor == null ? null : labelDescriptor.getForeground(value);
    if (valueColor != null) {
        valueColor.dispose();
    }
    valueColor = color == null ? null : color.createColor(Display.getCurrent());

    FontDescriptor font = labelDescriptor == null ? null : labelDescriptor.getFont(value);
    if (valueFont != null) {
        valueFont.dispose();
    }
    valueFont = font == null ? null : font.createFont(Display.getCurrent());

    if (valueLayout != null && !valueLayout.isDisposed()) {
        String valueText = labelDescriptor == null ? (value == null ? "" //$NON-NLS-1$
                : value.toString()) : labelDescriptor.getText(value);
        if (valueText == null)
            valueText = ""; //$NON-NLS-1$
        valueLayout.setText(valueText);
        valueLayout.setStyle(new TextStyle(valueFont == null ? canvas.getFont() : valueFont,
                valueColor == null ? canvas.getForeground() : valueColor, null), 0, valueText.length());
    }
    //        canvas.layout(true);
    canvas.redraw();
}

From source file:org.xmind.ui.viewers.CachedLabelProvider.java

License:Open Source License

public Color getForeground(Object element) {
    if (labelDescriptor == null)
        return getDefaultForeground(element);
    Color color = foregroundColors.get(element);
    if (color == null || color.isDisposed()) {
        ColorDescriptor colorDescriptor = labelDescriptor.getForeground(element);
        color = colorDescriptor == null ? null : colorDescriptor.createColor(Display.getCurrent());
        foregroundColors.put(element, color);
    }//from   w w  w .j  av a 2 s .c o m
    return color;
}

From source file:org.xmind.ui.viewers.CachedLabelProvider.java

License:Open Source License

public Color getBackground(Object element) {
    if (labelDescriptor == null)
        return getDefaultBackground(element);
    Color color = backgroundColors.get(element);
    if (color == null || color.isDisposed()) {
        ColorDescriptor colorDescriptor = labelDescriptor.getBackground(element);
        color = colorDescriptor == null ? null : colorDescriptor.createColor(Display.getCurrent());
        backgroundColors.put(element, color);
    }//w  w w.j a v  a  2  s  .  c  om
    return color;
}