1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-21 16:05:25 +02:00

2004-11-08 Chris Wiebe

fix for 68883
	* browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingContentProvider.java
	* browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java
This commit is contained in:
Chris Wiebe 2004-11-08 22:06:29 +00:00
parent 486c5d2c70
commit 17b5f315da
3 changed files with 350 additions and 223 deletions

View file

@ -1,3 +1,9 @@
2004-11-08 Chris Wiebe
fix for 68883
* browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingContentProvider.java
* browser/org/eclipse/cdt/internal/ui/browser/cbrowsing/CBrowsingPart.java
2004-09-15 Chris Wiebe 2004-09-15 Chris Wiebe
fixed selection fixed selection

View file

@ -14,18 +14,34 @@ import java.util.Collection;
import org.eclipse.cdt.core.browser.AllTypesCache; import org.eclipse.cdt.core.browser.AllTypesCache;
import org.eclipse.cdt.core.browser.ITypeCacheChangedListener; import org.eclipse.cdt.core.browser.ITypeCacheChangedListener;
import org.eclipse.cdt.core.browser.ITypeInfo;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ElementChangedEvent;
import org.eclipse.cdt.core.model.IArchive;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICElementVisitor;
import org.eclipse.cdt.core.model.ICModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IElementChangedListener;
import org.eclipse.cdt.core.model.ISourceRoot;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.ui.BaseCElementContentProvider; import org.eclipse.cdt.internal.ui.BaseCElementContentProvider;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer; import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
public abstract class CBrowsingContentProvider extends BaseCElementContentProvider public abstract class CBrowsingContentProvider extends BaseCElementContentProvider implements ITreeContentProvider,
implements ITreeContentProvider, ITypeCacheChangedListener { IElementChangedListener, ITypeCacheChangedListener {
public static final Object CONTENT_CANCELLED = new Object(); public static final Object CONTENT_CANCELLED = new Object();
public static final Object CONTENT_ERROR = new Object(); public static final Object CONTENT_ERROR = new Object();
public static final Object CONTENT_EMPTY = new Object(); public static final Object CONTENT_EMPTY = new Object();
@ -35,217 +51,314 @@ public abstract class CBrowsingContentProvider extends BaseCElementContentProvid
protected static final Object[] EMPTY_CHILDREN = NO_CHILDREN; protected static final Object[] EMPTY_CHILDREN = NO_CHILDREN;
protected static final Object[] INVALID_INPUT = NO_CHILDREN; protected static final Object[] INVALID_INPUT = NO_CHILDREN;
protected StructuredViewer fViewer; protected StructuredViewer fViewer;
protected Object fInput; protected Object fInput = null;
protected CBrowsingPart fBrowsingPart; protected IProject fProject = null;
protected int fReadsInDisplayThread; protected CBrowsingPart fBrowsingPart;
protected int fReadsInDisplayThread;
public CBrowsingContentProvider(CBrowsingPart browsingPart) {
fBrowsingPart= browsingPart;
fViewer= fBrowsingPart.getViewer();
AllTypesCache.addTypeCacheChangedListener(this);
}
/* (non-Javadoc) public CBrowsingContentProvider(CBrowsingPart browsingPart) {
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) fBrowsingPart = browsingPart;
*/ fViewer = fBrowsingPart.getViewer();
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { AllTypesCache.addTypeCacheChangedListener(this);
if (newInput instanceof Collection) { CoreModel.getDefault().addElementChangedListener(this);
// Get a template object from the collection }
Collection col = (Collection) newInput;
if (!col.isEmpty())
newInput = col.iterator().next();
else
newInput = null;
}
fInput = newInput;
}
/* (non-Javadoc) /*
* @see org.eclipse.jface.viewers.IContentProvider#dispose() * (non-Javadoc)
*/ *
public void dispose() { * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
AllTypesCache.removeTypeCacheChangedListener(this); * java.lang.Object, java.lang.Object)
} */
public synchronized void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
if (newInput instanceof Collection) {
// Get a template object from the collection
Collection col = (Collection) newInput;
if (!col.isEmpty())
newInput = col.iterator().next();
else
newInput = null;
}
fInput = newInput;
fProject = getProject(newInput);
}
public void typeCacheChanged(IProject project) { /*
if (project.exists() && project.isOpen()) { * (non-Javadoc)
postAdjustInputAndSetSelection(CoreModel.getDefault().create(project)); *
} else { * @see org.eclipse.jface.viewers.IContentProvider#dispose()
postAdjustInputAndSetSelection(CoreModel.getDefault().getCModel()); */
} public void dispose() {
} AllTypesCache.removeTypeCacheChangedListener(this);
CoreModel.getDefault().removeElementChangedListener(this);
/* (non-Javadoc) }
* @see org.eclipse.cdt.core.model.IElementChangedListener#elementChanged(org.eclipse.cdt.core.model.ElementChangedEvent)
public void elementChanged(ElementChangedEvent event) {
try {
processDelta(event.getDelta());
} catch(CModelException e) {
CUIPlugin.getDefault().log(e.getStatus());
}
}
protected boolean isPathEntryChange(ICElementDelta delta) { public void typeCacheChanged(IProject project) {
int flags= delta.getFlags(); if (fInput != null && (fProject == null || !fProject.isAccessible() || fProject.equals(project))) {
return (delta.getKind() == ICElementDelta.CHANGED && Object input = null;
((flags & ICElementDelta.F_BINARY_PARSER_CHANGED) != 0 || if (project != null && project.isAccessible()) {
(flags & ICElementDelta.F_ADDED_PATHENTRY_LIBRARY) != 0 || input = getNewInput(project);
(flags & ICElementDelta.F_ADDED_PATHENTRY_SOURCE) != 0 || }
(flags & ICElementDelta.F_REMOVED_PATHENTRY_LIBRARY) != 0 || postAdjustInputAndSetSelection(input);
(flags & ICElementDelta.F_PATHENTRY_REORDER) != 0 || }
(flags & ICElementDelta.F_REMOVED_PATHENTRY_SOURCE) != 0 || }
(flags & ICElementDelta.F_CHANGED_PATHENTRY_INCLUDE) != 0));
}
*//** private IProject getProject(Object input) {
* Processes a delta recursively. When more than two children are affected the if (input instanceof ICElement) {
* tree is fully refreshed starting at this node. The delta is processed in the ICProject cProj = ((ICElement) input).getCProject();
* current thread but the viewer updates are posted to the UI thread. if (cProj != null)
*//* return cProj.getProject();
protected void processDelta(ICElementDelta delta) throws CModelException { }
int kind= delta.getKind(); if (input instanceof ITypeInfo) {
int flags= delta.getFlags(); return ((ITypeInfo) input).getEnclosingProject();
ICElement element= delta.getElement(); }
return null;
}
//System.out.println("Processing " + element); private Object getNewInput(IProject project) {
if (fInput == null || fInput instanceof ICModel) {
return fInput;
}
if (fInput instanceof ICProject) {
ICProject cproject = CoreModel.getDefault().create(project);
if (!cproject.equals(fInput))
return cproject;
return fInput;
}
if (fInput instanceof ISourceRoot) {
ICProject cproject = CoreModel.getDefault().create(project);
ISourceRoot cSourceRoot = (ISourceRoot) fInput;
if (!cSourceRoot.getCProject().equals(cproject)) {
return cSourceRoot;
}
try {
ISourceRoot[] roots = cproject.getSourceRoots();
for (int i = 0; i < roots.length; ++i) {
ISourceRoot root = roots[i];
if (!(root.getResource() instanceof IProject) && root.equals(cSourceRoot)) {
return root;
}
}
} catch (CModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fInput instanceof ICElement) {
ICProject cproject = CoreModel.getDefault().create(project);
ICElement celem = (ICElement) fInput;
if (!celem.getCProject().equals(cproject)) {
return celem;
}
final ICElement[] searchInfo = new ICElement[] { celem, null };
try {
cproject.accept(new ICElementVisitor() {
public boolean visit(ICElement element) throws CoreException {
if (searchInfo[1] != null)
return false;
if (element.equals(searchInfo[0])) {
searchInfo[1] = element;
return false;
}
return true;
}
});
} catch (CoreException e) {
}
if (searchInfo[1] != null)
return searchInfo[1];
return null;
}
if (fInput instanceof ITypeInfo) {
ITypeInfo info = (ITypeInfo) fInput;
if (info.exists())
return info;
IProject infoProj = info.getEnclosingProject();
if (infoProj == null) {
return null;
}
if (!infoProj.equals(project)) {
return info;
}
ITypeInfo globalNS = AllTypesCache.getGlobalNamespace(project);
if (info.equals(globalNS))
return globalNS;
info = AllTypesCache.getType(project, info.getCElementType(), info.getQualifiedTypeName());
if (info != null) {
return info;
}
return null;
}
return null;
}
// handle open and closing of a solution or project public void elementChanged(ElementChangedEvent event) {
if (((flags & ICElementDelta.F_CLOSED) != 0) || ((flags & ICElementDelta.F_OPENED) != 0)) { try {
postRefresh(element); processDelta(event.getDelta());
} } catch (CModelException e) {
CUIPlugin.getDefault().log(e.getStatus());
}
}
if (kind == ICElementDelta.REMOVED) { protected boolean isPathEntryChange(ICElementDelta delta) {
postRemove(element); int flags = delta.getFlags();
} return (delta.getKind() == ICElementDelta.CHANGED && ((flags & ICElementDelta.F_BINARY_PARSER_CHANGED) != 0
|| (flags & ICElementDelta.F_ADDED_PATHENTRY_LIBRARY) != 0
|| (flags & ICElementDelta.F_ADDED_PATHENTRY_SOURCE) != 0
|| (flags & ICElementDelta.F_REMOVED_PATHENTRY_LIBRARY) != 0
|| (flags & ICElementDelta.F_PATHENTRY_REORDER) != 0
|| (flags & ICElementDelta.F_REMOVED_PATHENTRY_SOURCE) != 0 || (flags & ICElementDelta.F_CHANGED_PATHENTRY_INCLUDE) != 0));
}
if (kind == ICElementDelta.ADDED) { /**
Object parent= internalGetParent(element); * Processes a delta recursively. When more than two children are affected
postAdd(parent, element); * the tree is fully refreshed starting at this node. The delta is processed
} * in the current thread but the viewer updates are posted to the UI thread.
*/
protected void processDelta(ICElementDelta delta) throws CModelException {
int kind = delta.getKind();
int flags = delta.getFlags();
ICElement element = delta.getElement();
if (kind == ICElementDelta.CHANGED) { if (element instanceof ITranslationUnit && ((ITranslationUnit) element).isWorkingCopy()) {
if (element instanceof ITranslationUnit || element instanceof IBinary || element instanceof IArchive) { // ignore working copies
postRefresh(element); return;
return; }
}
}
if (isPathEntryChange(delta)) { //System.out.println("Processing " + element);
// throw the towel and do a full refresh of the affected C project.
postRefresh(element.getCProject());
}
ICElementDelta[] affectedChildren= delta.getAffectedChildren();
for (int i= 0; i < affectedChildren.length; i++) {
processDelta(affectedChildren[i]);
}
}
*/
private void postAdjustInputAndSetSelection(final Object element) {
postRunnable(new Runnable() {
public void run() {
Control ctrl= fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
ctrl.setRedraw(false);
fBrowsingPart.adjustInputPreservingSelection(element);
ctrl.setRedraw(true);
}
}
});
}
/* private void postRefresh(final Object element) {
//System.out.println("UI refresh:" + root);
postRunnable(new Runnable() {
public void run() {
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
Control ctrl= fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()){
if(element instanceof IWorkingCopy){
if(fViewer.testFindItem(element) != null){
fViewer.refresh(element);
}else {
fViewer.refresh(((IWorkingCopy)element).getOriginalElement());
}
} else {
fViewer.refresh(element);
}
}
}
});
}
private void postAdd(final Object parent, final Object element) { // handle open and closing of a solution or project
//System.out.println("UI add:" + parent + " " + element); if (((flags & ICElementDelta.F_CLOSED) != 0) || ((flags & ICElementDelta.F_OPENED) != 0)) {
postRunnable(new Runnable() { postRefresh(element);
public void run() { }
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
Control ctrl= fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()){
if(parent instanceof IWorkingCopy){
if(fViewer.testFindItem(parent) != null){
fViewer.refresh(parent);
}else {
fViewer.refresh(((IWorkingCopy)parent).getOriginalElement());
}
}else {
fViewer.refresh(parent);
}
}
}
});
}
private void postRemove(final Object element) { if (kind == ICElementDelta.REMOVED) {
//System.out.println("UI remove:" + element); postRemove(element);
postRunnable(new Runnable() { }
public void run() {
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
Control ctrl= fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
Object parent = internalGetParent(element);
if(parent instanceof IWorkingCopy){
if(fViewer.testFindItem(parent) != null){
fViewer.refresh(parent);
}else {
fViewer.refresh(((IWorkingCopy)parent).getOriginalElement());
}
}else {
fViewer.refresh(parent);
}
}
}
});
}
*/
private void postRunnable(final Runnable r) {
Control ctrl= fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
ctrl.getDisplay().asyncExec(r);
}
}
protected void startReadInDisplayThread() { if (kind == ICElementDelta.ADDED) {
if (isDisplayThread()) Object parent = internalGetParent(element);
fReadsInDisplayThread++; postAdd(parent, element);
} }
protected void finishedReadInDisplayThread() { if (kind == ICElementDelta.CHANGED) {
if (isDisplayThread()) if (element instanceof ITranslationUnit || element instanceof IBinary || element instanceof IArchive) {
fReadsInDisplayThread--; postRefresh(element);
} return;
}
private boolean isDisplayThread() { }
Control ctrl= fViewer.getControl();
if (ctrl == null) if (isPathEntryChange(delta)) {
return false; // throw the towel and do a full refresh of the affected C project.
postRefresh(element.getCProject());
Display currentDisplay= Display.getCurrent(); }
return currentDisplay != null && currentDisplay.equals(ctrl.getDisplay());
} ICElementDelta[] affectedChildren = delta.getAffectedChildren();
for (int i = 0; i < affectedChildren.length; i++) {
} processDelta(affectedChildren[i]);
}
}
private void postAdjustInputAndSetSelection(final Object input) {
postRunnable(new Runnable() {
public void run() {
Control ctrl = fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
ctrl.setRedraw(false);
fBrowsingPart.adjustInputAndPreserveSelection(input);
ctrl.setRedraw(true);
}
}
});
}
private void postRefresh(final Object element) {
//System.out.println("UI refresh:" + root);
postRunnable(new Runnable() {
public void run() {
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
Control ctrl = fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
if (element instanceof IWorkingCopy) {
if (fViewer.testFindItem(element) != null) {
fViewer.refresh(element);
} else {
fViewer.refresh(((IWorkingCopy) element).getOriginalElement());
}
} else {
fViewer.refresh(element);
}
}
}
});
}
private void postAdd(final Object parent, final Object element) {
//System.out.println("UI add:" + parent + " " + element);
postRunnable(new Runnable() {
public void run() {
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
Control ctrl = fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
if (parent instanceof IWorkingCopy) {
if (fViewer.testFindItem(parent) != null) {
fViewer.refresh(parent);
} else {
fViewer.refresh(((IWorkingCopy) parent).getOriginalElement());
}
} else {
fViewer.refresh(parent);
}
}
}
});
}
private void postRemove(final Object element) {
//System.out.println("UI remove:" + element);
postRunnable(new Runnable() {
public void run() {
// 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
Control ctrl = fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
Object parent = internalGetParent(element);
if (parent instanceof IWorkingCopy) {
if (fViewer.testFindItem(parent) != null) {
fViewer.refresh(parent);
} else {
fViewer.refresh(((IWorkingCopy) parent).getOriginalElement());
}
} else {
fViewer.refresh(parent);
}
}
}
});
}
private void postRunnable(final Runnable r) {
Control ctrl = fViewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
ctrl.getDisplay().asyncExec(r);
}
}
protected void startReadInDisplayThread() {
if (isDisplayThread())
fReadsInDisplayThread++;
}
protected void finishedReadInDisplayThread() {
if (isDisplayThread())
fReadsInDisplayThread--;
}
private boolean isDisplayThread() {
Control ctrl = fViewer.getControl();
if (ctrl == null)
return false;
Display currentDisplay = Display.getCurrent();
return currentDisplay != null && currentDisplay.equals(ctrl.getDisplay());
}
}

