1 4 package org.hibernate.eclipse.console.wizards; 5 6 import java.util.Iterator ; 7 8 import org.eclipse.jface.viewers.IStructuredSelection; 9 import org.eclipse.jface.viewers.TableViewer; 10 import org.eclipse.swt.SWT; 11 import org.eclipse.swt.events.SelectionAdapter; 12 import org.eclipse.swt.events.SelectionEvent; 13 import org.eclipse.swt.events.SelectionListener; 14 import org.eclipse.swt.graphics.Font; 15 import org.eclipse.swt.layout.GridData; 16 import org.eclipse.swt.layout.GridLayout; 17 import org.eclipse.swt.widgets.Button; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Group; 20 import org.eclipse.swt.widgets.Label; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.swt.widgets.Table; 23 import org.eclipse.swt.widgets.TableItem; 24 25 29 public abstract class UpDownList { 30 31 private SelectionListener buttonListener= new SelectionAdapter() { 32 public void widgetSelected(SelectionEvent e) { 33 handleButtonPressed((Button) e.widget); 34 } 35 }; 36 37 private Button[] addButton; 38 private Button removeButton; 39 private Button upButton; 40 private Button downButton; 41 private TableViewer tableView; 42 private Shell shell; 43 44 private final Composite parent; 45 private final String title; 46 47 public UpDownList(Composite parent, Shell shell2, String title) { 48 this.parent = parent; 49 this.shell = shell2; 50 this.title = title; 51 build(); 52 } 53 54 void build() { 55 56 Font font = parent.getFont(); 57 58 Group topLevel = new Group(parent, SWT.NONE); 59 topLevel.setText(title); 60 GridData gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 61 gd.verticalSpan = 3; 62 gd.horizontalSpan = 3; 63 64 topLevel.setLayoutData(gd); 65 GridLayout layout = new GridLayout(); 66 67 layout.marginHeight = 0; 68 layout.marginWidth = 0; 69 layout.numColumns = 3; 70 topLevel.setLayout(layout); 71 72 tableView = new TableViewer(topLevel, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); 74 76 Table builderTable= tableView.getTable(); 77 builderTable.setLayoutData(new GridData(GridData.FILL_BOTH)); 78 builderTable.setFont(font); 79 builderTable.addSelectionListener(new SelectionAdapter() { 80 public void widgetSelected(SelectionEvent e) { 81 handleTableSelectionChanged(); 82 } 83 }); 84 85 Composite buttonArea = new Composite(topLevel, SWT.NONE); 87 layout = new GridLayout(); 88 layout.marginHeight = 0; 89 layout.marginWidth = 0; 90 buttonArea.setLayout(layout); 91 buttonArea.setFont(font); 92 buttonArea.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 93 94 String [] addButtonLabels = getAddButtonLabels(); 95 addButton = new Button[addButtonLabels.length]; 96 for (int i = 0; i < addButtonLabels.length; i++) { 97 String label = addButtonLabels[i]; 98 addButton[i] = createButton(buttonArea, label); addButton[i].setEnabled(true); 100 } 101 removeButton = createButton(buttonArea, "Remove"); new Label(buttonArea, SWT.LEFT); 103 upButton = createButton(buttonArea, "Up"); downButton = createButton(buttonArea, "Down"); 105 106 107 108 111 } 112 113 protected String [] getAddButtonLabels() { 114 return new String [] { "Add..." }; 115 } 116 117 120 private void handleButtonPressed(Button button) { 121 if (button == removeButton) { 122 handleRemoveButtonPressed(tableView); 123 } else if (button == upButton) { 124 moveSelectionUp(tableView); 125 } else if (button == downButton) { 126 moveSelectionDown(tableView); 127 } else { 128 for (int i = 0; i < addButton.length; i++) { 129 Button but = addButton[i]; 130 if(button == but) { 131 handleAddButtonPressed(i); 132 } 133 } 134 } 135 handleTableSelectionChanged(); 136 tableView.getTable().setFocus(); 137 138 } 139 140 private void moveSelectionDown(TableViewer viewer) { 141 Table table = viewer.getTable(); 142 int indices[]= table.getSelectionIndices(); 143 if (indices.length < 1) { 144 return; 145 } 146 int newSelection[]= new int[indices.length]; 147 int max= table.getItemCount() - 1; 148 for (int i = indices.length - 1; i >= 0; i--) { 149 int index= indices[i]; 150 if (index < max) { 151 move (viewer, table.getItem(index), index + 1); 152 newSelection[i]= index + 1; 153 } 154 } 155 table.setSelection(newSelection); 156 } 157 158 private void moveSelectionUp(TableViewer viewer) { 159 Table table = viewer.getTable(); 160 int indices[]= table.getSelectionIndices(); 161 int newSelection[]= new int[indices.length]; 162 for (int i = 0; i < indices.length; i++) { 163 int index= indices[i]; 164 if (index > 0) { 165 move (viewer, table.getItem(index), index - 1); 166 newSelection[i]= index - 1; 167 } 168 } 169 table.setSelection(newSelection); 170 } 171 172 175 private void move(TableViewer viewer, TableItem item, int index) { 176 Object data = item.getData(); 177 item.dispose(); 178 viewer.insert(data, index); 179 } 180 181 private void handleRemoveButtonPressed(TableViewer viewer) { 182 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); 183 if (selection != null) { 184 int numSelected= selection.size(); 185 186 Iterator iterator= selection.iterator(); 187 while (iterator.hasNext()) { 188 Object item= iterator.next(); 189 viewer.remove(item); 190 } 191 listChanged(); 192 } 193 } 194 195 private void handleAddButtonPressed(int i) { 196 Object [] o = handleAdd(i); 197 if(o!=null) { 198 add(o,true); 199 } 200 } 201 202 public void add(Object [] o, boolean notify) { 203 tableView.add(o); 204 if (notify) listChanged(); 205 } 206 207 abstract protected void listChanged(); 208 209 abstract protected Object [] handleAdd(int i); 210 211 protected Shell getShell() { 212 return shell; 213 } 214 215 219 private void handleTableSelectionChanged() { 220 for (int i = 0; i < addButton.length; i++) { 221 addButton[i].setEnabled(true); 222 } 223 Table builderTable= tableView.getTable(); 224 TableItem[] items = builderTable.getSelection(); 225 boolean validSelection= items != null && items.length > 0; 226 boolean enableEdit= validSelection; 227 boolean enableRemove= validSelection; 228 boolean enableUp= validSelection; 229 boolean enableDown= validSelection; 230 if (validSelection) { 231 int indices[]= builderTable.getSelectionIndices(); 232 int max = builderTable.getItemCount(); 233 enableUp= indices[0] != 0; 234 enableDown= indices[indices.length - 1] < max - 1; 235 enableRemove = true; 236 } 237 removeButton.setEnabled(enableRemove); 238 upButton.setEnabled(enableUp); 239 downButton.setEnabled(enableDown); 240 } 241 242 245 private Button createButton(Composite parent, String label) { 246 Button button = new Button(parent, SWT.PUSH); 247 GridData data = new GridData(); 248 data.horizontalAlignment = GridData.FILL; 251 253 button.setLayoutData(data); 254 button.setFont(parent.getFont()); 255 button.setText(label); 256 button.setEnabled(false); 257 button.addSelectionListener(buttonListener); 258 return button; 259 } 260 261 public Table getTable() { 262 return tableView.getTable(); 263 } 264 265 266 } 267 | Popular Tags |