Example usage for org.eclipse.jface.viewers ViewerCell getBackground

List of usage examples for org.eclipse.jface.viewers ViewerCell getBackground

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers ViewerCell getBackground.

Prototype

public Color getBackground() 

Source Link

Document

Gets the background color of the cell.

Usage

From source file:org.eclipse.team.svn.ui.composite.ResourceSelectionComposite.java

License:Open Source License

public void createControls() {
    GridLayout gridLayout = null;
    GridData data = null;//from  www . ja  v  a  2  s . c om

    gridLayout = new GridLayout();
    gridLayout.marginHeight = gridLayout.marginWidth = 0;
    this.setLayout(gridLayout);

    int style = SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER;
    Table table = new Table(this, this.showCheckBoxesAndButtons ? style | SWT.CHECK : style);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    TableLayout layout = new TableLayout();
    table.setLayout(layout);

    this.tableViewer = new CheckboxTableViewer(table);
    data = new GridData(GridData.FILL_BOTH);
    this.tableViewer.getTable().setLayoutData(data);

    // creating a comparator right now to get column listeners
    ResourcesTableComparator comparator = new ResourcesTableComparator(this.tableViewer);

    // checkbox
    TableColumn col = new TableColumn(table, SWT.NONE);
    col.setResizable(false);
    layout.addColumnData(new ColumnPixelData(20, false));

    // resource name
    col = new TableColumn(table, SWT.NONE);
    col.setResizable(true);
    col.setText(SVNUIMessages.ResourceSelectionComposite_Resource);
    layout.addColumnData(new ColumnWeightData(44, true));
    col.addSelectionListener(comparator);

    // status
    col = new TableColumn(table, SWT.NONE);
    col.setResizable(true);
    col.setText(SVNUIMessages.ResourceSelectionComposite_Content);
    layout.addColumnData(new ColumnWeightData(12, true));
    if (this.cacheEnabled) {
        col.addSelectionListener(comparator);
    }

    // propstatus
    col = new TableColumn(table, SWT.NONE);
    col.setResizable(true);
    col.setText(SVNUIMessages.ResourceSelectionComposite_Properties);
    layout.addColumnData(new ColumnWeightData(12, true));
    if (this.cacheEnabled) {
        col.addSelectionListener(comparator);
    }

    TableColumn treatAsEdit = null;
    if (this.allowTreatAsEditsColumn) {
        treatAsEdit = col = new TableColumn(table, SWT.NONE);
        col.setResizable(false);
        col.setText(SVNUIMessages.ResourceSelectionComposite_TreatAsEdit);
        layout.addColumnData(new ColumnWeightData(12, true));
    }

    // adding comparator and selection default sorting column and direction
    this.tableViewer.setComparator(comparator);
    comparator.setColumnNumber(ResourceSelectionComposite.COLUMN_STATUS);
    this.tableViewer.getTable()
            .setSortColumn(this.tableViewer.getTable().getColumn(ResourceSelectionComposite.COLUMN_STATUS));
    this.tableViewer.getTable().setSortDirection(SWT.UP);

    this.tableViewer.setLabelProvider(new ITableLabelProvider() {
        public Image getColumnImage(Object element, int columnIndex) {
            if (columnIndex == ResourceSelectionComposite.COLUMN_NAME && element instanceof IAdaptable) {
                IWorkbenchAdapter adapter = (IWorkbenchAdapter) ((IAdaptable) element)
                        .getAdapter(IWorkbenchAdapter.class);
                if (adapter == null) {
                    return null;
                }
                ImageDescriptor descriptor = adapter.getImageDescriptor(element);
                if (descriptor == null) {
                    return null;
                }

                boolean hasWarning = false;
                boolean hasError = false;
                try {
                    IResource currentResource = (IResource) element;
                    IMarker[] markers = currentResource.findMarkers(IMarker.PROBLEM, true,
                            IResource.DEPTH_INFINITE);
                    // Errors always with highest priority. So, other severities should be ignored.
                    for (int i = 0; i < markers.length && !hasError; i++) {
                        Integer severity = markers[i] != null
                                ? (Integer) markers[i].getAttribute(IMarker.SEVERITY)
                                : null;
                        if (severity != null) {
                            hasWarning |= severity.intValue() == IMarker.SEVERITY_WARNING;
                            hasError |= severity.intValue() == IMarker.SEVERITY_ERROR;
                        }
                    }
                } catch (CoreException e) {
                    // Markers are inaccessible: do not decorate resource icon
                }

                Image image = ResourceSelectionComposite.this.images.get(descriptor);
                if (image == null) {
                    ResourceSelectionComposite.this.images.put(descriptor, image = descriptor.createImage());
                }
                OverlayedImageDescriptor desc = null;
                if (hasError) {
                    desc = new OverlayedImageDescriptor(image, ResourceSelectionComposite.ERROR_IMAGE_DESC,
                            new Point(16, 16), OverlayedImageDescriptor.BOTTOM | OverlayedImageDescriptor.LEFT);
                } else if (hasWarning) {
                    desc = new OverlayedImageDescriptor(image, ResourceSelectionComposite.WARNING_IMAGE_DESC,
                            new Point(16, 16), OverlayedImageDescriptor.BOTTOM | OverlayedImageDescriptor.LEFT);
                } else {
                    desc = new OverlayedImageDescriptor(image, ResourceSelectionComposite.EMPTY_IMAGE_DESC,
                            new Point(16, 16), OverlayedImageDescriptor.BOTTOM | OverlayedImageDescriptor.LEFT);
                }
                image = this.createImage(desc);

                if (ResourceSelectionComposite.this.externalResources.contains(element)) {
                    desc = new OverlayedImageDescriptor(image, ResourceSelectionComposite.SWITCHED_IMAGE_DESC,
                            new Point(16, 16),
                            OverlayedImageDescriptor.BOTTOM | OverlayedImageDescriptor.RIGHT);
                }
                image = this.createImage(desc);

                return image;
            }
            return null;
        }

        protected Image createImage(OverlayedImageDescriptor descriptor) {
            Image image = ResourceSelectionComposite.this.images.get(descriptor);
            if (image == null) {
                ResourceSelectionComposite.this.images.put(descriptor, image = descriptor.createImage());
            }
            return image;
        }

        public String getColumnText(Object element, int columnIndex) {
            if (columnIndex == ResourceSelectionComposite.COLUMN_CHECKBOX) {
                return ""; //$NON-NLS-1$
            }
            IResource resource = (IResource) element;
            if (columnIndex == ResourceSelectionComposite.COLUMN_NAME) {
                String path = resource.getFullPath().toString();
                return path.startsWith("/") ? path.substring(1) : path; //$NON-NLS-1$
            }
            ILocalResource local = SVNRemoteStorage.instance().asLocalResource(resource);
            if (columnIndex == ResourceSelectionComposite.COLUMN_STATUS) {
                return ResourceSelectionComposite.this.contentStatusAsString(local);
            } else if (columnIndex == ResourceSelectionComposite.COLUMN_PROPSTATUS) {
                return ResourceSelectionComposite.this.propertiesStatusAsString(local);
            }
            return ""; //$NON-NLS-1$
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void dispose() {
        }

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void removeListener(ILabelProviderListener listener) {
        }
    });

    if (this.allowTreatAsEditsColumn) {
        // the order is important otherwise the common label provider overrides this one
        TableViewerColumn cbColumn = new TableViewerColumn(this.tableViewer, treatAsEdit);
        cbColumn.setLabelProvider(new ColumnLabelProvider() {
            public void update(ViewerCell cell) {
                IResource resource = (IResource) cell.getElement();
                ILocalResource local = SVNRemoteStorage.instance().asLocalResource(resource);
                if (IStateFilter.SF_PREREPLACEDREPLACED.accept(local)) {
                    TableItem item = (TableItem) cell.getItem();
                    Button button;
                    if (ResourceSelectionComposite.this.treatAsEditButtons.containsKey(cell.getElement())) {
                        button = ResourceSelectionComposite.this.treatAsEditButtons.get(cell.getElement());
                    } else {
                        button = new Button((Composite) cell.getViewerRow().getControl(), SWT.CHECK);
                        button.setData(resource);
                        button.setSelection(ResourceSelectionComposite.this.treatAsEdit.contains(resource));
                        button.setBackground(cell.getBackground());
                        button.addSelectionListener(new SelectionAdapter() {
                            public void widgetSelected(SelectionEvent e) {
                                if (((Button) e.getSource()).getSelection()) {
                                    ResourceSelectionComposite.this.treatAsEdit
                                            .add((IResource) ((Button) e.getSource()).getData());
                                } else {
                                    ResourceSelectionComposite.this.treatAsEdit
                                            .remove((IResource) ((Button) e.getSource()).getData());
                                }
                                ResourceSelectionComposite.this.getTableViewer()
                                        .refresh(((Button) e.getSource()).getData());
                            }
                        });
                        ResourceSelectionComposite.this.treatAsEditButtons.put(resource, button);
                    }
                    TableEditor editor = new TableEditor(item.getParent());
                    editor.grabHorizontal = true;
                    editor.grabVertical = true;
                    editor.setEditor(button, item, cell.getColumnIndex());
                    editor.layout();
                }
            }
        });
    }

    this.tableViewer.setContentProvider(new ArrayStructuredContentProvider());

    this.tableViewer.setInput(this.resources);
    for (int i = 0; i < this.resources.length; i++) {
        this.tableViewer.setChecked(this.resources[i], this.isSelectableResource(this.resources[i]));
    }
    this.updateSelectedResources();

    this.tableViewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            final IResource resource = (IResource) selection.getFirstElement();
            IResource[] resources = { resource };
            if (selection.size() == 1 && FileUtility.checkForResourcesPresence(resources,
                    IStateFilter.SF_ANY_CHANGE, IResource.DEPTH_ZERO)) {
                UIMonitorUtility.getDisplay().syncExec(new Runnable() {
                    public void run() {
                        ILocalResource local = SVNRemoteStorage.instance().asLocalResource(resource);
                        if (!IStateFilter.SF_INTERNAL_INVALID.accept(local)) {
                            IRepositoryResource remote = local.isCopied() ? SVNUtility.getCopiedFrom(resource)
                                    : SVNRemoteStorage.instance().asRepositoryResource(resource);
                            remote.setSelectedRevision(
                                    CompareResourcesOperation.getRemoteResourceRevisionForCompare(resource));
                            UIMonitorUtility.doTaskScheduledDefault(
                                    new CompareResourcesOperation(local, remote, true, true));
                        }
                    }
                });
            }
        }
    });

    if (!this.showCheckBoxesAndButtons) {
        return;
    }

    this.tableViewer.addSelectionChangedListener(this.selectionListener = new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            ResourceSelectionComposite.this.updateSelectedResources();

            ResourceSelectionComposite.this.fireResourcesSelectionChanged(
                    new ResourceSelectionChangedEvent(ResourceSelectionComposite.this.selectedResources));

            int selectedNumber = ResourceSelectionComposite.this.selectedResources.length;
            ResourceSelectionComposite.this.lblSelectedResourcesNumber
                    .setText(ResourceSelectionComposite.this.resourceNumberToString(selectedNumber));
        }
    });

    Composite tComposite = new Composite(this, SWT.RIGHT);
    GridLayout gLayout = new GridLayout();
    gLayout.numColumns = 4;
    gLayout.marginWidth = 0;
    tComposite.setLayout(gLayout);
    data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
    tComposite.setLayoutData(data);

    Button selectButton = new Button(tComposite, SWT.PUSH);
    selectButton.setText(SVNUIMessages.Button_SelectAll);
    data = new GridData();
    data.widthHint = DefaultDialog.computeButtonWidth(selectButton);
    selectButton.setLayoutData(data);
    SelectionListener listener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            ResourceSelectionComposite.this.tableViewer.setAllChecked(true);
            Object[] elements = ResourceSelectionComposite.this.tableViewer.getCheckedElements();
            ResourceSelectionComposite.this.selectionListener.selectionChanged(null);
            ResourceSelectionComposite.this.fireResourcesSelectionChanged(new ResourceSelectionChangedEvent(
                    Arrays.asList(elements).toArray(new IResource[elements.length])));
        }
    };
    selectButton.addSelectionListener(listener);

    Button deselectButton = new Button(tComposite, SWT.PUSH);
    deselectButton.setText(SVNUIMessages.Button_ClearSelection);
    data = new GridData();
    data.widthHint = DefaultDialog.computeButtonWidth(deselectButton);
    deselectButton.setLayoutData(data);
    listener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TableItem[] items = ResourceSelectionComposite.this.tableViewer.getTable().getSelection();
            for (TableItem item : items) {
                ResourceSelectionComposite.this.tableViewer.setChecked(item.getData(), false);
            }
            ResourceSelectionComposite.this.selectionListener.selectionChanged(null);
            ResourceSelectionComposite.this
                    .fireResourcesSelectionChanged(new ResourceSelectionChangedEvent(new IResource[0]));
        }
    };
    deselectButton.addSelectionListener(listener);

    deselectButton = new Button(tComposite, SWT.PUSH);
    deselectButton.setText(SVNUIMessages.Button_ClearAll);
    data = new GridData();
    data.widthHint = DefaultDialog.computeButtonWidth(deselectButton);
    deselectButton.setLayoutData(data);
    listener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            ResourceSelectionComposite.this.tableViewer.setAllChecked(false);
            ResourceSelectionComposite.this.selectionListener.selectionChanged(null);
            ResourceSelectionComposite.this
                    .fireResourcesSelectionChanged(new ResourceSelectionChangedEvent(new IResource[0]));
        }
    };
    deselectButton.addSelectionListener(listener);

    Composite lComposite = new Composite(tComposite, SWT.NONE);
    GridLayout lLayout = new GridLayout();
    lLayout.horizontalSpacing = 0;
    lLayout.marginWidth = 0;
    lComposite.setLayout(lLayout);
    data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
    lComposite.setLayoutData(data);

    this.lblSelectedResourcesNumber = new Label(lComposite, SWT.RIGHT);
    this.lblSelectedResourcesNumber.setText(this.resourceNumberToString(this.selectedResources.length));
    data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
    this.lblSelectedResourcesNumber.setLayoutData(data);
}

