Search in sources :

Example 1 with UndoableCellSelection

use of CCDD.CcddUndoHandler.UndoableCellSelection in project CCDD by nasa.

the class CcddFieldTableEditorDialog method createDataFieldTableEditorTable.

/**
 ********************************************************************************************
 * Create the data field table editor table
 *
 * @return Reference to the scroll pane in which the table is placed
 ********************************************************************************************
 */
private JScrollPane createDataFieldTableEditorTable() {
    // Create the table to display the structure tables and their corresponding user-selected
    // data fields
    dataFieldTable = new CcddJTableHandler() {

        /**
         ************************************************************************************
         * Allow resizing of the any column unless it displays a check box
         ************************************************************************************
         */
        @Override
        protected boolean isColumnResizable(int column) {
            return !checkBoxColumns.contains(column);
        }

        /**
         ************************************************************************************
         * Allow multiple line display in all columns except those displaying check boxes
         ************************************************************************************
         */
        @Override
        protected boolean isColumnMultiLine(int column) {
            return !checkBoxColumns.contains(column);
        }

        /**
         ************************************************************************************
         * Allow HTML-formatted text in the specified column(s)
         ************************************************************************************
         */
        @Override
        protected boolean isColumnHTML(int column) {
            return column == FieldTableEditorColumnInfo.OWNER.ordinal() || column == FieldTableEditorColumnInfo.PATH.ordinal();
        }

        /**
         ************************************************************************************
         * Hide the specified column(s)
         ************************************************************************************
         */
        @Override
        protected boolean isColumnHidden(int column) {
            return !isPath && column == FieldTableEditorColumnInfo.PATH.ordinal();
        }

        /**
         ************************************************************************************
         * Display the specified column(s) as check boxes
         ************************************************************************************
         */
        @Override
        protected boolean isColumnBoolean(int column) {
            return checkBoxColumns.contains(column);
        }

        /**
         ************************************************************************************
         * Allow editing of the table cells in the specified columns only
         ************************************************************************************
         */
        @Override
        public boolean isCellEditable(int row, int column) {
            // Convert the coordinates from view to model
            row = convertRowIndexToModel(row);
            column = convertColumnIndexToModel(column);
            // have the field specified by the column
            return column != FieldTableEditorColumnInfo.OWNER.ordinal() && column != FieldTableEditorColumnInfo.PATH.ordinal() && fieldHandler.getFieldInformationByName(getOwnerWithPath(getModel().getValueAt(row, FieldTableEditorColumnInfo.OWNER.ordinal()).toString(), getModel().getValueAt(row, FieldTableEditorColumnInfo.PATH.ordinal()).toString()), columnNames[column]) != null;
        }

        /**
         ************************************************************************************
         * Allow pasting data into the data field cells
         ************************************************************************************
         */
        @Override
        protected boolean isDataAlterable(Object[] rowData, int row, int column) {
            return isCellEditable(convertRowIndexToView(row), convertColumnIndexToView(column));
        }

        /**
         ************************************************************************************
         * Validate changes to the data field value cells; e.g., verify cell content and, if
         * found invalid, revert to the original value
         *
         * @param tableData
         *            list containing the table data row arrays
         *
         * @param row
         *            table model row number
         *
         * @param column
         *            table model column number
         *
         * @param oldValue
         *            original cell contents
         *
         * @param newValue
         *            new cell contents
         *
         * @param showMessage
         *            unused
         *
         * @param isMultiple
         *            unused
         *
         * @return Value of ShowMessage
         ***********************************************************************************
         */
        @Override
        protected Boolean validateCellContent(List<Object[]> tableData, int row, int column, Object oldValue, Object newValue, Boolean showMessage, boolean isMultiple) {
            // Reset the flag that indicates the last edited cell's content is invalid
            setLastCellValid(true);
            // Create a string version of the new value
            String newValueS = newValue.toString();
            // Check that the new cell value isn't blank
            if (!newValueS.isEmpty()) {
                // Get the owner name, with path if applicable
                String ownerAndPath = getOwnerWithPath(tableData.get(row)[FieldTableEditorColumnInfo.OWNER.ordinal()].toString(), tableData.get(row)[FieldTableEditorColumnInfo.PATH.ordinal()].toString());
                // Get the reference to the data field
                FieldInformation fieldInfo = fieldHandler.getFieldInformationByName(ownerAndPath, columnNames[column]);
                // Check that the data field was found
                if (fieldInfo != null) {
                    // this data field
                    if (fieldInfo.getInputType().getInputMatch().isEmpty() || newValueS.matches(fieldInfo.getInputType().getInputMatch())) {
                        // Store the new value in the table data array after formatting the
                        // cell value per its input type. This is needed primarily to clean up
                        // numeric formatting
                        newValueS = fieldInfo.getInputType().formatInput(newValueS);
                        tableData.get(row)[column] = newValueS;
                    } else // The value doesn't match the pattern for this data field
                    {
                        // Set the flag that indicates the last edited cell's content is
                        // invalid
                        setLastCellValid(false);
                        // Inform the user that the data field contents is invalid
                        new CcddDialogHandler().showMessageDialog(CcddFieldTableEditorDialog.this, "<html><b>Invalid characters in field '</b>" + fieldInfo.getFieldName() + "<b>'; " + fieldInfo.getInputType().getInputName().toLowerCase() + " expected", "Invalid " + fieldInfo.getInputType().getInputName(), JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
                        // Restore the cell contents to its original value
                        tableData.get(row)[column] = oldValue;
                        getUndoManager().undoRemoveEdit();
                    }
                }
            }
            return showMessage;
        }

        /**
         ************************************************************************************
         * Load the data field data into the table and format the table cells
         ************************************************************************************
         */
        @Override
        protected void loadAndFormatData() {
            // Get the default column names and tool tip text for the data field editor table
            String[] toolTips = new String[columnNames.length];
            toolTips[FieldTableEditorColumnInfo.OWNER.ordinal()] = FieldTableEditorColumnInfo.OWNER.getToolTip();
            toolTips[FieldTableEditorColumnInfo.PATH.ordinal()] = FieldTableEditorColumnInfo.PATH.getToolTip();
            // Create lists for any columns to be displayed as check boxes
            checkBoxColumns = new ArrayList<Integer>();
            // Get the owners, paths, data field values values, and check box columns based on
            // the specified data fields
            committedData = getDataFieldsToDisplay();
            // Place the data into the table model along with the column names, set up the
            // editors and renderers for the table cells, set up the table grid lines, and
            // calculate the minimum width required to display the table information
            setUpdatableCharacteristics(committedData, columnNames, null, toolTips, true, true, true);
        }

        /**
         ************************************************************************************
         * Override the CcddJTableHandler method in order to show/hide the data fields based on
         * the selected field filters
         ************************************************************************************
         */
        @Override
        protected void setTableSortable() {
            super.setTableSortable();
            // Get the table's row sorter and add the event type filter
            TableRowSorter<?> sorter = (TableRowSorter<?>) getRowSorter();
            // filter hasn't been set, and that there is a field owner row filter
            if (sorter != null && sorter.getRowFilter() != rowFilter && rowFilter != null) {
                // Apply the row filter that shows/hides the event types
                sorter.setRowFilter(rowFilter);
            }
        }

        /**
         ************************************************************************************
         * Override prepareRenderer to allow adjusting the background colors of table cells
         ************************************************************************************
         */
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            JComponent comp = (JComponent) super.prepareRenderer(renderer, row, column);
            // Get the column index in model coordinates
            int columnModel = convertColumnIndexToModel(column);
            // highlight colors override the invalid highlight color
            if (comp.getBackground() != ModifiableColorInfo.FOCUS_BACK.getColor() && comp.getBackground() != ModifiableColorInfo.SELECTED_BACK.getColor() && columnModel != FieldTableEditorColumnInfo.OWNER.ordinal()) {
                // Get the row index in model coordinates
                int rowModel = convertRowIndexToModel(row);
                // Get a reference to the table model to shorten subsequent calls
                TableModel tableModel = getModel();
                // Get the contents of the owner and path columns
                String ownerName = tableModel.getValueAt(rowModel, FieldTableEditorColumnInfo.OWNER.ordinal()).toString();
                String pathValue = tableModel.getValueAt(rowModel, FieldTableEditorColumnInfo.PATH.ordinal()).toString();
                // Get the owner, including the path (if a child structure table), with any
                // highlighting removed (this is the field owner as stored in the project's
                // data field table)
                String ownerPath = getOwnerWithPath(ownerName, pathValue);
                // Check if this is the structure path column
                if (columnModel == FieldTableEditorColumnInfo.PATH.ordinal()) {
                    // Check if the cell is blank and that the owner is a structure table
                    if (pathValue.isEmpty() && (nonStructureTableNames.contains(ownerPath) || ownerIsNotTable(ownerName))) {
                        // Set the cell's background color to indicate the structure path isn't
                        // applicable for this table
                        comp.setBackground(ModifiableColorInfo.PROTECTED_BACK.getColor());
                    }
                } else // Check if this table has the data field identified by the column
                if (fieldHandler.getFieldInformationByName(ownerPath, columnNames[columnModel]) != null) {
                    // Check if the cell is a data field selected for removal
                    if (selectedCells.contains(row, column)) {
                        // Change the cell's colors to indicate the data field represented by
                        // the cell is selected for removal
                        comp.setForeground(Color.GRAY);
                        comp.setBackground(Color.RED);
                    } else // a boolean value (these are not compared for duplicate values)
                    if (!checkBoxColumns.contains(columnModel)) {
                        // Get the input data type for this data field
                        InputDataType inputType = fieldHandler.getFieldInformationByName(ownerPath, columnNames[columnModel]).getInputType();
                        // Get the text in the cell, formatted per its input type, but without
                        // preserving the leading zeroes for hexadecimal values
                        String value = inputType.formatInput(tableModel.getValueAt(rowModel, columnModel).toString(), false);
                        // Step through each row in the table
                        for (int checkRow = 0; checkRow < tableModel.getRowCount(); checkRow++) {
                            // row of the same column
                            if (rowModel != checkRow && !value.isEmpty() && inputType.formatInput(tableModel.getValueAt(checkRow, columnModel).toString(), false).equals(value)) {
                                // Change the cell's background color to indicate it has the
                                // same value as another cell in the same column
                                comp.setBackground(ModifiableColorInfo.REQUIRED_BACK.getColor());
                                break;
                            }
                        }
                    }
                } else // The table indicated by this row does not have a data field as identified by
                // the column
                {
                    // Set the cell's background color to indicate the data field doesn't exist
                    // for this table
                    comp.setBackground(ModifiableColorInfo.PROTECTED_BACK.getColor());
                }
            }
            return comp;
        }

        /**
         ************************************************************************************
         * Handle a change to the table's content
         ************************************************************************************
         */
        @Override
        protected void processTableContentChange() {
            // Add or remove the change indicator based on whether any unstored changes exist
            setTitle(DIALOG_TITLE + (isFieldTableChanged() ? "*" : ""));
            // Force the table to redraw so that changes to the cells are displayed
            repaint();
        }
    };
    // Place the table into a scroll pane
    JScrollPane scrollPane = new JScrollPane(dataFieldTable);
    // Disable storage of edit operations during table creation
    dataFieldTable.getUndoHandler().setAllowUndo(false);
    // Set up the field table parameters
    dataFieldTable.setFixedCharacteristics(scrollPane, false, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, TableSelectionMode.SELECT_BY_CELL, true, ModifiableColorInfo.TABLE_BACK.getColor(), true, true, ModifiableFontInfo.OTHER_TABLE_CELL.getFont(), true);
    // Set the reference to the cell selection container in the undo handler, then create a
    // cell selection object
    dataFieldTable.getUndoHandler().setSelectedCells(selectedCells);
    cellSelect = dataFieldTable.getUndoHandler().new UndoableCellSelection();
    // Re-enable storage of edit operations
    dataFieldTable.getUndoHandler().setAllowUndo(true);
    return scrollPane;
}
Also used : JScrollPane(javax.swing.JScrollPane) TableCellRenderer(javax.swing.table.TableCellRenderer) UndoableCellSelection(CCDD.CcddUndoHandler.UndoableCellSelection) ArrayList(java.util.ArrayList) JComponent(javax.swing.JComponent) Point(java.awt.Point) Component(java.awt.Component) JComponent(javax.swing.JComponent) TableRowSorter(javax.swing.table.TableRowSorter) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) InputDataType(CCDD.CcddConstants.InputDataType) FieldInformation(CCDD.CcddClassesDataTable.FieldInformation)

Aggregations

FieldInformation (CCDD.CcddClassesDataTable.FieldInformation)1 InputDataType (CCDD.CcddConstants.InputDataType)1 UndoableCellSelection (CCDD.CcddUndoHandler.UndoableCellSelection)1 Component (java.awt.Component)1 Point (java.awt.Point)1 ArrayList (java.util.ArrayList)1 JComponent (javax.swing.JComponent)1 JScrollPane (javax.swing.JScrollPane)1 DefaultTableModel (javax.swing.table.DefaultTableModel)1 TableCellRenderer (javax.swing.table.TableCellRenderer)1 TableModel (javax.swing.table.TableModel)1 TableRowSorter (javax.swing.table.TableRowSorter)1