KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > swing > spreadsheet > ComponentEditor


1 // ============================================================================
2
// $Id: ComponentEditor.java,v 1.2 2005/08/14 20:25:52 davidahall Exp $
3
// Copyright (c) 2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.swing.spreadsheet;
34
35 import java.awt.Component JavaDoc;
36 import java.awt.KeyboardFocusManager JavaDoc;
37 import javax.swing.AbstractCellEditor JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JTable JavaDoc;
41 import javax.swing.UIManager JavaDoc;
42 import javax.swing.table.TableCellEditor JavaDoc;
43
44 /**
45  * TableCellEditor used when the contents of the cell are a JComponent. The
46  * component itself will be used as the editor
47  * <p>
48  * Copyright &copy; 2005 David A. Hall
49  */

50
51 public class ComponentEditor extends AbstractCellEditor JavaDoc implements TableCellEditor JavaDoc {
52
53     static final long serialVersionUID = -2906356624578134466L;
54
55     // default instance for general use
56
static private ComponentEditor _instance;
57
58     /**
59      * Returns a sharable ComponentEditor instance.
60      */

61     static public synchronized ComponentEditor getInstance() {
62         if (_instance == null)
63             _instance = new ComponentEditor();
64
65         return _instance;
66     }
67
68     public ComponentEditor (){
69     }
70
71     // Temporarily stores the 'value' of the editor, from the time that the component
72
// is to be shown to the user to the time that the user is finished with it, in
73
// which case the it is returned by the getCellEditorValue() method during the
74
// handling of the editingStopped message.
75
private Object JavaDoc _value;
76
77     // Bogus component used when we get an unexpected value
78
private JLabel JavaDoc _label = new JLabel JavaDoc();
79     
80     // ---------------------------------
81
// AbstractCellEditor Implementation
82
// ---------------------------------
83

84     /**
85      * Returns the value of the cell once the editing is complete. The value is the
86      * same component that was stored in the cell, but it's state may have been changed
87      * in some way.
88      */

89     public Object JavaDoc getCellEditorValue() {
90         return _value;
91     }
92         
93     // ------------------------------
94
// TableCellEditor Implementation
95
// ------------------------------
96

97     /**
98      * Returns the Component stored in the table at the given cell (expecting that the
99      * component is being passed as the 'object' parm), so that it may be temporarily
100      * made available to the user for interaction.
101      */

102     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc object, boolean isSelected,
103                                                  int row, int col)
104     {
105         _value = object;
106         
107         if(object instanceof JComponent JavaDoc) {
108             return (JComponent JavaDoc) _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.getBorder("Table.focusCellHighlightBorder"));
118         _label.setFont(table.getFont());
119         _label.setText(object.toString());
120         _label.setOpaque(true);
121         return _label;
122     }
123
124     /**
125      * Overrides stopCellEditing to remove the focus from the component stored in
126      * the currently active cell.
127      */

128     public boolean stopCellEditing() {
129         KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
130         return super.stopCellEditing();
131     }
132 }
133
Popular Tags