From source file:org.jboss.tools.openshift.express.internal.ui.propertytable.PropertyValueCellLabelProvider.java

License:Open Source License

protected void createLink(IProperty property, final ViewerCell cell) {
    if (StringUtils.isEmpty(property.getValue())) {
        return;/*from  w  w w.  j a v a 2 s. co m*/
    }
    final Hyperlink link = new Hyperlink((Tree) cell.getControl(), SWT.NONE); // SWT.NO_BACKGROUND
    link.setBackground(cell.getBackground());
    link.setForeground(JFaceResources.getColorRegistry().get(JFacePreferences.ACTIVE_HYPERLINK_COLOR));
    link.setFont(cell.getFont());
    link.setUnderlined(true);
    link.setText(property.getValue());
    link.setBackground(cell.getBackground());
    link.addMouseListener(onLinkClicked(property.getValue()));

    TreeUtils.createTreeEditor(link, property.getValue(), cell);
}

From source file:rabbit.ui.internal.viewers.TreePathDurationLabelProviderTest.java

License:Apache License

@Test
public void updateShouldSetTheCellColorUsingTheGivenColorProvider() throws Exception {
    ViewerCell cell = newCell(0, new Object());

    IValueProvider valueProvider = mock(IValueProvider.class);
    given(valueProvider.shouldPaint(cell.getElement())).willReturn(TRUE);

    Display display = PlatformUI.getWorkbench().getDisplay();
    Color foreground = display.getSystemColor(SWT.COLOR_CYAN);
    Color background = display.getSystemColor(SWT.COLOR_BLUE);
    IColorProvider colors = mock(IColorProvider.class);
    given(colors.getForeground(cell.getElement())).willReturn(foreground);
    given(colors.getBackground(cell.getElement())).willReturn(background);

    TreePathDurationLabelProvider labelProvider = new TreePathDurationLabelProvider(valueProvider, colors);

    labelProvider.update(cell);/*from w  w  w .  ja v a  2s  . co m*/
    assertThat(cell.getForeground(), is(foreground));
    assertThat(cell.getBackground(), is(background));
}

