Skip to content

Package: AbstractFilteredSelectionComposite$1

AbstractFilteredSelectionComposite$1

nameinstructionbranchcomplexitylinemethod
selectionChanged(SelectionChangedEvent)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2012 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: *
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.spi.common.ui.composites;
16:
17: import org.eclipse.emf.ecp.internal.common.ui.MessageKeys;
18: import org.eclipse.emf.ecp.spi.common.ui.CompositeProvider;
19: import org.eclipse.emf.ecp.spi.common.ui.ECPViewerFilter;
20: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
21: import org.eclipse.jface.layout.GridDataFactory;
22: import org.eclipse.jface.layout.GridLayoutFactory;
23: import org.eclipse.jface.viewers.ISelectionChangedListener;
24: import org.eclipse.jface.viewers.IStructuredSelection;
25: import org.eclipse.jface.viewers.SelectionChangedEvent;
26: import org.eclipse.jface.viewers.StructuredViewer;
27: import org.eclipse.swt.SWT;
28: import org.eclipse.swt.events.ModifyEvent;
29: import org.eclipse.swt.events.ModifyListener;
30: import org.eclipse.swt.widgets.Composite;
31: import org.eclipse.swt.widgets.Label;
32: import org.eclipse.swt.widgets.Text;
33:
34: /**
35: * This {@link CompositeProvider} provides Composite containing a {@link Text} widget and a viewer.
36: * The contents of the viewer can be filtered by typing a text into the Text widget.
37: *
38: * @author Eugen Neufeld
39: *
40: * @param <T> the type of the Viewer. This must extend a {@link StructuredViewer}
41: */
42: public abstract class AbstractFilteredSelectionComposite<T extends StructuredViewer> implements CompositeProvider {
43:
44:         private T viewer;
45:
46:         private Object[] selection;
47:
48:         /**
49:          * Default Constructor.
50:          */
51:         public AbstractFilteredSelectionComposite() {
52:                 super();
53:         }
54:
55:         /**
56:          * {@inheritDoc}
57:          */
58:         @Override
59:         public Composite createUI(Composite parent) {
60:                 final Composite composite = new Composite(parent, SWT.NULL);
61:
62:                 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(composite);
63:
64:                 final Label filterLabel = new Label(composite, SWT.LEFT);
65:                 filterLabel.setText(LocalizationServiceHelper.getString(AbstractFilteredSelectionComposite.class,
66:                         MessageKeys.AbstractModelElementHelper_FilterLabel));
67:                 final Text filterInput = new Text(composite, SWT.SEARCH);
68:                 filterInput.setMessage(LocalizationServiceHelper.getString(AbstractFilteredSelectionComposite.class,
69:                         MessageKeys.AbstractModelElementHelper_FilterText));
70:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(filterInput);
71:
72:                 viewer = createViewer(composite);
73:
74:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).minSize(0, 150).span(2, 1)
75:                         .applyTo(viewer.getControl());
76:
77:                 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
78:
79:                         @Override
80:                         public void selectionChanged(SelectionChangedEvent event) {
81:                                 selection = ((IStructuredSelection) viewer.getSelection()).toArray();
82:                         }
83:                 });
84:                 viewer.addFilter(getFilter());
85:
86:                 filterInput.addModifyListener(new ModifyListener() {
87:                         @Override
88:                         public void modifyText(ModifyEvent e) {
89:                                 final String text = filterInput.getText();
90:                                 getFilter().setSearchTerm(text);
91:                                 expandViewer();
92:                                 if (text != null && text.length() == 0) {
93:                                         collapsViewer();
94:                                 }
95:                                 viewer.refresh();
96:                         }
97:                 });
98:
99:                 return composite;
100:         }
101:
102:         /**
103:          * Subclasses can redefine the expand behavior of the viewer.
104:          */
105:         protected void expandViewer() {
106:
107:         }
108:
109:         /**
110:          * Subclasses can redefine the collaps behavior of the viewer.
111:          */
112:         protected void collapsViewer() {
113:         }
114:
115:         /**
116:          * Returns the used {@link StructuredViewer}.
117:          *
118:          * @return the viewer
119:          */
120:         public T getViewer() {
121:                 return viewer;
122:         }
123:
124:         /**
125:          * Returns the selected objects.
126:          *
127:          * @return the selection
128:          */
129:         public Object[] getSelection() {
130:                 return selection;
131:         }
132:
133:         /**
134:          * Creates a {@link StructuredViewer} on top of the provided {@link Composite}. The result is the created
135:          * {@link StructuredViewer}.
136:          *
137:          * @param composite the {@link Composite} to create the viewer on
138:          * @return the created {@link StructuredViewer}
139:          */
140:         protected abstract T createViewer(Composite composite);
141:
142:         /**
143:          * Returns a Filter to use.
144:          *
145:          * @return the {@link ECPViewerFilter} to use
146:          */
147:         protected abstract ECPViewerFilter getFilter();
148: }