Project

General

Profile

Download (7.66 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.ui.combo;
10

    
11
import java.util.ArrayList;
12

    
13
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
14
import org.eclipse.jface.fieldassist.ComboContentAdapter;
15
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
16
import org.eclipse.jface.fieldassist.IContentProposal;
17
import org.eclipse.jface.fieldassist.IContentProposalListener;
18
import org.eclipse.jface.fieldassist.IContentProposalProvider;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.DisposeEvent;
21
import org.eclipse.swt.events.DisposeListener;
22
import org.eclipse.swt.events.SelectionEvent;
23
import org.eclipse.swt.events.SelectionListener;
24
import org.eclipse.swt.graphics.Color;
25
import org.eclipse.swt.widgets.Combo;
26
import org.eclipse.swt.widgets.Label;
27
import org.eclipse.ui.forms.widgets.TableWrapData;
28

    
29
import eu.etaxonomy.taxeditor.model.AbstractUtility;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.preference.Resources;
32
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
35
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
36
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
37
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
38
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
39

    
40
/**
41
 * @author pplitzner
42
 * @date Aug 11, 2016
43
 *
44
 */
45
public abstract class AbstractComboElement<T> extends
46
AbstractCdmFormElement implements SelectionListener,
47
IEnableableFormElement, ISelectable,
48
DisposeListener {
49

    
50
    protected static final int DEFAULT_VISIBLE_ITEMS = 10;
51

    
52
    protected T selection;
53

    
54
    protected Label label;
55

    
56
    protected Combo combo;
57

    
58
    public boolean hasNullValue;
59

    
60

    
61

    
62

    
63
    public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement, boolean hasNullValue) {
64
        super(formFactory, formElement);
65

    
66
        label = formFactory.createLabel(getLayoutComposite(), "");
67
        addControl(label);
68

    
69
        // create combo
70
        combo = new Combo(getLayoutComposite(), SWT.BORDER);
71

    
72
        addControl(combo);
73
        TableWrapData fill_HORIZONTALLY = LayoutConstants.FILL_HORIZONTALLY();
74
        combo.setLayoutData(fill_HORIZONTALLY);
75
        fill_HORIZONTALLY.maxWidth = 50;
76
//        combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
77
        //disable mouse-wheel selection
78
        combo.addListener(SWT.MouseWheel, e->e.doit=false);
79
        this.hasNullValue = hasNullValue;
80

    
81

    
82
    }
83

    
84
    public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement){
85
        this(formFactory, formElement, false);
86
    }
87

    
88

    
89

    
90
    /** {@inheritDoc} */
91
    @Override
92
    public void setBackground(Color color) {
93
        if (label != null) {
94
            label.setBackground(color);
95
        }
96
    }
97

    
98
    /** {@inheritDoc} */
99
    @Override
100
    public void setIrrelevant(boolean irrelevant) {
101
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
102
                : Resources.COLOR_COMPOSITE_BACKGROUND;
103

    
104
        Color color = AbstractUtility.getColor(colorId);
105
        combo.setBackground(color);
106
        if (label != null) {
107
            label.setBackground(color);
108
        }
109

    
110
    }
111

    
112
    public void setVisibleItemCount(int count){
113
        combo.setVisibleItemCount(count);
114
    }
115

    
116
    /** {@inheritDoc} */
117
    @Override
118
    public void setSelected(boolean selected) {
119
        setBackground(selected ? SELECTED : getPersistentBackground());
120
    }
121

    
122
    public T getSelection() {
123
        return selection;
124
    }
125

    
126
    public void addSelectionListener(SelectionListener listener) {
127
        combo.addSelectionListener(listener);
128
    }
129

    
130
    public void removeSelectionListener(SelectionListener listener) {
131
        combo.removeSelectionListener(listener);
132
    }
133

    
134
    /** {@inheritDoc} */
135
    @Override
136
    public void widgetDisposed(DisposeEvent e) {
137
        PreferencesUtil.getPreferenceStore().removePropertyChangeListener(this);
138
    }
139

    
140
    @Override
141
    public void widgetDefaultSelected(SelectionEvent e) {
142
    }
143

    
144
    @Override
145
    public boolean isEnabled() {
146
        return combo.isEnabled();
147
    }
148

    
149
    /** {@inheritDoc} */
150
    @Override
151
    public void setEnabled(boolean enabled) {
152
        combo.setEnabled(enabled);
153
    }
154

    
155
    public abstract void setSelection(T selection);
156

    
157

    
158

    
159
    private AbstractComboElement<T> getComboElement(){
160
        return this;
161
    }
162

    
163

    
164
    protected void addContentProposalAdapter() {
165
        ContentProposalAdapter adapter;
166

    
167
        adapter = new ContentProposalAdapter(combo, new ComboContentAdapter(), getProposalProvider(), null, null);
168
        adapter.setPropagateKeys(true);
169
        adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
170
        adapter.addContentProposalListener(new IContentProposalListener() {
171
            @SuppressWarnings("unchecked")
172
            @Override
173
            public void proposalAccepted(IContentProposal proposal) {
174
                setSelection((T)combo.getData(proposal.getContent()));
175
                firePropertyChangeEvent(new CdmPropertyChangeEvent(getComboElement(), null));
176
            }
177
        });
178
    }
179

    
180
    IContentProposalProvider getProposalProvider() {
181
        return new IContentProposalProvider() {
182
            @Override
183
            public IContentProposal[] getProposals(String contents, int position) {
184
                String[] items = combo.getItems();
185
                if (contents.length() == 0 || items.length == 0) {
186
                    return new IContentProposal[0];
187
                }
188
                StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
189
                ArrayList<String> matches = new ArrayList<String>();
190
                for (int i = 0; i < items.length; i++) {
191
                    if (matcher.match(items[i])) {
192
                        matches.add(items[i]);
193
                    }
194
                }
195

    
196
                // We don't want to autoactivate if the only proposal exactly matches
197
                // what is in the combo.  This prevents the popup from
198
                // opening when the user is merely scrolling through the combo values or
199
                // has accepted a combo value.
200
                if (matches.size() == 1 && matches.get(0).equals(combo.getText())) {
201
                    return new IContentProposal[0];
202
                }
203

    
204
                if (matches.isEmpty()) {
205
                    return new IContentProposal[0];
206
                }
207

    
208
                // Make the proposals
209
                IContentProposal[] proposals = new IContentProposal[matches.size()];
210
                for (int i = 0; i < matches.size(); i++) {
211
                    final String proposal = matches.get(i);
212
                    proposals[i] = new IContentProposal() {
213

    
214
                        @Override
215
                        public String getContent() {
216
                            return proposal;
217
                        }
218

    
219
                        @Override
220
                        public int getCursorPosition() {
221
                            return proposal.length();
222
                        }
223

    
224
                        @Override
225
                        public String getDescription() {
226
                            return null;
227
                        }
228

    
229
                        @Override
230
                        public String getLabel() {
231
                            return null;
232
                        }
233
                    };
234
                }
235

    
236
                return proposals;
237
            }
238
        };
239
    }
240

    
241
}
(2-2/8)