Search in sources :

Example 11 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddFileIOHandler method importSelectedFileIntoTable.

/**
 ********************************************************************************************
 * Import the contents of a file selected by the user into the specified existing table
 *
 * @param tableHandler
 *            reference to the table handler for the table into which to import the data
 ********************************************************************************************
 */
protected void importSelectedFileIntoTable(CcddTableEditorHandler tableHandler) {
    // Set the initial layout manager characteristics
    GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing() / 2, ModifiableSpacingInfo.LABEL_HORIZONTAL_SPACING.getSpacing() / 2, 0, 0), 0, 0);
    // Create an empty border
    Border emptyBorder = BorderFactory.createEmptyBorder();
    // Create overwrite check box
    JCheckBox overwriteChkBx = new JCheckBox("Overwrite existing cells");
    overwriteChkBx.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
    overwriteChkBx.setBorder(emptyBorder);
    overwriteChkBx.setToolTipText(CcddUtilities.wrapText("Overwrite existing cell data; if unchecked then new " + "rows are inserted to contain the imported data", ModifiableSizeInfo.MAX_TOOL_TIP_LENGTH.getSize()));
    overwriteChkBx.setSelected(false);
    // Create a check box for indicating existing tables can be replaced
    JCheckBox useExistingFieldsCb = new JCheckBox("Use existing field if duplicate");
    useExistingFieldsCb.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
    useExistingFieldsCb.setBorder(emptyBorder);
    useExistingFieldsCb.setToolTipText(CcddUtilities.wrapText("Use the existing data field definition if " + "a field with the same name is imported", ModifiableSizeInfo.MAX_TOOL_TIP_LENGTH.getSize()));
    useExistingFieldsCb.setSelected(true);
    // Create a panel to contain the overwrite check box
    JPanel checkBoxPnl = new JPanel(new GridBagLayout());
    checkBoxPnl.setBorder(emptyBorder);
    checkBoxPnl.add(overwriteChkBx, gbc);
    gbc.insets.top = ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing();
    gbc.gridy++;
    checkBoxPnl.add(useExistingFieldsCb, gbc);
    // Allow the user to select the data file path + name to import from
    FileEnvVar[] dataFile = new CcddDialogHandler().choosePathFile(ccddMain, tableHandler.getOwner(), null, "export", new FileNameExtensionFilter[] { new FileNameExtensionFilter(FileExtension.CSV.getDescription(), FileExtension.CSV.getExtensionName()), new FileNameExtensionFilter(FileExtension.EDS.getDescription(), FileExtension.EDS.getExtensionName()), new FileNameExtensionFilter(FileExtension.JSON.getDescription(), FileExtension.JSON.getExtensionName()), new FileNameExtensionFilter(FileExtension.XTCE.getDescription(), FileExtension.XTCE.getExtensionName()) }, false, false, "Import Table Data", ccddMain.getProgPrefs().get(ModifiablePathInfo.TABLE_EXPORT_PATH.getPreferenceKey(), null), DialogOption.IMPORT_OPTION, checkBoxPnl);
    // Check if a file was chosen
    if (dataFile != null && dataFile[0] != null) {
        try {
            List<TableDefinition> tableDefinitions = null;
            CcddImportExportInterface ioHandler = null;
            // Check if the file to import is in CSV format based on the extension
            if (dataFile[0].getAbsolutePath().endsWith(FileExtension.CSV.getExtension())) {
                // Create a CSV handler
                ioHandler = new CcddCSVHandler(ccddMain, tableHandler.getFieldHandler(), tableHandler.getOwner());
            } else // Check if the file to import is in EDS XML format based on the extension
            if (dataFile[0].getAbsolutePath().endsWith(FileExtension.EDS.getExtension())) {
                // Create an EDS handler
                ioHandler = new CcddEDSHandler(ccddMain, tableHandler.getFieldHandler(), tableHandler.getOwner());
            } else // Check if the file to import is in JSON format based on the extension
            if (dataFile[0].getAbsolutePath().endsWith(FileExtension.JSON.getExtension())) {
                // Create a JSON handler
                ioHandler = new CcddJSONHandler(ccddMain, tableHandler.getFieldHandler(), tableHandler.getOwner());
            } else // Check if the file to import is in XTCE XML format based on the extension
            if (dataFile[0].getAbsolutePath().endsWith(FileExtension.XTCE.getExtension())) {
                // Create an XTCE handler
                ioHandler = new CcddXTCEHandler(ccddMain, tableHandler.getFieldHandler(), tableHandler.getOwner());
            } else // The file extension isn't recognized
            {
                throw new CCDDException("Cannot import file '" + dataFile[0].getAbsolutePath() + "' into table; unrecognized file type");
            }
            // Check that no error occurred creating the format conversion handler
            if (!ioHandler.getErrorStatus()) {
                // Store the current table type information so that it can be restored
                List<TypeDefinition> originalTableTypes = tableTypeHandler.getTypeDefinitions();
                // Import the data file into a table definition
                ioHandler.importFromFile(dataFile[0], ImportType.FIRST_DATA_ONLY);
                tableDefinitions = ioHandler.getTableDefinitions();
                // Check if a table definition was successfully created
                if (tableDefinitions != null && !tableDefinitions.isEmpty()) {
                    // Get a short-cut to the table definition to shorten subsequent calls
                    TableDefinition tableDefn = tableDefinitions.get(0);
                    // End any active edit sequence, then disable auto-ending so that the
                    // import operation can be handled as a single edit for undo/redo purposes
                    tableHandler.getTable().getUndoManager().endEditSequence();
                    tableHandler.getTable().getUndoHandler().setAutoEndEditSequence(false);
                    // Update the table description field in case the description changed
                    tableHandler.setDescription(tableDefn.getDescription());
                    // Add the imported data field(s) to the table
                    addImportedDataField(tableHandler.getFieldHandler(), tableDefn, tableHandler.getTableInformation().getTablePath(), useExistingFieldsCb.isSelected());
                    // Update the field information in case the field values changed
                    tableHandler.getFieldHandler().setFieldDefinitions(tableDefn.getDataFields());
                    tableHandler.getFieldHandler().buildFieldInformation(tableHandler.getTableInformation().getTablePath());
                    // Rebuild the table's editor panel which contains the data fields
                    tableHandler.createDataFieldPanel(true);
                    // Check if cell data is provided in the table definition
                    if (tableDefn.getData() != null && !tableDefn.getData().isEmpty()) {
                        // Get the original number of rows in the table
                        int numRows = tableHandler.getTableModel().getRowCount();
                        // importing the table following a cell validation error
                        if (!tableHandler.getTable().pasteData(tableDefn.getData().toArray(new String[0]), tableHandler.getTable().getColumnCount(), !overwriteChkBx.isSelected(), !overwriteChkBx.isSelected(), true, false)) {
                            // Let the user know how many rows were added
                            new CcddDialogHandler().showMessageDialog(tableHandler.getOwner(), "<html><b>" + (tableHandler.getTableModel().getRowCount() - numRows) + " row(s) added", "Paste Table Data", JOptionPane.INFORMATION_MESSAGE, DialogOption.OK_OPTION);
                        }
                    }
                    // Restore the table types to the values prior to the import operation
                    tableTypeHandler.setTypeDefinitions(originalTableTypes);
                    // Re-enable auto-ending of the edit sequence and end the sequence. The
                    // imported data can be removed with a single undo if desired
                    tableHandler.getTable().getUndoHandler().setAutoEndEditSequence(true);
                    tableHandler.getTable().getUndoManager().endEditSequence();
                    // Store the data file path in the program preferences backing store
                    storePath(ccddMain, dataFile[0].getAbsolutePathWithEnvVars(), true, ModifiablePathInfo.TABLE_EXPORT_PATH);
                }
            }
        } catch (IOException ioe) {
            // Inform the user that the data file cannot be read
            new CcddDialogHandler().showMessageDialog(tableHandler.getOwner(), "<html><b>Cannot read import file<br>'</b>" + dataFile[0].getAbsolutePath() + "<b>'", "File Error", JOptionPane.ERROR_MESSAGE, DialogOption.OK_OPTION);
        } catch (CCDDException ce) {
            // Check if an error message is provided
            if (!ce.getMessage().isEmpty()) {
                // Inform the user that an error occurred reading the import file
                new CcddDialogHandler().showMessageDialog(tableHandler.getOwner(), "<html><b>" + ce.getMessage(), "File Error", ce.getMessageType(), DialogOption.OK_OPTION);
            }
        } catch (Exception e) {
            // Display a dialog providing details on the unanticipated error
            CcddUtilities.displayException(e, tableHandler.getOwner());
        }
    }
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) CCDDException(CCDD.CcddClassesDataTable.CCDDException) GridBagLayout(java.awt.GridBagLayout) FileEnvVar(CCDD.CcddClassesComponent.FileEnvVar) IOException(java.io.IOException) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) CCDDException(CCDD.CcddClassesDataTable.CCDDException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) JCheckBox(javax.swing.JCheckBox) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) Border(javax.swing.border.Border)

Example 12 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getStructureVariableName.

/**
 ********************************************************************************************
 * Get the variable name at the specified row in the structure data. Macro expansion is
 * controlled by the input flag
 *
 * @param row
 *            table data row index
 *
 * @param expandMacros
 *            true to replace any macros with their corresponding value; false to return the
 *            data with any macro names in place
 *
 * @return Variable name at the specified row in the structure data; null if the row index is
 *         invalid
 ********************************************************************************************
 */
private String getStructureVariableName(int row, boolean expandMacros) {
    String variableName = null;
    // Get the table type definition for the structure table referenced in the specified row
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(getStructureTypeNameByRow(row));
    // Check if the table type exists and represents a structure
    if (typeDefn != null && typeDefn.isStructure()) {
        // Get the reference to the table information
        TableInformation tableInfo = getTableInformation(TYPE_STRUCTURE);
        // Get the variable name
        variableName = tableInfo.getData()[row][typeDefn.getColumnIndexByInputType(InputDataType.VARIABLE)];
        // Check if any macros should be expanded
        if (expandMacros) {
            // Expand any macros
            variableName = macroHandler.getMacroExpansion(variableName);
        }
    }
    return variableName;
}
Also used : TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 13 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getNumCommandArguments.

/**
 ********************************************************************************************
 * Get the number of arguments associated with the specified command table type
 *
 * @param tableType
 *            table type (case insensitive)
 *
 * @return Number of arguments associated with the specified command table type; -1 if the
 *         table type is invalid
 ********************************************************************************************
 */
