Example usage for org.eclipse.swt.graphics Color dispose

List of usage examples for org.eclipse.swt.graphics Color dispose

Introduction

In this page you can find the example usage for org.eclipse.swt.graphics Color dispose.

Prototype

public void dispose() 

Source Link

Usage

From source file:CustomControlExample.java

/**
 * Creates the "Colors" group.//from  ww  w  .j  a v  a2s. c o m
 */
void createColorGroup() {
    super.createColorGroup();

    itemGroup = new Group(colorGroup, SWT.NONE);
    itemGroup.setText(ControlExample.getResourceString("Table_Item_Colors"));
    GridData data = new GridData();
    data.horizontalSpan = 2;
    itemGroup.setLayoutData(data);
    itemGroup.setLayout(new GridLayout(2, false));
    new Label(itemGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Color"));
    itemForegroundButton = new Button(itemGroup, SWT.PUSH);
    new Label(itemGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Color"));
    itemBackgroundButton = new Button(itemGroup, SWT.PUSH);
    itemFontButton = new Button(itemGroup, SWT.PUSH);
    itemFontButton.setText(ControlExample.getResourceString("Font"));
    itemFontButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    Shell shell = colorGroup.getShell();
    final ColorDialog foregroundDialog = new ColorDialog(shell);
    final ColorDialog backgroundDialog = new ColorDialog(shell);
    final FontDialog fontDialog = new FontDialog(shell);

    int imageSize = 12;
    Display display = shell.getDisplay();
    itemForegroundImage = new Image(display, imageSize, imageSize);
    itemBackgroundImage = new Image(display, imageSize, imageSize);

    /* Add listeners to set the colors and font */
    itemForegroundButton.setImage(itemForegroundImage);
    itemForegroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = itemForegroundColor;
            if (oldColor == null)
                oldColor = table1.getItem(0).getForeground();
            foregroundDialog.setRGB(oldColor.getRGB());
            RGB rgb = foregroundDialog.open();
            if (rgb == null)
                return;
            oldColor = itemForegroundColor;
            itemForegroundColor = new Color(event.display, rgb);
            setItemForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    itemBackgroundButton.setImage(itemBackgroundImage);
    itemBackgroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = itemBackgroundColor;
            if (oldColor == null)
                oldColor = table1.getItem(0).getBackground();
            backgroundDialog.setRGB(oldColor.getRGB());
            RGB rgb = backgroundDialog.open();
            if (rgb == null)
                return;
            oldColor = itemBackgroundColor;
            itemBackgroundColor = new Color(event.display, rgb);
            setItemBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    itemFontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = itemFont;
            if (oldFont == null)
                oldFont = table1.getItem(0).getFont();
            fontDialog.setFontList(oldFont.getFontData());
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = itemFont;
            itemFont = new Font(event.display, fontData);
            setItemFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent event) {
            if (itemBackgroundImage != null)
                itemBackgroundImage.dispose();
            if (itemForegroundImage != null)
                itemForegroundImage.dispose();
            if (itemBackgroundColor != null)
                itemBackgroundColor.dispose();
            if (itemForegroundColor != null)
                itemForegroundColor.dispose();
            if (itemFont != null)
                itemFont.dispose();
            itemBackgroundColor = null;
            itemForegroundColor = null;
            itemFont = null;
        }
    });
}

From source file:CustomControlExample.java

/**
 * Creates the "Colors" group. This is typically a child of the "Control"
 * group. Subclasses override this method to customize and set system
 * colors.// w w w.ja v a2s .  c  o m
 */
