Skip to content

Commit

Permalink
Queue up all properties for a row when one is requested.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalv authored and egonw committed Mar 25, 2010
1 parent 499f63f commit 8a850c5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
Expand Up @@ -20,6 +20,8 @@
******************************************************************************/
package net.bioclipse.cdk.ui.sdfeditor.editor;

import static net.bioclipse.cdk.ui.sdfeditor.editor.properties.PropertyOrder.createPropertyKey;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -241,49 +243,63 @@ public int getRowCount() {
return 0;
}

private void initExecutorService() {
logger.debug( "Creating ExecutorService" );
executorService = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()) {
@Override
protected void afterExecute( Runnable r,
Throwable t ) {
super.afterExecute( r, t );
Display.getDefault().asyncExec( new Runnable() {
public void run() {
viewer.refresh();
}
});
}
};
}

public Object getDataValue( final int col, final int row ) {
if ( row >= getNumberOfMolecules() ) return "";

final int i = col - 1;
if ( properties == null || i >= properties.size() ) {
return null;
}
if ( executorService == null ) {
logger.debug( "Creating ExecutorService" );
executorService = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()) {
@Override
protected void afterExecute( Runnable r,
Throwable t ) {
super.afterExecute( r, t );
Display.getDefault().asyncExec( new Runnable() {
public void run() {
viewer.refresh();
}
});
}
};
}
if ( executorService == null ) initExecutorService();

String propertyName = col==0?null:(String) properties.get( i );
String propertyKey = PropertyOrder.createPropertyKey( propertyName,
row, col );
row);
Future<Object> p = moleculeProperties.get( propertyKey );
if ( p == null ) {

PropertyOrder order = new PropertyOrder( model, propertyName, row, col );
Future<Object> future = executorService.submit( order );
// cache property future.
cacheFuture( propertyKey, future );

}else {
if(col == 0) {
PropertyOrder order = new PropertyOrder(model,null,row);
Future<Object> future = executorService.submit( order );
cacheFuture( propertyKey, future );
}
for ( Object propertyObject : properties ) {
if ( propertyObject instanceof String ) {
propertyName = (String)propertyObject;
propertyKey = createPropertyKey( propertyName ,row);
if ( !moleculeProperties.containsKey( propertyKey ) ) {
PropertyOrder order = new PropertyOrder( model,
propertyName,
row);
Future<Object> future = executorService.submit( order );
cacheFuture( propertyKey, future );
}
}
}
} else {
if(p.isDone() ) {
try{
return p.get();
} catch( Exception ex) {
moleculeProperties.remove( propertyKey );
return "[ Faild ]";
return "[ Failed ]";
}
}
}
Expand All @@ -292,7 +308,7 @@ public void run() {

private void cacheFuture(String propertyKey, Future<Object> future) {
int visibleRows = ((NatTable)viewer.getControl()).getRowCount();
if( moleculeProperties.size() > visibleRows *(properties.size()+1)){
if( moleculeProperties.size() > 3 *visibleRows *(properties.size()+1)){
Object key = moleculePropertiesQueue.remove( 0 );
Future<Object> value = moleculeProperties.remove( key );
value.cancel( false );
Expand Down
Expand Up @@ -25,31 +25,29 @@ public class PropertyOrder implements Callable<Object> {
IMoleculesEditorModel model;
String propertyName;
int row;
int col;

public PropertyOrder( IMoleculesEditorModel model,
String propertyName, int row, int col) {
String propertyName, int row) {

this.model = model;
this.propertyName = propertyName;
this.row = row;
this.col = col;
}

public static String createPropertyKey(String propertyName, int row, int col) {
if ( col == 0 ) {
return "the-molecule" + "|" + row + "|" + col;
public static String createPropertyKey(String propertyName, int row) {
if ( propertyName == null ) {
return "the-molecule" + "|" + row;
}
else {
return row + "|" + col + "|" + propertyName;
return row + "|" + propertyName;
}
}

public Object call() throws Exception {

ICDKMolecule molecule = model.getMoleculeAt( row );

if(col == 0) return calculateCoordinates( molecule );
if(propertyName == null) return calculateCoordinates( molecule );

Object property = molecule.getProperty( propertyName, Property.USE_CACHED );
if(property == null) {
Expand Down

0 comments on commit 8a850c5

Please sign in to comment.