Skip to content

Commit

Permalink
progress reporting and cancel support on iterator
Browse files Browse the repository at this point in the history
Extended the iterator reachable from the moleculesmanager to support cancel (it seems to work sometimes and sometimes not) and to also give some progress report and time estimate.
  • Loading branch information
jonalv committed May 23, 2012
1 parent b5e16dc commit 4a81e15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
import org.openscience.cdk.Molecule;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void createSDFIndex( IFile file,
IProgressMonitor monitor ) {

returner.completeReturn(
new SDFIndexEditorModel(createIndex( file, monitor ) ) );
new SDFIndexEditorModel(createIndex( file, monitor ), monitor ) );

}

Expand Down Expand Up @@ -381,7 +382,7 @@ private SDFIndexEditorModel saveSDF(IMoleculesEditorModel model, IFile file,
SDFileIndex index=
createIndex( file, subMonitor.newChild( 1000 ) );
if(index!=null) {
sdfModel = new SDFIndexEditorModel(index);
sdfModel = new SDFIndexEditorModel(index, new NullProgressMonitor());
}else
throw new BioclipseException("Failed to create new index");
} catch ( CoreException e1 ) {
Expand Down Expand Up @@ -499,7 +500,7 @@ public void calculatePropertiesFor( IFile file,
}

SDFileIndex index = createIndex( file, progress.newChild( 1000 ) );
SDFIndexEditorModel model = new SDFIndexEditorModel(index);
SDFIndexEditorModel model = new SDFIndexEditorModel(index, new NullProgressMonitor());

IFile target = null;
SubMonitor loopProgress = progress.newChild( 8000 );
Expand Down
Expand Up @@ -31,6 +31,7 @@
import net.bioclipse.cdk.ui.views.IFileMoleculesEditorModel;
import net.bioclipse.core.domain.IMolecule.Property;
import net.bioclipse.core.util.LogUtils;
import net.bioclipse.core.util.TimeCalculator;

import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFile;
Expand All @@ -39,7 +40,10 @@
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector;
import org.openscience.cdk.atomtype.CDKAtomTypeMatcher;
Expand Down Expand Up @@ -88,6 +92,8 @@ public class SDFIndexEditorModel implements IFileMoleculesEditorModel,

private boolean dirty = false;

private IProgressMonitor monitor;

private SDFIndexEditorModel() {
molProps = new HashMap<Integer, Map<String,Object>>();
propertyList = new HashMap<String, Class<?>>();
Expand All @@ -100,9 +106,10 @@ private SDFIndexEditorModel() {
}
}

public SDFIndexEditorModel(SDFileIndex input) {
public SDFIndexEditorModel(SDFileIndex input, IProgressMonitor monitor) {
this();
this.input = input;
this.monitor = monitor;
if(getNumberOfMolecules()>0) getMoleculeAt( 0 );
}

Expand All @@ -123,6 +130,9 @@ private void setDirty(boolean dirty) {
*/
public synchronized ICDKMolecule getMoleculeAt( int index ) {

if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
ICDKMolecule mol = edited.get( index );
if(mol == null) {
if ( cache.containsKey( index ) ) {
Expand Down Expand Up @@ -318,13 +328,26 @@ public Iterator<ICDKMolecule> iterator() {

return new Iterator<ICDKMolecule>() {

IProgressMonitor m = new SubProgressMonitor(
monitor, IProgressMonitor.UNKNOWN );
int pos = 0;
int size = getNumberOfMolecules();
long before = System.currentTimeMillis();
public boolean hasNext() {
return pos < getNumberOfMolecules();
}

public ICDKMolecule next() {

if ( pos % (size/100) == 0 ) {
synchronized ( monitor ) {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
}
monitor.subTask( "Read molecule " + pos + "/" + size+" ("
+TimeCalculator.generateTimeRemainEst(
before, pos, size ) + ")" );
}
return getMoleculeAt( ++pos );
}

Expand Down

0 comments on commit 4a81e15

Please sign in to comment.