| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2006 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.jdt.astview.views; |
| 16 | |
| 17 | import org.eclipse.swt.SWT; |
| 18 | import org.eclipse.swt.graphics.Color; |
| 19 | import org.eclipse.swt.graphics.Image; |
| 20 | import org.eclipse.swt.widgets.Display; |
| 21 | |
| 22 | import org.eclipse.jface.viewers.IColorProvider; |
| 23 | import org.eclipse.jface.viewers.LabelProvider; |
| 24 | import org.eclipse.jface.viewers.LabelProviderChangedEvent; |
| 25 | |
| 26 | import org.eclipse.jdt.core.Signature; |
| 27 | import org.eclipse.jdt.core.dom.ASTNode; |
| 28 | |
| 29 | public class TrayLabelProvider extends LabelProvider implements IColorProvider { |
| 30 | |
| 31 | private Color fBlue, fRed; |
| 32 | private Color fWidgetForeground; |
| 33 | |
| 34 | private Object fViewerElement; |
| 35 | |
| 36 | public TrayLabelProvider() { |
| 37 | Display display= Display.getCurrent(); |
| 38 | |
| 39 | fRed= display.getSystemColor(SWT.COLOR_RED); |
| 40 | fBlue= display.getSystemColor(SWT.COLOR_DARK_BLUE); |
| 41 | fWidgetForeground= display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND); |
| 42 | } |
| 43 | |
| 44 | public void setViewerElement(Object viewerElement) { |
| 45 | if (fViewerElement != viewerElement) { |
| 46 | fViewerElement= viewerElement; |
| 47 | fireLabelProviderChanged(new LabelProviderChangedEvent(this)); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String getText(Object obj) { |
| 53 | if (obj instanceof DynamicBindingProperty) { |
| 54 | DynamicBindingProperty dynamicBindingProperty= (DynamicBindingProperty) obj; |
| 55 | dynamicBindingProperty.setViewerElement(fViewerElement instanceof Binding ? (Binding) fViewerElement : null); |
| 56 | return dynamicBindingProperty.getLabel(); |
| 57 | } else if (obj instanceof DynamicAttributeProperty) { |
| 58 | DynamicAttributeProperty dynamicAttributeProperty= (DynamicAttributeProperty) obj; |
| 59 | dynamicAttributeProperty.setViewerElement(fViewerElement); |
| 60 | return dynamicAttributeProperty.getLabel(); |
| 61 | } else if (obj instanceof ASTAttribute) { |
| 62 | return ((ASTAttribute) obj).getLabel(); |
| 63 | } else if (obj instanceof ASTNode) { |
| 64 | return Signature.getSimpleName(((ASTNode) obj).getClass().getName()); |
| 65 | } else { |
| 66 | return ""; // https://bugs.eclipse.org/bugs/show_bug.cgi?id=126017 |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Image getImage(Object obj) { |
| 72 | if (obj instanceof DynamicBindingProperty) { |
| 73 | DynamicBindingProperty dynamicBindingProperty= (DynamicBindingProperty) obj; |
| 74 | dynamicBindingProperty.setViewerElement(fViewerElement instanceof Binding ? (Binding) fViewerElement : null); |
| 75 | return dynamicBindingProperty.getImage(); |
| 76 | } else if (obj instanceof DynamicAttributeProperty) { |
| 77 | DynamicAttributeProperty dynamicAttributeProperty= (DynamicAttributeProperty) obj; |
| 78 | dynamicAttributeProperty.setViewerElement(fViewerElement); |
| 79 | return dynamicAttributeProperty.getImage(); |
| 80 | } else if (obj instanceof ASTAttribute) { |
| 81 | return ((ASTAttribute) obj).getImage(); |
| 82 | } else { |
| 83 | return null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose() |
| 89 | */ |
| 90 | @Override |
| 91 | public void dispose() { |
| 92 | super.dispose(); |
| 93 | fViewerElement= null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Color getForeground(Object element) { |
| 98 | if (element instanceof Binding) { |
| 99 | return fBlue; |
| 100 | |
| 101 | } else if (element instanceof ExceptionAttribute) { |
| 102 | if (element instanceof DynamicBindingProperty) { |
| 103 | ((DynamicBindingProperty) element).setViewerElement(fViewerElement instanceof Binding ? (Binding) fViewerElement : null); |
| 104 | } else if (element instanceof DynamicAttributeProperty) { |
| 105 | ((DynamicAttributeProperty) element).setViewerElement(fViewerElement); |
| 106 | } |
| 107 | |
| 108 | if (((ExceptionAttribute) element).getException() == null) |
| 109 | // return null; //Bug 75022: Does not work when label is updated (retains old color, doesn't get default) |
| 110 | //TODO remove hackaround when bug 75022 is fixed |
| 111 | return fWidgetForeground; |
| 112 | else |
| 113 | return fRed; |
| 114 | |
| 115 | } else { |
| 116 | return null; // normal color |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object) |
| 122 | */ |
| 123 | @Override |
| 124 | public Color getBackground(Object element) { |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | } |
| 129 |
Members