Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

TableEditor in SWT (See related posts)

This code shows some table cell editors for SWT
it's not complete, but you can get a hint

   1  
   2  import java.util.HashMap;
   3  import java.util.Random;
   4  
   5  import org.eclipse.swt.SWT;
   6  import org.eclipse.swt.custom.TableEditor;
   7  import org.eclipse.swt.events.ModifyEvent;
   8  import org.eclipse.swt.events.ModifyListener;
   9  import org.eclipse.swt.events.SelectionAdapter;
  10  import org.eclipse.swt.events.SelectionEvent;
  11  import org.eclipse.swt.graphics.Color;
  12  import org.eclipse.swt.graphics.Image;
  13  import org.eclipse.swt.graphics.Point;
  14  import org.eclipse.swt.graphics.RGB;
  15  import org.eclipse.swt.graphics.Rectangle;
  16  import org.eclipse.swt.layout.FillLayout;
  17  import org.eclipse.swt.widgets.Button;
  18  import org.eclipse.swt.widgets.ColorDialog;
  19  import org.eclipse.swt.widgets.Control;
  20  import org.eclipse.swt.widgets.Display;
  21  import org.eclipse.swt.widgets.Event;
  22  import org.eclipse.swt.widgets.Listener;
  23  import org.eclipse.swt.widgets.Shell;
  24  import org.eclipse.swt.widgets.Table;
  25  import org.eclipse.swt.widgets.TableColumn;
  26  import org.eclipse.swt.widgets.TableItem;
  27  import org.eclipse.swt.widgets.Text;
  28  
  29  public class TableTester {
  30  	
  31  	private Display display;
  32  	private Shell shell;
  33  	private HashMap<String, Image> hashImages;
  34  	private Table table;
  35  	
  36  	private Color GRAY;
  37  	private Color WHITE;
  38  	private int EDITABLECOLUMN;
  39  	
  40  	public TableTester() {
  41  		display = new Display();
  42  		shell = new Shell(display);
  43  		
  44  		init();
  45  		createGUI();
  46  		
  47  		shell.open();
  48  		
  49  		while(!shell.isDisposed()) {
  50  			if(!display.readAndDispatch()) {
  51  				display.sleep();
  52  			}
  53  		}
  54  	}
  55  	
  56  	private void init() {
  57  		hashImages = new HashMap<String, Image>();
  58  		hashImages.put("help", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\help.png"));
  59  		hashImages.put("about", new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\about_kde.png"));
  60  		
  61  		GRAY = new Color(display, 220, 220, 220);
  62  		WHITE = new Color(display, 255, 255, 255);
  63  	}
  64  	
  65  	private void createGUI() {
  66  		shell.setLayout(new FillLayout());
  67  		shell.setText("TableTester");
  68  		
  69  		shell.setImage(hashImages.get("about"));
  70  		
  71  		table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.VIRTUAL);
  72  		table.setHeaderVisible(true);
  73  		table.setLinesVisible(true);
  74  		
  75  		table.addListener (SWT.MouseDown, new Listener () {
  76  			public void handleEvent (Event event) {
  77  				Rectangle clientArea = table.getClientArea ();
  78  				Point selectedPoint = new Point (event.x, event.y);
  79  				int index = table.getTopIndex ();
  80  				while (index < table.getItemCount ()) {
  81  					boolean visible = false;
  82  					TableItem item = table.getItem (index);
  83  					for (int i=0; i < table.getColumnCount(); i++) {
  84  						Rectangle rect = item.getBounds (i);
  85  						if (rect.contains (selectedPoint)) {
  86  //							System.out.println ("Item " + index + "-" + i);
  87  							EDITABLECOLUMN = i;
  88  						}
  89  						if (!visible && rect.intersects (clientArea)) {
  90  							visible = true;
  91  						}
  92  					}
  93  					if (!visible) return;
  94  					index++;
  95  				}
  96  			}
  97  		});
  98  		
  99  		final TableEditor editor = new TableEditor(table);
 100  		//The editor must have the same size as the cell and must
 101  		//not be any smaller than 50 pixels.
 102  		editor.horizontalAlignment = SWT.LEFT;
 103  		editor.grabHorizontal = true;
 104  		editor.minimumWidth = 50;
 105  		
 106  		table.addSelectionListener(new SelectionAdapter() {
 107  			public void widgetSelected(SelectionEvent e) {
 108  				// Clean up any previous editor control
 109  				Control oldEditor = editor.getEditor();
 110  				if (oldEditor != null) oldEditor.dispose();
 111  		
 112  				// Identify the selected row
 113  				TableItem item = (TableItem)e.item;
 114  				if (item == null) {
 115  					return;
 116  				}
 117  
 118  				// The control that will be the editor must be a child of the Table
 119  				if(EDITABLECOLUMN == 0) { // boolean
 120  					if("true".equalsIgnoreCase(item.getText())) {
 121  						final Button newEditor = new Button(table, SWT.CHECK);
 122  						newEditor.setText("Click to change");
 123  						newEditor.setSelection(true);
 124  						newEditor.addSelectionListener(new SelectionAdapter() {
 125  							public void widgetSelected(SelectionEvent e) {
 126  								editor.getItem().setText("" + newEditor.getSelection());
 127  							}
 128  						});
 129  						newEditor.setFocus();
 130  						editor.setEditor(newEditor, item, EDITABLECOLUMN);
 131  					} else {
 132  						final Button newEditor = new Button(table, SWT.CHECK);
 133  						newEditor.setText("Click to change");
 134  						newEditor.setSelection(false);
 135  						newEditor.addSelectionListener(new SelectionAdapter() {
 136  							public void widgetSelected(SelectionEvent e) {
 137  								editor.getItem().setText("" + newEditor.getSelection());
 138  							}
 139  						});
 140  						newEditor.setFocus();
 141  						editor.setEditor(newEditor, item, EDITABLECOLUMN);
 142  					}
 143  				} else if(EDITABLECOLUMN == 3) { // password string
 144  					Button newEditor = new Button(table, SWT.NONE);
 145  					newEditor.setText("Generate");
 146  					newEditor.addSelectionListener(new SelectionAdapter() {
 147  						public void widgetSelected(SelectionEvent e) {
 148  							String s = Utilities.getRandomString(12, true, true, true, true);
 149  							editor.getItem().setText(EDITABLECOLUMN, s);
 150  						}
 151  					});
 152  					newEditor.setFocus();
 153  					editor.setEditor(newEditor, item, EDITABLECOLUMN);
 154  				} else if(EDITABLECOLUMN == 4) { // color
 155  					ColorDialog newEditor = new ColorDialog(shell, SWT.NONE);
 156  					String[] data = item.getText(EDITABLECOLUMN).split(",");
 157  					newEditor.setRGB(new RGB(Integer.parseInt(data[0].trim()),
 158  							Integer.parseInt(data[1].trim()),
 159  							Integer.parseInt(data[2].trim())));
 160  					RGB rgb = newEditor.open();
 161  					item.setText(EDITABLECOLUMN, rgb.red + "," + rgb.green + "," + rgb.blue);
 162  				} else {
 163  					Text newEditor = new Text(table, SWT.NONE);
 164  					newEditor.setText(item.getText(EDITABLECOLUMN));
 165  					newEditor.addModifyListener(new ModifyListener() {
 166  						public void modifyText(ModifyEvent me) {
 167  							Text text = (Text)editor.getEditor();
 168  							editor.getItem().setText(EDITABLECOLUMN, text.getText());
 169  						}
 170  					});
 171  					newEditor.selectAll();
 172  					newEditor.setFocus();
 173  					editor.setEditor(newEditor, item, EDITABLECOLUMN);
 174  				}
 175  			}
 176  		});
 177  		
 178  		fillWithData();
 179  	}
 180  	
 181  	private void fillWithData() {
 182  		TableColumn col = new TableColumn(table, SWT.NONE);
 183  		col.setText("boolean");
 184  		col.setWidth(100);
 185  		
 186  		col = new TableColumn(table, SWT.NONE);
 187  		col.setText("Integer");
 188  		col.setWidth(100);
 189  		
 190  		col = new TableColumn(table, SWT.NONE);
 191  		col.setText("Float");
 192  		col.setWidth(100);
 193  		
 194  		col = new TableColumn(table, SWT.NONE);
 195  		col.setText("Password");
 196  		col.setWidth(100);
 197  
 198  		col = new TableColumn(table, SWT.NONE);
 199  		col.setText("Color");
 200  		col.setWidth(100);
 201  		
 202  		TableItem row;
 203  		Random r = new Random(System.nanoTime());
 204  		for (int i = 0; i < 1000; i++) {
 205  			row = new TableItem(table, SWT.NONE);
 206  			row.setText(new String[] {
 207  					"" + r.nextBoolean(),
 208  					"" + r.nextInt(),
 209  					"" + r.nextFloat(),
 210  					"" + Utilities.getRandomString(12, true, true, true, true),
 211  					"" + r.nextInt(255) + "," + r.nextInt(255) + "," + r.nextInt(255)});
 212  			row.setBackground((i % 2 == 0) ? WHITE : GRAY);
 213  		}
 214  	}
 215  
 216  	public static void main(String[] args) {
 217  		new TableTester();
 218  	}
 219  }

You need to create an account or log in to post comments to this site.


Click here to browse all 5521 code snippets

Related Posts