KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunch;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.ILaunchConfigurationListener;
25 import org.eclipse.debug.core.ILaunchListener;
26 import org.eclipse.debug.core.ILaunchManager;
27 import org.eclipse.debug.internal.ui.DebugUIPlugin;
28 import org.eclipse.debug.ui.IDebugUIConstants;
29 import org.eclipse.debug.ui.ILaunchGroup;
30 import org.eclipse.ui.activities.WorkbenchActivityHelper;
31
32 /**
33  * A history of launches and favorites for a launch group
34  */

35 public class LaunchHistory implements ILaunchListener, ILaunchConfigurationListener {
36
37     /**
38      * Listing of the complete launch history, which includes favorites in the launched ordering
39      */

40     private Vector JavaDoc fCompleteHistory = new Vector JavaDoc();
41     
42     /**
43      * The launch group this history is provided for
44      */

45     private ILaunchGroup fGroup;
46     
47     /**
48      * Ordered listing of the favorites of this history
49      */

50     private Vector JavaDoc fFavorites = new Vector JavaDoc();
51     
52     /**
53      * A new saved flag to prevent save participants from serializing unchanged launch histories.
54      * @since 3.3.1
55      */

56     private boolean fSaved = true;
57     
58     /**
59      * List of instances of this launch history
60      */

61     private static List JavaDoc fgLaunchHistoryInstances = new ArrayList JavaDoc();
62     
63     /**
64      * Creates a new launch history for the given launch group
65      */

66     public LaunchHistory(ILaunchGroup group) {
67         fGroup = group;
68         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
69         manager.addLaunchListener(this);
70         manager.addLaunchConfigurationListener(this);
71         fgLaunchHistoryInstances.add(this);
72     }
73     
74     /**
75      * Disposes this history
76      */

77     public void dispose() {
78         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
79         manager.removeLaunchListener(this);
80         manager.removeLaunchConfigurationListener(this);
81         fgLaunchHistoryInstances.remove(this);
82     }
83
84     /**
85      * @see org.eclipse.debug.core.ILaunchListener#launchAdded(org.eclipse.debug.core.ILaunch)
86      */

87     public void launchAdded(ILaunch launch) {
88         ILaunchConfiguration configuration = launch.getLaunchConfiguration();
89         if (configuration != null && !configuration.isWorkingCopy() && DebugUIPlugin.doLaunchConfigurationFiltering(configuration) && accepts(configuration)) {
90             addHistory(configuration, true);
91         }
92     }
93     
94     /**
95      * Returns if the current history contains the specified <code>ILaunchConfiguration</code>
96      * @param config the configuration to look for
97      * @return true if the current history contains the specified configuration, false otherwise
98      * @since 3.3
99      */

100     public synchronized boolean contains(ILaunchConfiguration configuration) {
101         return fCompleteHistory.contains(configuration);
102     }
103     
104     /**
105      * Adds the given configuration to this history
106      *
107      * @param configuration
108      * @param prepend whether the configuration should be added to the beginning of
109      * the history list
110      */

111     protected void addHistory(ILaunchConfiguration configuration, boolean prepend) {
112         synchronized (this) {
113             if(configuration.isWorkingCopy()) {
114                 return;
115             }
116             checkFavorites(configuration);
117             int index = fCompleteHistory.indexOf(configuration);
118             if(index == 0) {
119                 return;
120             }
121             if(index < 0) {
122                 if(prepend) {
123                     fCompleteHistory.add(0, configuration);
124                 }
125                 else {
126                     fCompleteHistory.add(configuration);
127                 }
128             }
129             else {
130                 fCompleteHistory.add(0, fCompleteHistory.remove(index));
131             }
132             resizeHistory();
133         }
134         fireLaunchHistoryChanged();
135     }
136
137     /**
138      * Notifies all <code>ILaunchHistoryChangedListener</code>s that the launch history has been modified
139      *
140      * @since 3.3
141      */

142     private void fireLaunchHistoryChanged() {
143         DebugUIPlugin.getDefault().getLaunchConfigurationManager().fireLaunchHistoryChanged();
144         setSaved(false);
145     }
146     
147     /**
148      * Returns if the launch history requires saving or not
149      * @return true if the history needs to be saved, false otherwise
150      * @since 3.3.1
151      */

152     public boolean needsSaving() {
153         return !fSaved;
154     }
155     
156     /**
157      * Allows the dirty flag for this launch history to be set.
158      * It is the clients of this class that must set the saved flag to true
159      * if they have persisted the launch history
160      * @param saved
161      * @since 3.3.1
162      */

163     public void setSaved(boolean saved) {
164         fSaved = saved;
165     }
166     
167     /**
168      * @see org.eclipse.debug.core.ILaunchListener#launchChanged(org.eclipse.debug.core.ILaunch)
169      */

170     public void launchChanged(ILaunch launch) {}
171
172     /**
173      * @see org.eclipse.debug.core.ILaunchListener#launchRemoved(org.eclipse.debug.core.ILaunch)
174      */

175     public void launchRemoved(ILaunch launch) {}
176
177     /**
178      * Returns the most recently launched configuration in this history, or
179      * <code>null</code> if none.
180      *
181      * @return the most recently launched configuration in this history, or
182      * <code>null</code> if none
183      */

184     public synchronized ILaunchConfiguration getRecentLaunch() {
185         ILaunchConfiguration[] history = getCompleteLaunchHistory();
186         if(history.length > 0) {
187             return history[0];
188         }
189         return null;
190     }
191     
192     /**
193      * Returns the launch configurations in this history, in most recently
194      * launched order, not including any entries from the favorites listing.
195      *
196      * @return launch history
197      */

198     public synchronized ILaunchConfiguration[] getHistory() {
199         Vector JavaDoc history = new Vector JavaDoc();
200         try {
201             ILaunchConfiguration config = null;
202             for(Iterator JavaDoc iter = fCompleteHistory.iterator(); iter.hasNext();) {
203                 config = (ILaunchConfiguration) iter.next();
204                 if(config.exists() && !fFavorites.contains(config) &&
205                         DebugUIPlugin.doLaunchConfigurationFiltering(config) &&
206                         !WorkbenchActivityHelper.filterItem(new LaunchConfigurationTypeContribution(config.getType()))) {
207                     history.add(config);
208                 }
209             }
210             //size it to the max specified history size
211
if(history.size() > getMaxHistorySize()) {
212                 history.setSize(getMaxHistorySize());
213             }
214         }
215         catch(CoreException ce) {DebugUIPlugin.log(ce);}
216         return (ILaunchConfiguration[]) history.toArray(new ILaunchConfiguration[history.size()]);
217     }
218     
219     /**
220      * Returns the complete launch history in the order they were last launched, this listing includes all
221      * entries including those from the favorites listing, but not those that have been filtered via
222      * launch configuration filtering or capabilities filtering
223      * @return the list of last launched <code>ILaunchConfiguration</code>s
224      *
225      * @since 3.3
226      */

227     public synchronized ILaunchConfiguration[] getCompleteLaunchHistory() {
228         Vector JavaDoc history = new Vector JavaDoc();
229         try {
230             ILaunchConfiguration config = null;
231             for(Iterator JavaDoc iter = fCompleteHistory.listIterator(); iter.hasNext();){
232                 config = (ILaunchConfiguration) iter.next();
233                 if(config.exists() && DebugUIPlugin.doLaunchConfigurationFiltering(config) &&
234                 !WorkbenchActivityHelper.filterItem(new LaunchConfigurationTypeContribution(config.getType()))) {
235                     history.add(config);
236                 }
237             }
238         }
239         catch (CoreException ce) {DebugUIPlugin.log(ce);}
240         return (ILaunchConfiguration[]) history.toArray(new ILaunchConfiguration[history.size()]);
241     }
242     
243     /**
244      * Returns the favorite launch configurations in this history, in the order
245      * they were created.
246      *
247      * @return launch favorites
248      */

249     public synchronized ILaunchConfiguration[] getFavorites() {
250         return (ILaunchConfiguration[])fFavorites.toArray(new ILaunchConfiguration[fFavorites.size()]);
251     }
252     
253     /**
254      * Sets this container's favorites.
255      *
256      * @param favorites
257      */

258     public synchronized void setFavorites(ILaunchConfiguration[] favorites) {
259         fFavorites = new Vector JavaDoc(Arrays.asList(favorites));
260         setSaved(false);
261     }
262     
263     /**
264      * Adds the given configuration to the favorites list.
265      *
266      * @param configuration
267      */

268     public synchronized void addFavorite(ILaunchConfiguration configuration) {
269         if (!fFavorites.contains(configuration)) {
270             fFavorites.add(configuration);
271             setSaved(false);
272         }
273     }
274     
275     /**
276      * Returns the launch group associated with this history
277      *
278      * @return group
279      */

280     public ILaunchGroup getLaunchGroup() {
281         return fGroup;
282     }
283     
284     /**
285      * Returns whether the given configuration is included in the group
286      * associated with this launch history.
287      *
288      * @param launch
289      * @return boolean
290      */

291     public boolean accepts(ILaunchConfiguration configuration) {
292         try {
293             if (!LaunchConfigurationManager.isVisible(configuration)) {
294                 return false;
295             }
296             if (configuration.getType().supportsMode(getLaunchGroup().getMode())) {
297                 String JavaDoc launchCategory = null;
298                 launchCategory = configuration.getCategory();
299                 String JavaDoc category = getLaunchGroup().getCategory();
300                 if (launchCategory == null || category == null) {
301                     return launchCategory == category;
302                 }
303                 return category.equals(launchCategory);
304             }
305         } catch (CoreException e) {
306             DebugUIPlugin.log(e);
307         }
308         return false;
309     }
310     
311     /**
312      * Notifies all launch histories that the launch history size has changed.
313      */

314     public static void launchHistoryChanged() {
315         for(Iterator JavaDoc iter = fgLaunchHistoryInstances.iterator(); iter.hasNext();) {
316             ((LaunchHistory) iter.next()).resizeHistory();
317         }
318     }
319     
320     /**
321      * The max history size has changed - remove any histories if current
322      * collection is too long.
323      */

324     protected synchronized void resizeHistory() {
325         int max = getMaxHistorySize() + fFavorites.size();
326         if (fCompleteHistory.size() > max) {
327             fCompleteHistory.setSize(max);
328         }
329     }
330
331     /**
332      * Returns the maximum number of entries allowed in this history
333      *
334      * @return the maximum number of entries allowed in this history
335      */

336     protected int getMaxHistorySize() {
337         return DebugUIPlugin.getDefault().getPreferenceStore().getInt(IDebugUIConstants.PREF_MAX_HISTORY_SIZE);
338     }
339     
340     /**
341      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration)
342      */

343     public void launchConfigurationAdded(ILaunchConfiguration configuration) {
344         checkFavorites(configuration);
345     }
346     
347     /**
348      * Adds the given config to the favorites list if it is a favorite, and
349      * returns whether the config was added to the favorites list.
350      *
351      * @param configuration
352      * @return whether added to the favorites list
353      */

354     protected boolean checkFavorites(ILaunchConfiguration configuration) {
355         // update favorites
356
if (configuration.isWorkingCopy()) {
357             return false;
358         }
359         try {
360             List JavaDoc favoriteGroups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List JavaDoc)null);
361             if (favoriteGroups == null) {
362                 // check deprecated attributes for backwards compatibility
363
String JavaDoc groupId = getLaunchGroup().getIdentifier();
364                 boolean fav = false;
365                 if (groupId.equals(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP)) {
366                     fav = configuration.getAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, false);
367                 } else if (groupId.equals(IDebugUIConstants.ID_RUN_LAUNCH_GROUP)) {
368                     fav = configuration.getAttribute(IDebugUIConstants.ATTR_RUN_FAVORITE, false);
369                 }
370                 if (fav) {
371                     addFavorite(configuration);
372                     return true;
373                 }
374                 removeFavorite(configuration);
375                 return false;
376             }
377             else if (favoriteGroups.contains(getLaunchGroup().getIdentifier())) {
378                 addFavorite(configuration);
379                 return true;
380             }
381             else {
382                 removeFavorite(configuration);
383                 return false;
384             }
385         }
386         catch (CoreException e) {}
387         return false;
388     }
389     
390     /**
391      * Removes the given config from the favorites list, if needed.
392      *
393      * @param configuration
394      */