void createColorGroup() {
    /* Create the group */
    colorGroup = new Group(controlGroup, SWT.NONE);
    colorGroup.setLayout(new GridLayout(2, false));
    colorGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    colorGroup.setText(ControlExample.getResourceString("Colors"));
    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Color"));
    foregroundButton = new Button(colorGroup, SWT.PUSH);
    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Color"));
    backgroundButton = new Button(colorGroup, SWT.PUSH);
    fontButton = new Button(colorGroup, SWT.PUSH);
    fontButton.setText(ControlExample.getResourceString("Font"));
    fontButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    Button defaultsButton = new Button(colorGroup, SWT.PUSH);
    defaultsButton.setText(ControlExample.getResourceString("Defaults"));

    Shell shell = controlGroup.getShell();
    final ColorDialog foregroundDialog = new ColorDialog(shell);
    final ColorDialog backgroundDialog = new ColorDialog(shell);
    final FontDialog fontDialog = new FontDialog(shell);

    /* Create images to display current colors */
    int imageSize = 12;
    Display display = shell.getDisplay();
    foregroundImage = new Image(display, imageSize, imageSize);
    backgroundImage = new Image(display, imageSize, imageSize);

    /* Add listeners to set the colors and font */
    foregroundButton.setImage(foregroundImage); // sets the size of the
    // button
    foregroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = foregroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getForeground();
            }
            if (oldColor != null)
                foregroundDialog.setRGB(oldColor.getRGB()); // seed dialog
            // with current
            // color
            RGB rgb = foregroundDialog.open();
            if (rgb == null)
                return;
            oldColor = foregroundColor; // save old foreground color to
            // dispose when done
            foregroundColor = new Color(event.display, rgb);
            setExampleWidgetForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    backgroundButton.setImage(backgroundImage); // sets the size of the
    // button
    backgroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = backgroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getBackground(); // seed dialog
                // with current
                // color
            }
            if (oldColor != null)
                backgroundDialog.setRGB(oldColor.getRGB());
            RGB rgb = backgroundDialog.open();
            if (rgb == null)
                return;
            oldColor = backgroundColor; // save old background color to
            // dispose when done
            backgroundColor = new Color(event.display, rgb);
            setExampleWidgetBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    fontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = font;
            if (oldFont == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldFont = controls[0].getFont();
            }
            if (oldFont != null)
                fontDialog.setFontList(oldFont.getFontData()); // seed
            // dialog
            // with
            // current
            // font
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = font; // dispose old font when done
            font = new Font(event.display, fontData);
            setExampleWidgetFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });
    defaultsButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            resetColorsAndFonts();
        }
    });
    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent event) {
            if (foregroundImage != null)
                foregroundImage.dispose();
            if (backgroundImage != null)
                backgroundImage.dispose();
            if (foregroundColor != null)
                foregroundColor.dispose();
            if (backgroundColor != null)
                backgroundColor.dispose();
            if (font != null)
                font.dispose();
            foregroundColor = null;
            backgroundColor = null;
            font = null;
        }
    });
}

From source file:CustomControlExample.java

/**
 * Creates the "Fonts" group./*from  ww w.  j a va2  s. co m*/
 */
void createColorGroup() {
    /* Create the group */
    colorGroup = new Group(controlGroup, SWT.NONE);
    colorGroup.setLayout(new GridLayout(2, false));
    colorGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    colorGroup.setText(ControlExample.getResourceString("Colors"));

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Color"));
    foregroundButton = new Button(colorGroup, SWT.PUSH);

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Color"));
    backgroundButton = new Button(colorGroup, SWT.PUSH);

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Selection_Foreground_Color"));
    foregroundSelectionButton = new Button(colorGroup, SWT.PUSH);

    new Label(colorGroup, SWT.NONE).setText(ControlExample.getResourceString("Selection_Background_Color"));
    backgroundSelectionButton = new Button(colorGroup, SWT.PUSH);

    fontButton = new Button(colorGroup, SWT.PUSH);
    fontButton.setText(ControlExample.getResourceString("Font"));
    fontButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));

    itemFontButton = new Button(colorGroup, SWT.PUSH);
    itemFontButton.setText(ControlExample.getResourceString("Item_Font"));
    itemFontButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));

    Button defaultsButton = new Button(colorGroup, SWT.PUSH);
    defaultsButton.setText(ControlExample.getResourceString("Defaults"));

    Shell shell = controlGroup.getShell();
    final ColorDialog colorDialog = new ColorDialog(shell);
    final FontDialog fontDialog = new FontDialog(shell);

    /* Create images to display current colors */
    int imageSize = 12;
    Display display = shell.getDisplay();
    foregroundImage = new Image(display, imageSize, imageSize);
    backgroundImage = new Image(display, imageSize, imageSize);
    foregroundSelectionImage = new Image(display, imageSize, imageSize);
    backgroundSelectionImage = new Image(display, imageSize, imageSize);

    /* Add listeners to set the colors and font */
    foregroundButton.setImage(foregroundImage); // sets the size of the
    // button
    foregroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = foregroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getForeground();
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB()); // seed dialog with
            // current color
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = foregroundColor; // save old foreground color to
            // dispose when done
            foregroundColor = new Color(event.display, rgb);
            setExampleWidgetForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    backgroundButton.setImage(backgroundImage); // sets the size of the
    // button
    backgroundButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = backgroundColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getBackground(); // seed dialog
                // with current
                // color
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB());
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = backgroundColor; // save old background color to
            // dispose when done
            backgroundColor = new Color(event.display, rgb);
            setExampleWidgetBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    foregroundSelectionButton.setImage(foregroundSelectionImage); // sets
    // the
    // size
    // of
    // the
    // button
    foregroundSelectionButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = foregroundSelectionColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getForeground();
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB()); // seed dialog with
            // current color
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = foregroundSelectionColor; // save old foreground
            // color to dispose when
            // done
            foregroundSelectionColor = new Color(event.display, rgb);
            setExampleWidgetForeground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    backgroundSelectionButton.setImage(backgroundSelectionImage); // sets
    // the
    // size
    // of
    // the
    // button
    backgroundSelectionButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Color oldColor = backgroundSelectionColor;
            if (oldColor == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldColor = controls[0].getBackground(); // seed dialog
                // with current
                // color
            }
            if (oldColor != null)
                colorDialog.setRGB(oldColor.getRGB());
            RGB rgb = colorDialog.open();
            if (rgb == null)
                return;
            oldColor = backgroundSelectionColor; // save old background
            // color to dispose when
            // done
            backgroundSelectionColor = new Color(event.display, rgb);
            setExampleWidgetBackground();
            if (oldColor != null)
                oldColor.dispose();
        }
    });
    fontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = font;
            if (oldFont == null) {
                Control[] controls = getExampleWidgets();
                if (controls.length > 0)
                    oldFont = controls[0].getFont();
            }
            if (oldFont != null)
                fontDialog.setFontList(oldFont.getFontData()); // seed
            // dialog
            // with
            // current
            // font
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = font; // dispose old font when done
            font = new Font(event.display, fontData);
            setExampleWidgetFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });

    /* Add listeners to set the colors and font */
    itemFontButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Font oldFont = itemFont;
            if (oldFont == null)
                oldFont = tabFolder1.getItem(0).getFont();
            fontDialog.setFontList(oldFont.getFontData());
            FontData fontData = fontDialog.open();
            if (fontData == null)
                return;
            oldFont = itemFont;
            itemFont = new Font(event.display, fontData);
            setItemFont();
            setExampleWidgetSize();
            if (oldFont != null)
                oldFont.dispose();
        }
    });

    defaultsButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            resetColorsAndFonts();
        }
    });

    shell.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent event) {
            if (foregroundImage != null)
                foregroundImage.dispose();
            if (backgroundImage != null)
                backgroundImage.dispose();
            if (foregroundColor != null)
                foregroundColor.dispose();
            if (backgroundColor != null)
                backgroundColor.dispose();
            if (font != null)
                font.dispose();
            foregroundColor = null;
            backgroundColor = null;
            font = null;
            if (foregroundSelectionImage != null)
                foregroundSelectionImage.dispose();
            if (backgroundSelectionImage != null)
                backgroundSelectionImage.dispose();
            if (foregroundSelectionColor != null)
                foregroundSelectionColor.dispose();
            if (backgroundSelectionColor != null)
                backgroundSelectionColor.dispose();
            foregroundSelectionColor = null;
            backgroundSelectionColor = null;
            if (itemFont != null)
                itemFont.dispose();
            itemFont = null;
        }
    });
}