|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* Michael Krkoska - initial API and implementation (bug 188333) |
| 11 |
*******************************************************************************/ |
| 12 |
package org.eclipse.jface.snippets.viewers; |
| 13 |
|
| 14 |
import java.io.File; |
| 15 |
import java.text.MessageFormat; |
| 16 |
import java.util.Date; |
| 17 |
|
| 18 |
import org.eclipse.jface.viewers.CellLabelProvider; |
| 19 |
import org.eclipse.jface.viewers.ColumnLabelProvider; |
| 20 |
import org.eclipse.jface.viewers.ColumnViewer; |
| 21 |
import org.eclipse.jface.viewers.ILabelProvider; |
| 22 |
import org.eclipse.jface.viewers.ITreeContentProvider; |
| 23 |
import org.eclipse.jface.viewers.SimpleStyledCellLabelProvider; |
| 24 |
import org.eclipse.jface.viewers.TreeViewer; |
| 25 |
import org.eclipse.jface.viewers.TreeViewerColumn; |
| 26 |
import org.eclipse.jface.viewers.Viewer; |
| 27 |
import org.eclipse.swt.SWT; |
| 28 |
import org.eclipse.swt.custom.StyleRange; |
| 29 |
import org.eclipse.swt.events.SelectionAdapter; |
| 30 |
import org.eclipse.swt.events.SelectionEvent; |
| 31 |
import org.eclipse.swt.graphics.Color; |
| 32 |
import org.eclipse.swt.graphics.Image; |
| 33 |
import org.eclipse.swt.layout.GridData; |
| 34 |
import org.eclipse.swt.layout.GridLayout; |
| 35 |
import org.eclipse.swt.widgets.Button; |
| 36 |
import org.eclipse.swt.widgets.Composite; |
| 37 |
import org.eclipse.swt.widgets.Display; |
| 38 |
import org.eclipse.swt.widgets.Label; |
| 39 |
import org.eclipse.swt.widgets.Shell; |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Using a {@link SimpleStyledCellLabelProvider} on tree viewer. Compare the result with a native tree viewer. |
| 44 |
*/ |
| 45 |
public class Snippet049SimpleStyledCellLabelProvider { |
| 46 |
|
| 47 |
|
| 48 |
private static final int SHELL_WIDTH= 640; |
| 49 |
private static final Display DISPLAY= Display.getDefault(); |
| 50 |
|
| 51 |
|
| 52 |
public static void main(String[] args) { |
| 53 |
|
| 54 |
Shell shell= new Shell(DISPLAY, SWT.CLOSE | SWT.RESIZE); |
| 55 |
shell.setSize(SHELL_WIDTH, 300); |
| 56 |
shell.setLayout(new GridLayout(1, false)); |
| 57 |
|
| 58 |
Snippet049SimpleStyledCellLabelProvider example= new Snippet049SimpleStyledCellLabelProvider(); |
| 59 |
example.createPartControl(shell); |
| 60 |
|
| 61 |
shell.open(); |
| 62 |
|
| 63 |
while (!shell.isDisposed()) { |
| 64 |
if (!DISPLAY.readAndDispatch()) { |
| 65 |
DISPLAY.sleep(); |
| 66 |
} |
| 67 |
} |
| 68 |
DISPLAY.dispose(); |
| 69 |
} |
| 70 |
|
| 71 |
public Snippet049SimpleStyledCellLabelProvider() { |
| 72 |
} |
| 73 |
|
| 74 |
public void createPartControl(Composite parent) { |
| 75 |
Composite composite= new Composite(parent, SWT.NONE); |
| 76 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 77 |
composite.setLayout(new GridLayout(2, true)); |
| 78 |
|
| 79 |
ExampleLabelProvider labelProvider= new ExampleLabelProvider(); |
| 80 |
ModifiedDateLabelProvider dateLabelProvider= new ModifiedDateLabelProvider(); |
| 81 |
|
| 82 |
final ColumnViewer ownerDrawViewer= createViewer("Owner draw viewer:", composite, new DecoratingLabelProvider(labelProvider), new DecoratingDateLabelProvider(dateLabelProvider)); //$NON-NLS-1$ |
| 83 |
|
| 84 |
final ColumnViewer normalViewer= createViewer("Normal viewer:", composite, labelProvider, dateLabelProvider); //$NON-NLS-1$ |
| 85 |
|
| 86 |
Composite buttons= new Composite(parent, SWT.NONE); |
| 87 |
buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 88 |
buttons.setLayout(new GridLayout(3, false)); |
| 89 |
|
| 90 |
|
| 91 |
Button button1= new Button(buttons, SWT.PUSH); |
| 92 |
button1.setText("Refresh Viewers"); //$NON-NLS-1$ |
| 93 |
button1.addSelectionListener(new SelectionAdapter() { |
| 94 |
|
| 95 |
public void widgetSelected(SelectionEvent e) { |
| 96 |
ownerDrawViewer.refresh(); |
| 97 |
normalViewer.refresh(); |
| 98 |
} |
| 99 |
}); |
| 100 |
|
| 101 |
final Button button2= new Button(buttons, SWT.CHECK); |
| 102 |
button2.setText("Owner draw on column 1"); //$NON-NLS-1$ |
| 103 |
button2.setSelection(true); |
| 104 |
button2.addSelectionListener(new SelectionAdapter() { |
| 105 |
|
| 106 |
public void widgetSelected(SelectionEvent e) { |
| 107 |
boolean newState= button2.getSelection(); |
| 108 |
((DecoratingLabelProvider) ownerDrawViewer.getLabelProvider(0)).setOwnerDrawEnabled(newState); |
| 109 |
ownerDrawViewer.refresh(); |
| 110 |
} |
| 111 |
}); |
| 112 |
|
| 113 |
final Button button3= new Button(buttons, SWT.CHECK); |
| 114 |
button3.setText("Owner draw on column 2"); //$NON-NLS-1$ |
| 115 |
button3.setSelection(true); |
| 116 |
button3.addSelectionListener(new SelectionAdapter() { |
| 117 |
|
| 118 |
public void widgetSelected(SelectionEvent e) { |
| 119 |
boolean newState= button3.getSelection(); |
| 120 |
((DecoratingDateLabelProvider) ownerDrawViewer.getLabelProvider(1)).setOwnerDrawEnabled(newState); |
| 121 |
ownerDrawViewer.refresh(); |
| 122 |
} |
| 123 |
}); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
private static class FileSystemRoot { |
| 128 |
public File[] getRoots() { |
| 129 |
return File.listRoots(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private ColumnViewer createViewer(String description, Composite parent, CellLabelProvider labelProvider1, CellLabelProvider labelProvider2) { |
| 134 |
|
| 135 |
Composite composite= new Composite(parent, SWT.NONE); |
| 136 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); |
| 137 |
composite.setLayout(new GridLayout(1, true)); |
| 138 |
|
| 139 |
Label label= new Label(composite, SWT.NONE); |
| 140 |
label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); |
| 141 |
label.setText(description); |
| 142 |
|
| 143 |
TreeViewer treeViewer= new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
| 144 |
treeViewer.getTree().setHeaderVisible(true); |
| 145 |
treeViewer.setContentProvider(new FileSystemContentProvider()); |
| 146 |
|
| 147 |
TreeViewerColumn tvc1 = new TreeViewerColumn(treeViewer, SWT.NONE); |
| 148 |
tvc1.getColumn().setText("Name"); |
| 149 |
tvc1.getColumn().setWidth(200); |
| 150 |
tvc1.setLabelProvider(labelProvider1); |
| 151 |
|
| 152 |
TreeViewerColumn tvc2 = new TreeViewerColumn(treeViewer, SWT.NONE); |
| 153 |
tvc2.getColumn().setText("Date Modified"); |
| 154 |
tvc2.getColumn().setWidth(200); |
| 155 |
tvc2.setLabelProvider(labelProvider2); |
| 156 |
|
| 157 |
GridData data= new GridData(GridData.FILL, GridData.FILL, true, true); |
| 158 |
treeViewer.getControl().setLayoutData(data); |
| 159 |
|
| 160 |
treeViewer.setInput(new FileSystemRoot()); |
| 161 |
|
| 162 |
return treeViewer; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Implements a {@link SimpleStyledCellLabelProvider} that wraps a normal label |
| 167 |
* provider and adds some decorations in color |
| 168 |
*/ |
| 169 |
private static class DecoratingLabelProvider extends SimpleStyledCellLabelProvider { |
| 170 |
|
| 171 |
private static final StyleRange[] NO_RANGES= new StyleRange[0]; |
| 172 |
private final ILabelProvider fWrappedLabelProvider; |
| 173 |
|
| 174 |
public DecoratingLabelProvider(ILabelProvider labelProvider) { |
| 175 |
fWrappedLabelProvider= labelProvider; |
| 176 |
} |
| 177 |
|
| 178 |
protected LabelPresentationInfo getLabelPresentationInfo(Object element) { |
| 179 |
String text= fWrappedLabelProvider.getText(element); |
| 180 |
Image image= fWrappedLabelProvider.getImage(element); |
| 181 |
|
| 182 |
|
| 183 |
StyleRange[] ranges= NO_RANGES; |
| 184 |
if (element instanceof File) { |
| 185 |
File file= (File) element; |
| 186 |
if (file.isFile()) { |
| 187 |
String decoration= MessageFormat.format(" ({0} bytes)", new Object[] { new Long(file.length()) }); //$NON-NLS-1$ |
| 188 |
|
| 189 |
int decorationStart= text.length(); |
| 190 |
int decorationLength= decoration.length(); |
| 191 |
|
| 192 |
text+= decoration; |
| 193 |
|
| 194 |
Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE); |
| 195 |
|
| 196 |
StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null); |
| 197 |
ranges= new StyleRange[] { styleRange }; |
| 198 |
} |
| 199 |
} |
| 200 |
return new LabelPresentationInfo(text, ranges, image, null, null, null); |
| 201 |
} |
| 202 |
|
| 203 |
public void dispose() { |
| 204 |
super.dispose(); |
| 205 |
fWrappedLabelProvider.dispose(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
private static class DecoratingDateLabelProvider extends SimpleStyledCellLabelProvider { |
| 210 |
|
| 211 |
private static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; |
| 212 |
private static final StyleRange[] NO_RANGES= new StyleRange[0]; |
| 213 |
private final ILabelProvider fWrappedLabelProvider; |
| 214 |
|
| 215 |
public DecoratingDateLabelProvider(ILabelProvider labelProvider) { |
| 216 |
fWrappedLabelProvider= labelProvider; |
| 217 |
} |
| 218 |
|
| 219 |
protected LabelPresentationInfo getLabelPresentationInfo(Object element) { |
| 220 |
String text= fWrappedLabelProvider.getText(element); |
| 221 |
Image image= fWrappedLabelProvider.getImage(element); |
| 222 |
|
| 223 |
StyleRange[] ranges= NO_RANGES; |
| 224 |
if (element instanceof File) { |
| 225 |
File file= (File) element; |
| 226 |
String decoration= " " + DAYS[new Date(file.lastModified()).getDay()]; |
| 227 |
|
| 228 |
int decorationStart= text.length(); |
| 229 |
int decorationLength= decoration.length(); |
| 230 |
|
| 231 |
text+= decoration; |
| 232 |
|
| 233 |
Color decorationColor= Display.getDefault().getSystemColor(SWT.COLOR_GRAY); |
| 234 |
|
| 235 |
StyleRange styleRange= new StyleRange(decorationStart, decorationLength, decorationColor, null); |
| 236 |
ranges= new StyleRange[] { styleRange }; |
| 237 |
} |
| 238 |
return new LabelPresentationInfo(text, ranges, image, null, null, null); |
| 239 |
} |
| 240 |
|
| 241 |
public void dispose() { |
| 242 |
super.dispose(); |
| 243 |
fWrappedLabelProvider.dispose(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* A simple label provider |
| 250 |
*/ |
| 251 |
private static class ExampleLabelProvider extends ColumnLabelProvider { |
| 252 |
|
| 253 |
private static int IMAGE_SIZE= 16; |
| 254 |
private static final Image IMAGE1= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_WARNING).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE)); |
| 255 |
private static final Image IMAGE2= new Image(DISPLAY, DISPLAY.getSystemImage(SWT.ICON_ERROR).getImageData().scaledTo(IMAGE_SIZE, IMAGE_SIZE)); |
| 256 |
|
| 257 |
public Image getImage(Object element) { |
| 258 |
if (element instanceof File) { |
| 259 |
File file= (File) element; |
| 260 |
if (file.isDirectory()) { |
| 261 |
return IMAGE1; |
| 262 |
} else { |
| 263 |
return IMAGE2; |
| 264 |
} |
| 265 |
} |
| 266 |
return null; |
| 267 |
} |
| 268 |
|
| 269 |
public String getText(Object element) { |
| 270 |
if (element instanceof File) { |
| 271 |
File file= (File) element; |
| 272 |
if (file.getName().length() == 0) { |
| 273 |
return file.getAbsolutePath(); |
| 274 |
} |
| 275 |
return file.getName(); |
| 276 |
} |
| 277 |
return "null"; //$NON-NLS-1$ |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
private static class ModifiedDateLabelProvider extends ColumnLabelProvider { |
| 283 |
public String getText(Object element) { |
| 284 |
if (element instanceof File) { |
| 285 |
File file= (File) element; |
| 286 |
return new Date(file.lastModified()).toLocaleString(); |
| 287 |
} |
| 288 |
return "-"; //$NON-NLS-1$ |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
private static class FileSystemContentProvider implements ITreeContentProvider { |
| 293 |
|
| 294 |
public Object[] getChildren(Object element) { |
| 295 |
if (element instanceof File) { |
| 296 |
File file= (File) element; |
| 297 |
if (file.isDirectory()) { |
| 298 |
File[] listFiles= file.listFiles(); |
| 299 |
if (listFiles != null) { |
| 300 |
return listFiles; |
| 301 |
} |
| 302 |
} |
| 303 |
} else if (element instanceof FileSystemRoot) { |
| 304 |
return ((FileSystemRoot) element).getRoots(); |
| 305 |
} |
| 306 |
return new Object[0]; |
| 307 |
} |
| 308 |
|
| 309 |
public Object getParent(Object element) { |
| 310 |
if (element instanceof File) { |
| 311 |
File file= (File) element; |
| 312 |
return file.getParentFile(); |
| 313 |
} |
| 314 |
return null; |
| 315 |
} |
| 316 |
|
| 317 |
public boolean hasChildren(Object element) { |
| 318 |
return getChildren(element).length > 0; |
| 319 |
} |
| 320 |
|
| 321 |
public Object[] getElements(Object inputElement) { |
| 322 |
return getChildren(inputElement); |
| 323 |
} |
| 324 |
|
| 325 |
public void dispose() { |
| 326 |
} |
| 327 |
|
| 328 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
} |