]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/contentassist/ContentAssistTextField.java
Fixed diagram mouse wheel zooming for HiDPI monitors with zoom
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / contentassist / ContentAssistTextField.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.contentassist;
13
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.TreeMap;
18
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;
35
36
37 /**
38  * @author Tuukka Lehtonen
39  *
40  * @param <T>
41  */
42 public class ContentAssistTextField extends Composite {
43
44     private static final boolean DEBUG = false;
45     
46     Text text;
47     INamedObject acceptedProposal;
48     INamedObject result;
49     OpenableContentAssistCommandAdapter contentAssist;
50
51     public <T extends INamedObject> ContentAssistTextField(final Composite parent, T initialValue, final T[] allowedValues, int style) {
52         this(parent, initialValue, Arrays.asList(allowedValues), style);
53     }
54
55     public <T extends INamedObject> ContentAssistTextField(final Composite parent, T initialValue, final Collection<T> allowedValues, int style) {
56         super(parent, style);
57         this.result = initialValue;
58
59         GridLayoutFactory.fillDefaults().applyTo(this);
60
61         text = new Text(this, style);
62         text.setText(initialValue == null ? "" : initialValue.getName());
63         GridDataFactory.fillDefaults().grab(true, true).applyTo(text);
64
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);
68         }
69
70         contentAssist = new OpenableContentAssistCommandAdapter(text,
71                 new TextContentAdapter(),
72                 new NamedObjectContentProposalProvider<T>(allowedValues),
73                 (String) null,
74                 (char[]) null,
75                 true
76         );
77         contentAssist.addContentProposalListener(new IContentProposalListener() {
78             @Override
79             public void proposalAccepted(IContentProposal proposal) {
80                 if (DEBUG)
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());
86                 //text.selectAll();
87             }
88         });
89
90         text.addModifyListener(new ModifyListener() {
91             @Override
92             public void modifyText(ModifyEvent e) {
93                 String t = text.getText();
94                 if (DEBUG) {
95                     System.out.println("text: " + t);
96                     System.out.println("value set: " + valueMap);
97                 }
98                 INamedObject obj = valueMap.get(t);
99                 if (obj == null) {
100                     result = null;
101                     if (DEBUG)
102                         System.out.println("no result");
103                     contentAssist.open();
104                 } else {
105                     result = obj;
106                     if (DEBUG)
107                         System.out.println("result: " + result);
108                 }
109             }
110         });
111         text.addKeyListener(new KeyAdapter() {
112
113             @Override
114             public void keyPressed(KeyEvent e) {
115                 if(e.keyCode == SWT.CR) {
116                     String name = text.getText();
117                     if (DEBUG)
118                         System.out.println("CR: " + name);
119                     if(name.isEmpty()) {
120                         return;
121                     }
122                     if(acceptedProposal == null)
123                         acceptedProposal = FindExactMatch.exec(name, allowedValues);
124                     if(acceptedProposal == null)
125                         contentAssist.open();
126                     else {
127                         ContentAssistTextField.this.result = acceptedProposal;
128                         //text.setText(acceptedProposal.getName());
129                         //text.traverse(SWT.TRAVERSE_TAB_NEXT);
130                         //text.traverse(SWT.TRAVERSE_RETURN);
131                     }
132                 }
133             }
134
135         });
136         text.addFocusListener(new FocusListener() {
137
138             TraverseListener listener = new TraverseListener() {
139                 @Override
140                 public void keyTraversed(TraverseEvent e) {
141 //                    System.out.println("CA TraverseListener PARENT TRAVERSE: " + e.detail);
142                     if (e.detail == SWT.TRAVERSE_RETURN) {
143                         e.doit = false;
144                     }
145                 }
146             };
147
148             @Override
149             public void focusLost(FocusEvent e) {
150                 parent.removeTraverseListener(listener);
151             }
152
153             @Override
154             public void focusGained(FocusEvent e) {
155                 parent.addTraverseListener(listener);
156             }
157         });
158
159         contentAssist.setPropagateKeys(true);
160     }
161
162     public INamedObject getResult() {
163         return result;
164     }
165
166     public Text getControl() {
167         return text;
168     }
169
170     public void openAssist() {
171         contentAssist.open();
172     }
173
174 }