import java.util.Vector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/****************************************************************************
*
***************************************************************************/
public class AJAXLike
implements VerifyListener, ModifyListener {
// Constants used for conversions
private List list;
//private static ArrayList<String> inhalt = new ArrayList<String>();
private static Vector content = new Vector();
// Widgets used in the window
private Text txt;
Display display;
Shell shell;
/**************************************************************************
* Runs the application
*
*************************************************************************/
public void run() {
display = new Display();
shell = new Shell(display);
shell.setMinimumSize(new Point(300, 200));
shell.setText("AJAX-like Textfield");
createContents(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}//run()
/**************************************************************************
* Create the main window's contents
* @param Shell shell
*************************************************************************/
private void createContents(final Shell shell) {
shell.setLayout(new GridLayout(3, true));
fillList();
// Create the label and input box for txt
final Label label = new Label(shell, SWT.LEFT);
label.setLayoutData(new GridData());
label.setText("Text");
//textfield
txt = new Text(shell, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
txt.setLayoutData(data);
// Add the listeners
txt.addVerifyListener(this);
txt.addModifyListener(this);
new Label(shell, SWT.NONE);
//list to show possible results
list = new List(shell, SWT.V_SCROLL | SWT.BORDER);
final GridData gd_list = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
gd_list.widthHint = 50;
gd_list.heightHint = 80;
list.setLayoutData(gd_list);
//SelectionListener to select element
list.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
System.out.println(list.getItem(list.getSelectionIndex()));
//this doesn't work because of the verifyListener
//if you disable all verifyLister for txt everything's fine
txt.setText(list.getItem(list.getSelectionIndex()).toString());
}
});
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
}//createContents()
/**************************************************************************
* Called when the user types into a text box, but before the text box gets
* what the user typed
*************************************************************************/
public void verifyText(VerifyEvent event) {
// don't allow assume of event
event.doit = false;
// Get the character typed
char myChar = event.character;
String text = ((Text) event.widget).getText();
// Allow '-' if first character
if (myChar == '-' && text.length() == 0) event.doit = true;
// Allow 0-9
if (Character.isDigit(myChar)) event.doit = true;
// Allow backspace
if (myChar == '\b') event.doit = true;
// Allow letter
if(Character.isLetter(myChar)) event.doit = true;
}//verifyText()
/**************************************************************************
* Called when the user modifies the text in a text box
*************************************************************************/
public void modifyText(ModifyEvent event) {
// Remove all the listeners, so we don't enter any infinite loops
txt.removeVerifyListener(this);
txt.removeModifyListener(this);
// Get the widget whose text was modified
Text text = (Text) event.widget;
checkList(text.getText());
// Add the listeners back
txt.addVerifyListener(this);
txt.addModifyListener(this);
}//modifyText()
/**************************************************************************
* Fill list with compared strings.
* @param String text
*************************************************************************/
private void checkList(String text) {
list.removeAll();
for(int i=0;i<content.size();i++) {
String tmp = content.elementAt(i).toString();
if(compare(tmp, text)){
list.add(tmp);
}
}
shell.layout();
}//checkList()
/**************************************************************************
* Return whether 'content' contains the submitted text.
* @param String content
* @param String text
* @return boolean
*************************************************************************/
private boolean compare(String content, String text) {
boolean result = false;
for(int i=0;i<text.length();i++) {
if(i < content.length()) {
if(content.charAt(i) == (text.charAt(i))) {
result = true;
}
else {
result = false;
break;
}//if_else#1.1
} else {
result = false;
}//if_else#1
}//for
return result;
}//compare()
/**************************************************************************
* Fill a List with some values.
*
*************************************************************************/
private static void fillList(){
content.add("alf");
content.add("alfred");
content.add("albert");
content.add("anton");
content.add("alles anders");
content.add("ahoi");
content.add("ah, na so");
content.add("aha");
content.add("andre");
content.add("ariane");
content.add("berta");
content.add("bert");
content.add("bernd");
}//fillList()
/**************************************************************************
* The application entry point
* @param args the command line arguments
*************************************************************************/
public static void main(String[] args) {
new AJAXLike().run();
}
}