KickJava   Java API By Example, From Geeks To Geeks.

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


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-2002 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 java.beans.*;
20 import java.sql.*;
21 import java.util.*;
22 import java.text.*;
23
24 import org.compiere.apps.*;
25 import org.compiere.model.*;
26 import org.compiere.util.*;
27 import org.compiere.swing.*;
28 import org.compiere.plaf.*;
29 import org.compiere.apps.search.*;
30
31 /**
32  * Resource Assignment Entry
33  *
34  * @author Jorg Janke
35  * @version $Id: VAssignment.java,v 1.13 2003/09/05 04:58:59 jjanke Exp $
36  */

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

43     public VAssignment()
44     {
45         this (false, false, true);
46     } // VAssigment
47

48     /**
49      * Create Resource Assigment.
50      * <pre>
51      * Resource DateTimeFrom Qty UOM Button
52      * </pre>
53      * @param mandatory mandatory
54      * @param isReadOnly read only
55      * @param isUpdateable updateable
56      */

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

99     /** Data Value */
100     private Object JavaDoc m_value = null;
101     /** Get Info */
102     private PreparedStatement m_pstmt = null;
103
104     /** The Text Field */
105     private JTextField m_text = new JTextField (VLookup.DISPLAY_LENGTH);
106     /** The Button */
107     private CButton m_button = new CButton();
108
109     JPopupMenu popupMenu = new JPopupMenu();
110     private JMenuItem menuEditor;
111
112     private boolean m_readWrite;
113     private boolean m_mandatory;
114
115     /** The Format */
116     private DateFormat m_dateFormat = DisplayType.getDateFormat(DisplayType.DateTime);
117     private NumberFormat m_qtyFormat = DisplayType.getNumberFormat(DisplayType.Quantity);
118
119     /**
120      * Dispose resources
121      */

122     public void dispose()
123     {
124         try
125         {
126             if (m_pstmt != null)
127                 m_pstmt.close();
128         }
129         catch (Exception JavaDoc e)
130         {
131             Log.error("VAssignment.dispose");
132         }
133         m_text = null;
134         m_button = null;
135     } // dispose
136

137     /**
138      * Set Mandatory
139      * @param mandatory mandatory
140      */

141     public void setMandatory (boolean mandatory)
142     {
143         m_mandatory = mandatory;
144         m_button.setMandatory(mandatory);
145         setBackground (false);
146     } // setMandatory
147

148     /**
149      * Get Mandatory
150      * @return mandatory
151      */

152     public boolean isMandatory()
153     {
154         return m_mandatory;
155     } // isMandatory
156

157     /**
158      * Set ReadWrite
159      * @param rw read rwite
160      */

161     public void setReadWrite (boolean rw)
162     {
163         m_readWrite = rw;
164         m_button.setReadWrite(rw);
165         setBackground (false);
166     } // setReadWrite
167

168     /**
169      * Is Read Write
170      * @return read write
171      */

172     public boolean isReadWrite()
173     {
174         return m_readWrite;
175     } // isReadWrite
176

177     /**
178      * Set Foreground
179      * @param color color
180      */

181     public void setForeground (Color color)
182     {
183         m_text.setForeground(color);
184     } // SetForeground
185

186     /**
187      * Set Background
188      * @param error Error
189      */

190     public void setBackground (boolean error)
191     {
192         if (error)
193             setBackground(CompierePLAF.getFieldBackground_Error());
194         else if (!m_readWrite)
195             setBackground(CompierePLAF.getFieldBackground_Inactive());
196         else if (m_mandatory)
197             setBackground(CompierePLAF.getFieldBackground_Mandatory());
198         else
199             setBackground(CompierePLAF.getFieldBackground_Normal());
200     } // setBackground
201

202     /**
203      * Set Background
204      * @param color Color
205      */

206     public void setBackground (Color color)
207     {
208         m_text.setBackground(color);
209     } // setBackground
210

211     /*************************************************************************/
212
213     /**
214      * Set/lookup Value
215      * @param value value
216      */

