Search in sources :

Example 6 with ClientRequestManager

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 };
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Hashtable(java.util.Hashtable) FieldDataFileOperationSpec(cbit.vcell.field.io.FieldDataFileOperationSpec) DocumentManager(cbit.vcell.clientdb.DocumentManager) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) DataFormatException(java.util.zip.DataFormatException) UserCancelException(org.vcell.util.UserCancelException) RequestManager(cbit.vcell.client.RequestManager) ClientRequestManager(cbit.vcell.client.ClientRequestManager) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) RegionImage(cbit.vcell.geometry.RegionImage) FieldDataDBOperationSpec(cbit.vcell.field.FieldDataDBOperationSpec)

Aggregations

ClientRequestManager (cbit.vcell.client.ClientRequestManager)6 RequestManager (cbit.vcell.client.RequestManager)6 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)6 Hashtable (java.util.Hashtable)5 DataFormatException (java.util.zip.DataFormatException)5 UserCancelException (org.vcell.util.UserCancelException)5 FieldDataFileOperationSpec (cbit.vcell.field.io.FieldDataFileOperationSpec)4 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)3 TreePath (javax.swing.tree.TreePath)3 ExternalDataIdentifier (org.vcell.util.document.ExternalDataIdentifier)3 FieldDataFileOperationResults (cbit.vcell.field.io.FieldDataFileOperationResults)2 DataIdentifier (cbit.vcell.simdata.DataIdentifier)2 FieldDataWindowManager (cbit.vcell.client.FieldDataWindowManager)1 FDSimBioModelInfo (cbit.vcell.client.TopLevelWindowManager.FDSimBioModelInfo)1 FDSimMathModelInfo (cbit.vcell.client.TopLevelWindowManager.FDSimMathModelInfo)1 OpenModelInfoHolder (cbit.vcell.client.TopLevelWindowManager.OpenModelInfoHolder)1 DocumentManager (cbit.vcell.clientdb.DocumentManager)1 FieldDataDBOperationSpec (cbit.vcell.field.FieldDataDBOperationSpec)1 Geometry (cbit.vcell.geometry.Geometry)1