1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.contentassist;
14 import java.util.Arrays;
15 import java.util.Collection;
17 import java.util.TreeMap;
19 import org.eclipse.jface.fieldassist.IContentProposal;
20 import org.eclipse.jface.fieldassist.IContentProposalListener;
21 import org.eclipse.jface.fieldassist.TextContentAdapter;
22 import org.eclipse.jface.layout.GridDataFactory;
23 import org.eclipse.jface.layout.GridLayoutFactory;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.FocusListener;
27 import org.eclipse.swt.events.KeyAdapter;
28 import org.eclipse.swt.events.KeyEvent;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.TraverseEvent;
32 import org.eclipse.swt.events.TraverseListener;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Text;
38 * @author Tuukka Lehtonen
42 public class ContentAssistTextField extends Composite {
44 private static final boolean DEBUG = false;
47 INamedObject acceptedProposal;
49 OpenableContentAssistCommandAdapter contentAssist;
51 public <T extends INamedObject> ContentAssistTextField(final Composite parent, T initialValue, final T[] allowedValues, int style) {
52 this(parent, initialValue, Arrays.asList(allowedValues), style);
55 public <T extends INamedObject> ContentAssistTextField(final Composite parent, T initialValue, final Collection<T> allowedValues, int style) {
57 this.result = initialValue;
59 GridLayoutFactory.fillDefaults().applyTo(this);
61 text = new Text(this, style);
62 text.setText(initialValue == null ? "" : initialValue.getName());
63 GridDataFactory.fillDefaults().grab(true, true).applyTo(text);
65 final Map<String, INamedObject> valueMap = new TreeMap<String, INamedObject>(String.CASE_INSENSITIVE_ORDER);
66 for (T t : allowedValues) {
67 valueMap.put(t.getName(), t);
70 contentAssist = new OpenableContentAssistCommandAdapter(text,
71 new TextContentAdapter(),
72 new NamedObjectContentProposalProvider<T>(allowedValues),
77 contentAssist.addContentProposalListener(new IContentProposalListener() {
79 public void proposalAccepted(IContentProposal proposal) {
81 System.out.println("accepted: " + proposal);
82 acceptedProposal = ((INamedObjectContentProposal) proposal).getNamedObject();
83 ContentAssistTextField.this.result = acceptedProposal;
84 text.setText(acceptedProposal.getName());
85 text.setSelection(acceptedProposal.getName().length());
90 text.addModifyListener(new ModifyListener() {
92 public void modifyText(ModifyEvent e) {
93 String t = text.getText();
95 System.out.println("text: " + t);
96 System.out.println("value set: " + valueMap);
98 INamedObject obj = valueMap.get(t);
102 System.out.println("no result");
103 contentAssist.open();
107 System.out.println("result: " + result);
111 text.addKeyListener(new KeyAdapter() {
114 public void keyPressed(KeyEvent e) {
115 if(e.keyCode == SWT.CR) {
116 String name = text.getText();
118 System.out.println("CR: " + name);
122 if(acceptedProposal == null)
123 acceptedProposal = FindExactMatch.exec(name, allowedValues);
124 if(acceptedProposal == null)
125 contentAssist.open();
127 ContentAssistTextField.this.result = acceptedProposal;
128 //text.setText(acceptedProposal.getName());
129 //text.traverse(SWT.TRAVERSE_TAB_NEXT);
130 //text.traverse(SWT.TRAVERSE_RETURN);
136 text.addFocusListener(new FocusListener() {
138 TraverseListener listener = new TraverseListener() {
140 public void keyTraversed(TraverseEvent e) {
141 // System.out.println("CA TraverseListener PARENT TRAVERSE: " + e.detail);
142 if (e.detail == SWT.TRAVERSE_RETURN) {
149 public void focusLost(FocusEvent e) {
150 parent.removeTraverseListener(listener);
154 public void focusGained(FocusEvent e) {
155 parent.addTraverseListener(listener);
159 contentAssist.setPropagateKeys(true);
162 public INamedObject getResult() {
166 public Text getControl() {
170 public void openAssist() {
171 contentAssist.open();