mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-15 13:05:22 +02:00
[243263] NPE on expanding a filter
This commit is contained in:
parent
065c58167f
commit
6fa782f068
2 changed files with 188 additions and 111 deletions
|
@ -40,6 +40,7 @@ import org.eclipse.swt.graphics.Image;
|
|||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.MessageBox;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
import org.eclipse.ui.IWindowListener;
|
||||
import org.eclipse.ui.IWorkbench;
|
||||
import org.eclipse.ui.IWorkbenchWindow;
|
||||
import org.eclipse.ui.PlatformUI;
|
||||
|
@ -61,6 +62,13 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
|
|||
*/
|
||||
protected static Logger log = null;
|
||||
|
||||
/**
|
||||
* Active workbench window
|
||||
*/
|
||||
private static volatile IWorkbenchWindow activeWindow = null;
|
||||
private static volatile IWorkbenchWindow previousActiveWindow = null;
|
||||
private static IWindowListener windowListener = null;
|
||||
|
||||
// instance variables
|
||||
private Hashtable imageDescriptorRegistry = null;
|
||||
|
||||
|
@ -112,7 +120,7 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
|
|||
if (Display.getCurrent() != null) {
|
||||
return wb.getActiveWorkbenchWindow();
|
||||
}
|
||||
// otherwise, get a list of all the windows, and simply return the first one
|
||||
// otherwise, get a list of all the windows, and try to guess which one is right
|
||||
// KM: why do we need this??
|
||||
else {
|
||||
// for bug 244454, this ends up returning the wrong window
|
||||
|
@ -126,18 +134,80 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
|
|||
// will fail if we make the change now
|
||||
//
|
||||
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
|
||||
|
||||
if (windows != null && windows.length > 0) {
|
||||
return windows[0];
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
if (windows.length == 1) {
|
||||
return windows[0];
|
||||
} else {
|
||||
IWorkbenchWindow bestCandidate = windows[0];
|
||||
int candidateRank = 0;
|
||||
for (int i = 0; i < windows.length; i++) {
|
||||
if (windows[i] == activeWindow) {
|
||||
return activeWindow;
|
||||
} else if (windows[i] == previousActiveWindow) {
|
||||
//Windows get deactivated when a sub-dialog is opened or user switches
|
||||
//to another application. Such action still makes the previous window
|
||||
//the best candidate for the active one.
|
||||
bestCandidate = previousActiveWindow;
|
||||
candidateRank=10;
|
||||
} else if (windows[i].getActivePage()!= null && candidateRank==0) {
|
||||
bestCandidate = windows[i];
|
||||
candidateRank = 1;
|
||||
}
|
||||
}
|
||||
return bestCandidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class WindowListener implements IWindowListener {
|
||||
public void windowActivated(IWorkbenchWindow window) {
|
||||
activeWindow = window;
|
||||
previousActiveWindow = null; // not needed any more, allow gc
|
||||
}
|
||||
|
||||
public void windowDeactivated(IWorkbenchWindow window) {
|
||||
if (window == activeWindow) {
|
||||
previousActiveWindow = activeWindow;
|
||||
activeWindow = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void windowClosed(IWorkbenchWindow window) {
|
||||
windowDeactivated(window);
|
||||
}
|
||||
|
||||
public void windowOpened(IWorkbenchWindow window) {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addWindowListener() {
|
||||
synchronized (WindowListener.class) {
|
||||
if (windowListener == null) {
|
||||
try {
|
||||
IWorkbench wb = PlatformUI.getWorkbench();
|
||||
windowListener = new WindowListener();
|
||||
wb.addWindowListener(windowListener);
|
||||
} catch (IllegalStateException e) {
|
||||
/* will try again later when workbench becomes available */
|
||||
System.out.println("Workbench not yet available"); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void removeWindowListener() {
|
||||
synchronized (WindowListener.class) {
|
||||
if (windowListener != null) {
|
||||
try {
|
||||
IWorkbench wb = PlatformUI.getWorkbench();
|
||||
wb.removeWindowListener(windowListener);
|
||||
} finally {
|
||||
windowListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -541,12 +611,15 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
|
|||
log = LoggerFactory.getLogger(this);
|
||||
log.logInfo("Loading " + this.getClass()); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
addWindowListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
|
||||
*/
|
||||
public void stop(BundleContext context) throws Exception {
|
||||
removeWindowListener();
|
||||
logDebugMessage(this.getClass().getName(), "SHUTDOWN"); //$NON-NLS-1$
|
||||
LoggerFactory.freeLogger(this);
|
||||
super.stop(context);
|
||||
|
@ -569,6 +642,8 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
|
|||
IWorkbench wb = null;
|
||||
try {
|
||||
wb = PlatformUI.getWorkbench();
|
||||
if (windowListener == null)
|
||||
addWindowListener();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
|
|
|
@ -303,25 +303,27 @@ public class SystemFetchOperation extends JobChangeAdapter implements IRunnableW
|
|||
Object[] children = null;
|
||||
// we first test to see if this is an expand-to filter in effect for this
|
||||
// object, and if so use it...
|
||||
if (_adapter instanceof ISystemRemoteElementAdapter)
|
||||
if ((_part==null || _part instanceof SystemViewPart) && _adapter instanceof ISystemRemoteElementAdapter)
|
||||
{
|
||||
class GetExpandToFilter implements Runnable
|
||||
{
|
||||
private String expandToFilter = null;
|
||||
public void run()
|
||||
{
|
||||
// fetching part here ourselves, because right now there's no guarantee that _part will be the correct one
|
||||
// fetching part here ourselves, because the correct part can not always be determined from a background Thread
|
||||
// see bug 244454 for details
|
||||
IWorkbenchPart activePart = null;
|
||||
IWorkbenchWindow win = SystemBasePlugin.getActiveWorkbenchWindow();
|
||||
if (win != null){
|
||||
IWorkbenchPage page = win.getActivePage();
|
||||
if (page != null){
|
||||
activePart = page.getActivePart();
|
||||
if (activePart != null){
|
||||
_part = activePart;
|
||||
}
|
||||
}
|
||||
IWorkbenchPart activePart = _part;
|
||||
if (activePart==null) {
|
||||
IWorkbenchWindow win = SystemBasePlugin.getActiveWorkbenchWindow();
|
||||
if (win != null){
|
||||
IWorkbenchPage page = win.getActivePage();
|
||||
if (page != null){
|
||||
activePart = page.getActivePart();
|
||||
if (activePart != null){
|
||||
_part = activePart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (activePart instanceof SystemViewPart){
|
||||
|
|
Loading…
Add table
Reference in a new issue