/**
* This file Copyright (c) 2008-2009 OnPositive Technologies, Inc.
*/
package com.onpositive.profiler.launching;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationListener;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchesListener2;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import com.onpositive.profiler.core.ISnapshot;
/**
* Launch manager.
* @author Denis Denisenko
*
*/
public class ProfilerLaunchManager
{
/**
* Launch manager listener.
* @author Denis Denisenko
*
*/
public interface ILaunchManagerListener
{
/**
* Notifies new launch is added.
* @param launchInfo - launch info.
*/
void launchAdded(LaunchInfo launchInfo);
/**
* Notifies snapshot is added.
* @param launchInfo - launch info.
* @param snapshot - snapshot.
*/
void snapshotAdded(LaunchInfo launchInfo, ISnapshot snapshot);
/**
* Notifies launch configurations are changed.
*/
void launchConfigurationsChanged();
/**
* Some launches are terminated.
*/
void launchesTerminated();
}
/**
* Snapshots.
*/
private static Map<LaunchInfo, ISnapshot> snapshots = new HashMap<LaunchInfo, ISnapshot>();
/**
* Launches.
*/
private static List<LaunchInfo> launches = new ArrayList<LaunchInfo>();
/**
* Listeners.
*/
private static Set<ILaunchManagerListener> listeners = new HashSet<ILaunchManagerListener>();
/**
* Launch configuration listener.
*/
private static ILaunchConfigurationListener launchConfigurationListener;
/**
* Launches listener.
*/
private static ILaunchesListener2 launchesListener;
/**
* Gets recent launches.
* @return recent launches.
*/
public synchronized static ILaunchConfiguration[] getRecentLaunchConfigurations()
{
return LaunchConfigurationManager.filterConfigs(internalGetHistory().getHistory());
}
/**
* Gets profiler launch configurations.
* @return profiler launch configurations.
* @throws CoreException
*/
public synchronized static ILaunchConfiguration[] getLaunchConfigurations() throws CoreException
{
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType configType =
manager.getLaunchConfigurationType(IProfilerConstants.ID_LAUNCH_CONFIGURATION_TYPE);
if (configType == null)
{
return new ILaunchConfiguration[]{};
}
return manager.getLaunchConfigurations(configType);
}
/**
* Gets launch configuration type.
* @return
*/
public static ILaunchConfigurationType getLaunchConfigurationType()
{
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
return
manager.getLaunchConfigurationType(IProfilerConstants.ID_LAUNCH_CONFIGURATION_TYPE);
}
/**
* Gets recent launches.
* @return recent launches.
*/
public synchronized static LaunchInfo[] getRecentLaunches()
{
return launches.toArray(new LaunchInfo[launches.size()]);
}
public synchronized static void registerLaunch(LaunchInfo info)
{
if (launches.contains(info))
{
launches.remove(info);
}
launches.add(0, info);
notifyLaunchAdded(info);
Display.getDefault().asyncExec(new Runnable(){
public void run() {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (activePage!=null){
activePage.setPerspective(PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId("com.onpositive.profiler.ui.perspective1"));
}
}
});
}
/**
* Registers snapshot.
* @param info - in
* @param snapshot
*/
public synchronized static void registerSnapshot(LaunchInfo info, ISnapshot snapshot)
{
if (!launches.contains(info))
{
return;
}
snapshots.put(info, snapshot);
notifySnapshotAdded(info, snapshot);
}
/**
* Removes launch.
* @param info - launch info.
*/
public synchronized static void removeLaunch(LaunchInfo info)
{
launches.remove(info);
snapshots.remove(info);
}
/**
* Notifies manager some launchers are terminated
* @param terminatedLaunches - terminated launches.
*/
public synchronized static void launchesTerminated(ILaunch[] terminatedLaunches)
{
if (terminatedLaunches == null || terminatedLaunches.length == 0)
{
return;
}
for (ILaunch launch : terminatedLaunches)
{
for (Map.Entry<LaunchInfo, ISnapshot> entry : snapshots.entrySet())
{
if (entry.getKey().getLaunch().equals(launch))
{
entry.getValue().initialize();
}
}
}
notifyLaunchesTerminated();
}
/**
* Gets launch snapshot, if any exist.
* @param info - launch.
* @return launch snapshot
*/
public static synchronized ISnapshot getSnapshot(LaunchInfo info)
{
return snapshots.get(info);
}
/**
* Activates profiler manager.
*/
public static void activate()
{
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
final ILaunchConfigurationType configType =
manager.getLaunchConfigurationType(IProfilerConstants.ID_LAUNCH_CONFIGURATION_TYPE);
if (configType == null)
{
return;
}
launchConfigurationListener = new ILaunchConfigurationListener()
{
public void launchConfigurationAdded(
ILaunchConfiguration configuration)
{
notifyLaunchConfigurationsChanged();
}
public void launchConfigurationChanged(
ILaunchConfiguration configuration)
{
notifyLaunchConfigurationsChanged();
}
public void launchConfigurationRemoved(
ILaunchConfiguration configuration)
{
notifyLaunchConfigurationsChanged();
}
};
manager.addLaunchConfigurationListener(launchConfigurationListener);
launchesListener = new ILaunchesListener2(){
public void launchesRemoved(ILaunch[] launches)
{
}
public void launchesChanged(ILaunch[] launches)
{
}
public void launchesAdded(ILaunch[] launches)
{
}
public void launchesTerminated(ILaunch[] launches)
{
if (launches == null || launches.length == 0)
{
return;
}
ProfilerLaunchManager.launchesTerminated(launches);
}
};
manager.addLaunchListener(launchesListener);
}
/**
* Disposes profiler manager.
*/
public static void dispose()
{
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
if (launchConfigurationListener != null)
{
manager.removeLaunchConfigurationListener(launchConfigurationListener);
}
if (launchesListener != null)
{
manager.removeLaunchListener(launchesListener);
}
}
public synchronized static void addListener(ILaunchManagerListener listener)
{
listeners.add(listener);
}
public synchronized static void removeListener(ILaunchManagerListener listener)
{
listeners.remove(listener);
}
/**
* Gets internal launch history.
* @return internal launch history.
*/
private static LaunchHistory internalGetHistory()
{
return getLaunchConfigurationManager().getLaunchHistory(IProfilerConstants.ID_PROFILE_LAUNCH_GROUP);
}
/**
* Gets launch configuration manager.
* @return
*/
private static LaunchConfigurationManager getLaunchConfigurationManager() {
return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
}
/**
* Notifies listeners that new launch is added.
* @param info - launch info.
*/
private static void notifyLaunchAdded(LaunchInfo info)
{
List<ILaunchManagerListener> toNotify = new ArrayList<ILaunchManagerListener>();
toNotify.addAll(listeners);
for (ILaunchManagerListener listener : listeners)
{
listener.launchAdded(info);
}
}
/**
* Notifies listeners that launch configurations are changed.
*/
private static void notifyLaunchConfigurationsChanged()
{
List<ILaunchManagerListener> toNotify = new ArrayList<ILaunchManagerListener>();
toNotify.addAll(listeners);
for (ILaunchManagerListener listener : listeners)
{
listener.launchConfigurationsChanged();
}
}
/**
* Notifies listeners that some launches are terminated.
*/
private static void notifyLaunchesTerminated()
{
List<ILaunchManagerListener> toNotify = new ArrayList<ILaunchManagerListener>();
toNotify.addAll(listeners);
for (ILaunchManagerListener listener : listeners)
{
listener.launchesTerminated();
}
}
/**
* Notifies listeners that new snapshot is added.
* @param info - launch info.
*/
private static void notifySnapshotAdded(LaunchInfo info, ISnapshot snapshot)
{
List<ILaunchManagerListener> toNotify = new ArrayList<ILaunchManagerListener>();
toNotify.addAll(listeners);
for (ILaunchManagerListener listener : listeners)
{
listener.snapshotAdded(info, snapshot);
}
}
}