1 package com.bull.eclipse.jonas.editors; 2 3 import java.util.ArrayList ; 4 import java.util.StringTokenizer ; 5 6 import org.eclipse.jface.dialogs.IDialogConstants; 7 import org.eclipse.jface.dialogs.InputDialog; 8 import org.eclipse.jface.preference.ListEditor; 9 import org.eclipse.jface.util.Assert; 10 import org.eclipse.swt.SWT; 11 import org.eclipse.swt.events.DisposeEvent; 12 import org.eclipse.swt.events.DisposeListener; 13 import org.eclipse.swt.events.SelectionAdapter; 14 import org.eclipse.swt.events.SelectionEvent; 15 import org.eclipse.swt.events.SelectionListener; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.layout.GridLayout; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.List; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.swt.widgets.Widget; 24 25 import com.bull.eclipse.jonas.JonasPluginResources; 26 27 28 public class ListFieldEditor extends ListEditor implements JonasPluginResources { 29 30 34 protected List list; 35 36 40 protected Composite buttonBox; 41 42 protected Button addButton; 43 protected Button removeButton; 44 protected Button updateButton; 45 protected Button upButton; 46 protected Button downButton; 47 48 51 protected SelectionListener selectionListener; 52 55 public ListFieldEditor() { 56 } 57 64 public ListFieldEditor(String name, String labelText, Composite parent) { 65 init(name, labelText); 66 createControl(parent); 67 } 68 69 70 73 protected void addPressed() { 74 setPresentsDefaultValue(false); 75 String input = getNewInputObject(); 76 77 if (input != null) { 78 int index = list.getSelectionIndex(); 79 if (index >= 0) 80 list.add(input, index + 1); 81 else 82 list.add(input, 0); 83 selectionChanged(); 84 } 85 } 86 87 90 protected void updatePressed() { 91 String input = getNewInputObject(); 92 93 if (input != null) { 94 int index = list.getSelectionIndex(); 95 if (index >= 0) 96 list.setItem(index, input); 97 selectionChanged(); 98 } 99 } 100 101 104 protected void adjustForNumColumns(int numColumns) { 105 Control control = getLabelControl(); 106 ((GridData) control.getLayoutData()).horizontalSpan = numColumns; 107 ((GridData) list.getLayoutData()).horizontalSpan = numColumns - 1; 108 } 109 114 protected void createButtons(Composite buttonBox) { 115 addButton = createPushButton(buttonBox, PREF_PAGE_ADDBUTTON_LABEL); removeButton = createPushButton(buttonBox, PREF_PAGE_REMOVEBUTTON_LABEL); updateButton = createPushButton(buttonBox, PREF_PAGE_UPDATEBUTTON_LABEL); upButton = createPushButton(buttonBox, PREF_PAGE_UPBUTTON_LABEL); downButton = createPushButton(buttonBox, PREF_PAGE_DOWNBUTTON_LABEL); } 121 122 128 protected Button createPushButton(Composite parent, String label) { 129 Button button = new Button(parent, SWT.PUSH); 130 button.setText(label); 131 GridData data = new GridData(GridData.FILL_HORIZONTAL); 132 data.heightHint = convertVerticalDLUsToPixels(button, IDialogConstants.BUTTON_HEIGHT); 133 int widthHint = convertHorizontalDLUsToPixels(button, IDialogConstants.BUTTON_WIDTH); 134 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 135 button.setLayoutData(data); 137 button.addSelectionListener(getSelectionListener()); 138 return button; 139 } 140 143 public void createSelectionListener() { 144 selectionListener = new SelectionAdapter() { 145 public void widgetSelected(SelectionEvent event) { 146 Widget widget = event.widget; 147 if (widget == addButton) { 148 addPressed(); 149 } else if (widget == removeButton) { 150 removePressed(); 151 } else if (widget == updateButton) { 152 updatePressed(); 153 } else if (widget == upButton) { 154 upPressed(); 155 } else if (widget == downButton) { 156 downPressed(); 157 } else if (widget == list) { 158 selectionChanged(); 159 } 160 } 161 }; 162 } 163 166 protected void doFillIntoGrid(Composite parent, int numColumns) { 167 168 Control control = getLabelControl(parent); 169 GridData gd = new GridData(); 170 gd.horizontalSpan = numColumns; 171 control.setLayoutData(gd); 172 173 list = getListControl(parent); 174 gd = new GridData(GridData.FILL_HORIZONTAL); 175 gd.verticalAlignment = GridData.FILL; 176 gd.horizontalSpan = numColumns - 1; 177 gd.horizontalAlignment = GridData.FILL; 178 gd.grabExcessHorizontalSpace = true; 179 list.setLayoutData(gd); 180 181 buttonBox = getButtonBoxControl(parent); 182 gd = new GridData(); 183 gd.verticalAlignment = GridData.BEGINNING; 184 buttonBox.setLayoutData(gd); 185 } 186 189 protected void doLoad() { 190 if (list != null) { 191 String s = getPreferenceStore().getString(getPreferenceName()); 192 String [] array = parseString(s); 193 for (int i = 0; i < array.length; i++) { 194 list.add(array[i]); 195 } 196 } 197 } 198 201 protected void doLoadDefault() { 202 if (list != null) { 203 list.removeAll(); 204 String s = getPreferenceStore().getDefaultString(getPreferenceName()); 205 String [] array = parseString(s); 206 for (int i = 0; i < array.length; i++) { 207 list.add(array[i]); 208 } 209 } 210 } 211 214 protected void doStore() { 215 String s = createList(list.getItems()); 216 if (s != null) 217 getPreferenceStore().setValue(getPreferenceName(), s); 218 } 219 222 protected void downPressed() { 223 swap(false); 224 } 225 232 public Composite getButtonBoxControl(Composite parent) { 233 if (buttonBox == null) { 234 buttonBox = new Composite(parent, SWT.NULL); 235 GridLayout layout = new GridLayout(); 236 layout.numColumns = 1; layout.marginWidth = 0; 238 layout.marginHeight = 0; 239 layout.verticalSpacing = 0; 240 buttonBox.setLayout(layout); 241 createButtons(buttonBox); 242 buttonBox.addDisposeListener(new DisposeListener() { 243 public void widgetDisposed(DisposeEvent event) { 244 addButton = null; 245 removeButton = null; 246 upButton = null; 247 downButton = null; 248 buttonBox = null; 249 } 250 }); 251 252 } else { 253 checkParent(buttonBox, parent); 254 } 255 256 selectionChanged(); 257 return buttonBox; 258 } 259 265 public List getListControl(Composite parent) { 266 if (list == null) { 267 list = new List (parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL); 268 list.addSelectionListener(getSelectionListener()); 269 list.addDisposeListener(new DisposeListener() { 270 public void widgetDisposed(DisposeEvent event) { 271 list = null; 272 } 273 }); 274 } else { 275 checkParent(list, parent); 276 } 277 return list; 278 } 279 280 283 public int getNumberOfControls() { 284 return 2; 285 } 286 292 protected SelectionListener getSelectionListener() { 293 if (selectionListener == null) 294 createSelectionListener(); 295 return selectionListener; 296 } 297 306 protected Shell getShell() { 307 if (addButton == null) 308 return null; 309 return addButton.getShell(); 310 } 311 312 315 protected void removePressed() { 316 setPresentsDefaultValue(false); 317 int index = list.getSelectionIndex(); 318 if (index >= 0) { 319 list.remove(index); 320 selectionChanged(); 321 } 322 } 323 326 protected void selectionChanged() { 327 328 int index = list.getSelectionIndex(); 329 int size = list.getItemCount(); 330 331 removeButton.setEnabled(index >= 0); 332 upButton.setEnabled(size > 1 && index > 0); 333 downButton.setEnabled(size > 1 && index >= 0 && index < size - 1); 334 } 335 338 public void setFocus() { 339 if (list != null) { 340 list.setFocus(); 341 } 342 } 343 349 protected void swap(boolean up) { 350 setPresentsDefaultValue(false); 351 int index = list.getSelectionIndex(); 352 int target = up ? index - 1 : index + 1; 353 354 if (index >= 0) { 355 String [] selection = list.getSelection(); 356 Assert.isTrue(selection.length == 1); 357 list.remove(index); 358 list.add(selection[0], target); 359 list.setSelection(target); 360 } 361 selectionChanged(); 362 } 363 366 protected void upPressed() { 367 swap(true); 368 } 369 370 381 382 protected String createList(String [] items) { 383 StringBuffer path = new StringBuffer (""); 384 385 for (int i = 0; i < items.length; i++) { 386 path.append(items[i]); 387 path.append(JonasPluginResources.PREF_PAGE_LIST_SEPARATOR); 388 } 389 return path.toString(); 390 } 391 392 400 protected String getNewInputObject() { 401 402 String defaultValue = ""; 403 if (list.getSelection().length != 0) { 404 defaultValue = list.getSelection()[0]; 405 } 406 407 InputDialog dialog = new InputDialog(getShell(), "New Jonas JVM paramater", "Enter a JVM parameter", defaultValue, null); 408 String param = null; 409 int dialogCode = dialog.open(); 410 if (dialogCode == InputDialog.OK) { 411 param = dialog.getValue(); 412 if (param != null) { 413 param = param.trim(); 414 if (param.length() == 0) 415 return null; 416 } 417 } 418 return param; 419 } 420 421 432 protected String [] parseString(String stringList) { 433 StringTokenizer st = new StringTokenizer (stringList, JonasPluginResources.PREF_PAGE_LIST_SEPARATOR); ArrayList v = new ArrayList (); 435 while (st.hasMoreElements()) { 436 v.add(st.nextElement()); 437 } 438 return (String []) v.toArray(new String [v.size()]); 439 } 440 441 } 442 | Popular Tags |