1 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 37 public class VAssignment extends JComponent 38 implements VEditor, ActionListener 39 { 40 43 public VAssignment() 44 { 45 this (false, false, true); 46 } 48 57 public VAssignment (boolean mandatory, boolean isReadOnly, boolean isUpdateable) 58 { 59 super(); 60 LookAndFeel.installBorder(this, "TextField.border"); 62 this.setLayout(new BorderLayout()); 63 this.setPreferredSize(m_text.getPreferredSize()); 65 int height = m_text.getPreferredSize().height; 66 67 m_text.setEditable(false); 69 m_text.setFocusable(false); 70 m_text.setBorder(null); 71 m_text.setHorizontalAlignment(JTextField.LEADING); 72 setMandatory(mandatory); 74 this.add(m_text, BorderLayout.CENTER); 75 76 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 this.setPreferredSize(this.getPreferredSize()); if (isReadOnly || !isUpdateable) 88 setReadWrite(false); 89 else 90 setReadWrite(true); 91 92 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 } 99 100 private Object m_value = null; 101 102 private PreparedStatement m_pstmt = null; 103 104 105 private JTextField m_text = new JTextField (VLookup.DISPLAY_LENGTH); 106 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 116 private DateFormat m_dateFormat = DisplayType.getDateFormat(DisplayType.DateTime); 117 private NumberFormat m_qtyFormat = DisplayType.getNumberFormat(DisplayType.Quantity); 118 119 122 public void dispose() 123 { 124 try 125 { 126 if (m_pstmt != null) 127 m_pstmt.close(); 128 } 129 catch (Exception e) 130 { 131 Log.error("VAssignment.dispose"); 132 } 133 m_text = null; 134 m_button = null; 135 } 137 141 public void setMandatory (boolean mandatory) 142 { 143 m_mandatory = mandatory; 144 m_button.setMandatory(mandatory); 145 setBackground (false); 146 } 148 152 public boolean isMandatory() 153 { 154 return m_mandatory; 155 } 157 161 public void setReadWrite (boolean rw) 162 { 163 m_readWrite = rw; 164 m_button.setReadWrite(rw); 165 setBackground (false); 166 } 168 172 public boolean isReadWrite() 173 { 174 return m_readWrite; 175 } 177 181 public void setForeground (Color color) 182 { 183 m_text.setForeground(color); 184 } 186 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 } 202 206 public void setBackground (Color color) 207 { 208 m_text.setBackground(color); 209 } 211 212 213 217 public void setValue(Object 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 ) 224 S_ResourceAssignment_ID = ((Integer )m_value).intValue(); 225 if (S_ResourceAssignment_ID == 0) 227 { 228 m_text.setText(""); 229 return; 230 } 231 232 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 try 242 { 243 m_pstmt.setInt(1, S_ResourceAssignment_ID); 244 ResultSet rs = m_pstmt.executeQuery(); 245 if (rs.next()) 246 { 247 StringBuffer sb = new StringBuffer (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 e) 258 { 259 Log.error("VAssigment.setValue", e); 260 } 261 } 263 267 public Object getValue() 268 { 269 return m_value; 270 } 272 276 public String getDisplay() 277 { 278 return m_text.getText(); 279 } 281 282 283 287 public void setField(MField mField) 288 { 289 } 291 295 public void addActionListener(ActionListener listener) 296 { 297 } 300 304 public void actionPerformed(ActionEvent e) 305 { 306 if (!m_button.isEnabled()) 307 return; 308 m_button.setEnabled(false); 309 Integer oldValue = (Integer )getValue(); 311 int S_ResourceAssignment_ID = oldValue == null ? 0 : oldValue.intValue(); 312 MAssignment ma = new MAssignment(Env.getCtx(), S_ResourceAssignment_ID); 313 314 if (S_ResourceAssignment_ID != 0) 316 { 317 VAssignmentDialog vad = new VAssignmentDialog (Env.getFrame(this), ma, true, true); 318 ma = vad.getMAssignment(); 319 } 320 else 322 { 323 InfoSchedule is = new InfoSchedule(Env.getFrame(this), ma, true); 324 ma = is.getMAssignment(); 325 } 326 327 if (ma != null && ma.getS_ResourceAssignment_ID() != 0) 329 { 330 setValue(new Integer (ma.getS_ResourceAssignment_ID())); 331 try 332 { 333 fireVetoableChange("S_ResourceAssignment_ID", new Object (), getValue()); 334 } 335 catch (PropertyVetoException pve) 336 { 337 Log.error("VAssignment.actionPerformed", pve); 338 } 339 } 340 m_button.setEnabled(true); 341 requestFocus(); 342 } 344 348 public void propertyChange (PropertyChangeEvent evt) 349 { 350 if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY)) 351 setValue(evt.getNewValue()); 352 } 354 } 356 359 final class VAssignment_mouseAdapter extends MouseAdapter 360 { 361 365 VAssignment_mouseAdapter(VAssignment adaptee) 366 { 367 this.adaptee = adaptee; 368 } 370 private VAssignment adaptee; 371 372 376 public void mouseClicked(MouseEvent e) 377 { 378 if (e.getClickCount() > 1) 380 adaptee.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Mouse")); 381 if (SwingUtilities.isRightMouseButton(e)) 383 adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY()); 384 } 386 } 388 | Popular Tags |