Search in sources :

Example 1 with TableFocusManager

use of com.haulmont.cuba.desktop.sys.vcl.TableFocusManager in project cuba by cuba-platform.

the class DesktopAbstractTable method initComponent.

protected void initComponent() {
    layout = new MigLayout("flowy, fill, insets 0", "", "[min!][fill]");
    panel = new JPanel(layout);
    topPanel = new JPanel(new BorderLayout());
    topPanel.setVisible(false);
    panel.add(topPanel, "growx");
    scrollPane = new JScrollPane(impl);
    impl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    impl.setFillsViewportHeight(true);
    panel.add(scrollPane, "grow");
    impl.setShowGrid(true);
    impl.setGridColor(Color.lightGray);
    impl.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                handleClickAction();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            showPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            showPopup(e);
        }

        protected void showPopup(MouseEvent e) {
            if (e.isPopupTrigger() && contextMenuEnabled) {
                // select row
                Point p = e.getPoint();
                int viewRowIndex = impl.rowAtPoint(p);
                int rowNumber;
                if (viewRowIndex >= 0) {
                    rowNumber = impl.convertRowIndexToModel(viewRowIndex);
                } else {
                    rowNumber = -1;
                }
                ListSelectionModel model = impl.getSelectionModel();
                if (!model.isSelectedIndex(rowNumber)) {
                    model.setSelectionInterval(rowNumber, rowNumber);
                }
                // show popup menu
                JPopupMenu popupMenu = createPopupMenu();
                if (popupMenu.getComponentCount() > 0) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
    });
    ColumnControlButton columnControlButton = new ColumnControlButton(impl) {

        @Override
        protected ColumnVisibilityAction createColumnVisibilityAction(TableColumn column) {
            ColumnVisibilityAction columnVisibilityAction = super.createColumnVisibilityAction(column);
            columnVisibilityAction.addPropertyChangeListener(evt -> {
                if ("SwingSelectedKey".equals(evt.getPropertyName()) && evt.getNewValue() instanceof Boolean) {
                    ColumnVisibilityAction action = (ColumnVisibilityAction) evt.getSource();
                    String columnName = action.getActionCommand();
                    boolean collapsed = !((boolean) evt.getNewValue());
                    Column col = getColumn(columnName);
                    if (col != null) {
                        col.setCollapsed(collapsed);
                    }
                }
            });
            return columnVisibilityAction;
        }
    };
    impl.setColumnControl(columnControlButton);
    impl.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
    impl.getActionMap().put("enter", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (enterPressAction != null) {
                enterPressAction.actionPerform(DesktopAbstractTable.this);
            } else {
                handleClickAction();
            }
        }
    });
    Messages messages = AppBeans.get(Messages.NAME);
    // localize default column control actions
    for (Object actionKey : impl.getActionMap().allKeys()) {
        if ("column.packAll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packAll"));
        } else if ("column.packSelected".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packSelected"));
        } else if ("column.horizontalScroll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.horizontalScroll"));
        }
    }
    // Ability to configure fonts in table
    // Add action to column control
    String configureFontsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.configureFontsLabel");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "fonts", new AbstractAction(configureFontsLabel) {

        @Override
        public void actionPerformed(ActionEvent e) {
            Component rootComponent = SwingUtilities.getRoot(impl);
            final FontDialog fontDialog = FontDialog.show(rootComponent, impl.getFont());
            fontDialog.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosed(WindowEvent e) {
                    Font result = fontDialog.getResult();
                    if (result != null) {
                        impl.setFont(result);
                        packRows();
                    }
                }
            });
            fontDialog.open();
        }
    });
    // Ability to reset settings
    String resetSettingsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.resetSettings");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "resetSettings", new AbstractAction(resetSettingsLabel) {

        @Override
        public void actionPerformed(ActionEvent e) {
            resetPresentation();
        }
    });
    scrollPane.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            if (!columnsInitialized) {
                adjustColumnHeaders();
            }
            columnsInitialized = true;
        }
    });
    if (impl instanceof FocusableTable) {
        FocusableTable table = (FocusableTable) impl;
        table.setFocusManager(new TableFocusManager(impl) {

            @Override
            public void scrollToSelectedRow(int selectedRow) {
                if (automaticScrollEnabled) {
                    super.scrollToSelectedRow(selectedRow);
                }
            }
        });
    }
    // init default row height
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            if (!fontInitialized) {
                applyFont(impl, impl.getFont());
            }
        }
    });
}
Also used : FontDialog(com.haulmont.cuba.desktop.sys.FontDialog) FocusableTable(com.haulmont.cuba.desktop.sys.vcl.FocusableTable) TableColumn(javax.swing.table.TableColumn) TableFocusManager(com.haulmont.cuba.desktop.sys.vcl.TableFocusManager) Component(java.awt.Component) AbstractAction(javax.swing.AbstractAction) ColumnControlButton(org.jdesktop.swingx.table.ColumnControlButton) MigLayout(net.miginfocom.swing.MigLayout) TableColumn(javax.swing.table.TableColumn) BoundAction(org.jdesktop.swingx.action.BoundAction)

Aggregations

FontDialog (com.haulmont.cuba.desktop.sys.FontDialog)1 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)1 TableFocusManager (com.haulmont.cuba.desktop.sys.vcl.TableFocusManager)1 Component (java.awt.Component)1 AbstractAction (javax.swing.AbstractAction)1 TableColumn (javax.swing.table.TableColumn)1 MigLayout (net.miginfocom.swing.MigLayout)1 BoundAction (org.jdesktop.swingx.action.BoundAction)1 ColumnControlButton (org.jdesktop.swingx.table.ColumnControlButton)1