001: // ============================================================================
002: // $Id: ComponentEditor.java,v 1.2 2005/08/14 20:25:52 davidahall Exp $
003: // Copyright (c) 2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.swing.spreadsheet;
034:
035: import java.awt.Component;
036: import java.awt.KeyboardFocusManager;
037: import javax.swing.AbstractCellEditor;
038: import javax.swing.JComponent;
039: import javax.swing.JLabel;
040: import javax.swing.JTable;
041: import javax.swing.UIManager;
042: import javax.swing.table.TableCellEditor;
043:
044: /**
045: * TableCellEditor used when the contents of the cell are a JComponent. The
046: * component itself will be used as the editor
047: * <p>
048: * Copyright © 2005 David A. Hall
049: */
050:
051: public class ComponentEditor extends AbstractCellEditor implements
052: TableCellEditor {
053:
054: static final long serialVersionUID = -2906356624578134466L;
055:
056: // default instance for general use
057: static private ComponentEditor _instance;
058:
059: /**
060: * Returns a sharable ComponentEditor instance.
061: */
062: static public synchronized ComponentEditor getInstance() {
063: if (_instance == null)
064: _instance = new ComponentEditor();
065:
066: return _instance;
067: }
068:
069: public ComponentEditor() {
070: }
071:
072: // Temporarily stores the 'value' of the editor, from the time that the component
073: // is to be shown to the user to the time that the user is finished with it, in
074: // which case the it is returned by the getCellEditorValue() method during the
075: // handling of the editingStopped message.
076: private Object _value;
077:
078: // Bogus component used when we get an unexpected value
079: private JLabel _label = new JLabel();
080:
081: // ---------------------------------
082: // AbstractCellEditor Implementation
083: // ---------------------------------
084:
085: /**
086: * Returns the value of the cell once the editing is complete. The value is the
087: * same component that was stored in the cell, but it's state may have been changed
088: * in some way.
089: */
090: public Object getCellEditorValue() {
091: return _value;
092: }
093:
094: // ------------------------------
095: // TableCellEditor Implementation
096: // ------------------------------
097:
098: /**
099: * Returns the Component stored in the table at the given cell (expecting that the
100: * component is being passed as the 'object' parm), so that it may be temporarily
101: * made available to the user for interaction.
102: */
103: public Component getTableCellEditorComponent(JTable table,
104: Object object, boolean isSelected, int row, int col) {
105: _value = object;
106:
107: if (object instanceof JComponent) {
108: return (JComponent) _value;
109: }
110:
111: // We don't know how to handle non-component values, so let's just display the value
112: // we were givin in something the user can't mess with. We'll do our best to pretend
113: // to be a default renderer
114:
115: _label.setForeground(table.getSelectionForeground());
116: _label.setBackground(table.getSelectionBackground());
117: _label.setBorder(UIManager
118: .getBorder("Table.focusCellHighlightBorder"));
119: _label.setFont(table.getFont());
120: _label.setText(object.toString());
121: _label.setOpaque(true);
122: return _label;
123: }
124:
125: /**
126: * Overrides stopCellEditing to remove the focus from the component stored in
127: * the currently active cell.
128: */
129: public boolean stopCellEditing() {
130: KeyboardFocusManager.getCurrentKeyboardFocusManager()
131: .clearGlobalFocusOwner();
132: return super.stopCellEditing();
133: }
134: }
|