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.table.renderers;
21  
22  import com.cosylab.util.ObjectList;
23  
24  import java.awt.Component;
25  import java.awt.event.FocusEvent;
26  import java.awt.event.FocusListener;
27  
28  import java.util.EventObject;
29  
30  import javax.swing.JTable;
31  import javax.swing.JTextField;
32  import javax.swing.event.CellEditorListener;
33  import javax.swing.event.ChangeEvent;
34  import javax.swing.table.TableCellEditor;
35  
36  
37  /**
38   * Abstract editor, which allows edited value to be applied only when enter is
39   * pressed.
40   *
41   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
42   * @version $id$
43   */
44  public abstract class JTextFieldCellEditor extends JTextField
45  	implements TableCellEditor
46  {
47  	private ObjectList listeners = new ObjectList(CellEditorListener.class);
48  	private boolean firstGain=true;
49  	/**
50  	 * Creates a new DoubleCellEditor object.
51  	 */
52  	public JTextFieldCellEditor()
53  	{
54  		/*addActionListener(new ActionListener() {
55  				public void actionPerformed(ActionEvent e)
56  				{
57  					//System.out.println("ACTION "+e);
58  					if (isValueValid()) {
59  						fireStop(new ChangeEvent(JTextFieldCellEditor.this));
60  					} else {
61  						fireCancle(new ChangeEvent(JTextFieldCellEditor.this));
62  					}
63  				}
64  			});*/
65  		/*addKeyListener(new KeyListener() {
66  			public void keyPressed(KeyEvent e) {
67  				System.out.println("PRESSED "+e.getKeyChar());
68  			}
69  
70  			public void keyReleased(KeyEvent e) {
71  				System.out.println("RELEASED "+e.getKeyChar());
72  			}
73  
74  			public void keyTyped(KeyEvent e) {
75  				System.out.println("TYPED "+e.getKeyChar());
76  			}
77  		});*/
78  		addFocusListener(new FocusListener() {
79  			public void focusGained(FocusEvent e) {
80  				//System.out.println("FOCUS GAINED");
81  				if (!firstGain) fireCancle(new ChangeEvent(this));
82  				else firstGain=false;
83  			}
84  
85  			public void focusLost(FocusEvent e) {
86  				firstGain=true;
87  				//System.out.println("FOCUS LOST");
88  				fireCancle(new ChangeEvent(this));
89  			}
90  		});
91  		/*addMouseListener(new MouseListener() {
92  			public void mouseClicked(MouseEvent e) {
93  				System.out.println("M CLICKED");
94  			}
95  
96  			public void mouseEntered(MouseEvent e) {
97  				System.out.println("M ENTERED");
98  			}
99  
100 			public void mouseExited(MouseEvent e) {
101 				System.out.println("M EXITED");
102 			}
103 
104 			public void mousePressed(MouseEvent e) {
105 				System.out.println("M PRESSED");
106 			}
107 
108 			public void mouseReleased(MouseEvent e) {
109 				System.out.println("M RELEASED");
110 			}
111 		});*/
112 		
113 	}
114 
115 	/**
116 	 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable,
117 	 *      java.lang.Object, boolean, int, int)
118 	 */
119 	public Component getTableCellEditorComponent(JTable table, Object cell,
120 	    boolean isSelected, int row, int column)
121 	{
122 		return this;
123 	}
124 
125 	/* (non-Javadoc)
126 	 * @see javax.swing.DefaultCellEditor#stopCellEditing()
127 	 */
128 	public boolean stopCellEditing()
129 	{
130 		firstGain=true;
131 		//Thread.dumpStack();
132 		fireStop(new ChangeEvent(this));
133 
134 		return false;
135 	}
136 
137 	/* (non-Javadoc)
138 	 * @see javax.swing.CellEditor#cancelCellEditing()
139 	 */
140 	public void cancelCellEditing()
141 	{
142 		firstGain=true;
143 		//Thread.dumpStack();
144 		fireCancle(new ChangeEvent(this));
145 	}
146 
147 	/* (non-Javadoc)
148 	 * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject)
149 	 */
150 	public boolean isCellEditable(EventObject anEvent)
151 	{
152 		return true;
153 	}
154 
155 	/* (non-Javadoc)
156 	 * @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject)
157 	 */
158 	public boolean shouldSelectCell(EventObject anEvent)
159 	{
160 		return true;
161 	}
162 
163 	/* (non-Javadoc)
164 	 * @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener)
165 	 */
166 	public void addCellEditorListener(CellEditorListener l)
167 	{
168 		listeners.add(l);
169 	}
170 
171 	private void fireCancle(ChangeEvent ev)
172 	{
173 		//System.out.println("CANCLE "+ev);
174 		CellEditorListener[] l = (CellEditorListener[])listeners.toArray();
175 	    
176 		for (int i = 0; i < l.length; i++) {
177 			try {
178 				l[i].editingCanceled(ev);
179 			} catch (Exception e) {
180 				e.printStackTrace();
181 			}
182 		}
183 	}
184 
185 	private void fireStop(ChangeEvent ev)
186 	{
187 		//System.out.println("STOP "+ev);
188 		CellEditorListener[] l = (CellEditorListener[])listeners.toArray();
189 
190 		for (int i = 0; i < l.length; i++) {
191 			try {
192 				l[i].editingStopped(ev);
193 			} catch (Exception e) {
194 				e.printStackTrace();
195 			}
196 		}
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener)
201 	 */
202 	public void removeCellEditorListener(CellEditorListener l)
203 	{
204 		listeners.remove(l);
205 	}
206 
207 	/**
208 	 * Should return <code>true</code> only if edited value is valid and can be
209 	 * applied to table cell.  Default implementation returns
210 	 * <code>true</code> allways. Override this method to provide own
211 	 * implementation.
212 	 *
213 	 * @return <code>true</code> only if value can be applied
214 	 */
215 	protected boolean isValueValid()
216 	{
217 		return true;
218 	}
219 }
220 
221 /* __oOo__ */