View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.components;
20  
21  import java.awt.Color;
22  import java.awt.Component;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  
26  import javax.swing.DefaultCellEditor;
27  import javax.swing.JButton;
28  import javax.swing.JCheckBox;
29  import javax.swing.JTable;
30  
31  /***
32  * A TableCellEditor that edits colors - it launches a color dialog when clicked.
33  *
34  * @author michael@mpowers.net
35  * @author $Author: cgruber $
36  * @version $Revision: 904 $
37  */
38  class ColorCellEditor extends DefaultCellEditor {
39      Color currentColor = null;
40  
41      public ColorCellEditor(JButton b)
42      {
43          super(new JCheckBox()); // unfortunately, the constructor
44                                  // expects a check box, combo box,
45                                  // or text field.
46          editorComponent = b;
47          setClickCountToStart(1); // this is usually 1 or 2.
48  
49          // must do this so that editing stops when appropriate.
50          b.addActionListener(new ActionListener()
51          {
52              public void actionPerformed(ActionEvent e)
53              {
54                  fireEditingStopped();
55              }
56          }
57          );
58      }
59  
60      protected void fireEditingStopped()
61      {
62          super.fireEditingStopped();
63      }
64  
65      public Object getCellEditorValue()
66      {
67          return currentColor;
68      }
69  
70      public Component getTableCellEditorComponent(JTable table,
71                                                   Object value,
72                                                   boolean isSelected,
73                                                   int row,
74                                                   int column)
75      {
76          ((JButton)editorComponent).setText(value.toString());
77          currentColor = (Color)value;
78          return editorComponent;
79      }
80  }
81  
82  
83  
84