]>
git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.ui/src/org/argeo/app/ui/widgets/ConnectAbstractDropDown.java
1 package org
.argeo
.app
.ui
.widgets
;
3 import java
.util
.Arrays
;
6 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
7 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
8 import org
.eclipse
.rap
.rwt
.widgets
.DropDown
;
9 import org
.eclipse
.swt
.SWT
;
10 import org
.eclipse
.swt
.events
.FocusEvent
;
11 import org
.eclipse
.swt
.events
.FocusListener
;
12 import org
.eclipse
.swt
.widgets
.Event
;
13 import org
.eclipse
.swt
.widgets
.Listener
;
14 import org
.eclipse
.swt
.widgets
.Text
;
15 import org
.eclipse
.swt
.widgets
.Widget
;
18 * Enable easy addition of a {@code DropDown} widget to a text with listeners
21 public abstract class ConnectAbstractDropDown
{
23 private final Text text
;
24 private final DropDown dropDown
;
25 private boolean modifyFromList
= false;
27 // Current displayed text
28 private String userText
= "";
29 // Current displayed list items
30 private String
[] values
;
34 boolean refreshOnFocus
;
36 /** Implementing classes should call refreshValues() after initialisation */
37 public ConnectAbstractDropDown(Text text
) {
38 this(text
, SWT
.NONE
, false);
42 * Implementing classes should call refreshValues() after initialisation
46 * only SWT.READ_ONLY is understood, check if the entered text is
47 * part of the legal choices.
49 public ConnectAbstractDropDown(Text text
, int style
) {
50 this(text
, style
, false);
54 * Implementers should call refreshValues() once init has been done.
58 * only SWT.READ_ONLY is understood, check if the entered text is
59 * part of the legal choices.
60 * @param refreshOnFocus
61 * if true, the possible values are computed each time the focus is
62 * gained. It enables, among other to fine tune the getFilteredValues
63 * method depending on the current context
65 public ConnectAbstractDropDown(Text text
, int style
, boolean refreshOnFocus
) {
67 dropDown
= new DropDown(text
);
68 Object obj
= dropDown
;
69 if (obj
instanceof Widget
)
70 CmsSwtUtils
.markup((Widget
) obj
);
71 readOnly
= (style
& SWT
.READ_ONLY
) != 0;
72 this.refreshOnFocus
= refreshOnFocus
;
77 * Overwrite to force the refresh of the possible values on focus gained event
79 protected boolean refreshOnFocus() {
80 return refreshOnFocus
;
83 public String
getText() {
84 return text
.getText();
91 public void reset(String value
) {
92 modifyFromList
= true;
93 if (EclipseUiUtils
.notEmpty(value
))
98 modifyFromList
= false;
101 /** Overwrite to provide specific filtering */
102 protected abstract List
<String
> getFilteredValues(String filter
);
104 protected void refreshValues() {
105 List
<String
> filteredValues
= getFilteredValues(text
.getText());
106 values
= filteredValues
.toArray(new String
[filteredValues
.size()]);
107 dropDown
.setItems(values
);
110 protected void addListeners() {
112 addSelectionListener();
113 addDefaultSelectionListener();
117 protected void addFocusListener() {
118 text
.addFocusListener(new FocusListener() {
119 private static final long serialVersionUID
= -7179112097626535946L;
121 public void focusGained(FocusEvent event
) {
122 if (refreshOnFocus
) {
123 modifyFromList
= true;
125 modifyFromList
= false;
127 dropDown
.setVisible(true);
130 public void focusLost(FocusEvent event
) {
131 dropDown
.setVisible(false);
132 if (readOnly
&& values
!= null && !Arrays
.asList(values
).contains(userText
)) {
133 modifyFromList
= true;
136 modifyFromList
= false;
142 private void addSelectionListener() {
143 Object obj
= dropDown
;
144 if (obj
instanceof Widget
)
145 ((Widget
) obj
).addListener(SWT
.Selection
, new Listener() {
146 private static final long serialVersionUID
= -2357157809365135142L;
148 public void handleEvent(Event event
) {
149 if (event
.index
!= -1) {
150 modifyFromList
= true;
151 text
.setText(values
[event
.index
]);
152 modifyFromList
= false;
155 text
.setText(userText
);
156 text
.setSelection(userText
.length(), userText
.length());
163 private void addDefaultSelectionListener() {
164 Object obj
= dropDown
;
165 if (obj
instanceof Widget
)
166 ((Widget
) obj
).addListener(SWT
.DefaultSelection
, new Listener() {
167 private static final long serialVersionUID
= -5958008322630466068L;
169 public void handleEvent(Event event
) {
170 if (event
.index
!= -1) {
171 text
.setText(values
[event
.index
]);
172 text
.setSelection(event
.text
.length());
173 dropDown
.setVisible(false);
179 private void addModifyListener() {
180 text
.addListener(SWT
.Modify
, new Listener() {
181 private static final long serialVersionUID
= -4373972835244263346L;
183 public void handleEvent(Event event
) {
184 if (!modifyFromList
) {
185 userText
= text
.getText();
187 if (values
.length
== 1)
188 dropDown
.setSelectionIndex(0);
189 dropDown
.setVisible(true);