1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Bug 303808 Allow page participants for GdbFullCliConsole.

This commit borrows code from ConsoleView and ConsoleManager to allow
adding page participants to GdbFullCliConsole.

Change-Id: I4b99600bc4fba3bfecb9a4822f94e2be7d3aa601
Signed-off-by: Stephen Flynn <stephen.flynn@dell.com>
This commit is contained in:
Stephen Flynn 2016-12-13 14:10:22 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent 2c8c7a9bca
commit 576d41fe69
2 changed files with 289 additions and 4 deletions

View file

@ -11,13 +11,19 @@
package org.eclipse.cdt.debug.internal.ui.views.debuggerconsole;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.cdt.debug.ui.debuggerconsole.IDebuggerConsole;
import org.eclipse.cdt.debug.ui.debuggerconsole.IDebuggerConsoleManager;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.IViewPart;
@ -25,8 +31,12 @@ import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleListener;
import org.eclipse.ui.console.IConsolePageParticipant;
import org.eclipse.ui.internal.console.ConsolePageParticipantExtension;
import org.eclipse.ui.progress.WorkbenchJob;
/**
@ -41,6 +51,9 @@ public class DebuggerConsoleManager implements IDebuggerConsoleManager {
/** A list of listeners registered for notifications of changes to consoles */
private ListenerList<IConsoleListener> fConsoleListeners = new ListenerList<>();
/** A list of registered console page participants */
private List<ConsolePageParticipantExtension> fPageParticipants;
private OpenDebuggerConsoleViewJob fOpenDebuggerConsoleViewJob = new OpenDebuggerConsoleViewJob();
private ShowDebuggerConsoleViewJob fShowDebuggerConsoleViewJob = new ShowDebuggerConsoleViewJob();
@ -91,6 +104,34 @@ public class DebuggerConsoleManager implements IDebuggerConsoleManager {
fOpenDebuggerConsoleViewJob.schedule(100);
}
// Code for page participants borrowed from
// org.eclipse.ui.internal.console.ConsoleManager
public IConsolePageParticipant[] getPageParticipants(IDebuggerConsole console) {
if (fPageParticipants == null) {
fPageParticipants = new ArrayList<>();
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(
ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.EXTENSION_POINT_CONSOLE_PAGE_PARTICIPANTS);
IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
for (int i = 0; i < elements.length; i++) {
IConfigurationElement config = elements[i];
ConsolePageParticipantExtension extension = new ConsolePageParticipantExtension(config);
fPageParticipants.add(extension);
}
}
ArrayList<IConsolePageParticipant> list = new ArrayList<>();
for (Iterator<ConsolePageParticipantExtension> i = fPageParticipants.iterator(); i.hasNext();) {
ConsolePageParticipantExtension extension = i.next();
try {
if (extension.isEnabledFor(console)) {
list.add(extension.createDelegate());
}
} catch (CoreException e) {
CDebugUIPlugin.log(e);
}
}
return list.toArray(new IConsolePageParticipant[0]);
}
private class ShowDebuggerConsoleViewJob extends WorkbenchJob {
ShowDebuggerConsoleViewJob() {
super("Show GDB Console View"); //$NON-NLS-1$

View file

@ -7,7 +7,9 @@
*******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.views.debuggerconsole;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -15,15 +17,23 @@ import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.cdt.debug.ui.debuggerconsole.IDebuggerConsole;
import org.eclipse.cdt.debug.ui.debuggerconsole.IDebuggerConsoleManager;
import org.eclipse.cdt.debug.ui.debuggerconsole.IDebuggerConsoleView;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.IBasicPropertyConstants;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleListener;
import org.eclipse.ui.console.IConsolePageParticipant;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.part.IPage;
@ -42,11 +52,15 @@ import org.eclipse.ui.part.PageSwitcher;
* @see {@link IDebuggerConsoleManager}
*/
public class DebuggerConsoleView extends PageBookView
implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChangeListener {
implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChangeListener, IPartListener2 {
public static final String DEBUGGER_CONSOLE_VIEW_ID = "org.eclipse.cdt.debug.ui.debuggerConsoleView"; //$NON-NLS-1$
public static final String DROP_DOWN_ACTION_ID = DEBUGGER_CONSOLE_VIEW_ID + ".DebuggerConsoleDropDownAction"; //$NON-NLS-1$
/**
* Stack of consoles in MRU order
*/
private List<IDebuggerConsole> fStack = new ArrayList<>();
/** The console being displayed, or <code>null</code> if none */
private IDebuggerConsole fActiveConsole;
@ -59,6 +73,18 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
private DebuggerConsoleDropDownAction fDisplayConsoleAction;
// Code for page participants borrowed from
// org.eclipse.ui.internal.console.ConsoleView
/**
* Map of consoles to array of page participants
*/
private Map<IDebuggerConsole, ListenerList<IConsolePageParticipant>> fConsoleToPageParticipants = new HashMap<>();
/**
* Whether this view is active
*/
private boolean fActive = false;
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
@ -71,7 +97,8 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
// add as a listener for new consoles
getConsoleManager().addConsoleListener(this);
// register for part events
getViewSite().getPage().addPartListener((IPartListener2) this);
getViewSite().getActionBars().updateActionBars();
initPageSwitcher();
}
@ -85,6 +112,27 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
page.createControl(getPageBook());
console.addPropertyChangeListener(this);
// initialize page participants
IConsolePageParticipant[] consoleParticipants = ((DebuggerConsoleManager)getConsoleManager()).getPageParticipants(console);
final ListenerList<IConsolePageParticipant> participants = new ListenerList<>();
for (int i = 0; i < consoleParticipants.length; i++) {
participants.add(consoleParticipants[i]);
}
fConsoleToPageParticipants.put(console, participants);
for (IConsolePageParticipant participant : participants) {
SafeRunner.run(new ISafeRunnable() {
@Override
public void run() throws Exception {
participant.init(page, console);
}
@Override
public void handleException(Throwable exception) {
CDebugUIPlugin.log(exception);
participants.remove(participant);
}
});
}
return new PageRec(dummyPart, page);
}
@ -100,7 +148,7 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
public void dispose() {
super.dispose();
getConsoleManager().removeConsoleListener(this);
getViewSite().getPage().removePartListener((IPartListener2) this);
if (fDisplayConsoleAction != null) {
fDisplayConsoleAction.dispose();
fDisplayConsoleAction = null;
@ -141,9 +189,62 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
super.showPageRec(pageRec);
fActiveConsole = recConsole;
IDebuggerConsole topOfStack = null;
if (!fStack.isEmpty()) {
topOfStack = fStack.get(0);
}
if (topOfStack != null && !topOfStack.equals(fActiveConsole) && fActive) {
deactivateParticipants(topOfStack);
}
if (fActiveConsole != null && !fActiveConsole.equals(topOfStack)) {
fStack.remove(fActiveConsole);
fStack.add(0, fActiveConsole);
activateParticipants(fActiveConsole);
}
updateTitle();
}
/**
* Activates the participants for the given console, if any.
*
* @param console the console
*/
private void activateParticipants(IDebuggerConsole console) {
// activate
if (console != null && fActive) {
final ListenerList<IConsolePageParticipant> listeners = getParticipants(console);
if (listeners != null) {
for (IConsolePageParticipant participant : listeners) {
SafeRunner.run(new ISafeRunnable() {
@Override
public void run() throws Exception {
participant.activated();
}
@Override
public void handleException(Throwable exception) {
CDebugUIPlugin.log(exception);
listeners.remove(participant);
}
});
}
}
}
}
/**
* Returns the page participants registered for the given console, or
* <code>null</code>
*
* @param console
* the console
* @return registered page participants or <code>null</code>
*/
private ListenerList<IConsolePageParticipant> getParticipants(IDebuggerConsole console) {
return fConsoleToPageParticipants.get(console);
}
/**
* Returns a set of consoles known by the view.
*/
@ -169,10 +270,29 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
@Override
protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
IDebuggerConsole console = fPartToConsole.remove(part);
// dispose page participants
ListenerList<IConsolePageParticipant> listeners = fConsoleToPageParticipants.remove(console);
if (listeners != null) {
for (IConsolePageParticipant participant : listeners) {
SafeRunner.run(new ISafeRunnable() {
@Override
public void run() throws Exception {
participant.dispose();
}
@Override
public void handleException(Throwable exception) {
CDebugUIPlugin.log(exception);
}
});
}
}
pageRecord.page.dispose();
pageRecord.dispose();
IConsole console = fPartToConsole.remove(part);
fConsoleToPart.remove(console);
console.removePropertyChangeListener(this);
@ -236,6 +356,7 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
asyncExec(() -> {
for (IConsole console : consoles) {
if (isAvailable()) {
fStack.remove(console);
DebuggerConsoleWorkbenchPart part = fConsoleToPart.get(console);
if (part != null) {
// partClosed() will also cleanup our maps
@ -387,4 +508,127 @@ implements IConsoleView, IDebuggerConsoleView, IConsoleListener, IPropertyChange
public boolean getWordWrap() {
return false;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> key) {
Object adapter = super.getAdapter(key);
if (adapter == null) {
IDebuggerConsole console = getCurrentConsole();
if (console != null) {
ListenerList<IConsolePageParticipant> participants = getParticipants(console);
// an adapter can be asked for before the participants are created
if (participants != null) {
for (IConsolePageParticipant participant : participants) {
adapter = participant.getAdapter(key);
if (adapter != null) {
return (T) adapter;
}
}
}
}
}
return (T) adapter;
}
@Override
public void partActivated(IWorkbenchPartReference partRef) {
if (isThisPart(partRef)) {
fActive = true;
activateParticipants(fActiveConsole);
}
}
@Override
public void partBroughtToTop(IWorkbenchPartReference partRef) {
// Do nothing
}
@Override
public void partClosed(IWorkbenchPartReference partRef) {
// Do nothing
}
@Override
public void partDeactivated(IWorkbenchPartReference partRef) {
if (isThisPart(partRef)) {
fActive = false;
deactivateParticipants(fActiveConsole);
}
}
/**
* Returns if the specified part reference is to this view part (if the part
* reference is the console view or not)
*
* @param partRef
* the workbench part reference
* @return true if the specified part reference is the console view
*/
protected boolean isThisPart(IWorkbenchPartReference partRef) {
if (partRef instanceof IViewReference) {
IViewReference viewRef = (IViewReference) partRef;
if (getViewSite() != null && viewRef.getId().equals(getViewSite().getId())) {
String secId = viewRef.getSecondaryId();
String mySec = null;
if (getSite() instanceof IViewSite) {
mySec = ((IViewSite) getSite()).getSecondaryId();
}
if (mySec == null) {
return secId == null;
}
return mySec.equals(secId);
}
}
return false;
}
/**
* Deactivates participants for the given console, if any.
*
* @param console
* console to deactivate
*/
private void deactivateParticipants(IDebuggerConsole console) {
// deactivate
if (console != null) {
final ListenerList<IConsolePageParticipant> listeners = getParticipants(console);
if (listeners != null) {
for (IConsolePageParticipant participant : listeners) {
SafeRunner.run(new ISafeRunnable() {
@Override
public void run() throws Exception {
participant.deactivated();
}
@Override
public void handleException(Throwable exception) {
CDebugUIPlugin.log(exception);
listeners.remove(participant);
}
});
}
}
}
}
@Override
public void partOpened(IWorkbenchPartReference partRef) {
// Do nothing
}
@Override
public void partHidden(IWorkbenchPartReference partRef) {
// Do nothing
}
@Override
public void partVisible(IWorkbenchPartReference partRef) {
// Do nothing
}
@Override
public void partInputChanged(IWorkbenchPartReference partRef) {
// Do nothing
}
}