From source file:rabbit.ui.internal.viewers.TreePathIntLabelProviderTest.java

License:Apache License

@Test
public void updateShouldSetTheCellColorUsingTheGivenColorProvider() throws Exception {
    ViewerCell cell = newCell(0, new Object());

    IValueProvider valueProvider = mock(IValueProvider.class);
    given(valueProvider.shouldPaint(cell.getElement())).willReturn(TRUE);

    Display display = PlatformUI.getWorkbench().getDisplay();
    Color foreground = display.getSystemColor(SWT.COLOR_CYAN);
    Color background = display.getSystemColor(SWT.COLOR_BLUE);
    IColorProvider colors = mock(IColorProvider.class);
    given(colors.getForeground(cell.getElement())).willReturn(foreground);
    given(colors.getBackground(cell.getElement())).willReturn(background);

    TreePathIntLabelProvider labelProvider = new TreePathIntLabelProvider(valueProvider, colors);

    labelProvider.update(cell);//from  w  w  w. j ava  2 s  . c  om
    assertThat(cell.getForeground(), is(foreground));
    assertThat(cell.getBackground(), is(background));
}

From source file:sernet.verinice.rcp.search.tables.SearchTableColumnLabelProvider.java

License:Open Source License

private void createStyleRanges(ViewerCell cell, String query, List<StyleRange> styleRanges) {
    int index = 0;
    for (;;) {//from  w ww .  j  av  a 2  s  . c  om

        index = cell.getText().toLowerCase().indexOf(query.toLowerCase(), index);

        if (index == -1) {
            break;
        } else {
            styleRanges.add(new StyleRange(index, query.length(), red, cell.getBackground()));
            index++;
        }
    }
}