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 2003 <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/gpl.html">General Public
24   * License (GPL) </a>, no warranty <br>
25   * 
26   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
27   *         Jacobs </a>
28   * @version 1.2 Apr 15, 2004
29   * @since 1.4
30   */
31  public class MyColorEditor extends AbstractCellEditor implements
32  		TableCellEditor
33  {
34  	/*** the value */
35  	protected Color value;
36  
37  	/*** the cellPanel */
38  	protected JPanel cellPanel = new JPanel();
39  
40  	/***
41  	 * The OK listener
42  	 */
43  	private class OKListener implements ActionListener
44  	{
45  		/*** the color chooser */
46  		private JColorChooser chooser;
47  
48  		/***
49  		 * constructs a new OKListener
50  		 * 
51  		 * @param chooser the color chooser.
52  		 */
53  		public OKListener(JColorChooser chooser)
54  		{
55  			this.chooser = chooser;
56  		}
57  
58  		/***
59  		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
60  		 */
61  		public synchronized void actionPerformed(ActionEvent event)
62  		{
63  			MyColorEditor.this.value = this.chooser.getColor();
64  			MyColorEditor.this.stopCellEditing();
65  			MyColorEditor.this.cellPanel.setBackground(MyColorEditor.this.value
66  					.darker());
67  			MyColorEditor.this.cellPanel
68  					.paintImmediately(MyColorEditor.this.cellPanel.getBounds());
69  		}
70  	}
71  
72  	/***
73  	 * The CancelListener
74  	 */
75  	private class CancelListener implements ActionListener
76  	{
77  		/***
78  		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
79  		 */
80  		public void actionPerformed(ActionEvent e)
81  		{
82  			MyColorEditor.this.cancelCellEditing();
83  		}
84  	}
85  
86  	/***
87  	 * @see javax.swing.CellEditor#getCellEditorValue()
88  	 */
89  	public Object getCellEditorValue()
90  	{
91  		return this.value;
92  	}
93  
94  	/***
95  	 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(JTable,
96  	 *      Object, boolean, int, int)
97  	 */
98  	public Component getTableCellEditorComponent(final JTable table,
99  			final Object value, final boolean isSelected, final int row,
100 			final int column)
101 	{
102 		this.cellPanel.setBackground(((Color) value).darker());
103 		this.value = (Color) value;
104 		JColorChooser chooser = new JColorChooser((Color) value);
105 		JDialog dialog = JColorChooser.createDialog(table, "Color selection",
106 				false, chooser, new OKListener(chooser), new CancelListener());
107 		dialog.setVisible(true);
108 		return this.cellPanel;
109 	}
110 }