KickJava   Java API By Example, From Geeks To Geeks.

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


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 Smart 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-2003 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 java.beans.PropertyChangeEvent JavaDoc;
19 import java.beans.PropertyVetoException JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24
25 import javax.swing.*;
26
27 import org.compiere.model.*;
28 import org.compiere.plaf.CompierePLAF;
29 import org.compiere.swing.CButton;
30 import org.compiere.util.*;
31
32 /**
33  * Product Attribute Set Editor
34  *
35  * @author Jorg Janke
36  * @version $Id: VPAttribute.java,v 1.10 2003/11/07 06:36:56 jjanke Exp $
37  */

38 public class VPAttribute extends JComponent
39     implements VEditor, ActionListener
40 {
41     /**
42      * IDE Constructor
43      */

44     public VPAttribute()
45     {
46         this (false, false, true, 0, null);
47     } // VAssigment
48

49     /**
50      * Create Product Attribute Editor.
51      * @param mandatory mandatory
52      * @param isReadOnly read only
53      * @param isUpdateable updateable
54      * @param WindowNo WindowNo
55      * @param lookup Model Product Attribute
56      */

57     public VPAttribute (boolean mandatory, boolean isReadOnly, boolean isUpdateable, int WindowNo, MPAttribute lookup)
58     {
59         super.setName("M_AttributeSetInstance_ID");
60         m_WindowNo = WindowNo;
61         m_mPAttribute = lookup;
62         LookAndFeel.installBorder(this, "TextField.border");
63         this.setLayout(new BorderLayout());
64         // Size
65
this.setPreferredSize(m_text.getPreferredSize());
66         int height = m_text.getPreferredSize().height;
67
68         // *** Text ***
69
m_text.setEditable(false);
70         m_text.setFocusable(false);
71         m_text.setBorder(null);
72         m_text.setHorizontalAlignment(JTextField.LEADING);
73         // Background
74
setMandatory(mandatory);
75         this.add(m_text, BorderLayout.CENTER);
76
77         // *** Button ***
78
m_button.setIcon(Env.getImageIcon("PAttribute10.gif"));
79         m_button.setMargin(new Insets(0, 0, 0, 0));
80         m_button.setPreferredSize(new Dimension(height, height));
81         m_button.addActionListener(this);
82         m_button.setFocusable(true);
83         this.add(m_button, BorderLayout.EAST);
84
85         // Prefereed Size
86
this.setPreferredSize(this.getPreferredSize()); // causes r/o to be the same length
87
// ReadWrite
88
if (isReadOnly || !isUpdateable)
89             setReadWrite(false);
90         else
91             setReadWrite(true);
92
93         // Popup
94
m_text.addMouseListener(new VPAttribute_mouseAdapter(this));
95         menuEditor = new JMenuItem(Msg.getMsg(Env.getCtx(), "PAttribute"), Env.getImageIcon("Zoom16.gif"));
96         menuEditor.addActionListener(this);
97         popupMenu.add(menuEditor);
98     } // VPAttribute
99

100     /** Data Value */
101     private Object JavaDoc m_value = new Object JavaDoc();
102
103     private MPAttribute m_mPAttribute;
104
105     /** The Text Field */
106     private JTextField m_text = new JTextField (VLookup.DISPLAY_LENGTH);
107     /** The Button */
108     private CButton m_button = new CButton();
109
110     JPopupMenu popupMenu = new JPopupMenu();
111     private JMenuItem menuEditor;
112
113     private boolean m_readWrite;
114     private boolean m_mandatory;
115     private int m_WindowNo;
116     /** Enter Product Attributes */
117     private boolean m_productWindow;
118     /** Ability to define Attribute */
119     private boolean m_instanceWindow;
120
121     /**
122      * Dispose resources
123      */

124     public void dispose()
125     {
126         m_text = null;
127         m_button = null;
128         m_mPAttribute.dispose();
129         m_mPAttribute = null;
130     } // dispose
131

132     /**
133      * Set Mandatory
134      * @param mandatory mandatory
135      */

136     public void setMandatory (boolean mandatory)
137     {
138         m_mandatory = mandatory;
139         m_button.setMandatory(mandatory);
140         setBackground (false);
141     } // setMandatory
142

143     /**
144      * Get Mandatory
145      * @return mandatory
146      */

147     public boolean isMandatory()
148     {
149         return m_mandatory;
150     } // isMandatory
151

152     /**
153      * Set ReadWrite
154      * @param rw read rwite
155      */

156     public void setReadWrite (boolean rw)
157     {
158         m_readWrite = rw;
159         m_button.setReadWrite(rw);
160         setBackground (false);
161     } // setReadWrite
162

163     /**
164      * Is Read Write
165      * @return read write
166      */

167     public boolean isReadWrite()
168     {
169         return m_readWrite;
170     } // isReadWrite
171

172     /**
173      * Set Foreground
174      * @param color color
175      */

176     public void setForeground (Color color)
177     {
178         m_text.setForeground(color);
179     } // SetForeground
180

181     /**
182      * Set Background
183      * @param error Error
184      */

185     public void setBackground (boolean error)
186     {
187         if (error)
188             setBackground(CompierePLAF.getFieldBackground_Error());
189         else if (!m_readWrite)
190             setBackground(CompierePLAF.getFieldBackground_Inactive());
191         else if (m_mandatory)
192             setBackground(CompierePLAF.getFieldBackground_Mandatory());
193         else
194             setBackground(CompierePLAF.getFieldBackground_Normal());
195     } // setBackground
196

197     /**
198      * Set Background
199      * @param color Color
200      */

201     public void setBackground (Color color)
202     {
203         m_text.setBackground(color);
204     } // setBackground
205

206     /*************************************************************************/
207
208     /**
209      * Set/lookup Value
210      * @param value value
211      */

212     public void setValue(Object JavaDoc value)
213     {
214         // The same
215
if ((value == null && m_value == null)
216             || (value != null && value.equals(m_value)) )
217             return;
218         Log.trace(8, "VPAttribute.setValue", value);
219         m_value = value;
220         m_text.setText(m_mPAttribute.getDisplay(value)); // loads value
221
} // setValue
222

223     /**
224      * Get Value
225      * @return value
226      */

227     public Object JavaDoc getValue()
228     {
229         return m_value;
230     } // getValue
231

232     /**
233      * Get Display Value
234      * @return info
235      */

236     public String JavaDoc getDisplay()
237     {
238         return m_text.getText();
239     } // getDisplay
240

241     /*************************************************************************/
242
243     /**
244      * Set Field - NOP
245      * @param mField MField
246      */

247     public void setField(MField mField)
248     {
249         int AD_Column_ID = mField.getAD_Column_ID();
250         // M_Product.M_AttributeSetInstance_ID = 8418
251
m_productWindow = AD_Column_ID == 8418; // HARDCODED
252
// M_InOutLine.M_AttributeSetInstance_ID = 8772
253
// M_ProductionLine.M_AttributeSetInstance_ID = 8552
254
m_instanceWindow = AD_Column_ID == 8772 || AD_Column_ID == 8552; // HARDCODED
255
} // setField
256

257     /**
258      * Action Listener Interface
259      * @param listener listener
260      */

261     public void addActionListener(ActionListener listener)
262     {
263     } // addActionListener
264

265     /**
266      * Action Listener - start dialog
267      * @param e Event
268      */

269     public void actionPerformed(ActionEvent e)
270     {
271         if (!m_button.isEnabled ())
272             return;
273         m_button.setEnabled (false);
274         //
275
Integer JavaDoc oldValue = (Integer JavaDoc)getValue ();
276         int M_AttributeSetInstance_ID = oldValue == null ? 0 : oldValue.intValue ();
277         int M_Product_ID = Env.getContextAsInt (Env.getCtx (), m_WindowNo, "M_Product_ID");
278         
279         // Call Dialog
280
VPAttributeDialog vad = new VPAttributeDialog (Env.getFrame (this), M_AttributeSetInstance_ID, M_Product_ID,
281             m_productWindow, m_instanceWindow);
282         m_text.setText(vad.getM_AttributeSetInstanceName());
283         M_AttributeSetInstance_ID = vad.getM_AttributeSetInstance_ID();
284                 
285         // Set Value
286
if (M_AttributeSetInstance_ID != vad.getM_AttributeSetInstance_ID())
287         {
288         // Log.trace(8, "VPAttribute.actionPerformed - changed - " + M_AttributeSetInstance_ID);
289
m_value = new Object JavaDoc(); // re-query display
290
if (M_AttributeSetInstance_ID == 0)
291                 setValue(null);
292             else
293                 setValue(new Integer JavaDoc(M_AttributeSetInstance_ID));
294             try
295             {
296                 fireVetoableChange("M_AttributeSetInstance_ID", new Object JavaDoc(), getValue());
297             }
298             catch (PropertyVetoException JavaDoc pve)
299             {
300                 Log.error("VPAttribute.actionPerformed", pve);
301             }
302         }
303         m_button.setEnabled(true);
304         requestFocus();
305     } // actionPerformed
306

307     /**
308      * Property Change Listener
309      * @param evt event
310      */

311     public void propertyChange (PropertyChangeEvent JavaDoc evt)
312     {
313         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
314             setValue(evt.getNewValue());
315     } // propertyChange
316

317 } // VPAttribute
318

319 /**
320  * Mouse Listener
321  */

322 final class VPAttribute_mouseAdapter extends MouseAdapter
323 {
324     /**
325      * Constructor
326      * @param adaptee adaptee
327      */

328     VPAttribute_mouseAdapter(VPAttribute adaptee)
329     {
330         this.adaptee = adaptee;
331     } // VPAttribute_mouseAdapter
332

333     private VPAttribute adaptee;
334
335     /**
336      * Mouse Listener
337      * @param e event
338      */

339     public void mouseClicked(MouseEvent e)
340     {
341         // Double Click
342
if (e.getClickCount() > 1)
343             adaptee.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Mouse"));
344         // popup menu
345
if (SwingUtilities.isRightMouseButton(e))
346             adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
347     } // mouse Clicked
348

349 } // VPAttribute_mouseAdapter
350
Popular Tags