217     public void setValue(Object JavaDoc value)
218     {
219         if (value == m_value)
220             return;
221         m_value = value;
222         int S_ResourceAssignment_ID = 0;
223         if (m_value != null && m_value instanceof Integer JavaDoc)
224             S_ResourceAssignment_ID = ((Integer JavaDoc)m_value).intValue();
225         // Set Empty
226
if (S_ResourceAssignment_ID == 0)
227         {
228             m_text.setText("");
229             return;
230         }
231
232         // Statement
233
if (m_pstmt == null)
234             m_pstmt = DB.prepareStatement("SELECT r.Name,ra.AssignDateFrom,ra.Qty,uom.UOMSymbol "
235                 + "FROM S_ResourceAssignment ra, S_Resource r, S_ResourceType rt, C_UOM uom "
236                 + "WHERE ra.S_ResourceAssignment_ID=?"
237                 + " AND ra.S_Resource_ID=r.S_Resource_ID"
238                 + " AND r.S_ResourceType_ID=rt.S_ResourceType_ID"
239                 + " and rt.C_UOM_ID=uom.C_UOM_ID");
240         //
241
try
242         {
243             m_pstmt.setInt(1, S_ResourceAssignment_ID);
244             ResultSet rs = m_pstmt.executeQuery();
245             if (rs.next())
246             {
247                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(rs.getString(1));
248                 sb.append(" ").append(m_dateFormat.format(rs.getTimestamp(2)))
249                     .append(" ").append(m_qtyFormat.format(rs.getBigDecimal(3)))
250                     .append(" ").append(rs.getString(4).trim());
251                 m_text.setText(sb.toString());
252             }
253             else
254                 m_text.setText("<" + S_ResourceAssignment_ID + ">");
255             rs.close();
256         }
257         catch (Exception JavaDoc e)
258         {
259             Log.error("VAssigment.setValue", e);
260         }
261     } // setValue
262

263     /**
264      * Get Value
265      * @return value
266      */

267     public Object JavaDoc getValue()
268     {
269         return m_value;
270     } // getValue
271

272     /**
273      * Get Display Value
274      * @return info
275      */

276     public String JavaDoc getDisplay()
277     {
278         return m_text.getText();
279     } // getDisplay
280

281     /*************************************************************************/
282
283     /**
284      * Set Field - NOP
285      * @param mField MField
286      */

287     public void setField(MField mField)
288     {
289     } // setField
290

291     /**
292      * Action Listener Interface
293      * @param listener listener
294      */

295     public void addActionListener(ActionListener listener)
296     {
297         // m_text.addActionListener(listener);
298
} // addActionListener
299

300     /**
301      * Action Listener - start dialog
302      * @param e Event
303      */

304     public void actionPerformed(ActionEvent e)
305     {
306         if (!m_button.isEnabled())
307             return;
308         m_button.setEnabled(false);
309         //
310
Integer JavaDoc oldValue = (Integer JavaDoc)getValue();
311         int S_ResourceAssignment_ID = oldValue == null ? 0 : oldValue.intValue();
312         MAssignment ma = new MAssignment(Env.getCtx(), S_ResourceAssignment_ID);
313
314         // Start VAssignment Dialog
315
if (S_ResourceAssignment_ID != 0)
316         {
317             VAssignmentDialog vad = new VAssignmentDialog (Env.getFrame(this), ma, true, true);
318             ma = vad.getMAssignment();
319         }
320         // Start InfoSchedule directly
321
else
322         {
323             InfoSchedule is = new InfoSchedule(Env.getFrame(this), ma, true);
324             ma = is.getMAssignment();
325         }
326
327         // Set Value
328
if (ma != null && ma.getS_ResourceAssignment_ID() != 0)
329         {
330             setValue(new Integer JavaDoc(ma.getS_ResourceAssignment_ID()));
331             try
332             {
333                 fireVetoableChange("S_ResourceAssignment_ID", new Object JavaDoc(), getValue());
334             }
335             catch (PropertyVetoException pve)
336             {
337                 Log.error("VAssignment.actionPerformed", pve);
338             }
339         }
340         m_button.setEnabled(true);
341         requestFocus();
342     } // actionPerformed
343

344     /**
345      * Property Change Listener
346      * @param evt event
347      */

348     public void propertyChange (PropertyChangeEvent evt)
349     {
350         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
351             setValue(evt.getNewValue());
352     } // propertyChange
353

354 } // VAssignment
355

356 /**
357  * Mouse Listener
358  */

359 final class VAssignment_mouseAdapter extends MouseAdapter
360 {
361     /**
362      * Constructor
363      * @param adaptee adaptee
364      */

365     VAssignment_mouseAdapter(VAssignment adaptee)
366     {
367         this.adaptee = adaptee;
368     } // VAssignment_mouseAdapter
369

370     private VAssignment adaptee;
371
372     /**
373      * Mouse Listener
374      * @param e event
375      */

376     public void mouseClicked(MouseEvent e)
377     {
378         // Double Click
379
if (e.getClickCount() > 1)
380             adaptee.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Mouse"));
381         // popup menu
382
if (SwingUtilities.isRightMouseButton(e))
383             adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
384     } // mouse Clicked
385

386 } // VAssignment_mouseAdapter
387

388
Popular Tags