Search in sources :

Example 1 with ModifiableColor

use of CCDD.CcddClassesComponent.ModifiableColor in project CCDD by nasa.

the class CcddJTableHandler method setFixedCharacteristics.

/**
 ********************************************************************************************
 * Set common table parameters and characteristics. These only need to be set when the table is
 * initially created and do not require to be updated if the table is changed while it is
 * visible
 *
 * @param scrollPane
 *            the scroll pane in which the table resides. Set to null if the table isn't in a
 *            scroll pane
 *
 * @param isRowsAlterable
 *            true if the table's rows are allowed to be changed (e.g., inserted, deleted,
 *            moved) via keyboard commands from the user
 *
 * @param intervalSelection
 *            ListSelectionModel selection mode used by the row and column selection models
 *
 * @param cellSelection
 *            table cell selection mode that determines the cell or cells selected when a cell
 *            has focus: SELECT_BY_ROW to select the entire row occupied the cell with focus;
 *            SELECT_BY_COLUMN to select the entire column containing the cell with focus;
 *            SELECT_BY_CELL to select only the cell with focus
 *
 * @param columnDragAllowed
 *            true if a column can be positioned by selecting the header via the mouse and
 *            dragging the column
 *
 * @param background
 *            table background color (when row not selected)
 *
 * @param selectWithoutFocus
 *            true to ignore if the table has focus when determining the row colors for
 *            selected rows
 *
 * @param allowUndo
 *            true to allow changes to the table to be undone and redone
 *
 * @param cellFont
 *            font to use for displaying the cell contents
 *
 * @param allowSort
 *            true to enable the rows to be sorted by selecting a column header; false to
 *            disable sorting
 *******************************************************************************************
 */
