KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
17 import java.awt.Frame JavaDoc;
18 import java.awt.GridBagConstraints JavaDoc;
19 import java.awt.GridBagLayout JavaDoc;
20 import java.awt.Insets JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Timestamp JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JDialog JavaDoc;
33 import javax.swing.WindowConstants JavaDoc;
34
35 import org.compiere.apps.AEnv;
36 import org.compiere.apps.ConfirmPanel;
37 import org.compiere.apps.search.InfoSchedule;
38 import org.compiere.model.*;
39 import org.compiere.plaf.CompiereColor;
40 import org.compiere.swing.CLabel;
41 import org.compiere.swing.CPanel;
42 import org.compiere.swing.CTextField;
43 import org.compiere.util.*;
44
45 /**
46  * Resource Assignment Dialog
47  *
48  * @author Jorg Janke
49  * @version $Id: VAssignmentDialog.java,v 1.10 2003/10/22 05:48:17 jjanke Exp $
50  */

51 public class VAssignmentDialog extends JDialog JavaDoc implements ActionListener JavaDoc
52 {
53     /**
54      * Assignment Dialog.
55      * <pre>
56      * Creates a new assignment oor displays an assignment
57      * Create new: (ID == 0)
58      * check availability & create assignment
59      * (confirmed when order/incoice/timeExpense is processed)
60      * alternatively let InfoResource do the assignment
61      * return ID
62      * Existing assignment: (ID != 0)
63      * if confirmed - no change.
64      * ability to delete or change assignment
65      * return ID
66      * </pre>
67      * @param frame parent
68      * @param mAssignment Assignment
69      * @param allowZoom allow to zoom to schedule
70      * @param allowDelete allow to delete recorde
71      */

72     public VAssignmentDialog (Frame JavaDoc frame, MAssignment mAssignment, boolean allowZoom, boolean allowDelete)
73     {
74         super (frame, Msg.getMsg(Env.getCtx(), "VAssignmentDialog"), true);
75         Log.trace(Log.l2_Sub, "VAssignmentDialog", mAssignment);
76         m_mAssignment = mAssignment;
77         m_frame = frame;
78         try
79         {
80             jbInit();
81             if (!allowZoom)
82                 confirmPanel.getZoomButton().setVisible(false);
83             delete.setVisible(allowDelete);
84         }
85         catch(Exception JavaDoc e)
86         {
87             Log.error("VAssignmentDialog", e);
88         }
89         setDisplay(); // from mAssignment
90
//
91
AEnv.showCenterScreen(this);
92     } // VAssignmentDialog
93

94     /** Assignment */
95     private MAssignment m_mAssignment;
96     /** Parent frame */
97     private Frame JavaDoc m_frame;
98     /** True if setting Value */
99     private boolean m_setting = false;
100
101     //
102
private CPanel mainPanel = new CPanel();
103     private GridBagLayout JavaDoc mainLayout = new GridBagLayout JavaDoc();
104     private CLabel lResource = new CLabel(Msg.translate(Env.getCtx(), "S_Resource_ID"));
105     private VComboBox fResource = new VComboBox(getResources());
106     private CLabel lDate = new CLabel(Msg.translate(Env.getCtx(), "DateFrom"));
107     private VDate fDateFrom = new VDate(DisplayType.DateTime);
108     private CLabel lQty = new CLabel(Msg.translate(Env.getCtx(), "Qty"));
109     private VNumber fQty = new VNumber();
110     private CLabel lUOM = new CLabel();
111     private CLabel lName = new CLabel(Msg.translate(Env.getCtx(), "Name"));
112     private CLabel lDescription = new CLabel(Msg.translate(Env.getCtx(), "Description"));
113     private CTextField fName = new CTextField (30);
114     private CTextField fDescription = new CTextField(30);
115     private ConfirmPanel confirmPanel = new ConfirmPanel(true, false, false, false, false, true, true);
116     private JButton JavaDoc delete = ConfirmPanel.createDeleteButton(true);
117
118
119     /**
120      * Static Init
121      * @throws Exception
122      */

123     private void jbInit() throws Exception JavaDoc
124     {
125         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
126         CompiereColor.setBackground(this);
127         fResource.addActionListener(this);
128         delete.addActionListener(this);
129         confirmPanel.addButton(delete);
130         confirmPanel.addActionListener(this);
131         //
132
mainPanel.setLayout(mainLayout);
133         mainPanel.add(lResource, new GridBagConstraints JavaDoc(0, 0, 1, 1, 0.0, 0.0
134             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(8, 8, 4, 4), 0, 0));
135         mainPanel.add(fResource, new GridBagConstraints JavaDoc(1, 0, 1, 1, 0.0, 0.0
136             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(8, 0, 4, 4), 0, 0));
137         mainPanel.add(lDate, new GridBagConstraints JavaDoc(0, 1, 1, 1, 0.0, 0.0
138             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(2, 8, 4, 4), 0, 0));
139         mainPanel.add(fDateFrom, new GridBagConstraints JavaDoc(1, 1, 2, 1, 0.0, 0.0
140             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(2, 0, 4, 8), 100, 0));
141         mainPanel.add(lQty, new GridBagConstraints JavaDoc(0, 2, 1, 1, 0.0, 0.0
142             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(2, 8, 4, 4), 0, 0));
143         mainPanel.add(fQty, new GridBagConstraints JavaDoc(1, 2, 1, 1, 0.0, 0.0
144             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(2, 0, 4, 4), 0, 0));
145         mainPanel.add(lUOM, new GridBagConstraints JavaDoc(2, 2, 1, 1, 0.0, 0.0
146             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(2, 4, 4, 8), 0, 0));
147         mainPanel.add(lName, new GridBagConstraints JavaDoc(0, 3, 1, 1, 0.0, 0.0
148             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(2, 8, 4, 4), 0, 0));
149         mainPanel.add(lDescription, new GridBagConstraints JavaDoc(0, 4, 1, 1, 0.0, 0.0
150             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(2, 8, 8, 4), 0, 0));
151         mainPanel.add(fName, new GridBagConstraints JavaDoc(1, 3, 2, 1, 0.0, 0.0
152             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(2, 0, 4, 8), 0, 0));
153         mainPanel.add(fDescription, new GridBagConstraints JavaDoc(1, 4, 2, 1, 0.0, 0.0
154             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(2, 0, 8, 8), 0, 0));
155         //
156
this.getContentPane().add(mainPanel, BorderLayout.CENTER);
157         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
158     } // jbInit
159

160     /**
161      * Initialize component & values from m_mAssignment
162      */

163     private void setDisplay()
164     {
165         m_setting = true;
166
167         // Set Resource
168
int S_Resource_ID = m_mAssignment.getS_Resource_ID();
169         KeyNamePair[] resources = new KeyNamePair[s_lookup.size()];
170         s_lookup.keySet().toArray(resources);
171         for (int i = 0; i < resources.length; i++)
172         {
173             if (resources[i].getKey() == S_Resource_ID)
174             {
175                 fResource.setSelectedItem(resources[i]);
176                 break;
177             }
178         }
179         KeyNamePair check = (KeyNamePair)fResource.getSelectedItem();
180         if (check == null || check.getKey() != S_Resource_ID)
181         {
182             if (m_mAssignment.getS_ResourceAssignment_ID() == 0) // new record select first
183
fResource.setSelectedItem(fResource.getSelectedItem()); // initiates UOM display
184
else
185                 Log.error("VAssignmentDialog.dynInit - Resource not found ID=" + S_Resource_ID);
186         }
187
188         // Set Date, Qty
189
fDateFrom.setValue(m_mAssignment.getAssignDateFrom());
190         fQty.setValue(m_mAssignment.getQty());
191
192         // Name, Description
193
fName.setValue(m_mAssignment.getName());
194         fDescription.setValue(m_mAssignment.getDescription());
195
196         // Set Editor to R/O if confirmed
197
boolean readWrite = true;
198         if (m_mAssignment.isConfirmed())
199             readWrite = false;
200         confirmPanel.getCancelButton().setVisible(readWrite);
201         fResource.setReadWrite(readWrite);
202         fDateFrom.setReadWrite(readWrite);
203         fQty.setReadWrite(readWrite);
204
205         m_setting = false;
206     } // dynInit
207

208     /**
209      * Action Listener
210      * @param e event
211      */

212     public void actionPerformed(ActionEvent JavaDoc e)
213     {
214         if (m_setting)
215             return;
216         // Update Assignment
217
KeyNamePair resource = (KeyNamePair)fResource.getSelectedItem();
218         if (resource != null)
219         {
220             int S_Resource_ID = resource.getKey();
221             m_mAssignment.setS_Resource_ID (S_Resource_ID);
222         }
223         Timestamp JavaDoc assignDateFrom = fDateFrom.getTimestamp();
224         if (assignDateFrom != null)
225             m_mAssignment.setAssignDateFrom (assignDateFrom);
226         BigDecimal JavaDoc qty = (BigDecimal JavaDoc)fQty.getValue();
227         if (qty != null)
228             m_mAssignment.setQty(qty);
229         m_mAssignment.setName((String JavaDoc)fName.getValue());
230         m_mAssignment.setDescription((String JavaDoc)fDescription.getValue());
231
232         // Resource - Look up UOM
233
if (e.getSource() == fResource)
234         {
235             Object JavaDoc o = s_lookup.get(fResource.getSelectedItem());
236             if (o == null)
237                 lUOM.setText(" ? ");
238             else
239                 lUOM.setText(o.toString());
240         }
241
242         // Zoom - InfoResource
243
else if (e.getActionCommand().equals(ConfirmPanel.A_ZOOM))
244         {
245             InfoSchedule is = new InfoSchedule (m_frame,
246                 m_mAssignment, true);
247             if (is.getMAssignment() != null)
248             {
249                 m_mAssignment = is.getMAssignment();
250             // setDisplay();
251
dispose();
252             }
253             is = null;
254         }
255
256         // cancel - return
257
else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
258         {
259             dispose();
260         }
261
262         // delete - delete and return
263
else if (e.getActionCommand().equals(ConfirmPanel.A_DELETE))
264         {
265             if (m_mAssignment.delete())
266             {
267                 m_mAssignment = null;
268                 dispose();
269             }
270         }
271
272         // OK - Save
273
else if (e.getActionCommand().equals(ConfirmPanel.A_OK))
274         {
275             if (cmd_save())
276                 dispose();
277         }
278     } // actionPerformed
279

280     /*************************************************************************/
281
282     /**
283      * Get Assignment
284      * @return Assignment
285      */

286     public MAssignment getMAssignment()
287     {
288         return m_mAssignment;
289     } // getMAssignment
290

291
292     /**
293      * Check availability and insert record
294      * @return true if saved/updated
295      */

296     private boolean cmd_save()
297     {
298         Log.trace(Log.l3_Util, "VAssignmentDialog.save");
299         // Set AssignDateTo
300
Timestamp JavaDoc assignDateFrom = fDateFrom.getTimestamp();
301         BigDecimal JavaDoc qty = (BigDecimal JavaDoc)fQty.getValue();
302         KeyNamePair uom = (KeyNamePair)s_lookup.get(fResource.getSelectedItem());
303         int minutes = UOMConversion.convertToMinutes(Env.getCtx(), uom.getKey(), qty);
304         Timestamp JavaDoc assignDateTo = TimeUtil.addMinutess(assignDateFrom, minutes);
305         m_mAssignment.setAssignDateTo (assignDateTo);
306         //
307
// m_mAssignment.dump();
308
return m_mAssignment.save();
309     } // cmdSave
310

311     /*************************************************************************/
312
313     /** Lookup with Resource & UOM */
314     private static HashMap JavaDoc s_lookup = new HashMap JavaDoc();
315
316     /**
317      * Load Resources
318      * @return Array with resources
319      */

320     static KeyNamePair[] getResources()
321     {
322         if (s_lookup.size() == 0)
323         {
324             String JavaDoc sql = MRole.getDefault().addAccessSQL(
325                 "SELECT r.S_Resource_ID, r.Name, r.IsActive," // 1..3
326
+ "uom.C_UOM_ID,uom.UOMSymbol " // 4..5
327
+ "FROM S_Resource r, S_ResourceType rt, C_UOM uom "
328                 + "WHERE r.S_ResourceType_ID=rt.S_ResourceType_ID AND rt.C_UOM_ID=uom.C_UOM_ID",
329                 "r", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO);
330             try
331             {
332                 PreparedStatement JavaDoc pstmt = DB.prepareStatement(sql);
333                 ResultSet JavaDoc rs = pstmt.executeQuery();
334                 while (rs.next())
335                 {
336                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc (rs.getString(2));
337                     if (!"Y".equals(rs.getString(3)))
338                         sb.insert(0,'~').append('~'); // inactive marker
339
// Key S_Resource_ID/Name
340
KeyNamePair key = new KeyNamePair (rs.getInt(1), sb.toString());
341                     // Value C_UOM_ID/Name
342
KeyNamePair value = new KeyNamePair (rs.getInt(4), rs.getString(5).trim());
343                     s_lookup.put(key, value);
344                 }
345                 rs.close();
346                 pstmt.close();
347             }
348             catch (SQLException JavaDoc e)
349             {
350                 Log.error("VAssignment.getResources", e);
351             }
352         }
353         // Convert to Array
354
KeyNamePair[] retValue = new KeyNamePair[s_lookup.size()];
355         s_lookup.keySet().toArray(retValue);
356         Arrays.sort(retValue);
357         return retValue;
358     } // getResources
359

360 } // VAssignmentDialog
361
Popular Tags