View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.introspection;
21  
22  import java.awt.Color;
23  import java.awt.Component;
24  import java.awt.event.KeyAdapter;
25  import java.awt.event.KeyEvent;
26  import java.awt.event.MouseEvent;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  import java.util.EventObject;
30  
31  import javax.swing.AbstractCellEditor;
32  import javax.swing.JComponent;
33  import javax.swing.JTable;
34  import javax.swing.table.TableCellEditor;
35  import javax.swing.table.TableCellRenderer;
36  
37  import com.cosylab.gui.property.editors.PropertyEditor;
38  
39  
40  /**
41   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc </a>
42   * @version $id$
43   */
44  public class PropertyCellEditorRenderer extends AbstractCellEditor
45  																	implements
46  																	TableCellEditor,
47  																	TableCellRenderer {
48  	private PropertyEditor			editor				= null;
49  
50  	private JComponent				editorComp			= null;
51  
52  	private Color					prevBack			= null;
53  
54  	private Color					prevFore			= null;
55  
56  	private Object					tempVal;
57  
58  	private PropertyChangeListener	pcl					= null;
59  
60  	private int						clickCountToStart	= 0;
61  
62  	/**
63  	 * Constructor for PropertyCellEditor.
64  	 */
65  	public PropertyCellEditorRenderer(PropertyEditor pe, int clickCountToStart) {
66  		this(pe);
67  		this.clickCountToStart = clickCountToStart;
68  	}
69  
70  	public PropertyCellEditorRenderer(PropertyEditor pe) {
71  		super();
72  		if (pe == null) {
73  			throw new NullPointerException("PropertyEditor");
74  		}
75  		this.editor = pe;
76  		if (editor instanceof JComponent) {
77  			this.editorComp = (JComponent)editor;
78  			prevFore = editorComp.getForeground();
79  			prevBack = editorComp.getBackground();
80  			editorComp.addKeyListener(new KeyAdapter() {
81  				public void keyPressed(KeyEvent e) {
82  					if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
83  						//System.out.println(e);
84  						cancelCellEditing();
85  					}
86  				}
87  			});
88  		}
89  	}
90  
91  	/**
92  	 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(JTable,
93  	 *      Object, boolean, int, int)
94  	 */
95  	public Component getTableCellEditorComponent(JTable table, Object value,
96  													boolean isSelected,
97  													int row, int column) {
98  		editor.setPropertyValue(value);
99  		tempVal = value;
100 		editorComp.setBackground(table.getBackground());
101 		editorComp.setForeground(table.getForeground());
102 		editorComp.setBorder(EditorsHelper.FOCUS_BORDER);
103 		if (pcl==null) {
104 			pcl = new PropertyChangeListener() {
105 				public void propertyChange(PropertyChangeEvent evt) {
106 					if (PropertyEditor.PROPERTY_VALUE_NAME.equals(evt.getPropertyName())) {
107 						stopCellEditing();
108 					}
109 				}
110 			};
111 			editor.addPropertyChangeListener(pcl);
112 		}
113 		editorComp.revalidate();
114 		editorComp.repaint();
115 		return editorComp;
116 	}
117 
118 	/*
119 	 * (non-Javadoc)
120 	 * 
121 	 * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject)
122 	 */
123 	public boolean isCellEditable(EventObject anEvent) {
124 		if (anEvent instanceof MouseEvent) {
125 			return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
126 		}
127 		return true;
128 	}
129 
130 	/**
131 	 * @see javax.swing.CellEditor#getCellEditorValue()
132 	 */
133 	public Object getCellEditorValue() {
134 		tempVal = null;
135 		return editor.getPropertyValue();
136 	}
137 
138 	/**
139 	 * @see javax.swing.CellEditor#stopCellEditing()
140 	 */
141 	public boolean stopCellEditing() {
142 		tempVal = null;
143 		super.stopCellEditing();
144 		if (pcl != null) {
145 			editor.removePropertyChangeListener(pcl);
146 			pcl=null;
147 		}
148 		return true;
149 	}
150 
151 	/**
152 	 * @see javax.swing.CellEditor#cancelCellEditing()
153 	 */
154 	public void cancelCellEditing() {
155 		editor.setPropertyValue(tempVal);
156 		tempVal = null;
157 		super.cancelCellEditing();
158 	}
159 
160 	public Component getTableCellRendererComponent(JTable table, Object value,
161 													boolean isSelected,
162 													boolean hasFocus, int row,
163 													int column) {
164 		editor.setPropertyValue(value);
165 		if (isSelected) {
166 			if (!table.isEditing()) {
167 				editorComp.setBackground(table.getSelectionBackground());
168 				editorComp.setForeground(table.getSelectionForeground());
169 			} else {
170 				editorComp.setBackground(prevBack);
171 				editorComp.setForeground(prevFore);
172 			}
173 		} else {
174 			editorComp.setBackground(table.getBackground());
175 			editorComp.setForeground(table.getForeground());
176 		}
177 		if (hasFocus) {
178 			editorComp.setBorder(EditorsHelper.FOCUS_BORDER);
179 		} else {
180 			editorComp.setBorder(EditorsHelper.EMPTY_BORDER);
181 		}
182 		editorComp.doLayout();
183 		editorComp.revalidate();
184 		editorComp.repaint();
185 		return editorComp;
186 	}
187 
188 }