Search in sources :

Example 1 with KeyStoreEntryDragGestureListener

use of org.kse.gui.dnd.KeyStoreEntryDragGestureListener in project keystore-explorer by kaikramer.

the class KseFrame method createEmptyKeyStoreTable.

private JTable createEmptyKeyStoreTable() {
    KeyStoreTableModel ksModel = new KeyStoreTableModel();
    final JTable jtKeyStore = new JKseTable(ksModel);
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(ksModel);
    jtKeyStore.setRowSorter(sorter);
    jtKeyStore.setShowGrid(false);
    jtKeyStore.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jtKeyStore.getTableHeader().setReorderingAllowed(false);
    jtKeyStore.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    // min. height of 18 because of 16x16 icons
    jtKeyStore.setRowHeight(Math.max(18, jtKeyStore.getRowHeight()));
    // Register cut, copy and paste actions with the relevant keystrokes
    jtKeyStore.getInputMap().put((KeyStroke) cutAction.getValue(Action.ACCELERATOR_KEY), CUT_KEY);
    jtKeyStore.getActionMap().put(CUT_KEY, cutAction);
    jtKeyStore.getInputMap().put((KeyStroke) copyAction.getValue(Action.ACCELERATOR_KEY), COPY_KEY);
    jtKeyStore.getActionMap().put(COPY_KEY, copyAction);
    jtKeyStore.getInputMap().put((KeyStroke) pasteAction.getValue(Action.ACCELERATOR_KEY), PASTE_KEY);
    jtKeyStore.getActionMap().put(PASTE_KEY, pasteAction);
    // open keystore entry details when user presses enter key
    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    jtKeyStore.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, "Enter");
    jtKeyStore.getActionMap().put("Enter", new AbstractAction() {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent ae) {
            try {
                int selectedRow = jtKeyStore.getSelectedRow();
                if (selectedRow != -1) {
                    CursorUtil.setCursorBusy(frame);
                    showSelectedEntryDetails(jtKeyStore, selectedRow);
                }
            } finally {
                CursorUtil.setCursorFree(frame);
            }
        }
    });
    // Add DnD source support to KeyStore
    DragSource ds = DragSource.getDefaultDragSource();
    ds.createDefaultDragGestureRecognizer(jtKeyStore, DnDConstants.ACTION_MOVE, new KeyStoreEntryDragGestureListener(this));
    // Add custom renderers for headers and cells
    for (int i = 0; i < jtKeyStore.getColumnCount(); i++) {
        TableColumn column = jtKeyStore.getColumnModel().getColumn(i);
        column.setHeaderRenderer(new KeyStoreTableHeadRend(jtKeyStore.getTableHeader().getDefaultRenderer()));
        column.setCellRenderer(new KeyStoreTableCellRend());
    }
    // Make the first three columns small and not resizable as they hold icons
    TableColumn typeCol = jtKeyStore.getColumnModel().getColumn(0);
    typeCol.setResizable(false);
    typeCol.setMinWidth(28);
    typeCol.setMaxWidth(28);
    typeCol.setPreferredWidth(28);
    TableColumn statusCol = jtKeyStore.getColumnModel().getColumn(1);
    statusCol.setResizable(false);
    statusCol.setMinWidth(28);
    statusCol.setMaxWidth(28);
    statusCol.setPreferredWidth(28);
    TableColumn certExpiredCol = jtKeyStore.getColumnModel().getColumn(2);
    certExpiredCol.setResizable(false);
    certExpiredCol.setMinWidth(28);
    certExpiredCol.setMaxWidth(28);
    certExpiredCol.setPreferredWidth(28);
    jtKeyStore.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent evt) {
            maybeShowSelectedEntryDetails(evt);
        }

        @Override
        public void mousePressed(MouseEvent evt) {
            maybeShowSelectedEntryPopupMenu(evt);
        }

        @Override
        public void mouseReleased(MouseEvent evt) {
            maybeShowSelectedEntryPopupMenu(evt);
        }
    });
    jtKeyStore.addKeyListener(new KeyAdapter() {

        boolean deleteLastPressed = false;

        @Override
        public void keyPressed(KeyEvent evt) {
            // Record delete pressed on non-Macs
            if (!OperatingSystem.isMacOs()) {
                deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE;
            }
        }

        @Override
        public void keyReleased(KeyEvent evt) {
            // Delete on non-Mac if delete was pressed and is now released
            if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) {
                deleteLastPressed = false;
                handleDeleteSelectedEntry();
            }
        }

        @Override
        public void keyTyped(KeyEvent evt) {
            // Delete on Mac if back space typed
            if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) {
                handleDeleteSelectedEntry();
            }
        }
    });
    return jtKeyStore;
}
Also used : MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseAdapter(java.awt.event.MouseAdapter) DragSource(java.awt.dnd.DragSource) TableColumn(javax.swing.table.TableColumn) Point(java.awt.Point) KeyEvent(java.awt.event.KeyEvent) KeyStoreEntryDragGestureListener(org.kse.gui.dnd.KeyStoreEntryDragGestureListener) JTable(javax.swing.JTable) KeyStroke(javax.swing.KeyStroke) AbstractAction(javax.swing.AbstractAction) TableModel(javax.swing.table.TableModel) TableRowSorter(javax.swing.table.TableRowSorter)

Aggregations

Point (java.awt.Point)1 DragSource (java.awt.dnd.DragSource)1 ActionEvent (java.awt.event.ActionEvent)1 KeyAdapter (java.awt.event.KeyAdapter)1 KeyEvent (java.awt.event.KeyEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 AbstractAction (javax.swing.AbstractAction)1 JTable (javax.swing.JTable)1 KeyStroke (javax.swing.KeyStroke)1 TableColumn (javax.swing.table.TableColumn)1 TableModel (javax.swing.table.TableModel)1 TableRowSorter (javax.swing.table.TableRowSorter)1 KeyStoreEntryDragGestureListener (org.kse.gui.dnd.KeyStoreEntryDragGestureListener)1