<MyAddFieldPlugin Project>/src/org/myorg/myaddfieldplugin/AddAtomCountFieldAction.java |
private static void fillData(final DFField newField) {
final DFEntity entity = newField.getEntity();
final DFField structureField = findStructureField(entity);
final String structureFieldId = structureField.getId();
final DFEntityDataProvider edp = DIFUtilities.findEntityDataProvider(entity);
String fillingDataTaskName = NbBundle.getMessage(AddAtomCountFieldAction.class, "MSG_ProgressNameFillingData");
UIBackgroundRunnerRW runner = new UIBackgroundRunnerRW(DIFUtilities.getLockable(edp), fillingDataTaskName, true) {
@Override
public void phase1InRequestProcessor() {
DFFeedback feedback = getEnvironment().getFeedback();
feedback.addMessage(DFFeedback.Type.PROGRESS_MESSAGE,NbBundle.getMessage(AddAtomCountFieldAction.class, "MSG_ProgressGrabbingData"), null);
List<? extends Comparable<?>> ids = edp.queryForIds(DFTermExpression.ALL_DATA, SortDirective.EMPTY, getEnvironment());
Map<String,Object> valuesToUpdate = new HashMap<String,Object>();
int total = ids.size();
feedback.switchToDeterminate(total);
int counter = 0;
for (Comparable<?> rowId: ids) {
if (getEnvironment().getFeedback().isCancelled()) {
throw new CancelException(NbBundle.getMessage(AddAtomCountFieldAction.class, "MSG_TaskCancelled", counter, total));
}
Map<Comparable<?>, Map<String, Object>> allData = edp.getData(Collections.singletonList(rowId), getEnvironment());
Object structureValue = allData.get(rowId).get(structureFieldId);
int atomCount = 0;
if (structureValue instanceof MarvinStructure) {
Molecule mol = ((MarvinStructure) structureValue).getNative();
atomCount = mol.getAtomCount();
atomCount += mol.getImplicitHcount();
atomCount += mol.getExplicitHcount();
}
valuesToUpdate.put(newField.getId(), atomCount);
DFUpdateDescription updateDescr = DFUpdateDescription.create(entity, rowId, valuesToUpdate);
DFUpdateResult result = edp.update(Collections.singletonList(updateDescr), DFUndoConfig.OFF, getEnvironment()).get(updateDescr);
if (!result.isSuccessful()) {
throw new RuntimeException(result.getException());
}
feedback.progress(counter);
feedback.addMessage(DFFeedback.Type.PROGRESS_MESSAGE,NbBundle.getMessage(AddAtomCountFieldAction.class, "MSG_ProgressFillingDataDetail", counter, total), null);
counter++;
}
feedback.addMessage(DFFeedback.Type.STATUS_BAR_MESSAGE,NbBundle.getMessage(AddAtomCountFieldAction.class, "StatusBar_TaskDone"), null);
}
@Override
public boolean phase1Cancelled(CancelException exc) {
DialogDisplayer.getDefault().notify(new DialogDescriptor.Message(exc.getMessage()));
return false;
}
@Override
public void phase2InAWT() {
DialogDisplayer.getDefault().notify(new DialogDescriptor.Message(NbBundle.getMessage(AddAtomCountFieldAction.class, "MSG_TaskDone")));
}
};
runner.start();
}