KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > VCellEditor


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.grid.ed;
15
16 import javax.swing.*;
17 import javax.swing.table.*;
18 import javax.swing.event.*;
19 import java.awt.*;
20 import java.awt.event.*;
21 import java.util.EventObject JavaDoc;
22 import java.beans.*;
23 import java.io.Serializable JavaDoc;
24
25 import org.compiere.util.*;
26 import org.compiere.model.*;
27 import org.compiere.plaf.*;
28
29 /**
30  * Cell Editor.
31  * <pre>
32  * Sequence of events:
33  * isCellEditable
34  * getTableCellEditor
35  * shouldSelectCell
36  * getCellEditorValue
37  * </pre>
38  * A new Editor is created for editable columns.
39  * @author Jorg Janke
40  * @version $Id: VCellEditor.java,v 1.11 2003/10/27 15:22:02 jjanke Exp $
41  */

42 public final class VCellEditor extends AbstractCellEditor
43     implements TableCellEditor, VetoableChangeListener, ActionListener
44 {
45     /**
46      * Constructor for Grid
47      * @param mField
48      */

49     public VCellEditor (MField mField)
50     {
51         super();
52         m_mField = mField;
53         // Click
54
} // VCellEditor
55

56     /** The Field */
57     private MField m_mField = null;
58     /** The Table Editor */
59     private VEditor m_editor = null;
60     /** Table */
61     private JTable m_table = null;
62     /** ClickCount */
63     private int m_clickCountToStart = 2;
64
65     /**
66      * Create Editor
67      */

68     private void createEditor()
69     {
70         m_editor = VEditorFactory.getEditor(m_mField, true);
71         m_editor.addVetoableChangeListener(this);
72         m_editor.addActionListener(this);
73     } // createEditor
74

75     /**
76      * Ask the editor if it can start editing using anEvent.
77      * This method is intended for the use of client to avoid the cost of
78      * setting up and installing the editor component if editing is not possible.
79      * If editing can be started this method returns true
80      * @param anEvent
81      * @return true if editable
82      */

83     public boolean isCellEditable(EventObject JavaDoc anEvent)
84     {
85     // Log.trace(Log.l4_Data, "VCellEditor.isCellEditable", anEvent);
86
if (!m_mField.isEditable (false)) // does not check IsActive,Processed,LinkColumn
87
return false;
88
89         if (anEvent instanceof MouseEvent)
90             return ((MouseEvent)anEvent).getClickCount() >= m_clickCountToStart;
91         if (m_editor == null)
92             createEditor();
93         return true;
94     } // isCellEditable
95

96     /**
97      * Sets an initial value for the editor. This will cause the editor to
98      * stopEditing and lose any partially edited value if the editor is editing
99      * when this method is called.
100      * Returns the component that should be added to the client's Component hierarchy.
101      * Once installed in the client's hierarchy this component
102      * will then be able to draw and receive user input.
103      *
104      * @param table
105      * @param value
106      * @param isSelected
107      * @param row
108      * @param col
109      * @return component
110      */

111     public Component getTableCellEditorComponent (JTable table, Object JavaDoc value, boolean isSelected, int row, int col)
112     {
113         Log.trace(Log.l5_DData, "VCellEditor.getTableCellEditorComponent", "Value=" + value + ", row=" + row + ", col=" + col);
114         table.setRowSelectionInterval(row,row); // force moving to new row
115
if (m_editor == null)
116             createEditor();
117
118         m_table = table;
119
120         // Set Value
121
m_editor.setValue(value);
122
123         // Set Background/Foreground to "normal" (unselected) colors
124
m_editor.setBackground(m_mField.isError());
125         m_editor.setForeground(CompierePLAF.getTextColor_Normal());
126
127         // Other UI
128
m_editor.setFont(table.getFont());
129         m_editor.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
130         //
131
return (Component)m_editor;
132     } // getTableCellEditorComponent
133

134     /**
135      * The editing cell should be selected or not
136      * @param e
137      * @return true (constant)
138      */

139     public boolean shouldSelectCell(EventObject JavaDoc e)
140     {
141     // Log.trace(Log.l5_DData, "VCellEditor.shouldSelectCell", e.toString());
142
return true;
143     } // shouldSelectCell
144

145     /**
146      * Returns the value contained in the editor
147      * @return value
148      */

149     public Object JavaDoc getCellEditorValue()
150     {
151     // Log.trace(Log.l5_DData, "VCellEditor.getCellEditorValue", m_editor.getValue());
152
return m_editor.getValue();
153     } // getCellEditorValue
154

155     /**
156      * VEditor Change Listener (property name is columnName).
157      * - indicate change (for String/Text/..) <br>
158      * When editing is complete the value is retrieved via getCellEditorValue
159      * @param e
160      */

161     public void vetoableChange(PropertyChangeEvent e)
162     {
163         if (m_table == null)
164             return;
165         Log.trace(Log.l5_DData, "VCellEditor.vetoableChange", e.getPropertyName() + "=" + e.getNewValue());
166         //
167
((MTable)m_table.getModel()).setChanged(true);
168     } // vetoableChange
169

170     /**
171      * Get Actual Editor.
172      * Called from GridController to add ActionListener to Button
173      * @return VEditor
174      */

175     public VEditor getEditor()
176     {
177         return m_editor;
178     } // getEditor
179

180     /**
181      * Action Editor - Stop Editor
182      * @param e
183      */

184     public void actionPerformed (ActionEvent e)
185     {
186         Log.trace(Log.l5_DData, "VCellEditor.actionPerformed", m_editor.getValue());
187         super.stopCellEditing();
188     } // actionPerformed
189

190     /**
191      * Dispose
192      */

193     public void dispose()
194     {
195         m_editor = null;
196         m_mField = null;
197         m_table = null;
198     } // dispose
199

200 } // VCellEditor
201
Popular Tags