KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > FavoritesDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.launchConfigurations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
55  * Dialog for organizing favorite launch configurations
56  */

57 public class FavoritesDialog extends TrayDialog {
58     
59     /**
60      * Table of favorite launch configurations
61      */

62     private TableViewer fFavoritesTable;
63
64     // history being organized
65
private LaunchHistory fHistory;
66     
67     // favorites collection under edit
68
private List JavaDoc fFavorites;
69
70     // buttons
71
protected Button fAddFavoriteButton;
72     protected Button fRemoveFavoritesButton;
73     protected Button fMoveUpButton;
74     protected Button fMoveDownButton;
75
76     // button action handler
77
/**
78      * Listener that delegates when a button is pressed
79      */

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     /**
96      * Listener that delegates when the selection changes in a table
97      */

98     private ISelectionChangedListener fSelectionChangedListener= new ISelectionChangedListener() {
99         public void selectionChanged(SelectionChangedEvent event) {
100             handleFavoriteSelectionChanged();
101         }
102     };
103     
104     /**
105      * Listener that delegates when a key is pressed in a table
106      */

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     /**
116      * Content provider for favorites table
117      */

118     protected class FavoritesContentProvider implements IStructuredContentProvider {
119         
120         /**
121          * @see IStructuredContentProvider#getElements(Object)
122          */

123         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
124             ILaunchConfiguration[] favorites= (ILaunchConfiguration[]) getFavorites().toArray(new ILaunchConfiguration[0]);
125             return LaunchConfigurationManager.filterConfigs(favorites);
126         }
127
128         /**
129          * @see IContentProvider#dispose()
130          */

131         public void dispose() {
132         }
133
134         /**
135          * @see IContentProvider#inputChanged(Viewer, Object, Object)
136          */

137         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
138         }
139
140     }
141     
142     /**
143      * Constructs a favorites dialog.
144      *
145      * @param parentShell shell to open the dialog on
146      * @param history launch history to edit
147      */

148     public FavoritesDialog(Shell parentShell, LaunchHistory history) {
149         super(parentShell);
150         setShellStyle(getShellStyle() | SWT.RESIZE);
151         fHistory = history;
152     }
153
154     /**
155      * The 'add config' button has been pressed
156      */

