suggestBox [message #482718] |
Thu, 27 August 2009 16:43 |
HC Messages: 20 Registered: July 2009 |
Junior Member |
|
|
I'm trying to make some kind of a suggestBox with Text(where user writes
his search) and Shell(list of suggestions).
The code is bellow.
I want the shell to disappear when the textbox loses focus, but i'm facing
some problems with this.
If the user clicks somewhere in the shell, the textbox loses focus, and
the shell disappears.
I tried to get around this with a flag, when the mouse enters the list of
suggestions the flag
gets to true, preventing the shell to close, and when the mouse leaves the
shell the flag goes to
false, so the shell can be closed.
This would work, if it wasn't for the scrollBar in the list of
suggestions... If the user takes the
mouse directly to the scrollbar without passing on the top of the list,
that flag never gets to true,
and i can't get the scrollBar to capture the mouseEnter or mouseOver
event!!
Is is possible to capture that mouseOver event from a scrollBar?
Is there some kind of event manager that i can use on the focusLost method
of the textbox to
determine the next focused control??
Thanks,
HC
//Create textbox
protected Control createControl(Composite parent) {
text = new Text(parent, SWT.BORDER);
GridLayout gl = new GridLayout();
gl.numColumns = 1;
GridData gd = new GridData(GridData.FILL_BOTH);
gd.minimumWidth = 200;
text.setLayoutData(gd);
//Select items from list using arrowKeys in textbox
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
boolean isUpKey = (e.stateMask == SWT.NONE) && (e.keyCode ==
16777217);
boolean isDownKey = (e.stateMask == SWT.NONE) &&
(e.keyCode == 16777218);
boolean changed = false;
List suggestList = suggestListViewer.getList();
int selIndex = suggestList.getSelectionIndex();
if(isUpKey){
if(selIndex > 1){
suggestList.setSelection(selIndex - 1);
changed = true;
}
}
else if(isDownKey){
if(selIndex < suggestList.getItemCount()){
suggestList.setSelection(selIndex + 1);
changed = true;
}
}
if(changed){
String selected = suggestList.getSelection()[0];
text.setText(selected);
text.setSelection(0, text.getCharCount());
}
}
});
text.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
if(shell != null && !shell.isDisposed() && !shell.getVisible()){
showSuggestBox();
}
else{
createShell();
}
onShell = false;
}
});
text.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
if(shell != null && !shell.isDisposed() &&
!shell.isFocusControl() && shell.getVisible() &&
!onShell && suggestListViewer != null &&
!suggestListViewer.getList().isFocusControl()){
shell.dispose();
}
}
});
return text;
}
private void createShell(){
shell = new Shell(parentComposite.getDisplay(), SWT.NO_TRIM);
GridLayout gl = new GridLayout();
gl.numColumns = 1;
GridData gd = new GridData(GridData.FILL_BOTH);
shell.setLayout(gl);
shell.setLayoutData(gd);
shell.setSize(300, 150);
shell.setLocation(text.toDisplay(text.getLocation().x - 10,
text.getLocation().y + text.getSize().y - 10));
gl = new GridLayout();
gl.numColumns = 1;
gd = new GridData(GridData.FILL_BOTH);
suggestListViewer = new ListViewer(shell, SWT.NO_SCROLL | SWT.V_SCROLL |
SWT.SINGLE);
suggestListViewer.setContentProvider(new SuggestBoxContentProvider());
suggestListViewer.setLabelProvider(new SuggestBoxLabelProvider());
suggestListViewer.setInput(selectableValues);
List suggestList = suggestListViewer.getList();
suggestList.setLayoutData(gd);
//copy selected text to textBox
suggestListViewer.addSelectionChangedListener(new
ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
List list = suggestListViewer.getList();
if(list.getSelectionCount() == 1){
String selected = list.getSelection()[0];
text.setText(selected);
shell.setVisible(false);
}
}
});
suggestList.addListener(SWT.MouseEnter, new Listener() {
@Override
public void handleEvent(Event event) {
onShell = true;
System.out.println("onList");
}
});
//THIS DOESN'T WORK...:(
suggestList.getVerticalBar().addListener(SWT.FocusIn, new Listener() {
@Override
public void handleEvent(Event event) {
onShell = true;
System.out.println("onList_|");
}
});
shell.open();
text.forceFocus();
shell.setVisible(true);
shell.moveAbove(null);
onShell = false;
shell.addListener(SWT.MouseExit, new Listener() {
@Override
public void handleEvent(Event event) {
onShell = false;
System.out.println("offTHEShell!!");
}
});
}
//make shell visible
public void showSuggestBox(){
shell.setLocation(text.toDisplay(text.getLocation().x - 10,
text.getLocation().y + text.getSize().y - 6));
shell.setSize(300, 150);
shell.setVisible(true);
shell.moveAbove(null);
text.forceFocus();
onShell = false;
}
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01723 seconds