|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 Tom Schindl 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 |
* Tom Schindl - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.jface.snippets.viewers; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
|
| 16 |
import org.eclipse.jface.resource.FontRegistry; |
| 17 |
import org.eclipse.jface.viewers.CellEditor; |
| 18 |
import org.eclipse.jface.viewers.EditingSupport; |
| 19 |
import org.eclipse.jface.viewers.ICellModifier; |
| 20 |
import org.eclipse.jface.viewers.ITableLabelProvider; |
| 21 |
import org.eclipse.jface.viewers.ITreeContentProvider; |
| 22 |
import org.eclipse.jface.viewers.LabelProvider; |
| 23 |
import org.eclipse.jface.viewers.TextCellEditor; |
| 24 |
import org.eclipse.jface.viewers.TreeViewer; |
| 25 |
import org.eclipse.jface.viewers.Viewer; |
| 26 |
import org.eclipse.swt.SWT; |
| 27 |
import org.eclipse.swt.graphics.Image; |
| 28 |
import org.eclipse.swt.layout.FillLayout; |
| 29 |
import org.eclipse.swt.widgets.Display; |
| 30 |
import org.eclipse.swt.widgets.Shell; |
| 31 |
import org.eclipse.swt.widgets.TreeColumn; |
| 32 |
import org.eclipse.swt.widgets.TreeItem; |
| 33 |
|
| 34 |
/** |
| 35 |
* A simple TreeViewer to demonstrate usage |
| 36 |
* |
| 37 |
* @author Tom Schindl <tom.schindl@bestsolution.at> |
| 38 |
* |
| 39 |
*/ |
| 40 |
public class Snippet026TreeViewerTabEditing { |
| 41 |
private class MyContentProvider implements ITreeContentProvider { |
| 42 |
|
| 43 |
/* |
| 44 |
* (non-Javadoc) |
| 45 |
* |
| 46 |
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
| 47 |
*/ |
| 48 |
public Object[] getElements(Object inputElement) { |
| 49 |
return ((MyModel) inputElement).child.toArray(); |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* (non-Javadoc) |
| 54 |
* |
| 55 |
* @see org.eclipse.jface.viewers.IContentProvider#dispose() |
| 56 |
*/ |
| 57 |
public void dispose() { |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* (non-Javadoc) |
| 63 |
* |
| 64 |
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, |
| 65 |
* java.lang.Object, java.lang.Object) |
| 66 |
*/ |
| 67 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* (non-Javadoc) |
| 73 |
* |
| 74 |
* @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) |
| 75 |
*/ |
| 76 |
public Object[] getChildren(Object parentElement) { |
| 77 |
return getElements(parentElement); |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* (non-Javadoc) |
| 82 |
* |
| 83 |
* @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) |
| 84 |
*/ |
| 85 |
public Object getParent(Object element) { |
| 86 |
if (element == null) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
return ((MyModel) element).parent; |
| 91 |
} |
| 92 |
|
| 93 |
/* |
| 94 |
* (non-Javadoc) |
| 95 |
* |
| 96 |
* @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) |
| 97 |
*/ |
| 98 |
public boolean hasChildren(Object element) { |
| 99 |
return ((MyModel) element).child.size() > 0; |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
public class MyModel { |
| 105 |
public MyModel parent; |
| 106 |
|
| 107 |
public ArrayList child = new ArrayList(); |
| 108 |
|
| 109 |
public int counter; |
| 110 |
|
| 111 |
public MyModel(int counter, MyModel parent) { |
| 112 |
this.parent = parent; |
| 113 |
this.counter = counter; |
| 114 |
} |
| 115 |
|
| 116 |
public String toString() { |
| 117 |
String rv = "Item "; |
| 118 |
if (parent != null) { |
| 119 |
rv = parent.toString() + "."; |
| 120 |
} |
| 121 |
|
| 122 |
rv += counter; |
| 123 |
|
| 124 |
return rv; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public class MyLabelProvider extends LabelProvider implements |
| 129 |
ITableLabelProvider { |
| 130 |
FontRegistry registry = new FontRegistry(); |
| 131 |
|
| 132 |
public Image getColumnImage(Object element, int columnIndex) { |
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
public String getColumnText(Object element, int columnIndex) { |
| 137 |
return "Column " + columnIndex + " => " + element.toString(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
public Snippet026TreeViewerTabEditing(Shell shell) { |
| 142 |
final TreeViewer v = new TreeViewer(shell,SWT.BORDER|SWT.FULL_SELECTION); |
| 143 |
|
| 144 |
TreeColumn column = new TreeColumn(v.getTree(),SWT.NONE); |
| 145 |
column.setWidth(200); |
| 146 |
column.setText("Column 1"); |
| 147 |
|
| 148 |
column = new TreeColumn(v.getTree(),SWT.NONE); |
| 149 |
column.setWidth(200); |
| 150 |
column.setText("Column 2"); |
| 151 |
|
| 152 |
v.setLabelProvider(new MyLabelProvider()); |
| 153 |
v.setContentProvider(new MyContentProvider()); |
| 154 |
v.setCellModifier(new ICellModifier() { |
| 155 |
|
| 156 |
/* (non-Javadoc) |
| 157 |
* @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String) |
| 158 |
*/ |
| 159 |
public boolean canModify(Object element, String property) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
/* (non-Javadoc) |
| 164 |
* @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String) |
| 165 |
*/ |
| 166 |
public Object getValue(Object element, String property) { |
| 167 |
return ((MyModel)element).counter + ""; |
| 168 |
} |
| 169 |
|
| 170 |
/* (non-Javadoc) |
| 171 |
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object) |
| 172 |
*/ |
| 173 |
public void modify(Object element, String property, Object value) { |
| 174 |
TreeItem item = (TreeItem)element; |
| 175 |
((MyModel)item.getData()).counter = Integer.parseInt(value.toString()); |
| 176 |
v.update(item.getData(), null); |
| 177 |
} |
| 178 |
|
| 179 |
}); |
| 180 |
|
| 181 |
v.setColumnProperties(new String[] { "column1", "column2" }); |
| 182 |
v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTree()), new TextCellEditor(v.getTree()) }); |
| 183 |
v.setTabEditingStyle(EditingSupport.TABING_HORIZONTAL|EditingSupport.TABING_MOVE_TO_ROW_NEIGHBOR|EditingSupport.TABING_VERTICAL); |
| 184 |
|
| 185 |
v.setInput(createModel()); |
| 186 |
} |
| 187 |
|
| 188 |
private MyModel createModel() { |
| 189 |
|
| 190 |
MyModel root = new MyModel(0, null); |
| 191 |
root.counter = 0; |
| 192 |
|
| 193 |
MyModel tmp; |
| 194 |
MyModel subItem; |
| 195 |
for (int i = 1; i < 10; i++) { |
| 196 |
tmp = new MyModel(i, root); |
| 197 |
root.child.add(tmp); |
| 198 |
for (int j = 1; j < i; j++) { |
| 199 |
subItem = new MyModel(j, tmp); |
| 200 |
subItem.child.add(new MyModel(j*100,subItem)); |
| 201 |
tmp.child.add(subItem); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return root; |
| 206 |
} |
| 207 |
|
| 208 |
public static void main(String[] args) { |
| 209 |
Display display = new Display(); |
| 210 |
Shell shell = new Shell(display); |
| 211 |
shell.setLayout(new FillLayout()); |
| 212 |
new Snippet026TreeViewerTabEditing(shell); |
| 213 |
shell.open(); |
| 214 |
|
| 215 |
while (!shell.isDisposed()) { |
| 216 |
if (!display.readAndDispatch()) |
| 217 |
display.sleep(); |
| 218 |
} |
| 219 |
|
| 220 |
display.dispose(); |
| 221 |
} |
| 222 |
} |