Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9e5960b62488e636ba383fed0d0168af61b7f2d6 (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
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
/*******************************************************************************
 * Copyright (c) 2010, 2011, 2012 Mia-Software.
 * 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:
 *    Nicolas Bros (Mia-Software) - initial API and implementation
 *    Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.composite;

import org.eclipse.papyrus.emf.facet.util.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.composite.IFilteredElementSelectionComposite;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.FilteredList;

/**
 * A control for selecting an element from a list of elements, that can be
 * filtered using the associated text field.
 * 
 * @since 0.3
 */
public class FilteredElementSelectionComposite extends Composite implements
		IFilteredElementSelectionComposite {

	private final Text filterText;
	private final FilteredList fFilteredList;
	private String matchPrefix;

	public FilteredElementSelectionComposite(final Composite parent,
			final boolean matchAnywhere, final boolean multiSelection) {
		super(parent, SWT.BORDER);
		setLayout(new GridLayout());

		this.filterText = createFilterText(this);
		this.fFilteredList = createFilteredList(this, multiSelection);

		this.filterText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent event) {
				getfFilteredList().setFilter(
						getMatchPrefix(matchAnywhere)
								+ getFilterText().getText());
			}
		});

		this.filterText.addKeyListener(new KeyListener() {
			public void keyPressed(final KeyEvent event) {
				if (event.keyCode == SWT.ARROW_DOWN) {
					getfFilteredList().setFocus();
				}
			}

			public void keyReleased(final KeyEvent event) {
				// nothing
			}
		});
	}

	protected String getMatchPrefix(final boolean matchAnywhere) {
		if (matchAnywhere) {
			this.matchPrefix = "*"; //$NON-NLS-1$
		} else {
			this.matchPrefix = ""; //$NON-NLS-1$
		}
		return this.matchPrefix;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.composite.
	 * IFilteredElementSelectionComposite#setElements(java.lang.Object[])
	 */
	public void setElements(final Object[] elements) {
		this.fFilteredList.setElements(elements);
	}

	protected static FilteredList createFilteredList(final Composite parent,
			final boolean multiSelection) {
		int multi;
		if (multiSelection) {
			multi = SWT.MULTI;
		} else {
			multi = SWT.SINGLE;
		}
		final FilteredList filteredList = new FilteredList(parent, SWT.BORDER
				| SWT.V_SCROLL | SWT.H_SCROLL | multi, new LabelProvider(),
				true, false, true);
		final GridData data = new GridData();
		data.grabExcessVerticalSpace = true;
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.FILL;
		filteredList.setLayoutData(data);
		filteredList.setFilter(""); //$NON-NLS-1$
		return filteredList;
	}

	protected static Text createFilterText(final Composite parent) {
		final Text text = new Text(parent, SWT.BORDER);
		text.setMessage(Messages.FilteredElementSelectionControl_type_filter_text);
		final GridData data = new GridData();
		data.grabExcessVerticalSpace = false;
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.BEGINNING;
		text.setLayoutData(data);
		return text;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.composite.
	 * IFilteredElementSelectionComposite#getFirstSelectedElement()
	 */
	public Object getFirstSelectedElement() {
		Object result = null;
		final Object[] selection = this.fFilteredList.getSelection();
		if (selection.length > 0) {
			result = selection[0];
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.composite.
	 * IFilteredElementSelectionComposite#getSelectedElements()
	 */
	public Object[] getSelectedElements() {
		Object[] result = null;
		if (this.fFilteredList.getSelection().length > 0) {
			result = this.fFilteredList.getSelection();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.composite.
	 * IFilteredElementSelectionComposite#getFilteredList()
	 */
	public FilteredList getFilteredList() {
		return this.fFilteredList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.composite.
	 * IFilteredElementSelectionComposite#getFilterText()
	 */
	public Text getFilterText() {
		return this.filterText;
	}

	protected FilteredList getfFilteredList() {
		return this.fFilteredList;
	}
}

Back to the top