1 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 ; 20 21 import javax.swing.*; 22 import javax.swing.border.TitledBorder ; 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 36 public class VPAttributeDialog extends CDialog 37 implements ActionListener 38 { 39 42 public VPAttributeDialog () 43 { 44 this (null, 0, 0, false, false); 45 } 47 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 try 68 { 69 jbInit(); 70 } 71 catch(Exception ex) 72 { 73 Log.error ("VPAttributeDialog" + ex); 74 } 75 if (!initAttributes ()) 77 { 78 dispose(); 79 return; 80 } 81 AEnv.showCenterWindow(frame, this); 82 } 84 private int m_WindowNo; 85 private MAttributeSetInstance m_masi; 86 private int m_M_AttributeSetInstance_ID; 87 private String m_M_AttributeSetInstanceName; 88 private int m_M_Product_ID; 89 90 private boolean m_productWindow; 91 92 private boolean m_instanceWindow; 93 94 95 private boolean m_changed = false; 96 private Logger log = Logger.getCLogger(getClass()); 97 98 private int m_row = 0; 99 100 private ArrayList m_editors = new ArrayList (); 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 private CTextField fieldDescription = new CTextField (20); 111 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 121 private void jbInit () throws Exception 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 confirmPanel.addActionListener(this); 129 } 131 136 private boolean initAttributes () 137 { 138 if (m_M_Product_ID == 0) 139 return false; 140 141 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 MAttributeSet as = m_masi.getMAttributeSet(); 151 if (as == null) 152 { 153 ADialog.error(m_WindowNo, this, "PAttributeNoAttributeSet"); 154 return false; 155 } 156 157 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 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 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 if (m_masi.getMAttributeSet().getM_LotCtl_ID() != 0) 184 { 185 centerPanel.add(bLot, null); 186 bLot.addActionListener(this); 187 } 188 } 190 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 if (m_masi.getMAttributeSet().getM_SerNoCtl_ID() != 0) 200 { 201 centerPanel.add(bSerNo, null); 202 bSerNo.addActionListener(this); 203 } 204 } 206 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 } 219 if (m_row == 0) 220 { 221 ADialog.error(m_WindowNo, this, "PAttributeNoInfo"); 222 return false; 223 } 224 225 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 Dimension dd = centerPanel.getPreferredSize(); 235 dd.width = Math.min(500, dd.width); 236 centerPanel.setPreferredSize(dd); 237 return true; 238 } 240 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 MAttributeInstance instance = attribute.getMAttributeInstance (m_M_AttributeSetInstance_ID); 257 if (attribute.isList()) 258 { 259 MAttributeValue[] values = attribute.getMAttributeValues(); 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 } 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 { 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 } 301 302 305 public void dispose() 306 { 307 removeAll(); 308 Env.clearWinContext(m_WindowNo); 309 super.dispose(); 310 } 312 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 } 339 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 } if (m_instanceWindow && as.isSerNo()) 356 { 357 log.debug("saveSelection - SerNo=" + fieldSerNo.getText()); 358 m_masi.setSerNo(fieldSerNo.getText()); 359 change = true; 360 } if (m_instanceWindow && as.isGuaranteeDate()) 362 { 363 log.debug("saveSelection - GuaranteeDate=" + fieldGuaranteeDate.getValue()); 364 m_masi.setGuaranteeDate((Timestamp)fieldGuaranteeDate.getValue()); 365 change = true; 366 } 368 if (m_instanceWindow || m_productWindow) 370 { 371 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 } change = true; 396 } 397 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 } 407 408 409 413 public int getM_AttributeSetInstance_ID() 414 { 415 return m_M_AttributeSetInstance_ID; 416 } 418 422 public String getM_AttributeSetInstanceName() 423 { 424 return m_M_AttributeSetInstanceName; 425 } 427 428 } | Popular Tags |