157     protected void handleAddConfigButtonSelected() {
158         SelectFavoritesDialog sfd = new SelectFavoritesDialog(fFavoritesTable.getControl().getShell(), getLaunchHistory(), getFavorites());
159         sfd.open();
160         Object JavaDoc[] 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     /**
170      * The 'remove favorites' button has been pressed
171      */

172     protected void removeSelectedFavorites() {
173         IStructuredSelection sel = (IStructuredSelection)getFavoritesTable().getSelection();
174         Iterator JavaDoc iter = sel.iterator();
175         while (iter.hasNext()) {
176             Object JavaDoc config = iter.next();
177             getFavorites().remove(config);
178         }
179         getFavoritesTable().refresh();
180     }
181     
182     /**
183      * The 'move up' button has been pressed
184      */

185     protected void handleMoveUpButtonSelected() {
186         handleMove(-1);
187     }
188     
189     /**
190      * The 'move down' button has been pressed
191      */

192     protected void handleMoveDownButtonSelected() {
193         handleMove(1);
194     }
195     
196     protected void handleMove(int direction) {
197         IStructuredSelection sel = (IStructuredSelection)getFavoritesTable().getSelection();
198         List JavaDoc selList= sel.toList();
199         Object JavaDoc[] movedFavs= new Object JavaDoc[getFavorites().size()];
200         int i;
201         for (Iterator JavaDoc favs = selList.iterator(); favs.hasNext();) {
202             Object JavaDoc 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 JavaDoc config = movedFavs[j];
211             if (config != null) {
212                 getFavorites().add(j, config);
213             }
214         }
215         
216         getFavoritesTable().refresh();
217         handleFavoriteSelectionChanged();
218     }
219     
220     /**
221      * Returns the table of favorite launch configurations.
222      *
223      * @return table viewer
224      */

225     protected TableViewer getFavoritesTable() {
226         return fFavoritesTable;
227     }
228     
229
230     /* (non-Javadoc)
231      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
232      */

233     protected Control createDialogArea(Composite parent) {
234         Composite composite = (Composite) super.createDialogArea(parent);
235         getShell().setText(MessageFormat.format(LaunchConfigurationsMessages.FavoritesDialog_1, new String JavaDoc[]{getModeLabel()}));
236         createFavoritesArea(composite);
237         return composite;
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
242      */

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     /**
250      * Returns a label to use for launch mode with accelerators removed.
251      *
252      * @return label to use for launch mode with accelerators removed
253      */

254     private String JavaDoc 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         // Create "favorite config" area
270
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     /**
281      * Creates a fully configured table with the given content provider
282      */

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     /**
299      * Creates and returns a fully configured push button in the given paren with the given label.
300      */

301     private Button createPushButton(Composite parent, String JavaDoc 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     /**
312      * Creates a fully configured composite to add buttons to.
313      */

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     /**
328      * Creates a fully configured label with the given text
329      */

330     private Label createLabel(Composite parent, String JavaDoc 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     /**
341      * Returns the current list of favorites.
342      */

343     protected List JavaDoc getFavorites() {
344         if (fFavorites == null) {
345             ILaunchConfiguration[] favs = getInitialFavorites();
346             fFavorites = new ArrayList JavaDoc(favs.length);
347             addAll(favs, fFavorites);
348         }
349         return fFavorites;
350     }
351     
352     protected LaunchHistory getLaunchHistory() {
353         return fHistory;
354     }
355     
356     /**
357      * Returns the initial content for the favorites list
358      */

359     protected ILaunchConfiguration[] getInitialFavorites() {
360         return getLaunchHistory().getFavorites();
361     }
362     
363     /**
364      * Returns the mode of this page - run or debug.
365      */

366     protected String JavaDoc getMode() {
367         return getLaunchHistory().getLaunchGroup().getMode();
368     }
369             
370     /**
371      * Copies the array into the list
372      */

373     protected void addAll(Object JavaDoc[] array, List JavaDoc list) {
374         for (int i = 0; i < array.length; i++) {
375             list.add(array[i]);
376         }
377     }
378     
379     /**
380      * Refresh all tables and buttons
381      */

382     protected void updateStatus() {
383         getFavoritesTable().refresh();
384         handleFavoriteSelectionChanged();
385     }
386
387     /**
388      * The selection in the favorites list has changed
389      */

390     protected void handleFavoriteSelectionChanged() {
391         IStructuredSelection selection = (IStructuredSelection)getFavoritesTable().getSelection();
392         List JavaDoc favs = getFavorites();
393         boolean notEmpty = !selection.isEmpty();
394         Iterator JavaDoc elements= selection.iterator();
395         boolean first= false;
396         boolean last= false;
397         int lastFav= favs.size() - 1;
398         while (elements.hasNext()) {
399             Object JavaDoc 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     /**
414      * Method performOK. Uses scheduled Job format.
415      * @since 3.2
416      */

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 JavaDoc current = getFavorites();
423                 String JavaDoc groupId = getLaunchHistory().getLaunchGroup().getIdentifier();
424                 
425                 int taskSize = Math.abs(initial.length-current.size());//get task size
426
monitor.beginTask(LaunchConfigurationsMessages.FavoritesDialog_8, taskSize);//and set it
427

428                 // removed favorites
429
for (int i = 0; i < initial.length; i++) {
430                     ILaunchConfiguration configuration = initial[i];
431                     if (!current.contains(configuration)) {
432                         // remove fav attributes
433
try {
434                             ILaunchConfigurationWorkingCopy workingCopy = configuration.getWorkingCopy();
435                             workingCopy.setAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, (String JavaDoc)null);
436                             workingCopy.setAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, (String JavaDoc)null);
437                             List JavaDoc groups = workingCopy.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List JavaDoc)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                 // update added favorites
455
Iterator JavaDoc favs = current.iterator();
456                 while (favs.hasNext()) {
457                     ILaunchConfiguration configuration = (ILaunchConfiguration)favs.next();
458                     try {
459                         List JavaDoc groups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List JavaDoc)null);
460                         if (groups == null) {
461                             groups = new ArrayList JavaDoc();
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 JavaDoc list) {
488         return (ILaunchConfiguration[])list.toArray(new ILaunchConfiguration[list.size()]);
489     }
490         
491     /* (non-Javadoc)
492      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
493      */

494     protected void okPressed() {
495         saveFavorites();
496         super.okPressed();
497     }
498     
499      /* (non-Javadoc)
500      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
501      */

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     /**
512      * Returns the name of the section that this dialog stores its settings in
513      *
514      * @return String
515      */

516     private String JavaDoc getDialogSettingsSectionName() {
517         return "FAVORITES_DIALOG_SECTION"; //$NON-NLS-1$
518
}
519 }
520
Popular Tags