Search in sources :

Example 21 with FocusListener

use of java.awt.event.FocusListener 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)

Example 22 with FocusListener

use of java.awt.event.FocusListener in project CCDD by nasa.

the class CcddJTableHandler method setCellEditors.

/**
 ********************************************************************************************
 * Set the editors for the editable cells based on the column type
 ********************************************************************************************
 */
private void setCellEditors() {
    // Create a focus listener to track the text cursor position when the cell loses focus
    FocusListener focusListener = new FocusListener() {

        /**
         ************************************************************************************
         * Handle loss of keyboard focus for the cell
         ************************************************************************************
         */
        @Override
        public void focusLost(FocusEvent fe) {
            // Check if editing is active in the cell
            if (table.isEditing()) {
                // Store the start and end positions of the selected text
                lastSelectionStart = ((JTextComponent) fe.getComponent()).getSelectionStart();
                lastSelectionEnd = ((JTextComponent) fe.getComponent()).getSelectionEnd();
            } else // Editing is inactive
            {
                // Reset the text selection positions
                lastSelectionStart = -1;
                lastSelectionEnd = -1;
            }
        }

        /**
         ************************************************************************************
         * Handle gain of keyboard focus for the cell
         ************************************************************************************
         */
        @Override
        public void focusGained(FocusEvent fe) {
        }
    };
    // Create a text area so that its properties can be set and then used to create a
    // multi-line editor for cells containing one or more lines of text
    JTextArea textFieldMulti = new JTextArea();
    textFieldMulti.setFont(cellFont);
    textFieldMulti.setBorder(editBorder);
    // Add a listener for cell focus changes
    textFieldMulti.addFocusListener(focusListener);
    // Create the cell editor for multi-line cells
    MultiLineCellEditor dceMulti = new MultiLineCellEditor(textFieldMulti);
    // Create a a cell editor for single line cells. The padding differs from the multi-line
    // cell; using this editor for single line cells prevents the text from changing vertical
    // alignment when editing is initiated
    JTextField textFieldSingle = new JTextField();
    textFieldSingle.setFont(cellFont);
    textFieldSingle.setBorder(editBorder);
    // Add a listener for cell focus changes
    textFieldSingle.addFocusListener(focusListener);
    // Create the cell editor
    DefaultCellEditor dceSingle = new DefaultCellEditor(textFieldSingle);
    // Step through each column in the table
    for (int column = 0; column < getColumnCount(); column++) {
        // Get the column index in model coordinates
        int columnModel = convertColumnIndexToModel(column);
        // Check if the column's contents is not displayed as a check box
        if (!isColumnBoolean(columnModel)) {
            // Set the editor so that the contents can be modified within the table cell. Use
            // the editor appropriate for the number of cell display lines
            getColumnModel().getColumn(column).setCellEditor(isColumnMultiLine(columnModel) ? dceMulti : dceSingle);
        }
    }
}
Also used : JTextArea(javax.swing.JTextArea) JTextField(javax.swing.JTextField) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent) Point(java.awt.Point) TableInsertionPoint(CCDD.CcddConstants.TableInsertionPoint) DefaultCellEditor(javax.swing.DefaultCellEditor)

Example 23 with FocusListener

use of java.awt.event.FocusListener in project freeplane by freeplane.

the class PathProperty method layout.

public void layout(final DefaultFormBuilder builder) {
    final Box box = Box.createHorizontalBox();
    box.setBorder(new EmptyBorder(5, 0, 5, 0));
    filenameField = new JTextField();
    filenameField.setText(path);
    filenameField.addFocusListener(new FocusListener() {

        public void focusLost(FocusEvent e) {
            final String text = filenameField.getText();
            if (text == null || text.length() == 0) {
                filenameField.setText(path);
                JOptionPane.showConfirmDialog(e.getComponent(), TextUtils.getText("OptionPanel.path_property_may_not_be_empty"), "", JOptionPane.WARNING_MESSAGE);
            } else {
                path = text;
            }
        }

        public void focusGained(FocusEvent e) {
        }
    });
    box.add(filenameField);
    box.add(Box.createHorizontalStrut(3));
    selectButton = new JButton();
    LabelAndMnemonicSetter.setLabelAndMnemonic(selectButton, TextUtils.getText("browse"));
    selectButton.addActionListener(new SelectFileAction());
    selectButton.setMaximumSize(new Dimension(1000, 1000));
    box.add(selectButton);
    layout(builder, box);
}
Also used : JButton(javax.swing.JButton) Box(javax.swing.Box) Dimension(java.awt.Dimension) EmptyBorder(javax.swing.border.EmptyBorder) JTextField(javax.swing.JTextField) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Example 24 with FocusListener

use of java.awt.event.FocusListener in project freeplane by freeplane.

the class MNoteController method getHtmlEditorPanel.

SHTMLPanel getHtmlEditorPanel() {
    if (htmlEditorPanel != null) {
        return htmlEditorPanel;
    }
    htmlEditorPanel = MTextController.getController().createSHTMLPanel(NoteModel.EDITING_PURPOSE);
    // make sure that SHTML gets notified of relevant config changes!
    ResourceController.getResourceController().addPropertyChangeListener(new FreeplaneToSHTMLPropertyChangeAdapter(htmlEditorPanel));
    htmlEditorPanel.setMinimumSize(new Dimension(100, 100));
    final SHTMLEditorPane editorPane = (SHTMLEditorPane) htmlEditorPanel.getEditorPane();
    for (InputMap inputMap = editorPane.getInputMap(); inputMap != null; inputMap = inputMap.getParent()) {
        inputMap.remove(KeyStroke.getKeyStroke("ctrl pressed T"));
        inputMap.remove(KeyStroke.getKeyStroke("ctrl shift pressed T"));
        inputMap.remove(KeyStroke.getKeyStroke("ctrl pressed SPACE"));
    }
    editorPane.addFocusListener(new FocusListener() {

        private SpellCheckerController spellCheckerController = null;

        private boolean enabled = false;

        public void focusLost(final FocusEvent e) {
            if (!e.isTemporary()) {
                spellCheckerController.enableAutoSpell(editorPane, false);
                enabled = false;
                noteManager.saveNote();
            }
        }

        public void focusGained(final FocusEvent e) {
            if (!enabled) {
                initSpellChecker();
                spellCheckerController.enableAutoSpell(editorPane, true);
                enabled = true;
            }
        }

        private void initSpellChecker() {
            if (spellCheckerController != null) {
                return;
            }
            spellCheckerController = SpellCheckerController.getController();
            spellCheckerController.addSpellCheckerMenu(editorPane.getPopup());
            spellCheckerController.enableShortKey(editorPane, true);
        }
    });
    htmlEditorPanel.getSourceEditorPane().addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                noteManager.saveNote();
            }
        }

        @Override
        public void focusGained(FocusEvent e) {
        }
    });
    return htmlEditorPanel;
}
Also used : SHTMLEditorPane(com.lightdev.app.shtm.SHTMLEditorPane) FreeplaneToSHTMLPropertyChangeAdapter(org.freeplane.features.text.mindmapmode.FreeplaneToSHTMLPropertyChangeAdapter) SpellCheckerController(org.freeplane.features.spellchecker.mindmapmode.SpellCheckerController) InputMap(javax.swing.InputMap) Dimension(java.awt.Dimension) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Example 25 with FocusListener

use of java.awt.event.FocusListener in project freeplane by freeplane.

the class UITools method executeWhenNodeHasFocus.

public static void executeWhenNodeHasFocus(final Runnable runnable) {
    final Component selectedComponent = Controller.getCurrentController().getMapViewManager().getSelectedComponent();
    if (selectedComponent != null && !selectedComponent.hasFocus()) {
        selectedComponent.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
            }

            @Override
            public void focusGained(FocusEvent e) {
                selectedComponent.removeFocusListener(this);
                runnable.run();
            }
        });
        selectedComponent.requestFocusInWindow();
    } else
        runnable.run();
}
Also used : Component(java.awt.Component) JComponent(javax.swing.JComponent) JTextComponent(javax.swing.text.JTextComponent) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Aggregations

FocusListener (java.awt.event.FocusListener)87 FocusEvent (java.awt.event.FocusEvent)80 ActionEvent (java.awt.event.ActionEvent)24 ActionListener (java.awt.event.ActionListener)23 JLabel (javax.swing.JLabel)20 JTextField (javax.swing.JTextField)17 Dimension (java.awt.Dimension)16 JPanel (javax.swing.JPanel)16 JButton (javax.swing.JButton)15 KeyEvent (java.awt.event.KeyEvent)12 JComboBox (javax.swing.JComboBox)11 BorderLayout (java.awt.BorderLayout)9 Component (java.awt.Component)9 MouseEvent (java.awt.event.MouseEvent)9 KeyListener (java.awt.event.KeyListener)8 JCheckBox (javax.swing.JCheckBox)8 ChangeListener (javax.swing.event.ChangeListener)8 Color (java.awt.Color)7 GridBagConstraints (java.awt.GridBagConstraints)7 MouseAdapter (java.awt.event.MouseAdapter)7