1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Widget;
/**
* A simple TableViewer to demonstrate the usage of a standard content provider
* with a virtual table
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class Snippet029VirtualTableViewer {
public class MyModel {
public int counter;
public String presentation;
public MyModel(final int counter) {
this.counter = counter;
final Random r = new Random(System.currentTimeMillis());
final int randomNumber = r.nextInt(10);
if (randomNumber % 2 == 0) {
presentation = "Even";
} else {
presentation = "Odd";
}
}
}
private class MyContentProvider implements IStructuredContentProvider {
private MyModel[] elements;
/**
* {@inheritDoc}
*/
@Override
public void dispose() {
}
/**
* {@inheritDoc}
*/
@Override
public Object[] getElements(final Object inputElement) {
return elements;
}
/**
* {@inheritDoc}
*/
@Override
public void inputChanged(final Viewer viewer, final Object oldInput,
final Object newInput) {
this.elements = (MyModel[]) newInput;
}
}
private class MyFilter extends ViewerFilter {
/**
* Flag indicating if this filter is active.
*/
private boolean isActive;
/**
* The presentation to filter on.
*/
private final String presentationToTest;
/**
* Default constructor.
*
* @param pPresentation
*/
public MyFilter(final String pPresentation) {
presentationToTest = pPresentation;
isActive = true;
}
public boolean isActive() {
return isActive;
}
@Override
public boolean select(final Viewer viewer, final Object parentElement,
final Object element) {
boolean filtered = true;
if (isActive) {
if (element instanceof MyModel) {
filtered =
((MyModel) element).presentation.equals(presentationToTest);
}
}
return filtered;
}
public void setActive(final boolean pIsActive) {
this.isActive = pIsActive;
}
}
public Snippet029VirtualTableViewer(final Shell shell) {
final Composite composite = new Composite(shell, SWT.NONE);
final TableColumnLayout layout = new TableColumnLayout();
composite.setLayout(layout);
final TableViewer v = new TableViewer(composite, SWT.VIRTUAL);
v.setContentProvider(new MyContentProvider());
v.setUseHashlookup(true);
final MyModel[] model = createModel();
v.setInput(model);
final TableViewerColumn columnIndex = new TableViewerColumn(v, SWT.LEFT);
columnIndex.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(final Object element) {
return "Item " + ((MyModel) element).counter;
}
});
layout.setColumnData(columnIndex.getColumn(), new ColumnWeightData(1));
final TableViewerColumn presentationIndex =
new TableViewerColumn(v, SWT.LEFT);
presentationIndex.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(final Object element) {
return ((MyModel) element).presentation;
}
});
layout
.setColumnData(presentationIndex.getColumn(), new ColumnWeightData(2));
final Table table = v.getTable();
table.setLinesVisible(true);
final Set<String> presValues = new HashSet<String>();
for (final MyModel currentModel : model) {
presValues.add(currentModel.presentation);
}
final Shell popupShell = new Shell(shell, SWT.BORDER);
popupShell.setLayout(new GridLayout());
Button filterButton;
final MyFilter[] filters = new MyFilter[presValues.size()];
int index = 0;
for (final String presentation : presValues) {
filterButton = new Button(popupShell, SWT.CHECK);
filterButton.setText(presentation);
filterButton.setSelection(true);
final MyFilter currentFilter = new MyFilter(presentation);
filterButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final Widget widget = e.widget;
if (widget instanceof Button) {
currentFilter.setActive(((Button) widget).getSelection());
}
}
});
filters[index] = currentFilter;
index++;
}
final Button applyButton = new Button(popupShell, SWT.NONE);
applyButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(final MouseEvent e) {
if (!popupShell.isDisposed() && popupShell.isVisible()) {
boolean allActive = true;
for (final MyFilter filter : filters) {
allActive &= filter.isActive();
}
for (final ViewerFilter filter : v.getFilters()) {
v.removeFilter(filter);
}
if (!allActive) {
v.setFilters(filters);
}
popupShell.setVisible(false);
}
}
});
applyButton.setText("Apply Filters");
applyButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(final MouseEvent e) {
if (e.button == 3) {
if (!popupShell.isDisposed()) {
if (popupShell.isVisible()) {
popupShell.setVisible(false);
}
popupShell.pack();
final Point cursorLocation =
popupShell.getDisplay().getCursorLocation();
popupShell
.setLocation(cursorLocation.x + 10, cursorLocation.y + 10);
popupShell.open();
}
}
}
});
}
/**
* @param args
*/
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet029VirtualTableViewer(shell);
shell.setSize(200, 800);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private MyModel[] createModel() {
final MyModel[] elements = new MyModel[10000];
for (int i = 0; i < 10000; i++) {
elements[i] = new MyModel(i);
}
return elements;
}
} |
Partager