Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f94edd321db3c389c535255e79a2103fd0dc1637 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013 Hendrik Still and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Hendrik Still<hendrik.still@gammas.de> - initial implementation, bug 417676
 *******************************************************************************/

package org.eclipse.jface.snippets.viewers;

import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.LayoutConstants;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * A simple ComboViewer to demonstrate usage
 *
 * @author Hendrik Still <hendrik.still@gammas.de>
 *
 */
public class Snippet063ComboViewer {
	private class MyContentProvider implements IStructuredContentProvider<MyModel,MyModel[]> {

		public void dispose() {

		}

		public void inputChanged(Viewer<? extends MyModel[]> viewer, MyModel[] oldInput,
				MyModel[] newInput) {
		}

		public MyModel[] getElements(MyModel[] inputElement) {
			return inputElement;
		}

	}

	public class MyModel {
		public int counter;

		public MyModel(int counter) {
			this.counter = counter;
		}

		@Override
		public String toString() {
			return "Item " + this.counter;
		}
	}

	public Snippet063ComboViewer(Shell shell) {

		GridLayoutFactory.fillDefaults().numColumns(2)
				.margins(LayoutConstants.getMargins()).generateLayout(shell);

		final Label l = new Label(shell, SWT.None);
		l.setText("Choose Item:");
		final ComboViewer<MyModel,MyModel[]> v = new ComboViewer<MyModel,MyModel[]>(shell);
		v.setLabelProvider(new LabelProvider<MyModel>());
		v.setContentProvider(new MyContentProvider());

		MyModel[] model = createModel();
		v.setInput(model);

		// Select the initial Element
		if (model.length > 0) {
			v.setSelection(new StructuredSelection(model[0]));
		}
	}

	private MyModel[] createModel() {
		MyModel[] elements = new MyModel[11];

		for (int i = 0; i < 10; i++) {
			elements[i] = new MyModel(i);
		}
		elements[10] = new MyModel(42);

		return elements;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		new Snippet063ComboViewer(shell);

		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();

	}

}

Back to the top