KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.event.*;
18 import javax.swing.*;
19 import javax.swing.table.*;
20 import javax.swing.event.*;
21 import javax.swing.border.*;
22 import java.util.*;
23 import java.beans.*;
24
25 import org.compiere.util.*;
26 import org.compiere.apps.*;
27 import org.compiere.plaf.*;
28 import org.compiere.swing.*;
29
30 /**
31  * Text Control (JTextArea embedded in JScrollPane)
32  *
33  * @author Jorg Janke
34  * @version $Id: VMemo.java,v 1.2 2003/09/05 04:58:59 jjanke Exp $
35  */

36 public class VMemo extends CText
37     implements VEditor, KeyListener, FocusListener, ActionListener
38 {
39     /**
40      * IDE Baan Constructor
41      */

42     public VMemo()
43     {
44         this("", false, false, true, 60, 4000);
45     } // VMemo
46

47     /**
48      * Standard Constructor
49      * @param columnName
50      * @param mandatory
51      * @param isReadOnly
52      * @param isUpdateable
53      * @param displayLength
54      * @param fieldLength
55      */

56     public VMemo (String JavaDoc columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
57         int displayLength, int fieldLength)
58     {
59         super (fieldLength/80, 50);
60         super.setName(columnName);
61         LookAndFeel.installBorder(this, "TextField.border");
62         this.addFocusListener(this); // to activate editor
63

64         // Create Editor
65
setColumns(displayLength>VString.MAXDISPLAY_LENGTH ? VString.MAXDISPLAY_LENGTH : displayLength); // 46
66
setForeground(CompierePLAF.getTextColor_Normal());
67         setBackground(CompierePLAF.getFieldBackground_Normal());
68
69         setLineWrap(true);
70         setWrapStyleWord(true);
71         addFocusListener(this);
72         setInputVerifier(new CInputVerifier()); //Must be set AFTER addFocusListener in order to work
73
setMandatory(mandatory);
74         m_columnName = columnName;
75
76         if (isReadOnly || !isUpdateable)
77             setReadWrite(false);
78         addKeyListener(this);
79
80         // Popup
81
addMouseListener(new VMemo_mouseAdapter(this));
82         if (columnName.equals("Script"))
83             menuEditor = new JMenuItem(Msg.getMsg(Env.getCtx(), "Script"), Env.getImageIcon("Script16.gif"));
84         else
85             menuEditor = new JMenuItem(Msg.getMsg(Env.getCtx(), "Editor"), Env.getImageIcon("Editor16.gif"));
86         menuEditor.addActionListener(this);
87         popupMenu.add(menuEditor);
88     } // VMemo
89

90     /**
91      * Dispose
92      */

93     public void dispose()
94     {
95     } // dispose
96

97     JPopupMenu popupMenu = new JPopupMenu();
98     private JMenuItem menuEditor;
99
100     private String JavaDoc m_columnName;
101     private String JavaDoc m_oldText = "";
102     private boolean m_firstChange;
103
104     /**
105      * Set Editor to value
106      * @param value
107      */

108     public void setValue(Object JavaDoc value)
109     {
110         super.setValue(value);
111         m_firstChange = true;
112     } // setValue
113

114     /**
115      * Property Change Listener
116      * @param evt
117      */

118     public void propertyChange (PropertyChangeEvent evt)
119     {
120         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
121             setValue(evt.getNewValue());
122     } // propertyChange
123

124     /**
125      * ActionListener
126      * @param e
127      */

128     public void actionPerformed(ActionEvent e)
129     {
130         if (e.getSource() == menuEditor)
131         {
132             menuEditor.setEnabled(false);
133             String JavaDoc s = null;
134             if (m_columnName.equals("Script"))
135                 s = ScriptEditor.start (Msg.translate(Env.getCtx(), m_columnName), getText(), isEditable(), 0);
136             else
137                 s = Editor.startEditor (this, Msg.translate(Env.getCtx(), m_columnName), getText(), isEditable());
138             menuEditor.setEnabled(true);
139             setValue(s);
140             try
141             {
142                 fireVetoableChange(m_columnName, null, getText());
143                 m_oldText = getText();
144             }
145             catch (PropertyVetoException pve) {}
146         }
147     } // actionPerformed
148

149     /**
150      * Action Listener Interface - NOP
151      * @param listener
152      */

153     public void addActionListener(ActionListener listener)
154     {
155     } // addActionListener
156

157     /**************************************************************************
158      * Key Listener Interface
159      * @param e
160      */

161     public void keyTyped(KeyEvent e) {}
162     public void keyPressed(KeyEvent e) {}
163
164     /**
165      * Escape - Restore old Text.
166      * Indicate Change
167      * @param e
168      */

169     public void keyReleased(KeyEvent e)
170     {
171         // ESC
172
if (e.getKeyCode() == KeyEvent.VK_ESCAPE && !getText().equals(m_oldText))
173         {
174             Log.trace(Log.l5_DData, "VMemo.keyReleased - ESC");
175             setText(m_oldText);
176             return;
177         }
178         // Indicate Change
179
if (m_firstChange && !m_oldText.equals(getText()))
180         {
181             Log.trace(Log.l5_DData, "VMemo.keyReleased - firstChange");
182             m_firstChange = false;
183             try
184             {
185                 String JavaDoc text = getText();
186                 fireVetoableChange(m_columnName, text, null); // No data committed - done when focus lost !!!
187
}
188             catch (PropertyVetoException pve) {}
189         } // firstChange
190
} // keyReleased
191

192     /**
193      * Focus Gained - Save for Escape
194      * @param e
195      */

196     public void focusGained (FocusEvent e)
197     {
198         Log.trace(Log.l4_Data, "VMemo.focusGained " + e.getSource(), e.paramString());
199         if (e.getSource() instanceof VMemo)
200             requestFocus();
201         else
202             m_oldText = getText();
203     } // focusGained
204

205     /**
206      * Data Binding to MTable (via GridController)
207      * @param e
208      */

209     public void focusLost (FocusEvent e)
210     {
211         //Log.trace(Log.l4_Data, "VMemo.focusLost " + e.getSource(), e.paramString());
212
// something changed?
213
return;
214
215     } // focusLost
216

217     /*************************************************************************/
218
219     /**
220      * Set Field/WindowNo for ValuePreference (NOP)
221      * @param mField
222      */

223     public void setField (org.compiere.model.MField mField)
224     {
225     } // setField
226

227
228
229 private class CInputVerifier extends InputVerifier {
230
231      public boolean verify(JComponent input) {
232
233
234         //NOTE: We return true no matter what since the InputVerifier is only introduced to fireVetoableChange in due time
235
if (getText() == null && m_oldText == null)
236             return true;
237         else if (getText().equals(m_oldText))
238             return true;
239         //
240
try
241         {
242             String JavaDoc text = getText();
243             fireVetoableChange(m_columnName, null, text);
244             m_oldText = text;
245             return true;
246         }
247         catch (PropertyVetoException pve) {}
248         return true;
249
250      } // verify
251

252    } // CInputVerifier
253

254
255
256
257 } // VMemo
258

259 /*****************************************************************************/
260
261 /**
262  * Mouse Listener
263  */

264 final class VMemo_mouseAdapter extends MouseAdapter
265 {
266     /**
267      * Constructor
268      * @param adaptee
269      */

270     VMemo_mouseAdapter(VMemo adaptee)
271     {
272         this.adaptee = adaptee;
273     } // VMemo_mouseAdapter
274

275     private VMemo adaptee;
276
277     /**
278      * Mouse Listener
279      * @param e
280      */

281     public void mouseClicked(MouseEvent e)
282     {
283         // popup menu
284
if (SwingUtilities.isRightMouseButton(e))
285             adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
286     } // mouse Clicked
287

288
289
290 } // VMemo_mouseAdapter
291

292
293
294
295
Popular Tags