use of cbit.vcell.client.ClientRequestManager in project vcell by virtualcell.
the class FieldDataGUIPanel method addNewExternalData.
public static AsynchClientTask[] addNewExternalData(final Component requester, final FieldDataGUIPanel fieldDataGUIPanel, final boolean isFromSimulation) {
final RequestManager clientRequestManager = fieldDataGUIPanel.fieldDataWindowManager.getLocalRequestManager();
AsynchClientTask task1 = new AsynchClientTask("creating field data", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
FieldDataFileOperationSpec fdos = (FieldDataFileOperationSpec) hashTable.get("fdos");
String initialExtDataName = (String) hashTable.get("initFDName");
fdos.specEDI = null;
FieldDataInfoPanel fdip = new FieldDataInfoPanel();
fdip.setSimulationMode(isFromSimulation);
fdip.initISize(fdos.isize);
fdip.initIOrigin(fdos.origin);
fdip.initIExtent(fdos.extent);
fdip.initTimes(fdos.times);
fdip.initNames(TokenMangler.fixTokenStrict(initialExtDataName), fdos.varNames);
fdip.setAnnotation(fdos.annotation);
FieldDataFileOperationSpec userDefinedFDOS = new FieldDataFileOperationSpec();
while (true) {
int choice = PopupGenerator.showComponentOKCancelDialog(requester, fdip, "Create new field data");
if (choice == JOptionPane.OK_OPTION) {
// Check values
try {
userDefinedFDOS.extent = fdip.getExtent();
} catch (Exception e) {
PopupGenerator.showErrorDialog(requester, "Problem with Extent values. Please re-enter.\n" + e.getMessage() + "\nTry Again.", e);
continue;
}
try {
userDefinedFDOS.origin = fdip.getOrigin();
} catch (Exception e) {
PopupGenerator.showErrorDialog(requester, "Problem with Origin values. Please re-enter.\n" + e.getMessage() + "\nTry Again.", e);
continue;
}
try {
userDefinedFDOS.varNames = fdip.getVariableNames();
} catch (Exception e) {
PopupGenerator.showErrorDialog(requester, "Problem with Variable names. Please re-enter.\n" + e.getMessage() + "\nTry Again.", e);
continue;
}
userDefinedFDOS.annotation = fdip.getAnnotation();
userDefinedFDOS.times = fdip.getTimes();
try {
if (fdip.getFieldName() == null || fdip.getFieldName().length() == 0 || !fdip.getFieldName().equals(TokenMangler.fixTokenStrict(fdip.getFieldName()))) {
throw new Exception("Field Data names can contain only letters,digits and underscores");
}
// Check to see if this name is already used
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) fieldDataGUIPanel.getJTree1().getModel().getRoot();
for (int i = 0; i < rootNode.getChildCount(); i += 1) {
ExternalDataIdentifier extDataID = ((FieldDataMainList) ((DefaultMutableTreeNode) rootNode.getChildAt(i)).getUserObject()).externalDataIdentifier;
if (fdip.getFieldName().equals(extDataID.getName())) {
throw new Exception("New Field Data name " + fdip.getFieldName() + " already used.");
}
}
} catch (Exception e) {
PopupGenerator.showErrorDialog(requester, "Error saving Field Data Name to Database. Try again.\n" + e.getMessage(), e);
continue;
}
hashTable.put("userDefinedFDOS", userDefinedFDOS);
hashTable.put("fieldName", fdip.getFieldName());
break;
} else {
throw UserCancelException.CANCEL_GENERIC;
}
}
}
};
AsynchClientTask task2 = new AsynchClientTask("saving field data", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
// Add to Server Disk
// save Database
FieldDataFileOperationSpec tempFDOS = (FieldDataFileOperationSpec) hashTable.get("userDefinedFDOS");
String fieldName = (String) hashTable.get("fieldName");
FieldDataFileOperationSpec fdos = (FieldDataFileOperationSpec) hashTable.get("fdos");
DocumentManager documentManager = clientRequestManager.getDocumentManager();
FieldDataDBOperationSpec newExtDataIDSpec = FieldDataDBOperationSpec.createSaveNewExtDataIDSpec(documentManager.getUser(), fieldName, tempFDOS.annotation);
tempFDOS.specEDI = documentManager.fieldDataDBOperation(newExtDataIDSpec).extDataID;
fdos.specEDI = tempFDOS.specEDI;
fdos.annotation = tempFDOS.annotation;
try {
if (!isFromSimulation) {
fdos.extent = tempFDOS.extent;
fdos.origin = tempFDOS.origin;
fdos.varNames = tempFDOS.varNames;
fdos.times = tempFDOS.times;
//
// Subvolumes and Regions NOT implemented now
//
fdos.cartesianMesh = CartesianMesh.createSimpleCartesianMesh(fdos.origin, fdos.extent, fdos.isize, new RegionImage(new // empty regions
VCImageUncompressed(// empty regions
null, // empty regions
new byte[fdos.isize.getXYZ()], fdos.extent, fdos.isize.getX(), fdos.isize.getY(), fdos.isize.getZ()), 0, null, null, RegionImage.NO_SMOOTHING));
}
// Add to Server Disk
documentManager.fieldDataFileOperation(fdos);
} catch (Exception e) {
try {
// try to cleanup new ExtDataID
documentManager.fieldDataDBOperation(FieldDataDBOperationSpec.createDeleteExtDataIDSpec(fdos.specEDI));
} catch (Exception e2) {
// ignore
}
fdos.specEDI = null;
throw e;
}
}
};
AsynchClientTask task3 = new AsynchClientTask("refreshing field data", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
FieldDataFileOperationSpec fdos = (FieldDataFileOperationSpec) hashTable.get("fdos");
DefaultMutableTreeNode root = ((DefaultMutableTreeNode) fieldDataGUIPanel.getJTree1().getModel().getRoot());
if (root.getChildCount() == 0) {
fieldDataGUIPanel.updateJTree(clientRequestManager);
} else {
int alphabeticalIndex = -1;
for (int i = 0; i < root.getChildCount(); i += 1) {
if ((((FieldDataMainList) ((DefaultMutableTreeNode) root.getChildAt(i)).getUserObject())).externalDataIdentifier.getName().compareToIgnoreCase(fdos.specEDI.getName()) > 0) {
alphabeticalIndex = i;
break;
}
}
if (alphabeticalIndex == -1) {
alphabeticalIndex = root.getChildCount();
}
DefaultMutableTreeNode mainNode = new DefaultMutableTreeNode(new FieldDataMainList(fdos.specEDI, fdos.annotation));
mainNode.add(new DefaultMutableTreeNode(new FieldDataVarMainList()));
((DefaultTreeModel) fieldDataGUIPanel.getJTree1().getModel()).insertNodeInto(mainNode, root, alphabeticalIndex);
}
}
};
return new AsynchClientTask[] { task1, task2, task3 };
}
Aggregations