Mega Code Archive

 
Categories / Java by API / Org Eclipse Swt Custom
 

New CCombo(Composite com, int style)

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; public class MainClass {   public static void main(String[] a) {     final Display d = new Display();     final Shell shell = new Shell(d);     shell.setSize(250, 200);     shell.setLayout(new FillLayout());     final int NUM = 5;     final String[] options = { "Option 1", "Option 2", "Option 3" };     final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);     table.setHeaderVisible(true);     table.setLinesVisible(true);     for (int i = 0; i < NUM; i++) {       TableColumn column = new TableColumn(table, SWT.CENTER);       column.setText("Column " + (i + 1));       column.pack();     }     TableEditor[] colorEditors = new TableEditor[NUM];     Button[] colorButtons = new Button[NUM];     for (int i = 0; i < NUM; i++) {       final TableItem item = new TableItem(table, SWT.NONE);       colorEditors[i] = new TableEditor(table);       colorButtons[i] = new Button(table, SWT.PUSH);       colorEditors[i].setEditor(colorButtons[i], item, 0);     }     final TableEditor editor = new TableEditor(table);     editor.horizontalAlignment = SWT.LEFT;     editor.grabHorizontal = true;     table.addMouseListener(new MouseAdapter() {       public void mouseDown(MouseEvent event) {         Control old = editor.getEditor();         if (old != null)           old.dispose();         Point pt = new Point(event.x, event.y);         final TableItem item = table.getItem(pt);         if (item != null) {           int column = -1;           for (int i = 0, n = table.getColumnCount(); i < n; i++) {             Rectangle rect = item.getBounds(i);             if (rect.contains(pt)) {               column = i;               break;             }           }           if (column == 1) {             final CCombo combo = new CCombo(table, SWT.READ_ONLY);             for (int i = 0, n = options.length; i < n; i++) {               combo.add(options[i]);             }             combo.select(combo.indexOf(item.getText(column)));             editor.minimumWidth = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;             table.getColumn(column).setWidth(editor.minimumWidth);             combo.setFocus();             editor.setEditor(combo, item, column);           } else if (column > 1) {             final Text text = new Text(table, SWT.NONE);             text.setForeground(item.getForeground());             text.setText(item.getText(column));             text.setForeground(item.getForeground());             text.selectAll();             text.setFocus();             editor.minimumWidth = text.getBounds().width;             editor.setEditor(text, item, column);           }         }       }     });     shell.open();     while (!shell.isDisposed()) {       if (!d.readAndDispatch())         d.sleep();     }     d.dispose();   } }