protected void setFixedCharacteristics(final JScrollPane scrollPane, boolean isRowsAlterable, int intervalSelection, TableSelectionMode cellSelection, boolean columnDragAllowed, ModifiableColor background, boolean selectWithoutFocus, boolean allowUndo, Font cellFont, boolean allowSort) {
    // Set the table's scroll pane
    this.scrollPane = scrollPane;
    // Set the table's non-selected background color
    setBackground(background);
    // Set to true to keep cell(s) highlighted when the table loses focus
    this.selectWithoutFocus = selectWithoutFocus;
    // Set to true to allow changes to the table to be undone/redone
    undoHandler.setAllowUndo(allowUndo);
    // Set the cell content font
    this.cellFont = cellFont;
    // Set to true to allow the rows to be sorted by selecting the column header
    this.allowSort = allowSort;
    // Initialize the special row color lists. These lists remain empty if no rows are assigned
    // special colors
    rowColorIndex = new ArrayList<Integer>();
    rowColor = new ArrayList<Color>();
    // Set the selection mode (single, contiguous, or multiple)
    setSelectionMode(intervalSelection);
    // Set row and column selection modes. If both are true then selection is by single cell
    this.cellSelection = cellSelection;
    setRowSelectionAllowed(cellSelection == TableSelectionMode.SELECT_BY_ROW || cellSelection == TableSelectionMode.SELECT_BY_CELL);
    setColumnSelectionAllowed(cellSelection == TableSelectionMode.SELECT_BY_COLUMN || cellSelection == TableSelectionMode.SELECT_BY_CELL);
    // Set if a column can be moved by selecting the header with the mouse and dragging it
    getTableHeader().setReorderingAllowed(columnDragAllowed);
    // Remove the table border
    setBorder(BorderFactory.createEmptyBorder());
    // Set the focus to a cell if the keyboard is used to select it. Needed under some
    // circumstances to make text cursor appear in table cell when editing
    setSurrendersFocusOnKeystroke(true);
    // Set so that the columns aren't automatically resized when the table is resized; this is
    // handled manually below
    setAutoResizeMode(AUTO_RESIZE_OFF);
    // Set the font for the table cells
    setFont(cellFont);
    // Replace the table's header mouse listener with a version that captures double clicks on
    // the column header borders in order to implement automatic resizing
    resizeColumnListener = new ResizeColumnListener();
    // Listen for changes made by the user to the table's cells
    new TableCellListener();
    // Change TAB/SHIFT-TAB behavior so that focus jumps between the tables and the buttons
    setTableKeyboardTraversal();
    // Create the table model. The data and column headers are added later in case these need
    // to be adjusted
    tableModel = undoHandler.new UndoableTableModel() {

        /**
         ************************************************************************************
         * Override the cell clean-up method
         ************************************************************************************
         */
        @Override
        protected Object cleanUpCellValue(Object value, int row, int column) {
            return table.cleanUpCellValue(value, row, column);
        }
    };
    setModel(tableModel);
    // Exit the cell's editor, if active, when the cell loses focus
    putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    // Add a listener for table focus changes
    addFocusListener(new FocusListener() {

        /**
         ************************************************************************************
         * Handle loss of keyboard focus for the table
         ************************************************************************************
         */
        @Override
        public void focusLost(FocusEvent fe) {
            // Force a repaint so that any highlighted rows are unhighlighted when the table
            // loses focus
            repaint();
        }

        /**
         ************************************************************************************
         * Handle gain of keyboard focus for the table
         ************************************************************************************
         */
        @Override
        public void focusGained(FocusEvent fe) {
            // Force a repaint so that all cells are highlighted in a row when the table row
            // gains focus
            repaint();
        }
    });
    // Row height, used to set the viewport size, needs to be determined prior to loading the
    // data, which can alter the height returned; and loading the data must be done prior to
    // setting the viewport size in order for the initial viewport size to be set correctly
    int rowHeight = getRowHeight();
    // Load the table data and format the table cells
    loadAndFormatData();
    // Set the initial number of rows to display in the table
    setPreferredScrollableViewportSize(new Dimension(getPreferredSize().width, initialViewableRows * (rowHeight + getRowMargin() * 2 + 4)));
    // Make the table fill the dialog
    setFillsViewportHeight(true);
    // Set the keys that initiate table cell editing and the keys to be ignored when initiating
    // editing. If the table cells and rows are editable then set the keys for these actions
    setEditorKeys(isRowsAlterable);
    // Add a listener for scroll bar thumb position changes
    scrollPane.getViewport().addChangeListener(new ChangeListener() {

        /**
         ************************************************************************************
         * Handle a scroll bar thumb position change for the table
         ************************************************************************************
         */
        @Override
        public void stateChanged(ChangeEvent ce) {
            // Update the row heights for the visible rows
            tableChanged(null);
        }
    });
}
Also used : Color(java.awt.Color) ModifiableColor(CCDD.CcddClassesComponent.ModifiableColor) Dimension(java.awt.Dimension) FocusEvent(java.awt.event.FocusEvent) Point(java.awt.Point) TableInsertionPoint(CCDD.CcddConstants.TableInsertionPoint) ChangeEvent(javax.swing.event.ChangeEvent) PropertyChangeEvent(java.beans.PropertyChangeEvent) UndoableTableModel(CCDD.CcddUndoHandler.UndoableTableModel) ChangeListener(javax.swing.event.ChangeListener) PropertyChangeListener(java.beans.PropertyChangeListener) FocusListener(java.awt.event.FocusListener)

Aggregations

ModifiableColor (CCDD.CcddClassesComponent.ModifiableColor)1 TableInsertionPoint (CCDD.CcddConstants.TableInsertionPoint)1 UndoableTableModel (CCDD.CcddUndoHandler.UndoableTableModel)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 FocusEvent (java.awt.event.FocusEvent)1 FocusListener (java.awt.event.FocusListener)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 ChangeEvent (javax.swing.event.ChangeEvent)1 ChangeListener (javax.swing.event.ChangeListener)1