1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.jface.dialogs.IDialogConstants; 14 import org.eclipse.jface.resource.JFaceResources; 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.DisposeEvent; 18 import org.eclipse.swt.events.DisposeListener; 19 import org.eclipse.swt.events.SelectionAdapter; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.events.SelectionListener; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.List; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.Widget; 30 31 42 public abstract class ListEditor extends FieldEditor { 43 44 48 private List list; 49 50 54 private Composite buttonBox; 55 56 59 private Button addButton; 60 61 64 private Button removeButton; 65 66 69 private Button upButton; 70 71 74 private Button downButton; 75 76 79 private SelectionListener selectionListener; 80 81 84 protected ListEditor() { 85 } 86 87 94 protected ListEditor(String name, String labelText, Composite parent) { 95 init(name, labelText); 96 createControl(parent); 97 } 98 99 102 private void addPressed() { 103 setPresentsDefaultValue(false); 104 String input = getNewInputObject(); 105 106 if (input != null) { 107 int index = list.getSelectionIndex(); 108 if (index >= 0) { 109 list.add(input, index + 1); 110 } else { 111 list.add(input, 0); 112 } 113 selectionChanged(); 114 } 115 } 116 117 120 protected void adjustForNumColumns(int numColumns) { 121 Control control = getLabelControl(); 122 ((GridData) control.getLayoutData()).horizontalSpan = numColumns; 123 ((GridData) list.getLayoutData()).horizontalSpan = numColumns - 1; 124 } 125 126 131 private void createButtons(Composite box) { 132 addButton = createPushButton(box, "ListEditor.add"); removeButton = createPushButton(box, "ListEditor.remove"); upButton = createPushButton(box, "ListEditor.up"); downButton = createPushButton(box, "ListEditor.down"); } 137 138 149 protected abstract String createList(String [] items); 150 151 158 private Button createPushButton(Composite parent, String key) { 159 Button button = new Button(parent, SWT.PUSH); 160 button.setText(JFaceResources.getString(key)); 161 button.setFont(parent.getFont()); 162 GridData data = new GridData(GridData.FILL_HORIZONTAL); 163 int widthHint = convertHorizontalDLUsToPixels(button, 164 IDialogConstants.BUTTON_WIDTH); 165 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, 166 SWT.DEFAULT, true).x); 167 button.setLayoutData(data); 168 button.addSelectionListener(getSelectionListener()); 169 return button; 170 } 171 172 175 public void createSelectionListener() { 176 selectionListener = new SelectionAdapter() { 177 public void widgetSelected(SelectionEvent event) { 178 Widget widget = event.widget; 179 if (widget == addButton) { 180 addPressed(); 181 } else if (widget == removeButton) { 182 removePressed(); 183 } else if (widget == upButton) { 184 upPressed(); 185 } else if (widget == downButton) { 186 downPressed(); 187 } else if (widget == list) { 188 selectionChanged(); 189 } 190 } 191 }; 192 } 193 194 197 protected void doFillIntoGrid(Composite parent, int numColumns) { 198 Control control = getLabelControl(parent); 199 GridData gd = new GridData(); 200 gd.horizontalSpan = numColumns; 201 control.setLayoutData(gd); 202 203 list = getListControl(parent); 204 gd = new GridData(GridData.FILL_HORIZONTAL); 205 gd.verticalAlignment = GridData.FILL; 206 gd.horizontalSpan = numColumns - 1; 207 gd.grabExcessHorizontalSpace = true; 208 list.setLayoutData(gd); 209 210 buttonBox = getButtonBoxControl(parent); 211 gd = new GridData(); 212 gd.verticalAlignment = GridData.BEGINNING; 213 buttonBox.setLayoutData(gd); 214 } 215 216 219 protected void doLoad() { 220 if (list != null) { 221 String s = getPreferenceStore().getString(getPreferenceName()); 222 String [] array = parseString(s); 223 for (int i = 0; i < array.length; i++) { 224 list.add(array[i]); 225 } 226 } 227 } 228 229 232 protected void doLoadDefault() { 233 if (list != null) { 234 list.removeAll(); 235 String s = getPreferenceStore().getDefaultString( 236 getPreferenceName()); 237 String [] array = parseString(s); 238 for (int i = 0; i < array.length; i++) { 239 list.add(array[i]); 240 } 241 } 242 } 243 244 247 protected void doStore() { 248 String s = createList(list.getItems()); 249 if (s != null) { 250 getPreferenceStore().setValue(getPreferenceName(), s); 251 } 252 } 253 254 257 private void downPressed() { 258 swap(false); 259 } 260 261 268 public Composite getButtonBoxControl(Composite parent) { 269 if (buttonBox == null) { 270 buttonBox = new Composite(parent, SWT.NULL); 271 GridLayout layout = new GridLayout(); 272 layout.marginWidth = 0; 273 buttonBox.setLayout(layout); 274 createButtons(buttonBox); 275 buttonBox.addDisposeListener(new DisposeListener() { 276 public void widgetDisposed(DisposeEvent event) { 277 addButton = null; 278 removeButton = null; 279 upButton = null; 280 downButton = null; 281 buttonBox = null; 282 } 283 }); 284 285 } else { 286 checkParent(buttonBox, parent); 287 } 288 289 selectionChanged(); 290 return buttonBox; 291 } 292 293 299 public List getListControl(Composite parent) { 300 if (list == null) { 301 list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL 302 | SWT.H_SCROLL); 303 list.setFont(parent.getFont()); 304 list.addSelectionListener(getSelectionListener()); 305 list.addDisposeListener(new DisposeListener() { 306 public void widgetDisposed(DisposeEvent event) { 307 list = null; 308 } 309 }); 310 } else { 311 checkParent(list, parent); 312 } 313 return list; 314 } 315 316 324 protected abstract String getNewInputObject(); 325 326 329 public int getNumberOfControls() { 330 return 2; 331 } 332 333 339 private SelectionListener getSelectionListener() { 340 if (selectionListener == null) { 341 createSelectionListener(); 342 } 343 return selectionListener; 344 } 345 346 355 protected Shell getShell() { 356 if (addButton == null) { 357 return null; 358 } 359 return addButton.getShell(); 360 } 361 362 373 protected abstract String [] parseString(String stringList); 374 375 378 private void removePressed() { 379 setPresentsDefaultValue(false); 380 int index = list.getSelectionIndex(); 381 if (index >= 0) { 382 list.remove(index); 383 selectionChanged(); 384 } 385 } 386 387 390 private void selectionChanged() { 391 392 int index = list.getSelectionIndex(); 393 int size = list.getItemCount(); 394 395 removeButton.setEnabled(index >= 0); 396 upButton.setEnabled(size > 1 && index > 0); 397 downButton.setEnabled(size > 1 && index >= 0 && index < size - 1); 398 } 399 400 403 public void setFocus() { 404 if (list != null) { 405 list.setFocus(); 406 } 407 } 408 409 415 private void swap(boolean up) { 416 setPresentsDefaultValue(false); 417 int index = list.getSelectionIndex(); 418 int target = up ? index - 1 : index + 1; 419 420 if (index >= 0) { 421 String [] selection = list.getSelection(); 422 Assert.isTrue(selection.length == 1); 423 list.remove(index); 424 list.add(selection[0], target); 425 list.setSelection(target); 426 } 427 selectionChanged(); 428 } 429 430 433 private void upPressed() { 434 swap(true); 435 } 436 437 440 public void setEnabled(boolean enabled, Composite parent) { 441 super.setEnabled(enabled, parent); 442 getListControl(parent).setEnabled(enabled); 443 addButton.setEnabled(enabled); 444 removeButton.setEnabled(enabled); 445 upButton.setEnabled(enabled); 446 downButton.setEnabled(enabled); 447 } 448 } 449 | Popular Tags |