View file

@ -1140,29 +1140,37 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I
void restoreSelection() { void restoreSelection() {
// Default is to do nothing // Default is to do nothing
} }
void adjustInputPreservingSelection(Object input) { void adjustInputAndPreserveSelection(Object o) {
Object elementToSelect = null; Object element = getOriginalElement(o);
ISelection selection = getSelectionProvider().getSelection(); if (!(element instanceof ICElement) && !(element instanceof ITypeInfo)) {
Object oldSelObj = null; setSelection(StructuredSelection.EMPTY, true);
if (selection instanceof IStructuredSelection) { return;
oldSelObj = getSingleElementFromSelection(selection);
if (input != null) {
elementToSelect = findChildInInput(input, oldSelObj);
if (elementToSelect != null) {
adjustInputAndSetSelection(elementToSelect);
return;
}
}
} }
adjustInputAndSetSelection(input);
Object elementToSelect= getSuitableElement(findElementToSelect(element));
Object newInput= findInputForElement(element);
Object oldInput= null;
Object viewerInput = getInput();
if (viewerInput instanceof ICElement || viewerInput instanceof ITypeInfo)
oldInput = viewerInput;
if (elementToSelect == null && !isValidInput(newInput) && (newInput == null && !isAncestorOf(element, oldInput)))
// Clear input
setInput(null);
else { // if (mustSetNewInput(elementToSelect, oldInput, newInput)) {
// Adjust input to selection
setInput(newInput);
// Recompute suitable element since it depends on the viewer's input
elementToSelect= getSuitableElement(elementToSelect);
}
if (elementToSelect != null /*&& elementToSelect.exists()*/)
setSelection(new StructuredSelection(elementToSelect), true);
else
setSelection(StructuredSelection.EMPTY, true);
} }
private Object findChildInInput(Object input, Object oldSelObj) {
//TODO
return null;
}
void adjustInputAndSetSelection(Object o) { void adjustInputAndSetSelection(Object o) {
Object element = getOriginalElement(o); Object element = getOriginalElement(o);
if (!(element instanceof ICElement) && !(element instanceof ITypeInfo)) { if (!(element instanceof ICElement) && !(element instanceof ITypeInfo)) {
@ -1245,7 +1253,7 @@ public abstract class CBrowsingPart extends ViewPart implements IMenuListener, I
* @return the closest C element used as input for this part * @return the closest C element used as input for this part
*/ */
abstract protected Object findInputForElement(Object element); abstract protected Object findInputForElement(Object element);
/** /**
* Finds the element which has to be selected in this part. * Finds the element which has to be selected in this part.
* *