| /******************************************************************************* |
| * Copyright (c) 2020 seanmuir. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * seanmuir - initial API and implementation |
| * |
| *******************************************************************************/ |
| package org.eclipse.mdht.cda.xml; |
| |
| import java.util.Arrays; |
| |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.CCombo; |
| import org.eclipse.swt.events.KeyEvent; |
| import org.eclipse.swt.events.KeyListener; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Shell; |
| |
| public class SWTComboExample { |
| Display d; |
| |
| Shell s; |
| |
| SWTComboExample() { |
| d = new Display(); |
| s = new Shell(d); |
| s.setSize(250, 250); |
| s.setText("A Combo Example"); |
| final CCombo c = new CCombo(s, SWT.READ_ONLY); |
| c.setBounds(50, 50, 150, 65); |
| c.setEditable(true); |
| String items[] = { "aaItem One", "Item Two", "Item Three", "Item Four", "Item Five" }; |
| c.setItems(items); |
| |
| // ControlListener new ControlListener () {} ; |
| |
| // ModifyListener new ModifyListener () {}; |
| // c.addModifyListener(new ModifyListener() { |
| // |
| // @Override |
| // public void modifyText(ModifyEvent arg0) { |
| // // TODO Auto-generated method stub |
| // System.out.println(arg0.toString()); |
| // |
| // } |
| // }); |
| |
| c.addKeyListener(new KeyListener() { |
| |
| @Override |
| public void keyPressed(KeyEvent arg0) { |
| System.out.println(arg0.toString()); |
| } |
| |
| @Override |
| public void keyReleased(KeyEvent arg0) { |
| |
| if (arg0.keyCode == 0xd) { |
| System.out.println(arg0.toString()); |
| |
| System.out.println(c.getText()); |
| |
| boolean result = Arrays.stream(c.getItems()).anyMatch(c.getText()::equals); |
| if (!result) { |
| c.add(c.getText()); |
| c.select(c.getItemCount()); |
| } |
| } |
| |
| } |
| }); |
| |
| s.open(); |
| while (!s.isDisposed()) { |
| if (!d.readAndDispatch()) |
| d.sleep(); |
| } |
| d.dispose(); |
| } |
| |
| public static void main(String[] argv) { |
| new SWTComboExample(); |
| } |
| } |