395     protected synchronized void removeFavorite(ILaunchConfiguration configuration) {
396         fFavorites.remove(configuration);
397         setSaved(false);
398     }
399
400     /**
401      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationChanged(org.eclipse.debug.core.ILaunchConfiguration)
402      */

403     public void launchConfigurationChanged(ILaunchConfiguration configuration) {
404         checkFavorites(configuration);
405     }
406
407     /**
408      * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationRemoved(org.eclipse.debug.core.ILaunchConfiguration)
409      */

410     public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
411         synchronized (this) {
412             ILaunchConfiguration newConfig = DebugPlugin.getDefault().getLaunchManager().getMovedTo(configuration);
413             if (newConfig == null) {
414                 //deleted
415
fCompleteHistory.remove(configuration);
416                 fFavorites.remove(configuration);
417             } else {
418                 // moved/renamed
419
int index = fCompleteHistory.indexOf(configuration);
420                 if (index >= 0) {
421                     fCompleteHistory.remove(index);
422                     fCompleteHistory.add(index, newConfig);
423                 }
424                 index = fFavorites.indexOf(configuration);
425                 if (index >= 0) {
426                     fFavorites.remove(index);
427                     fFavorites.add(index, newConfig);
428                 }
429                 checkFavorites(newConfig);
430             }
431         }
432         fireLaunchHistoryChanged();
433     }
434 }
435
Popular Tags