Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » suggestBox
suggestBox [message #482718] Thu, 27 August 2009 16:43 Go to next message
HC is currently offline HCFriend
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;
}
Re: suggestBox [message #482733 is a reply to message #482718] Thu, 27 August 2009 17:27 Go to previous messageGo to next message
HC is currently offline HCFriend
Messages: 20
Registered: July 2009
Junior Member
I found a solution...

text.getDisplay().getCursorControl() gives me the control where the mouse
event occurred even if the click was in the scrollbar, so before disposing
the shell on the focusLost event, i can test if the click was in the List.


Thanks a lot!

hc
Re: suggestBox [message #482917 is a reply to message #482733] Fri, 28 August 2009 14:30 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
The getCursorControl() approach sounds pretty hacky. The snippet below
demonstrates how to get the new focus control when FocusOut is received:

public static void main (String [] args) {
final Display display = new Display ();
final Shell shell = new Shell (display);
shell.setLayout(new RowLayout(SWT.VERTICAL));
Listener listener = new Listener() {
public void handleEvent(Event event) {
display.asyncExec(new Runnable() {
public void run() {
if (display.isDisposed()) return;
System.out.println("new focus control: " +
display.getFocusControl());
}
});
}
};
new Text(shell, SWT.SINGLE).addListener(SWT.FocusOut, listener);
new Button(shell, SWT.PUSH).addListener(SWT.FocusOut, listener);
shell.pack();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

Grant


" hc" <hcosta@portoeditora.pt> wrote in message
news:511d1515016bab985188baa5dfc53783$1@www.eclipse.org...
> I found a solution...
>
> text.getDisplay().getCursorControl() gives me the control where the mouse
> event occurred even if the click was in the scrollbar, so before disposing
> the shell on the focusLost event, i can test if the click was in the List.
>
>
> Thanks a lot!
>
> hc
>
Re: suggestBox [message #483202 is a reply to message #482718] Mon, 31 August 2009 14:45 Go to previous messageGo to next message
Remo Loetscher is currently offline Remo LoetscherFriend
Messages: 18
Registered: July 2009
Junior Member
Hi hc,

May you want to have a look at the
org.eclipse.jface.fieldassist.AutoCompleteField class (Sample:
http://javawiki.sowas.com/doku.php?id=swt-jface:autocomplete field).

Remo
Re: suggestBox [message #483225 is a reply to message #483202] Mon, 31 August 2009 16:20 Go to previous message
HC is currently offline HCFriend
Messages: 20
Registered: July 2009
Junior Member
Remo Loetscher wrote:

> Hi hc,

> May you want to have a look at the
> org.eclipse.jface.fieldassist.AutoCompleteField class (Sample:
> http://javawiki.sowas.com/doku.php?id=swt-jface:autocomplete field).

> Remo

Silly me...
I've been looking for suggestBox, didn't remember of autoComplete...:p

Anyway... I managed to get what I wanted.. The result is pretty similar,
and i get a better control.

Thanks,

hc
Previous Topic:Table selection via checkboxes
Next Topic:Accessing composite componets from another composite
Goto Forum:
  


Current Time: Tue Aug 20 16:14:14 GMT 2019

Powered by FUDForum. Page generated in 0.01914 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top