⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 columncontrolbutton.java

📁 java实现浏览器等本地桌面的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * be sure to have an extended TableColumnModel     *      * @return     */    private boolean canControl() {        return table.getColumnModel() instanceof TableColumnModelExt;    } //  ------------------------ updating the popupMenu    /**     * Populates the popup menu with actions related to controlling columns.     * Adds an action for each columns in the table (including both visible and     * hidden). Adds all column-related actions from the table's actionMap.     */    protected void populatePopupMenu() {        clearPopupMenu();        if (canControl()) {            addColumnMenuItems();        }        addColumnActions();    }    /**     *      * removes all components from the popup, making sure to release all     * columnVisibility actions.     *      */    private void clearPopupMenu() {        clearColumnVisibilityActions();        popupMenu.removeAll();    }    /**     * release actions and clear list of actions.     *      */    private void clearColumnVisibilityActions() {        if (columnVisibilityActions == null)            return;        for (Iterator<ColumnVisibilityAction> iter = columnVisibilityActions                .iterator(); iter.hasNext();) {            iter.next().releaseColumn();        }        columnVisibilityActions.clear();    }       /**     * adds a action to toggle column visibility for every column currently in     * the table. This includes both visible and invisible columns.     *      * pre: canControl()     *      */    private void addColumnMenuItems() {        // For each column in the view, add a JCheckBoxMenuItem to popup        List columns = table.getColumns(true);        ActionContainerFactory factory = new ActionContainerFactory(null);        for (Iterator iter = columns.iterator(); iter.hasNext();) {            TableColumn column = (TableColumn) iter.next();            ColumnVisibilityAction action = new ColumnVisibilityAction(column);            getColumnVisibilityActions().add(action);            popupMenu.add(factory.createMenuItem(action));        }    }    private List<ColumnVisibilityAction> getColumnVisibilityActions() {        if (columnVisibilityActions == null) {            columnVisibilityActions = new ArrayList<ColumnVisibilityAction>();        }        return columnVisibilityActions;    }    /**     * add additional actions from the xtable's actionMap.     *      */    private void addColumnActions() {        Object[] actionKeys = getColumnControlActionKeys();        Arrays.sort(actionKeys);        List actions = new ArrayList();        for (int i = 0; i < actionKeys.length; i++) {            if (isColumnControlActionKey(actionKeys[i])) {                actions.add(table.getActionMap().get(actionKeys[i]));            }        }        if (actions.size() == 0)            return;        if (canControl()) {            popupMenu.addSeparator();        }        ActionContainerFactory factory = new ActionContainerFactory(null);        for (Iterator iter = actions.iterator(); iter.hasNext();) {            Action action = (Action) iter.next();            popupMenu.add(factory.createMenuItem(action));        }    }    /**     * @return     */    private Object[] getColumnControlActionKeys() {        Object[] allKeys = table.getActionMap().allKeys();        List columnKeys = new ArrayList();        for (int i = 0; i < allKeys.length; i++) {            if (isColumnControlActionKey(allKeys[i])) {                columnKeys.add(allKeys[i]);            }        }        return columnKeys.toArray();    }    private boolean isColumnControlActionKey(Object actionKey) {        return (actionKey instanceof String) && ((String) actionKey).startsWith(COLUMN_CONTROL_MARKER);    }    //--------------------------- init    private void installTable(JXTable table) {        this.table = table;        table.addPropertyChangeListener(columnModelChangeListener);        updateFromColumnModelChange(null);        updateFromTableEnabledChanged();    }    /**     * Initialize the column control button's gui     */    private void init() {        setFocusPainted(false);        setFocusable(false);        // create the popup menu        popupMenu = new JPopupMenu();        // this is a trick to get hold of the client prop which        // prevents closing of the popup        JComboBox box = new JComboBox();        Object preventHide = box.getClientProperty("doNotCancelPopup");        putClientProperty("doNotCancelPopup", preventHide);    }    /**      * the action created for this.     *      * @param icon     * @return     */    private Action createControlAction(Icon icon) {        Action control = new AbstractAction() {            public void actionPerformed(ActionEvent e) {                togglePopup();            }        };        control.putValue(Action.SMALL_ICON, icon);        return control;    }        // -------------------------------- listeners    private PropertyChangeListener columnModelChangeListener = new PropertyChangeListener() {        public void propertyChange(PropertyChangeEvent evt) {            if ("columnModel".equals(evt.getPropertyName())) {                updateFromColumnModelChange((TableColumnModel) evt.getOldValue());            } else if ("enabled".equals(evt.getPropertyName())) {                updateFromTableEnabledChanged();            }        }    };    private TableColumnModelListener columnModelListener = new TableColumnModelListener() {        /** Tells listeners that a column was added to the model. */        public void columnAdded(TableColumnModelEvent e) {            // quickfix for #192            if (!isVisibilityChange(e, true)) {                populatePopupMenu();            }        }        /** Tells listeners that a column was removed from the model. */        public void columnRemoved(TableColumnModelEvent e) {            if (!isVisibilityChange(e, false)) {                populatePopupMenu();            }        }        /**         * check if the add/remove event is triggered by a move to/from the         * invisible columns.         *          * PRE: the event must be received in columnAdded/Removed.         *          * @param e         *            the received event         * @param added         *            if true the event is assumed to be received via         *            columnAdded, otherwise via columnRemoved.         * @return         */        private boolean isVisibilityChange(TableColumnModelEvent e,                boolean added) {            // can't tell            if (!(e.getSource() instanceof DefaultTableColumnModelExt))                return false;            DefaultTableColumnModelExt model = (DefaultTableColumnModelExt) e                    .getSource();            if (added) {                return model.isAddedFromInvisibleEvent(e.getToIndex());            } else {                return model.isRemovedToInvisibleEvent(e.getFromIndex());            }        }        /** Tells listeners that a column was repositioned. */        public void columnMoved(TableColumnModelEvent e) {        }        /** Tells listeners that a column was moved due to a margin change. */        public void columnMarginChanged(ChangeEvent e) {        }        /**         * Tells listeners that the selection model of the TableColumnModel         * changed.         */        public void columnSelectionChanged(ListSelectionEvent e) {        }    };} // end class ColumnControlButton

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -