1 11 12 package org.eclipse.jface.viewers; 13 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.swt.graphics.Color; 16 import org.eclipse.swt.graphics.Font; 17 import org.eclipse.swt.graphics.Image; 18 19 27 class WrappedViewerLabelProvider extends ColumnLabelProvider { 28 29 private static ILabelProvider defaultLabelProvider = new LabelProvider(); 30 31 private ILabelProvider labelProvider = defaultLabelProvider; 32 33 private IColorProvider colorProvider; 34 35 private IFontProvider fontProvider; 36 37 private IViewerLabelProvider viewerLabelProvider; 38 39 private ITreePathLabelProvider treePathLabelProvider; 40 41 46 public WrappedViewerLabelProvider(IBaseLabelProvider labelProvider) { 47 super(); 48 setProviders(labelProvider); 49 } 50 51 57 public void setProviders(Object provider) { 58 if (provider instanceof ITreePathLabelProvider) 59 treePathLabelProvider = ((ITreePathLabelProvider) provider); 60 61 if (provider instanceof IViewerLabelProvider) 62 viewerLabelProvider = ((IViewerLabelProvider) provider); 63 64 if (provider instanceof ILabelProvider) 65 labelProvider = ((ILabelProvider) provider); 66 67 if (provider instanceof IColorProvider) 68 colorProvider = (IColorProvider) provider; 69 70 if (provider instanceof IFontProvider) 71 fontProvider = (IFontProvider) provider; 72 73 } 74 75 80 public Font getFont(Object element) { 81 if (fontProvider == null) { 82 return null; 83 } 84 85 return fontProvider.getFont(element); 86 87 } 88 89 94 public Color getBackground(Object element) { 95 if (colorProvider == null) { 96 return null; 97 } 98 99 return colorProvider.getBackground(element); 100 } 101 102 107 public String getText(Object element) { 108 return getLabelProvider().getText(element); 109 } 110 111 116 public Image getImage(Object element) { 117 return getLabelProvider().getImage(element); 118 } 119 120 125 public Color getForeground(Object element) { 126 if (colorProvider == null) { 127 return null; 128 } 129 130 return colorProvider.getForeground(element); 131 } 132 133 138 ILabelProvider getLabelProvider() { 139 return labelProvider; 140 } 141 142 147 IColorProvider getColorProvider() { 148 return colorProvider; 149 } 150 151 156 IFontProvider getFontProvider() { 157 return fontProvider; 158 } 159 160 public void update(ViewerCell cell) { 161 Object element = cell.getElement(); 162 if(viewerLabelProvider == null && treePathLabelProvider == null){ 163 cell.setText(getText(element)); 165 Image image = getImage(element); 166 cell.setImage(image); 167 if (colorProvider != null) { 168 cell.setBackground(getBackground(element)); 169 cell.setForeground(getForeground(element)); 170 } 171 if (fontProvider != null) { 172 cell.setFont(getFont(element)); 173 } 174 return; 175 } 176 177 ViewerLabel label = new ViewerLabel(cell.getText(), cell.getImage()); 178 179 if (treePathLabelProvider != null) { 180 TreePath treePath = cell.getViewerRow().getTreePath(); 181 182 Assert.isNotNull(treePath); 183 treePathLabelProvider.updateLabel(label, treePath); 184 } else if (viewerLabelProvider != null) { 185 viewerLabelProvider.updateLabel(label, element); 186 } 187 if (!label.hasNewForeground() && colorProvider != null) 188 label.setForeground(getForeground(element)); 189 190 if (!label.hasNewBackground() && colorProvider != null) 191 label.setBackground(getBackground(element)); 192 193 if (!label.hasNewFont() && fontProvider != null) 194 label.setFont(getFont(element)); 195 196 applyViewerLabel(cell, label); 197 } 198 199 private void applyViewerLabel(ViewerCell cell, ViewerLabel label) { 200 if (label.hasNewText()) { 201 cell.setText(label.getText()); 202 } 203 if (label.hasNewImage()) { 204 cell.setImage(label.getImage()); 205 } 206 if (colorProvider!= null || label.hasNewBackground()) { 207 cell.setBackground(label.getBackground()); 208 } 209 if (colorProvider!= null || label.hasNewForeground()) { 210 cell.setForeground(label.getForeground()); 211 } 212 if (fontProvider!= null || label.hasNewFont()) { 213 cell.setFont(label.getFont()); 214 } 215 } 216 } 217 | Popular Tags |