KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.*;
17 import javax.swing.*;
18 import java.awt.event.*;
19 import java.beans.*;
20
21 import org.compiere.apps.*;
22 import org.compiere.util.*;
23 import org.compiere.plaf.*;
24 import org.compiere.swing.*;
25 import org.compiere.model.*;
26
27 /**
28  * Data Binding:
29  * VEditors call fireVetoableChange(m_columnName, null, getText());
30  * GridController (for Single-Row) and VCellExitor (for Multi-Row)
31  * listen to Vetoable Change Listener (vetoableChange)
32  * then set the value for that column in the current row in the table
33  *
34  * @author Jorg Janke
35  * @version $Id: VString.java,v 1.18 2003/09/05 04:58:59 jjanke Exp $
36  */

37 public final class VString extends CTextField
38     implements VEditor, KeyListener, ActionListener
39 {
40     public static final int MAXDISPLAY_LENGTH = org.compiere.model.MField.MAXDISPLAY_LENGTH;
41
42     /**
43      * IDE Bean Constructor for 30 character updateable field
44      */

45     public VString()
46     {
47         this("String", false, false, true, 30, 30, "");
48     } // VString
49

50     /**
51      * Detail Constructor
52      * @param columnName column name
53      * @param mandatory mandatory
54      * @param isReadOnly read only
55      * @param isUpdateable updateable
56      * @param displayLength display length
57      * @param fieldLength field length
58      * @param VFormat format
59      */

60     public VString (String JavaDoc columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
61         int displayLength, int fieldLength, String JavaDoc VFormat)
62     {
63         super(displayLength>MAXDISPLAY_LENGTH ? MAXDISPLAY_LENGTH : displayLength);
64         super.setName(columnName);
65         m_columnName = columnName;
66         if (VFormat == null)
67             VFormat = "";
68         m_VFormat = VFormat;
69         m_fieldLength = fieldLength;
70         if (m_VFormat.length() != 0 || m_fieldLength != 0)
71             setDocument(new MDocString(m_VFormat, m_fieldLength, this));
72         if (m_VFormat.length() != 0)
73             setCaret(new VOvrCaret());
74         //
75
setMandatory(mandatory);
76
77         // Editable
78
if (isReadOnly || !isUpdateable)
79         {
80             setEditable(false);
81             setBackground(CompierePLAF.getFieldBackground_Inactive());
82         }
83
84         this.addKeyListener(this);
85         this.addActionListener(this);
86         // Popup for Editor
87
if (fieldLength > displayLength)
88         {
89             addMouseListener(new VString_mouseAdapter(this));
90             mEditor = new JMenuItem (Msg.getMsg(Env.getCtx(), "Editor"), Env.getImageIcon("Editor16.gif"));
91             mEditor.addActionListener(this);
92             popupMenu.add(mEditor);
93         }
94         setForeground(CompierePLAF.getTextColor_Normal());
95         setBackground(CompierePLAF.getFieldBackground_Normal());
96     } // VString
97

98     /**
99      * Dispose
100      */

101     public void dispose()
102     {
103         m_mField = null;
104     } // dispose
105

106     JPopupMenu popupMenu = new JPopupMenu();
107     private JMenuItem mEditor;
108
109     private MField m_mField = null;
110
111     private String JavaDoc m_columnName;
112     private String JavaDoc m_oldText;
113     private String JavaDoc m_initialText;
114     private String JavaDoc m_VFormat;
115     private int m_fieldLength;
116     private volatile boolean m_setting = false;
117
118
119     /**
120      * Set Editor to value
121      * @param value value
122      */

123     public void setValue(Object JavaDoc value)
124     {
125     // Log.trace(Log.l4_Data, "VString.setValue", value);
126
if (value == null)
127             m_oldText = "";
128         else
129             m_oldText = value.toString();
130         // only set when not updated here
131
if (m_setting)
132             return;
133         setText (m_oldText);
134         m_initialText = m_oldText;
135     } // setValue
136

137     /**
138      * Property Change Listener
139      * @param evt event
140      */

141     public void propertyChange (PropertyChangeEvent evt)
142     {
143         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
144             setValue(evt.getNewValue());
145     } // propertyChange
146

147     /**
148      * Return Editor value
149      * @return value
150      */

151     public Object JavaDoc getValue()
152     {
153         return getText();
154     } // getValue
155

156     /**
157      * Return Display Value
158      * @return value
159      */

160     public String JavaDoc getDisplay()
161     {
162         return getText();
163     } // getDisplay
164

165     /**************************************************************************
166      * Key Listener Interface
167      * @param e event
168      */

169     public void keyTyped(KeyEvent e) {}
170     public void keyPressed(KeyEvent e) {}
171
172     /**
173      * Key Released.
174      * if Escape Restore old Text
175      * @param e event
176      */

177     public void keyReleased(KeyEvent e)
178     {
179         // ESC
180
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
181             setText(m_initialText);
182         m_setting = true;
183         try
184         {
185             fireVetoableChange (m_columnName, m_oldText, getText());
186         }
187         catch (PropertyVetoException pve) {}
188         m_setting = false;
189     } // keyReleased
190

191     /**
192      * Data Binding to MTable (via GridController) - Enter pressed
193      * @param e event
194      */

195     public void actionPerformed(ActionEvent e)
196     {
197         if (e.getActionCommand().equals(ValuePreference.NAME))
198         {
199             ValuePreference.start (m_mField, getValue());
200             return;
201         }
202
203         // Invoke Editor
204
if (e.getSource() == mEditor)
205         {
206             String JavaDoc s = Editor.startEditor(this, Msg.translate(Env.getCtx(), m_columnName), getText(), isEditable());
207             setText(s);
208         }
209         // Data Binding
210
try
211         {
212             fireVetoableChange(m_columnName, m_oldText, getText());
213         }
214         catch (PropertyVetoException pve) {}
215     } // actionPerformed
216

217     /**
218      * Set Field/WindowNo for ValuePreference
219      * @param mField field
220      */

221     public void setField (MField mField)
222     {
223         m_mField = mField;
224         if (m_mField != null)
225             ValuePreference.addMenu (this, popupMenu);
226     } // setField
227

228 } // VString
229

230 /*****************************************************************************/
231
232 /**
233  * Mouse Listener
234  */

235 final class VString_mouseAdapter extends MouseAdapter
236 {
237     /**
238      * Constructor
239      * @param adaptee adaptee
240      */

241     VString_mouseAdapter(VString adaptee)
242     {
243         this.adaptee = adaptee;
244     } // VString_mouseAdapter
245

246     private VString adaptee;
247
248     /**
249      * Mouse Listener
250      * @param e event
251      */

252     public void mouseClicked(MouseEvent e)
253     {
254         // popup menu
255
if (SwingUtilities.isRightMouseButton(e))
256             adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
257     } // mouseClicked
258

259 } // VText_mouseAdapter
260
Popular Tags