1 14 package org.compiere.swing; 15 16 import java.awt.Color ; 17 import java.awt.Dialog ; 18 import java.awt.Frame ; 19 import java.awt.Window ; 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 import java.lang.reflect.Constructor ; 23 import java.text.DateFormat ; 24 import java.text.DecimalFormat ; 25 import java.text.NumberFormat ; 26 import java.text.SimpleDateFormat ; 27 import java.util.regex.Pattern ; 28 29 import javax.swing.Icon ; 30 import javax.swing.JComboBox ; 31 import javax.swing.JOptionPane ; 32 import javax.swing.SwingUtilities ; 33 import javax.swing.plaf.TextUI ; 34 35 import org.compiere.plaf.CompierePLAF; 36 37 44 public class CField extends JComboBox 45 implements CEditor, ActionListener 46 { 47 48 public static CField createNumeric(NumberFormat format) 49 { 50 return null; 51 } 52 public static CField createNumeric() 53 { 54 return createNumeric(new DecimalFormat ()); 55 } 56 public static CField createDate(DateFormat format) 57 { 58 return null; 59 } 60 public static CField createDate() 61 { 62 return createDate(new SimpleDateFormat ()); 63 } 64 public static CField createText(Pattern p) 65 { 66 return null; 67 } 68 public static CField createText(int length) 69 { 70 return null; 71 } 72 73 74 75 public CField () 76 { 77 this (null, null, ""); 78 } 79 80 87 public CField (CFieldEditor editor, Class cFieldPopup, String title) 88 { 89 super(new Object [] {"1", "2"}); 90 if (editor != null) 91 setEditable(true); 93 m_title = title; 94 95 if (cFieldPopup != null) 97 { 98 Class [] interfaces = cFieldPopup.getInterfaces(); 99 boolean found = false; 100 for (int i = 0; i < interfaces.length; i++) 101 { 102 if (interfaces[i].equals(CFieldPopup.class)) 103 { 104 found = true; 105 break; 106 } 107 } 108 if (!found) 109 throw new IllegalArgumentException ("CField - Popup class must be CFieldPopup"); 110 } 111 super.addActionListener(this); 112 } 114 private CFieldEditor m_editor = null; 115 private Class m_popupClass = null; 116 private String m_title = null; 117 private Object m_oldValue = null; 118 119 120 121 122 private Icon m_icon = null; 123 124 128 public void setIcon (Icon defaultIcon) 129 { 130 m_icon = defaultIcon; 131 } 133 137 public Icon getIcon () 138 { 139 return m_icon; 140 } 142 143 147 public void setUI (TextUI ui) 148 { 149 super.setUI(ui); 150 } 152 158 public boolean displayPopup() 159 { 160 if (m_popupClass == null) 161 return false; 162 try 164 { 165 Window win = SwingUtilities.getWindowAncestor(this); 167 CFieldPopup popup = null; 168 if (win instanceof Dialog ) 169 { 170 Constructor constructor = m_popupClass.getConstructor 171 (new Class [] {Dialog .class, String .class, Boolean .class}); 172 popup = (CFieldPopup)constructor.newInstance(new Object [] 173 {(Dialog )win, m_title, new Boolean (true)}); 174 } 175 else if (win instanceof Frame ) 176 { 177 Constructor constructor = m_popupClass.getConstructor 178 (new Class [] {Frame .class, String .class, Boolean .class}); 179 popup = (CFieldPopup)constructor.newInstance(new Object [] 180 {(Frame )win, m_title, new Boolean (true)}); 181 } 182 if (popup == null) 183 return false; 184 popup.setValue (m_editor.getItem()); 186 popup.setFormat (m_editor.getFormat()); 187 popup.show(); 188 m_editor.setItem (popup.getValue()); 189 popup = null; 190 } 191 catch (Exception e) 192 { 193 notifyUser (e); 194 } 195 return false; 197 } 199 203 public void notifyUser (Exception e) 204 { 205 JOptionPane.showMessageDialog(this, e.toString(), "Field Error", JOptionPane.ERROR_MESSAGE); 206 } 208 209 210 211 private boolean m_mandatory = false; 212 213 217 public void setMandatory (boolean mandatory) 218 { 219 m_mandatory = mandatory; 220 setBackground(false); 221 } 223 227 public boolean isMandatory() 228 { 229 return m_mandatory; 230 } 232 236 public void setReadWrite (boolean rw) 237 { 238 if (super.isEnabled() != rw) 239 super.setEnabled (rw); 240 setBackground(false); 241 } 243 247 public boolean isReadWrite() 248 { 249 return super.isEnabled(); 250 } 252 253 257 public void setBackground (boolean error) 258 { 259 Color bg = null; 260 if (error) 261 bg = CompierePLAF.getFieldBackground_Error(); 262 else if (!isReadWrite()) 263 bg = CompierePLAF.getFieldBackground_Inactive(); 264 else if (m_mandatory) 265 bg = CompierePLAF.getFieldBackground_Mandatory(); 266 else 267 bg = CompierePLAF.getFieldBackground_Normal(); 268 if (bg.equals(m_editor.getBackground())) 269 return; 270 m_editor.setBackground(bg); 271 } 273 277 public void setValue (Object value) 278 { 279 m_oldValue = value; 280 } 283 287 public Object getValue() 288 { 289 return null; } 292 296 public String getDisplay() 297 { 298 return ""; 300 } 303 304 305 309 public void actionPerformed(ActionEvent e) 310 { 311 Object newValue = getValue(); 313 if ((newValue != null && newValue.equals(m_oldValue)) 314 || (newValue == null && m_oldValue == null) ) 315 return; 316 super.firePropertyChange("DataChanged", m_oldValue, newValue); 317 m_oldValue = newValue; 318 } 320 } | Popular Tags |