📄 columncontrolbutton.java
字号:
/* * $Id: ColumnControlButton.java,v 1.15 2005/10/13 08:59:56 kleopatra Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package org.jdesktop.swingx.table;import java.awt.ComponentOrientation;import java.awt.Dimension;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ItemEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JPopupMenu;import javax.swing.event.ChangeEvent;import javax.swing.event.ListSelectionEvent;import javax.swing.event.TableColumnModelEvent;import javax.swing.event.TableColumnModelListener;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;import org.jdesktop.swingx.JXTable;import org.jdesktop.swingx.action.AbstractActionExt;import org.jdesktop.swingx.action.ActionContainerFactory;/** * This class is installed in the upper right corner of the table and is a * control which allows for toggling the visibilty of individual columns. * * TODO: the table reference is a potential leak * * TODO: no need to extend JButton - use non-visual controller returning * a JComponent instead. * */public final class ColumnControlButton extends JButton { /** exposed for testing. */ protected JPopupMenu popupMenu = null; /** the table which is controlled by this. */ private JXTable table; /** a marker to auto-recognize actions which should be added to the popup */ public static final String COLUMN_CONTROL_MARKER = "column."; /** the list of actions for column menuitems.*/ private List<ColumnVisibilityAction> columnVisibilityActions; public ColumnControlButton(JXTable table, Icon icon) { super(); init(); setAction(createControlAction(icon)); installTable(table); } public void updateUI() { super.updateUI(); setMargin(new Insets(1, 2, 2, 1)); // Make this LAF-independent if (popupMenu != null) { // JW: Hmm, not really working.... popupMenu.updateUI(); } } //-------------------------- Action in synch with column properties /** * A specialized action which takes care of keeping in synch with * TableColumn state. * * NOTE: client must call releaseColumn if this action is no longer needed! * */ public class ColumnVisibilityAction extends AbstractActionExt { private TableColumn column; private PropertyChangeListener columnListener; public ColumnVisibilityAction(TableColumn column) { super((String) null); setStateAction(); installColumn(column); } /** * * release listening to column. Client must call this method if the * action is no longer needed. After calling it the action must not be * used any longer. */ public void releaseColumn() { column.removePropertyChangeListener(columnListener); column = null; } public boolean isEnabled() { return super.isEnabled() && canControl(); } private boolean canControl() { return (column instanceof TableColumnExt); } public void itemStateChanged(ItemEvent e) { if (canControl()) { if ((e.getStateChange() == ItemEvent.DESELECTED) && (table.getColumnCount() <= 1)) { reselect(); } else { ((TableColumnExt) column) .setVisible(e.getStateChange() == ItemEvent.SELECTED); } } } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } private void updateSelected() { boolean visible = true; if (canControl()) { visible = ((TableColumnExt) column).isVisible(); } setSelected(visible); } private void reselect() { firePropertyChange("selected", null, Boolean.TRUE); } // -------------- init private void installColumn(TableColumn column) { this.column = column; column.addPropertyChangeListener(getColumnListener()); setName(String.valueOf(column.getHeaderValue())); setActionCommand(column.getIdentifier()); updateSelected(); } private PropertyChangeListener getColumnListener() { if (columnListener == null) { columnListener = createPropertyChangeListener(); } return columnListener; } private PropertyChangeListener createPropertyChangeListener() { PropertyChangeListener l = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("visible".equals(evt.getPropertyName())) { updateSelected(); } else if ("headerValue".equals(evt.getPropertyName())) { setName(String.valueOf(evt.getNewValue())); } } }; return l; } } /** * open the popup. * * hmmm... really public? * * */ public void togglePopup() { if (popupMenu.isVisible()) { popupMenu.setVisible(false); } else if (popupMenu.getComponentCount() > 0) { Dimension buttonSize = getSize(); popupMenu.show(this, buttonSize.width - popupMenu.getPreferredSize().width, buttonSize.height); } } @Override public void applyComponentOrientation(ComponentOrientation o) { super.applyComponentOrientation(o); popupMenu.applyComponentOrientation(o); }//-------------------------- updates from table propertyChangelistnere /** * adjust internal state to after table's column model property has changed. * Handles cleanup of listeners to the old/new columnModel (listens to the * new only if we can control column visibility) and content of popupMenu. * * @param oldModel */ private void updateFromColumnModelChange(TableColumnModel oldModel) { if (oldModel != null) { oldModel.removeColumnModelListener(columnModelListener); } populatePopupMenu(); if (canControl()) { table.getColumnModel().addColumnModelListener(columnModelListener); } } protected void updateFromTableEnabledChanged() { getAction().setEnabled(table.isEnabled()); } /** * Method to check if we can control column visibility POST: if true we can
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -