📄 browsingcelleditor.java
字号:
/* * BrowsingCellEditor.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.components.table;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.Serializable;import java.util.EventObject;import javax.swing.BorderFactory;import javax.swing.DefaultCellEditor;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.UIManager;import javax.swing.border.Border;import javax.swing.border.LineBorder;import javax.swing.event.CellEditorListener;import javax.swing.event.ChangeEvent;import javax.swing.event.EventListenerList;import javax.swing.table.TableCellEditor;import javax.swing.table.TableCellRenderer;import org.executequery.Constants;import org.underworldlabs.swing.table.TableCellEditorValue;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * Table cell editor with a button to the right for option * selection or similar. * * @author Takis Diakoumis * @version $Revision: 1.5 $ * @date $Date: 2006/07/16 15:47:56 $ */public abstract class BrowsingCellEditor extends DefaultCellEditor implements TableCellEditorValue, TableCellRenderer, TableCellEditor, ActionListener, FocusListener { transient protected ChangeEvent changeEvent = null; protected EventListenerList listenerList = new EventListenerList(); protected EditorDelegate delegate; /** the selection button */ protected BrowseButton browseButton; /** the editor component */ protected JTextField textField; /** the editor component insets */ protected static Insets textFieldInsets; /** the editor component focus border */ protected static Border textFieldFocusBorder; /** the editor component focus border colour */ protected static Color focusBorderColor; /** the selection button background colour */ protected static Color buttonBackground; /** the selection button icon colour */ protected static Color iconColor; /** the base panel the components are rendered onto */ private RendererbasePanel base; static { Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder"); if (focusBorder instanceof LineBorder) { focusBorderColor = ((LineBorder)focusBorder).getLineColor(); textFieldFocusBorder = BorderFactory. createMatteBorder(1, 1, 1, 0, focusBorderColor); } textFieldInsets = new Insets(0, 2, 0, 0); iconColor = Color.DARK_GRAY.darker(); } /** Creates a new instance of ComboBoxCellRenderer */ public BrowsingCellEditor() { super(new CellTextField()); // assign the editor component textField = (CellTextField)editorComponent; //super(new BorderLayout()); base = new RendererbasePanel(); textField.setBorder(null); textField.setMargin(textFieldInsets); textField.setDisabledTextColor(UIManager.getColor("Table.foreground")); delegate = new EditorDelegate(); textField.addActionListener(delegate); textField.addFocusListener(this); browseButton = new BrowseButton(); browseButton.addActionListener(this); base.add(textField, BorderLayout.CENTER); base.add(browseButton, BorderLayout.EAST); } /** Indicates this has focus */ private static boolean hasFocusOnLabel; public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { //Log.debug("getTableCellEditorComponent "+row); hasFocusOnLabel = false; textField.setFont(table.getFont()); delegate.setValue(value); textField.setEnabled(true); return base; } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean cellHasFocus, int row, int col) { //Log.debug("getTableCellRendererComponent " +row); hasFocusOnLabel = cellHasFocus; textField.setEnabled(false); buttonBackground = table.getBackground(); textField.setBackground(buttonBackground); base.setBackground(buttonBackground); delegate.setValue(value); return base; } public boolean isFocusable() { return true; } public boolean getFocusTraversalKeysEnabled() { return true; } public void setDelegateValue(Object value) { delegate.setValue(value); } /** * Returns the current editor value from the component * defining this object. * * @return the editor's value */ public String getEditorValue() { return getCellEditorValue().toString(); } public void addKeyListener(KeyListener listener) { if (textField != null) { textField.addKeyListener(listener); } } public void removeKeyListener(KeyListener listener) { if (textField != null) { textField.removeKeyListener(listener); } } /** * Defines the action to be taken upon activation of the * selection button. * * @param e - the event */ public abstract void actionPerformed(ActionEvent e); public void focusGained(FocusEvent e) { //Log.debug("focusGained"); } public void focusLost(FocusEvent e) { fireEditingStopped(); } public void setFont(Font font) { textField.setFont(font); } /** * Returns a reference to the editor component. * * @return the editor <code>Component</code> */ public Component getComponent() { return textField; } // ---------------------------- // borrowed from javax.swing.DefaultCellEditor public void addCellEditorListener(CellEditorListener l) { listenerList.add(CellEditorListener.class, l); } public void removeCellEditorListener(CellEditorListener l) { listenerList.remove(CellEditorListener.class, l); } public CellEditorListener[] getCellEditorListeners() { return (CellEditorListener[])listenerList.getListeners( CellEditorListener.class); } protected void fireEditingStopped() { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length-2; i>=0; i-=2) { if (listeners[i]==CellEditorListener.class) { // Lazily create the event: if (changeEvent == null) changeEvent = new ChangeEvent(this); ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent); } } super.fireEditingStopped(); } protected void fireEditingCanceled() { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length-2; i>=0; i-=2) { if (listeners[i]==CellEditorListener.class) { // Lazily create the event: if (changeEvent == null) changeEvent = new ChangeEvent(this); ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent); } } super.fireEditingCanceled(); } public Object getCellEditorValue() { return delegate.getCellEditorValue(); } public boolean isCellEditable(EventObject anEvent) { return delegate.isCellEditable(anEvent); } public boolean shouldSelectCell(EventObject anEvent) { return delegate.shouldSelectCell(anEvent); } public boolean stopCellEditing() { return delegate.stopCellEditing(); } public void cancelCellEditing() { delegate.cancelCellEditing();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -