Example usage for org.eclipse.swt.widgets Display getSystemColor

List of usage examples for org.eclipse.swt.widgets Display getSystemColor

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display getSystemColor.

Prototype

@Override
    public Color getSystemColor(int id) 

Source Link

Usage

From source file:CustomControlExample.java

/**
 * Creates the "StyledText Style" group.
 *//*from   www . j a  v a 2 s  .  c o  m*/
void createStyledTextStyleGroup() {
    final Display display = controlGroup.getDisplay();
    styledTextStyleGroup = new Group(controlGroup, SWT.NONE);
    styledTextStyleGroup.setText(ControlExample.getResourceString("StyledText_Styles"));
    styledTextStyleGroup.setLayout(new GridLayout(7, false));
    GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    data.horizontalSpan = 2;
    styledTextStyleGroup.setLayoutData(data);

    /* Get images */
    boldImage = createBitmapImage(display, "bold");
    italicImage = createBitmapImage(display, "italic");
    redImage = createBitmapImage(display, "red");
    yellowImage = createBitmapImage(display, "yellow");
    underlineImage = createBitmapImage(display, "underline");
    strikeoutImage = createBitmapImage(display, "strikeout");

    /* Create controls to modify the StyledText */
    Label label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("StyledText_Style_Instructions"));
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 7;
    label.setLayoutData(data);
    new Label(styledTextStyleGroup, SWT.NONE).setText(ControlExample.getResourceString("Bold"));
    boldButton = new Button(styledTextStyleGroup, SWT.PUSH);
    boldButton.setImage(boldImage);
    new Label(styledTextStyleGroup, SWT.NONE).setText(ControlExample.getResourceString("Underline"));
    underlineButton = new Button(styledTextStyleGroup, SWT.PUSH);
    underlineButton.setImage(underlineImage);
    new Label(styledTextStyleGroup, SWT.NONE).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    new Label(styledTextStyleGroup, SWT.NONE).setText(ControlExample.getResourceString("Foreground_Style"));
    redButton = new Button(styledTextStyleGroup, SWT.PUSH);
    redButton.setImage(redImage);
    new Label(styledTextStyleGroup, SWT.NONE).setText(ControlExample.getResourceString("Italic"));
    italicButton = new Button(styledTextStyleGroup, SWT.PUSH);
    italicButton.setImage(italicImage);
    new Label(styledTextStyleGroup, SWT.NONE).setText(ControlExample.getResourceString("Strikeout"));
    strikeoutButton = new Button(styledTextStyleGroup, SWT.PUSH);
    strikeoutButton.setImage(strikeoutImage);
    new Label(styledTextStyleGroup, SWT.NONE).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    new Label(styledTextStyleGroup, SWT.NONE).setText(ControlExample.getResourceString("Background_Style"));
    yellowButton = new Button(styledTextStyleGroup, SWT.PUSH);
    yellowButton.setImage(yellowImage);
    SelectionListener styleListener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Point sel = styledText.getSelectionRange();
            if ((sel == null) || (sel.y == 0))
                return;
            StyleRange style;
            for (int i = sel.x; i < sel.x + sel.y; i++) {
                StyleRange range = styledText.getStyleRangeAtOffset(i);
                if (range != null) {
                    style = (StyleRange) range.clone();
                    style.start = i;
                    style.length = 1;
                } else {
                    style = new StyleRange(i, 1, null, null, SWT.NORMAL);
                }
                if (e.widget == boldButton) {
                    style.fontStyle ^= SWT.BOLD;
                } else if (e.widget == italicButton) {
                    style.fontStyle ^= SWT.ITALIC;
                } else if (e.widget == underlineButton) {
                    style.underline = !style.underline;
                } else if (e.widget == strikeoutButton) {
                    style.strikeout = !style.strikeout;
                }
                styledText.setStyleRange(style);
            }
            styledText.setSelectionRange(sel.x + sel.y, 0);
        }
    };
    SelectionListener colorListener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Point sel = styledText.getSelectionRange();
            if ((sel == null) || (sel.y == 0))
                return;
            Color fg = null, bg = null;
            if (e.widget == redButton) {
                fg = display.getSystemColor(SWT.COLOR_RED);
            } else if (e.widget == yellowButton) {
                bg = display.getSystemColor(SWT.COLOR_YELLOW);
            }
            StyleRange style;
            for (int i = sel.x; i < sel.x + sel.y; i++) {
                StyleRange range = styledText.getStyleRangeAtOffset(i);
                if (range != null) {
                    style = (StyleRange) range.clone();
                    style.start = i;
                    style.length = 1;
                    style.foreground = style.foreground != null ? null : fg;
                    style.background = style.background != null ? null : bg;
                } else {
                    style = new StyleRange(i, 1, fg, bg, SWT.NORMAL);
                }
                styledText.setStyleRange(style);
            }
            styledText.setSelectionRange(sel.x + sel.y, 0);
        }
    };
    boldButton.addSelectionListener(styleListener);
    italicButton.addSelectionListener(styleListener);
    underlineButton.addSelectionListener(styleListener);
    strikeoutButton.addSelectionListener(styleListener);
    redButton.addSelectionListener(colorListener);
    yellowButton.addSelectionListener(colorListener);
    yellowButton.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            boldImage.dispose();
            italicImage.dispose();
            redImage.dispose();
            yellowImage.dispose();
            underlineImage.dispose();
            strikeoutImage.dispose();
        }
    });
}