1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 310331 - [vm] Refactor VMViewerUpdate to avoid data duplication.

This commit is contained in:
Pawel Piech 2010-04-23 19:27:29 +00:00
parent fb54796aac
commit ecdf96f548

View file

@ -12,7 +12,6 @@ package org.eclipse.cdt.dsf.ui.viewmodel;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.cdt.dsf.concurrent.DsfExecutable; import org.eclipse.cdt.dsf.concurrent.DsfExecutable;
@ -52,31 +51,69 @@ public class VMViewerUpdate extends DsfExecutable implements IViewerUpdate {
final private IViewerUpdate fClientUpdate; final private IViewerUpdate fClientUpdate;
/** /**
* The flag indicating whether this update was cancelled. This flag is not used * Place holder for the client update. It is only used if the client update is
* if the {@link #fClientUpdate} is initialized. * not specified.
*/ */
final private AtomicBoolean fCanceled; private class ClientUpdatePlaceHolder implements IViewerUpdate {
/** ClientUpdatePlaceHolder(TreePath elementPath, Object viewerInput, IPresentationContext presentationContext)
* The viewer input object for this update. {
*/ fViewerInput = viewerInput;
final private Object fViewerInput; fElementPath = elementPath;
fPresentationContext = presentationContext;
/** }
* The element object of this update. /**
*/ * The flag indicating whether this update was cancelled. This flag is not used
final private Object fElement; * if the {@link #fClientUpdate} is initialized.
*/
/** final private AtomicBoolean fCanceled = new AtomicBoolean(false);
* The element path of this update.
*/ /**
final private TreePath fElementPath; * The viewer input object for this update.
*/
/** final private Object fViewerInput;
* The presentation context of this update.
*/ /**
final private IPresentationContext fPresentationContext; * The element path of this update.
*/
final private TreePath fElementPath;
/**
* The presentation context of this update.
*/
final private IPresentationContext fPresentationContext;
public void cancel() {
fCanceled.set(true);
}
public boolean isCanceled() {
return fCanceled.get();
}
public IPresentationContext getPresentationContext() {
return fPresentationContext;
}
public Object getElement() {
return fElementPath.getSegmentCount() != 0 ? fElementPath.getLastSegment() : fViewerInput;
}
public TreePath getElementPath() {
return fElementPath;
}
public Object getViewerInput() {
return fViewerInput;
}
public void done() { assert false; } // not used
public void setStatus(IStatus status) {assert false; } // not used
public IStatus getStatus() { assert false; return null; } // not used
}
/** /**
* Creates a viewer update based on a higher-level update. The update element * Creates a viewer update based on a higher-level update. The update element
* information as well as cancel requests are delegated to the given client * information as well as cancel requests are delegated to the given client
@ -90,13 +127,8 @@ public class VMViewerUpdate extends DsfExecutable implements IViewerUpdate {
* @param requestMonitor Call-back invoked when this update completes. * @param requestMonitor Call-back invoked when this update completes.
*/ */
public VMViewerUpdate(IViewerUpdate clientUpdate, RequestMonitor requestMonitor) { public VMViewerUpdate(IViewerUpdate clientUpdate, RequestMonitor requestMonitor) {
fViewerInput = clientUpdate.getViewerInput();
fElement = clientUpdate.getElement();
fElementPath = clientUpdate.getElementPath();
fPresentationContext = clientUpdate.getPresentationContext();
fRequestMonitor = requestMonitor; fRequestMonitor = requestMonitor;
fClientUpdate = clientUpdate; fClientUpdate = clientUpdate;
fCanceled = null;
} }
/** /**
@ -115,14 +147,9 @@ public class VMViewerUpdate extends DsfExecutable implements IViewerUpdate {
listDelta = listDelta.getParentDelta(); listDelta = listDelta.getParentDelta();
elementList.add(0, listDelta.getElement()); elementList.add(0, listDelta.getElement());
} }
fViewerInput = elementList.get(0); fClientUpdate = new ClientUpdatePlaceHolder(
fElement = elementList.get(elementList.size() - 1); new TreePath(elementList.toArray()), elementList.get(elementList.size() - 1), presentationContext);
elementList.remove(0);
fElementPath = new TreePath(elementList.toArray());
fPresentationContext = presentationContext;
fRequestMonitor = requestMonitor; fRequestMonitor = requestMonitor;
fClientUpdate = null;
fCanceled = new AtomicBoolean(false);
} }
/** /**
@ -134,56 +161,35 @@ public class VMViewerUpdate extends DsfExecutable implements IViewerUpdate {
* @param requestMonitor Call-back invoked when this update completes. * @param requestMonitor Call-back invoked when this update completes.
*/ */
public VMViewerUpdate(TreePath elementPath, Object viewerInput, IPresentationContext presentationContext, RequestMonitor requestMonitor) { public VMViewerUpdate(TreePath elementPath, Object viewerInput, IPresentationContext presentationContext, RequestMonitor requestMonitor) {
fViewerInput = viewerInput;
fElement = elementPath.getSegmentCount() != 0 ? elementPath.getLastSegment() : viewerInput;
fElementPath = elementPath;
fPresentationContext = presentationContext;
fRequestMonitor = requestMonitor; fRequestMonitor = requestMonitor;
fClientUpdate = null; fClientUpdate = new ClientUpdatePlaceHolder(elementPath, viewerInput, presentationContext);
fCanceled = new AtomicBoolean(false);
} }
protected RequestMonitor getRequestMonitor() { protected RequestMonitor getRequestMonitor() {
return fRequestMonitor; return fRequestMonitor;
} }
public Object getViewerInput() { return fViewerInput; } public Object getViewerInput() { return fClientUpdate.getViewerInput(); }
public Object getElement() { return fElement; } public Object getElement() { return fClientUpdate.getElement(); }
public TreePath getElementPath() { return fElementPath; } public TreePath getElementPath() { return fClientUpdate.getElementPath(); }
public IPresentationContext getPresentationContext() { return fPresentationContext; } public IPresentationContext getPresentationContext() { return fClientUpdate.getPresentationContext(); }
public IStatus getStatus() { return fRequestMonitor.getStatus(); } public IStatus getStatus() { return fRequestMonitor.getStatus(); }
public void setStatus(IStatus status) { fRequestMonitor.setStatus(status); } public void setStatus(IStatus status) { fRequestMonitor.setStatus(status); }
public boolean isCanceled() { public boolean isCanceled() {
if (fClientUpdate != null) { return fClientUpdate.isCanceled();
return fClientUpdate.isCanceled();
} else {
return fCanceled.get();
}
} }
public void cancel() { public void cancel() {
if (fClientUpdate != null) { fClientUpdate.cancel();
fClientUpdate.cancel();
} else {
fCanceled.set(true);
}
} }
public void done() { public void done() {
setSubmitted(); setSubmitted();
try { if ( isCanceled() ) {
if ( isCanceled() ) { fRequestMonitor.cancel();
fRequestMonitor.cancel(); fRequestMonitor.setStatus(new Status( IStatus.CANCEL, DsfUIPlugin.PLUGIN_ID," Update was cancelled") ); //$NON-NLS-1$
fRequestMonitor.setStatus(new Status( IStatus.CANCEL, DsfUIPlugin.PLUGIN_ID," Update was cancelled") ); //$NON-NLS-1$
}
fRequestMonitor.done();
} catch (RejectedExecutionException e) {
// If the request monitor cannot be invoked still, try to complete the update to avoid
// leaving the viewer in an inconsistent state.
if (fClientUpdate != null) {
fClientUpdate.done();
}
} }
fRequestMonitor.done();
} }
} }