1 14 package org.compiere.grid.ed; 15 16 import java.awt.BorderLayout ; 17 import java.awt.Frame ; 18 import java.awt.GridBagConstraints ; 19 import java.awt.GridBagLayout ; 20 import java.awt.Insets ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.math.BigDecimal ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Timestamp ; 28 import java.util.Arrays ; 29 import java.util.HashMap ; 30 31 import javax.swing.JButton ; 32 import javax.swing.JDialog ; 33 import javax.swing.WindowConstants ; 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 51 public class VAssignmentDialog extends JDialog implements ActionListener  52 { 53 72 public VAssignmentDialog (Frame 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 e) 86 { 87 Log.error("VAssignmentDialog", e); 88 } 89 setDisplay(); AEnv.showCenterScreen(this); 92 } 94 95 private MAssignment m_mAssignment; 96 97 private Frame m_frame; 98 99 private boolean m_setting = false; 100 101 private CPanel mainPanel = new CPanel(); 103 private GridBagLayout mainLayout = new GridBagLayout (); 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 delete = ConfirmPanel.createDeleteButton(true); 117 118 119 123 private void jbInit() throws Exception  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 mainPanel.setLayout(mainLayout); 133 mainPanel.add(lResource, new GridBagConstraints (0, 0, 1, 1, 0.0, 0.0 134 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (8, 8, 4, 4), 0, 0)); 135 mainPanel.add(fResource, new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0 136 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (8, 0, 4, 4), 0, 0)); 137 mainPanel.add(lDate, new GridBagConstraints (0, 1, 1, 1, 0.0, 0.0 138 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (2, 8, 4, 4), 0, 0)); 139 mainPanel.add(fDateFrom, new GridBagConstraints (1, 1, 2, 1, 0.0, 0.0 140 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (2, 0, 4, 8), 100, 0)); 141 mainPanel.add(lQty, new GridBagConstraints (0, 2, 1, 1, 0.0, 0.0 142 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (2, 8, 4, 4), 0, 0)); 143 mainPanel.add(fQty, new GridBagConstraints (1, 2, 1, 1, 0.0, 0.0 144 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets (2, 0, 4, 4), 0, 0)); 145 mainPanel.add(lUOM, new GridBagConstraints (2, 2, 1, 1, 0.0, 0.0 146 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (2, 4, 4, 8), 0, 0)); 147 mainPanel.add(lName, new GridBagConstraints (0, 3, 1, 1, 0.0, 0.0 148 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (2, 8, 4, 4), 0, 0)); 149 mainPanel.add(lDescription, new GridBagConstraints (0, 4, 1, 1, 0.0, 0.0 150 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets (2, 8, 8, 4), 0, 0)); 151 mainPanel.add(fName, new GridBagConstraints (1, 3, 2, 1, 0.0, 0.0 152 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (2, 0, 4, 8), 0, 0)); 153 mainPanel.add(fDescription, new GridBagConstraints (1, 4, 2, 1, 0.0, 0.0 154 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (2, 0, 8, 8), 0, 0)); 155 this.getContentPane().add(mainPanel, BorderLayout.CENTER); 157 this.getContentPane().add(confirmPanel, BorderLayout.SOUTH); 158 } 160 163 private void setDisplay() 164 { 165 m_setting = true; 166 167 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) fResource.setSelectedItem(fResource.getSelectedItem()); else 185 Log.error("VAssignmentDialog.dynInit - Resource not found ID=" + S_Resource_ID); 186 } 187 188 fDateFrom.setValue(m_mAssignment.getAssignDateFrom()); 190 fQty.setValue(m_mAssignment.getQty()); 191 192 fName.setValue(m_mAssignment.getName()); 194 fDescription.setValue(m_mAssignment.getDescription()); 195 196 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 } 208 212 public void actionPerformed(ActionEvent e) 213 { 214 if (m_setting) 215 return; 216 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 assignDateFrom = fDateFrom.getTimestamp(); 224 if (assignDateFrom != null) 225 m_mAssignment.setAssignDateFrom (assignDateFrom); 226 BigDecimal qty = (BigDecimal )fQty.getValue(); 227 if (qty != null) 228 m_mAssignment.setQty(qty); 229 m_mAssignment.setName((String )fName.getValue()); 230 m_mAssignment.setDescription((String )fDescription.getValue()); 231 232 if (e.getSource() == fResource) 234 { 235 Object o = s_lookup.get(fResource.getSelectedItem()); 236 if (o == null) 237 lUOM.setText(" ? "); 238 else 239 lUOM.setText(o.toString()); 240 } 241 242 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 dispose(); 252 } 253 is = null; 254 } 255 256 else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL)) 258 { 259 dispose(); 260 } 261 262 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 else if (e.getActionCommand().equals(ConfirmPanel.A_OK)) 274 { 275 if (cmd_save()) 276 dispose(); 277 } 278 } 280 281 282 286 public MAssignment getMAssignment() 287 { 288 return m_mAssignment; 289 } 291 292 296 private boolean cmd_save() 297 { 298 Log.trace(Log.l3_Util, "VAssignmentDialog.save"); 299 Timestamp assignDateFrom = fDateFrom.getTimestamp(); 301 BigDecimal qty = (BigDecimal )fQty.getValue(); 302 KeyNamePair uom = (KeyNamePair)s_lookup.get(fResource.getSelectedItem()); 303 int minutes = UOMConversion.convertToMinutes(Env.getCtx(), uom.getKey(), qty); 304 Timestamp assignDateTo = TimeUtil.addMinutess(assignDateFrom, minutes); 305 m_mAssignment.setAssignDateTo (assignDateTo); 306 return m_mAssignment.save(); 309 } 311 312 313 314 private static HashMap s_lookup = new HashMap (); 315 316 320 static KeyNamePair[] getResources() 321 { 322 if (s_lookup.size() == 0) 323 { 324 String sql = MRole.getDefault().addAccessSQL( 325 "SELECT r.S_Resource_ID, r.Name, r.IsActive," + "uom.C_UOM_ID,uom.UOMSymbol " + "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 pstmt = DB.prepareStatement(sql); 333 ResultSet rs = pstmt.executeQuery(); 334 while (rs.next()) 335 { 336 StringBuffer sb = new StringBuffer (rs.getString(2)); 337 if (!"Y".equals(rs.getString(3))) 338 sb.insert(0,'~').append('~'); KeyNamePair key = new KeyNamePair (rs.getInt(1), sb.toString()); 341 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 e) 349 { 350 Log.error("VAssignment.getResources", e); 351 } 352 } 353 KeyNamePair[] retValue = new KeyNamePair[s_lookup.size()]; 355 s_lookup.keySet().toArray(retValue); 356 Arrays.sort(retValue); 357 return retValue; 358 } 360 } | Popular Tags |