1 11 package org.eclipse.debug.internal.ui.launchConfigurations; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.core.runtime.jobs.Job; 22 import org.eclipse.debug.core.ILaunchConfiguration; 23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 24 import org.eclipse.debug.internal.ui.DebugUIPlugin; 25 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 26 import org.eclipse.debug.ui.DebugUITools; 27 import org.eclipse.debug.ui.IDebugUIConstants; 28 import org.eclipse.jface.dialogs.IDialogSettings; 29 import org.eclipse.jface.dialogs.TrayDialog; 30 import org.eclipse.jface.viewers.IContentProvider; 31 import org.eclipse.jface.viewers.ISelectionChangedListener; 32 import org.eclipse.jface.viewers.IStructuredContentProvider; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.SelectionChangedEvent; 35 import org.eclipse.jface.viewers.TableViewer; 36 import org.eclipse.jface.viewers.Viewer; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.events.KeyAdapter; 39 import org.eclipse.swt.events.KeyEvent; 40 import org.eclipse.swt.events.KeyListener; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.events.SelectionEvent; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.swt.widgets.Label; 49 import org.eclipse.swt.widgets.Shell; 50 import org.eclipse.ui.PlatformUI; 51 52 import com.ibm.icu.text.MessageFormat; 53 54 57 public class FavoritesDialog extends TrayDialog { 58 59 62 private TableViewer fFavoritesTable; 63 64 private LaunchHistory fHistory; 66 67 private List fFavorites; 69 70 protected Button fAddFavoriteButton; 72 protected Button fRemoveFavoritesButton; 73 protected Button fMoveUpButton; 74 protected Button fMoveDownButton; 75 76 80 private SelectionAdapter fButtonListener= new SelectionAdapter() { 81 public void widgetSelected(SelectionEvent e) { 82 Button button = (Button) e.widget; 83 if (button == fAddFavoriteButton) { 84 handleAddConfigButtonSelected(); 85 } else if (button == fRemoveFavoritesButton) { 86 removeSelectedFavorites(); 87 } else if (button == fMoveUpButton) { 88 handleMoveUpButtonSelected(); 89 } else if (button == fMoveDownButton) { 90 handleMoveDownButtonSelected(); 91 } 92 } 93 }; 94 95 98 private ISelectionChangedListener fSelectionChangedListener= new ISelectionChangedListener() { 99 public void selectionChanged(SelectionChangedEvent event) { 100 handleFavoriteSelectionChanged(); 101 } 102 }; 103 104 107 private KeyListener fKeyListener= new KeyAdapter() { 108 public void keyPressed(KeyEvent event) { 109 if (event.character == SWT.DEL && event.stateMask == 0) { 110 removeSelectedFavorites(); 111 } 112 } 113 }; 114 115 118 protected class FavoritesContentProvider implements IStructuredContentProvider { 119 120 123 public Object [] getElements(Object inputElement) { 124 ILaunchConfiguration[] favorites= (ILaunchConfiguration[]) getFavorites().toArray(new ILaunchConfiguration[0]); 125 return LaunchConfigurationManager.filterConfigs(favorites); 126 } 127 128 131 public void dispose() { 132 } 133 134 137 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 138 } 139 140 } 141 142 148 public FavoritesDialog(Shell parentShell, LaunchHistory history) { 149 super(parentShell); 150 setShellStyle(getShellStyle() | SWT.RESIZE); 151 fHistory = history; 152 } 153 154 157 protected void handleAddConfigButtonSelected() { 158 SelectFavoritesDialog sfd = new SelectFavoritesDialog(fFavoritesTable.getControl().getShell(), getLaunchHistory(), getFavorites()); 159 sfd.open(); 160 Object [] selection = sfd.getResult(); 161 if (selection != null) { 162 for (int i = 0; i < selection.length; i++) { 163 getFavorites().add(selection[i]); 164 } 165 updateStatus(); 166 } 167 } 168 169 172 protected void removeSelectedFavorites() { 173 IStructuredSelection sel = (IStructuredSelection)getFavoritesTable().getSelection(); 174 Iterator iter = sel.iterator(); 175 while (iter.hasNext()) { 176 Object config = iter.next(); 177 getFavorites().remove(config); 178 } 179 getFavoritesTable().refresh(); 180 } 181 182 185 protected void handleMoveUpButtonSelected() { 186 handleMove(-1); 187 } 188 189 192 protected void handleMoveDownButtonSelected() { 193 handleMove(1); 194 } 195 196 protected void handleMove(int direction) { 197 IStructuredSelection sel = (IStructuredSelection)getFavoritesTable().getSelection(); 198 List selList= sel.toList(); 199 Object [] movedFavs= new Object [getFavorites().size()]; 200 int i; 201 for (Iterator favs = selList.iterator(); favs.hasNext();) { 202 Object config = favs.next(); 203 i= getFavorites().indexOf(config); 204 movedFavs[i + direction]= config; 205 } 206 207 getFavorites().removeAll(selList); 208 209 for (int j = 0; j < movedFavs.length; j++) { 210 Object config = movedFavs[j]; 211 if (config != null) { 212 getFavorites().add(j, config); 213 } 214 } 215 216 getFavoritesTable().refresh(); 217 handleFavoriteSelectionChanged(); 218 } 219 220 225 protected TableViewer getFavoritesTable() { 226 return fFavoritesTable; 227 } 228 229 230 233 protected Control createDialogArea(Composite parent) { 234 Composite composite = (Composite) super.createDialogArea(parent); 235 getShell().setText(MessageFormat.format(LaunchConfigurationsMessages.FavoritesDialog_1, new String []{getModeLabel()})); 236 createFavoritesArea(composite); 237 return composite; 238 } 239 240 243 protected Control createContents(Composite parent) { 244 Control contents = super.createContents(parent); 245 PlatformUI.getWorkbench().getHelpSystem().setHelp(getDialogArea(), IDebugHelpContextIds.ORGANIZE_FAVORITES_DIALOG); 246 return contents; 247 } 248 249 254 private String getModeLabel() { 255 return DebugUIPlugin.removeAccelerators(fHistory.getLaunchGroup().getLabel()); 256 } 257 258 protected void createFavoritesArea(Composite parent) { 259 Composite topComp = new Composite(parent, SWT.NULL); 260 GridLayout layout = new GridLayout(); 261 layout.marginHeight = 0; 262 layout.marginWidth = 0; 263 layout.numColumns = 2; 264 topComp.setLayout(layout); 265 GridData gd = new GridData(GridData.FILL_BOTH); 266 topComp.setLayoutData(gd); 267 topComp.setFont(parent.getFont()); 268 269 createLabel(topComp, LaunchConfigurationsMessages.FavoritesDialog_2); 271 fFavoritesTable = createTable(topComp, new FavoritesContentProvider()); 272 Composite buttonComp = createButtonComposite(topComp); 273 fAddFavoriteButton = createPushButton(buttonComp,LaunchConfigurationsMessages.FavoritesDialog_3); 274 fAddFavoriteButton.setEnabled(true); 275 fRemoveFavoritesButton = createPushButton(buttonComp, LaunchConfigurationsMessages.FavoritesDialog_4); 276 fMoveUpButton = createPushButton(buttonComp, LaunchConfigurationsMessages.FavoritesDialog_5); 277 fMoveDownButton = createPushButton(buttonComp, LaunchConfigurationsMessages.FavoritesDialog_6); 278 } 279 280 283 private TableViewer createTable(Composite parent, IContentProvider contentProvider) { 284 TableViewer tableViewer= new TableViewer(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); 285 tableViewer.setLabelProvider(DebugUITools.newDebugModelPresentation()); 286 tableViewer.setContentProvider(contentProvider); 287 tableViewer.setInput(DebugUIPlugin.getDefault()); 288 GridData gd = new GridData(GridData.FILL_BOTH); 289 gd.widthHint = 100; 290 gd.heightHint = 100; 291 tableViewer.getTable().setLayoutData(gd); 292 tableViewer.getTable().setFont(parent.getFont()); 293 tableViewer.addSelectionChangedListener(fSelectionChangedListener); 294 tableViewer.getControl().addKeyListener(fKeyListener); 295 return tableViewer; 296 } 297 298 301 private Button createPushButton(Composite parent, String label) { 302 Button button = new Button(parent, SWT.PUSH); 303 button.setText(label); 304 button.setFont(parent.getFont()); 305 setButtonLayoutData(button); 306 button.addSelectionListener(fButtonListener); 307 button.setEnabled(false); 308 return button; 309 } 310 311 314 private Composite createButtonComposite(Composite parent) { 315 Composite composite = new Composite(parent, SWT.NONE); 316 GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 317 composite.setLayoutData(gd); 318 GridLayout layout = new GridLayout(); 319 layout.marginHeight = 0; 320 layout.marginWidth = 0; 321 layout.numColumns = 1; 322 composite.setLayout(layout); 323 composite.setFont(parent.getFont()); 324 return composite; 325 } 326 327 330 private Label createLabel(Composite parent, String labelText) { 331 Label label = new Label(parent, SWT.LEFT); 332 label.setText(labelText); 333 GridData gd = new GridData(); 334 gd.horizontalSpan = 2; 335 label.setLayoutData(gd); 336 label.setFont(parent.getFont()); 337 return label; 338 } 339 340 343 protected List getFavorites() { 344 if (fFavorites == null) { 345 ILaunchConfiguration[] favs = getInitialFavorites(); 346 fFavorites = new ArrayList (favs.length); 347 addAll(favs, fFavorites); 348 } 349 return fFavorites; 350 } 351 352 protected LaunchHistory getLaunchHistory() { 353 return fHistory; 354 } 355 356 359 protected ILaunchConfiguration[] getInitialFavorites() { 360 return getLaunchHistory().getFavorites(); 361 } 362 363 366 protected String getMode() { 367 return getLaunchHistory().getLaunchGroup().getMode(); 368 } 369 370 373 protected void addAll(Object [] array, List list) { 374 for (int i = 0; i < array.length; i++) { 375 list.add(array[i]); 376 } 377 } 378 379 382 protected void updateStatus() { 383 getFavoritesTable().refresh(); 384 handleFavoriteSelectionChanged(); 385 } 386 387 390 protected void handleFavoriteSelectionChanged() { 391 IStructuredSelection selection = (IStructuredSelection)getFavoritesTable().getSelection(); 392 List favs = getFavorites(); 393 boolean notEmpty = !selection.isEmpty(); 394 Iterator elements= selection.iterator(); 395 boolean first= false; 396 boolean last= false; 397 int lastFav= favs.size() - 1; 398 while (elements.hasNext()) { 399 Object element = elements.next(); 400 if(!first && favs.indexOf(element) == 0) { 401 first= true; 402 } 403 if (!last && favs.indexOf(element) == lastFav) { 404 last= true; 405 } 406 } 407 408 fRemoveFavoritesButton.setEnabled(notEmpty); 409 fMoveUpButton.setEnabled(notEmpty && !first); 410 fMoveDownButton.setEnabled(notEmpty && !last); 411 } 412 413 417 public void saveFavorites() { 418 419 final Job job = new Job(LaunchConfigurationsMessages.FavoritesDialog_8) { 420 protected IStatus run(IProgressMonitor monitor) { 421 ILaunchConfiguration[] initial = getInitialFavorites(); 422 List current = getFavorites(); 423 String groupId = getLaunchHistory().getLaunchGroup().getIdentifier(); 424 425 int taskSize = Math.abs(initial.length-current.size()); monitor.beginTask(LaunchConfigurationsMessages.FavoritesDialog_8, taskSize); 428 for (int i = 0; i < initial.length; i++) { 430 ILaunchConfiguration configuration = initial[i]; 431 if (!current.contains(configuration)) { 432 try { 434 ILaunchConfigurationWorkingCopy workingCopy = configuration.getWorkingCopy(); 435 workingCopy.setAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, (String )null); 436 workingCopy.setAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, (String )null); 437 List groups = workingCopy.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List )null); 438 if (groups != null) { 439 groups.remove(groupId); 440 if (groups.isEmpty()) { 441 groups = null; 442 } 443 workingCopy.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, groups); 444 } 445 workingCopy.doSave(); 446 } catch (CoreException e) { 447 DebugUIPlugin.log(e); 448 return Status.CANCEL_STATUS; 449 } 450 } 451 monitor.worked(1); 452 } 453 454 Iterator favs = current.iterator(); 456 while (favs.hasNext()) { 457 ILaunchConfiguration configuration = (ILaunchConfiguration)favs.next(); 458 try { 459 List groups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List )null); 460 if (groups == null) { 461 groups = new ArrayList (); 462 } 463 if (!groups.contains(groupId)) { 464 groups.add(groupId); 465 ILaunchConfigurationWorkingCopy workingCopy = configuration.getWorkingCopy(); 466 workingCopy.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, groups); 467 workingCopy.doSave(); 468 } 469 } catch (CoreException e) { 470 DebugUIPlugin.log(e); 471 return Status.CANCEL_STATUS; 472 } 473 monitor.worked(1); 474 } 475 476 fHistory.setFavorites(getArray(current)); 477 monitor.done(); 478 return Status.OK_STATUS; 479 } 480 }; 481 job.setPriority(Job.LONG); 482 PlatformUI.getWorkbench().getProgressService().showInDialog(getParentShell(), job); 483 job.schedule(); 484 485 } 486 487 protected ILaunchConfiguration[] getArray(List list) { 488 return (ILaunchConfiguration[])list.toArray(new ILaunchConfiguration[list.size()]); 489 } 490 491 494 protected void okPressed() { 495 saveFavorites(); 496 super.okPressed(); 497 } 498 499 502 protected IDialogSettings getDialogBoundsSettings() { 503 IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings(); 504 IDialogSettings section = settings.getSection(getDialogSettingsSectionName()); 505 if (section == null) { 506 section = settings.addNewSection(getDialogSettingsSectionName()); 507 } 508 return section; 509 } 510 511 516 private String getDialogSettingsSectionName() { 517 return "FAVORITES_DIALOG_SECTION"; } 519 } 520 | Popular Tags |