Project

General

Profile

Download (6.76 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.IContentProposalProvider;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.custom.CCombo;
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.Label;
26
import org.eclipse.ui.forms.widgets.TableWrapData;
27

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

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

    
48
    protected static final int DEFAULT_VISIBLE_ITEMS = 10;
49

    
50
    protected T selection;
51

    
52
    protected Label label;
53

    
54
    protected final CCombo combo;
55

    
56

    
57

    
58

    
59
    public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
60
        super(formFactory, formElement);
61

    
62
        label = formFactory.createLabel(getLayoutComposite(), "");
63
        addControl(label);
64

    
65
        // create combo
66
        combo = new CCombo(getLayoutComposite(), SWT.READ_ONLY|SWT.BORDER);
67

    
68
        addControl(combo);
69
        TableWrapData fill_HORIZONTALLY = LayoutConstants.FILL_HORIZONTALLY();
70
        combo.setLayoutData(fill_HORIZONTALLY);
71
        fill_HORIZONTALLY.maxWidth = 50;
72
        combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
73
        ContentProposalAdapter adapter = new ContentProposalAdapter(combo, new ComboContentAdapter(), getProposalProvider(), null, null);
74
        adapter.setPropagateKeys(true);
75
        adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
76
        //disable mouse-wheel selection
77
        combo.addListener(SWT.MouseWheel, e->e.doit=false);
78
    }
79

    
80
    /** {@inheritDoc} */
81
    @Override
82
    public void setBackground(Color color) {
83
        if (label != null) {
84
            label.setBackground(color);
85
        }
86
    }
87

    
88
    /** {@inheritDoc} */
89
    @Override
90
    public void setIrrelevant(boolean irrelevant) {
91
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
92
                : Resources.COLOR_COMPOSITE_BACKGROUND;
93

    
94
        Color color = AbstractUtility.getColor(colorId);
95
        combo.setBackground(color);
96
        if (label != null) {
97
            label.setBackground(color);
98
        }
99

    
100
    }
101

    
102
    public void setVisibleItemCount(int count){
103
        combo.setVisibleItemCount(count);
104
    }
105

    
106
    /** {@inheritDoc} */
107
    @Override
108
    public void setSelected(boolean selected) {
109
        setBackground(selected ? SELECTED : getPersistentBackground());
110
    }
111

    
112
    public T getSelection() {
113
        return selection;
114
    }
115

    
116
    public void addSelectionListener(SelectionListener listener) {
117
        combo.addSelectionListener(listener);
118
    }
119

    
120
    public void removeSelectionListener(SelectionListener listener) {
121
        combo.removeSelectionListener(listener);
122
    }
123

    
124
    /** {@inheritDoc} */
125
    @Override
126
    public void widgetDisposed(DisposeEvent e) {
127
        PreferencesUtil.getPreferenceStore().removePropertyChangeListener(this);
128
    }
129

    
130
    @Override
131
    public void widgetDefaultSelected(SelectionEvent e) {
132
    }
133

    
134
    @Override
135
    public boolean isEnabled() {
136
        return combo.isEnabled();
137
    }
138

    
139
    /** {@inheritDoc} */
140
    @Override
141
    public void setEnabled(boolean enabled) {
142
        combo.setEnabled(enabled);
143
    }
144

    
145
    public abstract void setSelection(T selection);
146

    
147
    IContentProposalProvider getProposalProvider() {
148
        return new IContentProposalProvider() {
149
            @Override
150
            public IContentProposal[] getProposals(String contents, int position) {
151
                String[] items = combo.getItems();
152
                if (contents.length() == 0 || items.length == 0) {
153
                    return new IContentProposal[0];
154
                }
155
                StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
156
                ArrayList<String> matches = new ArrayList<String>();
157
                for (int i = 0; i < items.length; i++) {
158
                    if (matcher.match(items[i])) {
159
                        matches.add(items[i]);
160
                    }
161
                }
162

    
163
                // We don't want to autoactivate if the only proposal exactly matches
164
                // what is in the combo.  This prevents the popup from
165
                // opening when the user is merely scrolling through the combo values or
166
                // has accepted a combo value.
167
                if (matches.size() == 1 && matches.get(0).equals(combo.getText())) {
168
                    return new IContentProposal[0];
169
                }
170

    
171
                if (matches.isEmpty()) {
172
                    return new IContentProposal[0];
173
                }
174

    
175
                // Make the proposals
176
                IContentProposal[] proposals = new IContentProposal[matches.size()];
177
                for (int i = 0; i < matches.size(); i++) {
178
                    final String proposal = matches.get(i);
179
                    proposals[i] = new IContentProposal() {
180

    
181
                        @Override
182
                        public String getContent() {
183
                            return proposal;
184
                        }
185

    
186
                        @Override
187
                        public int getCursorPosition() {
188
                            return proposal.length();
189
                        }
190

    
191
                        @Override
192
                        public String getDescription() {
193
                            return null;
194
                        }
195

    
196
                        @Override
197
                        public String getLabel() {
198
                            return null;
199
                        }
200
                    };
201
                }
202
                return proposals;
203
            }
204
        };
205
    }
206

    
207
}
(2-2/8)