public int getNumCommandArguments(String tableType) {
    int numArguments = -1;
    // Get the table type definition based on the table type name
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(tableType);
    // Check if the table type exists and represents a command
    if (typeDefn != null && typeDefn.isCommand()) {
        // Get the number of arguments associated with this command table type
        numArguments = typeDefn.getAssociatedCommandArgumentColumns(false).size();
    }
    return numArguments;
}
Also used : TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 14 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getTableColumnNames.

/**
 ********************************************************************************************
 * Get the table column names for the table referenced on the specified row of the table data
 * for the table type specified
 *
 * @param tableType
 *            table type (case insensitive). All structure table types are combined and are
 *            referenced by the type name "Structure", and all command table types are combined
 *            and are referenced by the type name "Command"
 *
 * @param row
 *            table data row index
 *
 * @return Array containing the names of the columns of the table type referenced in the
 *         specified row of the type's table data
 ********************************************************************************************
 */
public String[] getTableColumnNames(String tableType, int row) {
    String[] columnNames = null;
    // Get the type definition based on the table type name
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(getTypeNameByRow(tableType, row));
    // Check if the table type exists
    if (typeDefn != null) {
        // STore the names of the columns for this table type definition
        columnNames = typeDefn.getColumnNamesVisible();
    }
    return columnNames;
}
Also used : TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 15 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getCommandArgMaximum.

/**
 ********************************************************************************************
 * Get the argument maximum value (as a string) for the command argument specified at the
 * specified row in the command data. Macro expansion is controlled by the input flag
 *
 * @param argumentNumber
 *            command argument index. The first argument is 0
 *
 * @param row
 *            table data row index
 *
 * @param expandMacros
 *            true to replace any macros with their corresponding value; false to return the
 *            data with any macro names in place
 *
 * @return Argument maximum value (as a string) for the command argument specified at the
 *         specified row in the command data; null if the argument number or row index is
 *         invalid
 ********************************************************************************************
 */
private String getCommandArgMaximum(int argumentNumber, int row, boolean expandMacros) {
    String argMaximum = null;
    // Get the table type definition for the command table referenced in the specified row
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(getCommandTypeNameByRow(row));
    // Check if the table type exists and represents a command
    if (typeDefn != null && typeDefn.isCommand()) {
        // Get the list of command arguments associated with this command table type
        List<AssociatedColumns> commandArguments = typeDefn.getAssociatedCommandArgumentColumns(false);
        // Check if the argument number is valid and that the argument maximum value exists
        if (argumentNumber < commandArguments.size() && commandArguments.get(argumentNumber).getMaximum() != -1) {
            // Get the reference to the table information
            TableInformation tableInfo = getTableInformation(TYPE_COMMAND);
            // Get the argument maximum value
            argMaximum = tableInfo.getData()[row][commandArguments.get(argumentNumber).getMaximum()];
            // Check if any macros should be expanded
            if (expandMacros) {
                // Expand any macros
                argMaximum = macroHandler.getMacroExpansion(argMaximum);
            }
        }
    }
    return argMaximum;
}
Also used : AssociatedColumns(CCDD.CcddClassesDataTable.AssociatedColumns) TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Aggregations

TypeDefinition (CCDD.CcddTableTypeHandler.TypeDefinition)64 TableInformation (CCDD.CcddClassesDataTable.TableInformation)30 ArrayList (java.util.ArrayList)24 CCDDException (CCDD.CcddClassesDataTable.CCDDException)18 AssociatedColumns (CCDD.CcddClassesDataTable.AssociatedColumns)10 SQLException (java.sql.SQLException)9 JSONArray (org.json.simple.JSONArray)8 JSONObject (org.json.simple.JSONObject)8 TableTypeDefinition (CCDD.CcddClassesDataTable.TableTypeDefinition)6 IOException (java.io.IOException)6 TableDefinition (CCDD.CcddClassesDataTable.TableDefinition)5 BackgroundCommand (CCDD.CcddBackgroundCommand.BackgroundCommand)4 ArrayListMultiple (CCDD.CcddClassesComponent.ArrayListMultiple)4 FileEnvVar (CCDD.CcddClassesComponent.FileEnvVar)3 FieldInformation (CCDD.CcddClassesDataTable.FieldInformation)3 Component (java.awt.Component)3 GridBagConstraints (java.awt.GridBagConstraints)3 GridBagLayout (java.awt.GridBagLayout)3 Insets (java.awt.Insets)3 ResultSet (java.sql.ResultSet)3