1 11 package org.eclipse.debug.internal.ui.launchConfigurations; 12 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Vector ; 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 35 public class LaunchHistory implements ILaunchListener, ILaunchConfigurationListener { 36 37 40 private Vector fCompleteHistory = new Vector (); 41 42 45 private ILaunchGroup fGroup; 46 47 50 private Vector fFavorites = new Vector (); 51 52 56 private boolean fSaved = true; 57 58 61 private static List fgLaunchHistoryInstances = new ArrayList (); 62 63 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 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 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 100 public synchronized boolean contains(ILaunchConfiguration configuration) { 101 return fCompleteHistory.contains(configuration); 102 } 103 104 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 142 private void fireLaunchHistoryChanged() { 143 DebugUIPlugin.getDefault().getLaunchConfigurationManager().fireLaunchHistoryChanged(); 144 setSaved(false); 145 } 146 147 152 public boolean needsSaving() { 153 return !fSaved; 154 } 155 156 163 public void setSaved(boolean saved) { 164 fSaved = saved; 165 } 166 167 170 public void launchChanged(ILaunch launch) {} 171 172 175 public void launchRemoved(ILaunch launch) {} 176 177 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 198 public synchronized ILaunchConfiguration[] getHistory() { 199 Vector history = new Vector (); 200 try { 201 ILaunchConfiguration config = null; 202 for(Iterator 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 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 227 public synchronized ILaunchConfiguration[] getCompleteLaunchHistory() { 228 Vector history = new Vector (); 229 try { 230 ILaunchConfiguration config = null; 231 for(Iterator 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 249 public synchronized ILaunchConfiguration[] getFavorites() { 250 return (ILaunchConfiguration[])fFavorites.toArray(new ILaunchConfiguration[fFavorites.size()]); 251 } 252 253 258 public synchronized void setFavorites(ILaunchConfiguration[] favorites) { 259 fFavorites = new Vector (Arrays.asList(favorites)); 260 setSaved(false); 261 } 262 263 268 public synchronized void addFavorite(ILaunchConfiguration configuration) { 269 if (!fFavorites.contains(configuration)) { 270 fFavorites.add(configuration); 271 setSaved(false); 272 } 273 } 274 275 280 public ILaunchGroup getLaunchGroup() { 281 return fGroup; 282 } 283 284 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 launchCategory = null; 298 launchCategory = configuration.getCategory(); 299 String 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 314 public static void launchHistoryChanged() { 315 for(Iterator iter = fgLaunchHistoryInstances.iterator(); iter.hasNext();) { 316 ((LaunchHistory) iter.next()).resizeHistory(); 317 } 318 } 319 320 324 protected synchronized void resizeHistory() { 325 int max = getMaxHistorySize() + fFavorites.size(); 326 if (fCompleteHistory.size() > max) { 327 fCompleteHistory.setSize(max); 328 } 329 } 330 331 336 protected int getMaxHistorySize() { 337 return DebugUIPlugin.getDefault().getPreferenceStore().getInt(IDebugUIConstants.PREF_MAX_HISTORY_SIZE); 338 } 339 340 343 public void launchConfigurationAdded(ILaunchConfiguration configuration) { 344 checkFavorites(configuration); 345 } 346 347 354 protected boolean checkFavorites(ILaunchConfiguration configuration) { 355 if (configuration.isWorkingCopy()) { 357 return false; 358 } 359 try { 360 List favoriteGroups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List )null); 361 if (favoriteGroups == null) { 362 String 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 395 protected synchronized void removeFavorite(ILaunchConfiguration configuration) { 396 fFavorites.remove(configuration); 397 setSaved(false); 398 } 399 400 403 public void launchConfigurationChanged(ILaunchConfiguration configuration) { 404 checkFavorites(configuration); 405 } 406 407 410 public void launchConfigurationRemoved(ILaunchConfiguration configuration) { 411 synchronized (this) { 412 ILaunchConfiguration newConfig = DebugPlugin.getDefault().getLaunchManager().getMovedTo(configuration); 413 if (newConfig == null) { 414 fCompleteHistory.remove(configuration); 416 fFavorites.remove(configuration); 417 } else { 418 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 |