KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.*;
19 import java.util.ArrayList JavaDoc;
20
21 import javax.swing.*;
22 import javax.swing.border.TitledBorder JavaDoc;
23
24 import org.compiere.apps.*;
25 import org.compiere.model.*;
26 import org.compiere.plaf.CompiereColor;
27 import org.compiere.swing.*;
28 import org.compiere.util.*;
29
30 /**
31  * Product Attribute Set Product/Instance Dialog Editor
32  *
33  * @author Jorg Janke
34  * @version $Id: VPAttributeDialog.java,v 1.8 2003/11/07 06:36:56 jjanke Exp $
35  */

36 public class VPAttributeDialog extends CDialog
37     implements ActionListener
38 {
39     /**
40      * IDE Constructor
41      */

42     public VPAttributeDialog ()
43     {
44         this (null, 0, 0, false, false);
45     } // VPAttributeDialog
46

47     /**
48      * Product Attribute Instance Dialog
49      * @param frame parent frame
50      * @param M_AttributeSetInstance_ID Product Attribute Set Instance id
51      * @param M_Product_ID Product id
52      * @param isProductWindow true if Product Attribute Window
53      * @param isInstanceWindow true if Instance Attribute Window
54      */

55     public VPAttributeDialog (Frame frame, int M_AttributeSetInstance_ID, int M_Product_ID,
56         boolean isProductWindow, boolean isInstanceWindow)
57     {
58         super (frame, Msg.translate(Env.getCtx(), "M_AttributeSetInstance_ID") , true);
59         Log.trace(Log.l2_Sub, "VPAttributeDialog", "M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID + ", M_Product_ID=" + M_Product_ID
60             + ", ProductW=" + isProductWindow + ", InstanceW=" + isInstanceWindow);
61         m_WindowNo = Env.createWindowNo (this);
62         m_productWindow = isProductWindow;
63         m_instanceWindow = isInstanceWindow;
64         m_M_AttributeSetInstance_ID = M_AttributeSetInstance_ID;
65         m_M_Product_ID = M_Product_ID;
66         //
67
try
68         {
69             jbInit();
70         }
71         catch(Exception JavaDoc ex)
72         {
73             Log.error ("VPAttributeDialog" + ex);
74         }
75         // Dynamic Init
76
if (!initAttributes ())
77         {
78             dispose();
79             return;
80         }
81         AEnv.showCenterWindow(frame, this);
82     } // VPAttributeDialog
83

84     private int m_WindowNo;
85     private MAttributeSetInstance m_masi;
86     private int m_M_AttributeSetInstance_ID;
87     private String JavaDoc m_M_AttributeSetInstanceName;
88     private int m_M_Product_ID;
89     /** Enter Product Attributes */
90     private boolean m_productWindow;
91     /** Enter Instance Attributes */
92     private boolean m_instanceWindow;
93
94
95     private boolean m_changed = false;
96     private Logger log = Logger.getCLogger(getClass());
97     /** Row Counter */
98     private int m_row = 0;
99     /** List of Editors */
100     private ArrayList JavaDoc m_editors = new ArrayList JavaDoc();
101
102     private static final int INSTANCE_VALUE_LENGTH = 40;
103
104     private VString fieldLotString = new VString ("Lot", false, false, true, 20, 20, null);
105     private CButton bLot = new CButton(Msg.getMsg (Env.getCtx(), "New"));
106     private VString fieldSerNo = new VString ("SerNo", false, false, true, 20, 20, null);
107     private CButton bSerNo = new CButton(Msg.getMsg (Env.getCtx(), "New"));
108     private VDate fieldGuaranteeDate = new VDate ("GuaranteeDate", false, false, true, DisplayType.Date, Msg.translate(Env.getCtx(), "GuaranteeDate"));
109     //
110
private CTextField fieldDescription = new CTextField (20);
111     //
112
private JPanel centerPanel = new JPanel();
113     private BorderLayout mainLayout = new BorderLayout();
114     private ALayout centerLayout = new ALayout(5,5, true);
115     private ConfirmPanel confirmPanel = new ConfirmPanel (true);
116
117     /**
118      * Layout
119      * @throws Exception
120      */

121     private void jbInit () throws Exception JavaDoc
122     {
123         this.getContentPane().setLayout(mainLayout);
124         this.getContentPane().add(centerPanel, BorderLayout.CENTER);
125         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
126         centerPanel.setLayout(centerLayout);
127         //
128
confirmPanel.addActionListener(this);
129     } // jbInit
130

131     /**
132      * Dyanmic Init.
133      * @param M_Product_ID product
134      * @return true if initialized
135      */

136     private boolean initAttributes ()
137     {
138         if (m_M_Product_ID == 0)
139             return false;
140             
141         // Get Model
142
m_masi = MAttributeSetInstance.get(Env.getCtx(), m_M_AttributeSetInstance_ID, m_M_Product_ID);
143         if (m_masi == null)
144         {
145             log.error ("initAttributes - No Model for M_AttributeSetInstance_ID=" + m_M_AttributeSetInstance_ID + ", M_Product_ID=" + m_M_Product_ID);
146             return false;
147         }
148
149         // Get Attribute Set
150
MAttributeSet as = m_masi.getMAttributeSet();
151         if (as == null)
152         {
153             ADialog.error(m_WindowNo, this, "PAttributeNoAttributeSet");
154             return false;
155         }
156
157         // Show Product Attributes
158
if (m_productWindow)
159         {
160             MAttribute[] attributes = as.getMAttributes (false);
161             Log.trace (Log.l4_Data, "VPAttributeDialog.initAttributes - Product Attributes=" + attributes.length);
162             for (int i = 0; i < attributes.length; i++)
163                 addAttributeLine (attributes[i], true, !m_productWindow);
164         }
165         // Set Instance Attributes
166
if (m_instanceWindow)
167         {
168             MAttribute[] attributes = as.getMAttributes (true);
169             Log.trace (Log.l4_Data, "VPAttributeDialog.initAttributes - Instance Attributes=" + attributes.length);
170             for (int i = 0; i < attributes.length; i++)
171                 addAttributeLine (attributes[i], false, false);
172         }
173
174         // Lot
175
if (m_instanceWindow && as.isLot())
176         {
177             CLabel label = new CLabel (Msg.translate(Env.getCtx(), "Lot"));
178             label.setLabelFor (fieldLotString);
179             centerPanel.add(label, new ALayoutConstraint(m_row++,0));
180             centerPanel.add(fieldLotString, null);
181             fieldLotString.setText (m_masi.getLot ());
182             // New Lot Button
183
if (m_masi.getMAttributeSet().getM_LotCtl_ID() != 0)
184             {
185                 centerPanel.add(bLot, null);
186                 bLot.addActionListener(this);
187             }
188         } // Lot
189

190         // SerNo
191
if (m_instanceWindow && as.isSerNo())
192         {
193             CLabel label = new CLabel (Msg.translate(Env.getCtx(), "SerNo"));
194             label.setLabelFor(fieldSerNo);
195             fieldSerNo.setText(m_masi.getSerNo());
196             centerPanel.add(label, new ALayoutConstraint(m_row++,0));
197             centerPanel.add(fieldSerNo, null);
198             // New SerNo Button
199
if (m_masi.getMAttributeSet().getM_SerNoCtl_ID() != 0)
200             {
201                 centerPanel.add(bSerNo, null);
202                 bSerNo.addActionListener(this);
203             }
204         } // SerNo
205

206         // GuaranteeDate
207
if (m_instanceWindow && as.isGuaranteeDate())
208         {
209             CLabel label = new CLabel (Msg.translate(Env.getCtx(), "GuaranteeDate"));
210             label.setLabelFor(fieldGuaranteeDate);
211             if (m_M_AttributeSetInstance_ID == 0)
212                 fieldGuaranteeDate.setValue(m_masi.getGuaranteeDate(true));
213             else
214                 fieldGuaranteeDate.setValue(m_masi.getGuaranteeDate());
215             centerPanel.add(label, new ALayoutConstraint(m_row++,0));
216             centerPanel.add(fieldGuaranteeDate, null);
217         } // GuaranteeDate
218

219         if (m_row == 0)
220         {
221             ADialog.error(m_WindowNo, this, "PAttributeNoInfo");
222             return false;
223         }
224
225         // Attrribute Set Instance Description
226
CLabel label = new CLabel (Msg.translate(Env.getCtx(), "Description"));
227         label.setLabelFor(fieldDescription);
228         fieldDescription.setText(m_masi.getDescription());
229         fieldDescription.setEditable(false);
230         centerPanel.add(label, new ALayoutConstraint(m_row++,0));
231         centerPanel.add(fieldDescription, null);
232
233         // Window usually to wide (??)
234
Dimension dd = centerPanel.getPreferredSize();
235         dd.width = Math.min(500, dd.width);
236         centerPanel.setPreferredSize(dd);
237         return true;
238     } // initAttribute
239

240     /**
241      * Add Attribute Line
242      * @param attribute attribute
243      * @param product product level attribute
244      * @param readOnly value is read only
245      */

246     private void addAttributeLine (MAttribute attribute, boolean product, boolean readOnly)
247     {
248         Log.trace(Log.l5_DData, "VPAttributeDialog.addAttributeLine", "Attribute=" + attribute.getName() + ", Product=" + product + ", R/O=" + readOnly);
249         CLabel label = new CLabel (attribute.getName());
250         if (product)
251             label.setFont(new Font(label.getFont().getFontName(), Font.BOLD, label.getFont().getSize()));
252         if (attribute.getDescription() != null)
253             label.setToolTipText(attribute.getDescription());
254         centerPanel.add(label, new ALayoutConstraint(m_row++,0));
255         //
256
MAttributeInstance instance = attribute.getMAttributeInstance (m_M_AttributeSetInstance_ID);
257         if (attribute.isList())
258         {
259             MAttributeValue[] values = attribute.getMAttributeValues(); // optional = null
260
CComboBox editor = new CComboBox(values);
261             boolean found = false;
262             if (instance != null)
263             {
264                 for (int i = 0; i < values.length; i++)
265                 {
266                     if (values[i] != null && values[i].getM_AttributeValue_ID () == instance.getM_AttributeValue_ID ())
267                     {
268                         editor.setSelectedIndex (i);
269                         found = true;
270                         break;
271                     }
272                 }
273                 if (found)
274                     log.debug("addAttributeLine - Attribute=" + attribute.getName() + " #" + values.length + " - found: " + instance);
275                 else
276                     log.warn("addAttributeLine - Attribute=" + attribute.getName() + " #" + values.length + " - NOT found: " + instance);
277             } // setComboBox
278
else
279                 log.debug("addAttributeLine - Attribute=" + attribute.getName() + " #" + values.length + " no instance");
280             label.setLabelFor(editor);
281             centerPanel.add(editor, null);
282             if (readOnly)
283                 editor.setEnabled(false);
284             else
285                 m_editors.add (editor);
286         }
287         else // Text Field
288
{
289             VText editor = new VText(attribute.getName(), attribute.isMandatory(), false, true, 20, INSTANCE_VALUE_LENGTH);
290             if (instance != null)
291                 editor.setText(instance.getValue());
292             label.setLabelFor(editor);
293             centerPanel.add(editor, null);
294             if (readOnly)
295                 editor.setEnabled(false);
296             else
297                 m_editors.add (editor);
298         }
299     } // addAttributeLine
300

301
302     /**
303      * dispose
304      */

305     public void dispose()
306     {
307         removeAll();
308         Env.clearWinContext(m_WindowNo);
309         super.dispose();
310     } // dispose
311

312     /**
313      * ActionListener
314      * @param e event
315      */

316     public void actionPerformed(ActionEvent e)
317     {
318         if (e.getSource() == bLot)
319         {
320             fieldLotString.setText(m_masi.getLot(true, m_M_Product_ID));
321         }
322         else if (e.getSource() == bSerNo)
323         {
324             fieldSerNo.setText(m_masi.getSerNo(true));
325         }
326         else if (e.getActionCommand().equals(ConfirmPanel.A_OK))
327         {
328             m_changed = true;
329             saveSelection();
330             dispose();
331         }
332         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
333         {
334             m_changed = false;
335             dispose();
336         }
337     } // actionPerformed
338

339     /**
340      * Save Selection
341      */

342     private void saveSelection()
343     {
344         log.info("saveSelection");
345         MAttributeSet as = m_masi.getMAttributeSet();
346         if (as == null)
347             return;
348         boolean change = false;
349         if (m_instanceWindow && as.isLot())
350         {
351             log.debug ("saveSelection - Lot=" + fieldLotString.getText ());
352             m_masi.setLot (fieldLotString.getText ());
353             change = true;
354         } // Lot
355
if (m_instanceWindow && as.isSerNo())
356         {
357             log.debug("saveSelection - SerNo=" + fieldSerNo.getText());
358             m_masi.setSerNo(fieldSerNo.getText());
359             change = true;
360         } // SerNo
361
if (m_instanceWindow && as.isGuaranteeDate())
362         {
363             log.debug("saveSelection - GuaranteeDate=" + fieldGuaranteeDate.getValue());
364             m_masi.setGuaranteeDate((Timestamp)fieldGuaranteeDate.getValue());
365             change = true;
366         } // GuaranteeDate
367

368         // Save Attributes
369
if (m_instanceWindow || m_productWindow)
370         {
371             // New Instance
372
if (change || m_masi.getM_AttributeSetInstance_ID() == 0)
373             {
374                 m_masi.save ();
375                 m_M_AttributeSetInstance_ID = m_masi.getM_AttributeSetInstance_ID ();
376                 m_M_AttributeSetInstanceName = m_masi.getDescription();
377             }
378
379             MAttribute[] attributes = as.getMAttributes(m_instanceWindow);
380             for (int i = 0; i < attributes.length; i++)
381             {
382                 if (attributes[i].isList())
383                 {
384                     CComboBox editor = (CComboBox)m_editors.get(i);
385                     log.debug("saveSelection - " + attributes[i].getName() + "=" + editor.getSelectedItem());
386                     attributes[i].setMAttributeInstance(m_M_AttributeSetInstance_ID, (MAttributeValue)editor.getSelectedItem());
387                 }
388                 else
389                 {
390                     VText editor = (VText)m_editors.get(i);
391                     log.debug("saveSelection - " + attributes[i].getName() + "=" + editor.getText());
392                     attributes[i].setMAttributeInstance(m_M_AttributeSetInstance_ID, editor.getText());
393                 }
394             } // for all attributes
395
change = true;
396         }
397         // Save Model
398
if (change)
399         {
400             m_masi.setDescription ();
401             m_masi.save ();
402         }
403         m_M_AttributeSetInstance_ID = m_masi.getM_AttributeSetInstance_ID ();
404         m_M_AttributeSetInstanceName = m_masi.getDescription();
405     } // saveSelection
406

407     /*************************************************************************/
408
409     /**
410      * Get Instance ID
411      * @return Instance ID
412      */

413     public int getM_AttributeSetInstance_ID()
414     {
415         return m_M_AttributeSetInstance_ID;
416     } // getM_AttributeSetInstance_ID
417

418     /**
419      * Get Instance Name
420      * @return Instance Name
421      */

422     public String JavaDoc getM_AttributeSetInstanceName()
423     {
424         return m_M_AttributeSetInstanceName;
425     } // getM_AttributeSetInstanceName
426

427
428 } // VPAttributeDialog
429
Popular Tags