View Javadoc

1   package nl.tudelft.simulation.introspection.mapping;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   
8   import javax.swing.AbstractCellEditor;
9   import javax.swing.JColorChooser;
10  import javax.swing.JDialog;
11  import javax.swing.JPanel;
12  import javax.swing.JTable;
13  import javax.swing.table.TableCellEditor;
14  
15  /***
16   * Starts up a default {see javax.swing.JColorChooser}panel to edit the color
17   * value.
18   * <p>
19   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
20   * University of Technology </a>, the Netherlands. <br>
21   * See for project information <a
22   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
23   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
24   * General Public License (LGPL) </a>, no warranty.
25   * 
26   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
27   * @version 1.2 Apr 15, 2004
28   * @since 1.5
29   */
30  public class MyColorEditor extends AbstractCellEditor implements
31          TableCellEditor
32  {
33      /*** the value */
34      protected Color value;
35  
36      /*** the cellPanel */
37      protected JPanel cellPanel = new JPanel();
38  
39      /***
40       * The OK listener
41       */
42      private class OKListener implements ActionListener
43      {
44          /*** the color chooser */
45          private JColorChooser chooser;
46  
47          /***
48           * constructs a new OKListener
49           * 
50           * @param chooser the color chooser.
51           */
52          public OKListener(JColorChooser chooser)
53          {
54              this.chooser = chooser;
55          }
56  
57          /***
58           * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
59           */
60          public synchronized void actionPerformed(ActionEvent event)
61          {
62              MyColorEditor.this.value = this.chooser.getColor();
63              MyColorEditor.this.stopCellEditing();
64              MyColorEditor.this.cellPanel.setBackground(MyColorEditor.this.value
65                      .darker());
66              MyColorEditor.this.cellPanel
67                      .paintImmediately(MyColorEditor.this.cellPanel.getBounds());
68          }
69      }
70  
71      /***
72       * The CancelListener
73       */
74      private class CancelListener implements ActionListener
75      {
76          /***
77           * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
78           */
79          public void actionPerformed(ActionEvent e)
80          {
81              MyColorEditor.this.cancelCellEditing();
82          }
83      }
84  
85      /***
86       * @see javax.swing.CellEditor#getCellEditorValue()
87       */
88      public Object getCellEditorValue()
89      {
90          return this.value;
91      }
92  
93      /***
94       * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(JTable,
95       *      Object, boolean, int, int)
96       */
97      public Component getTableCellEditorComponent(final JTable table,
98              final Object value, final boolean isSelected, final int row,
99              final int column)
100     {
101         this.cellPanel.setBackground(((Color) value).darker());
102         this.value = (Color) value;
103         JColorChooser chooser = new JColorChooser((Color) value);
104         JDialog dialog = JColorChooser.createDialog(table, "Color selection",
105                 false, chooser, new OKListener(chooser), new CancelListener());
106         dialog.setVisible(true);
107         return this.cellPanel;
108     }
109 }