001 package market.swing; 002 003 import javax.swing.JTextField; 004 005 import market.ValueChecker; 006 007 /** 008 * A text field, which is provided with an ID that makes it possible to query its contents from outside 009 * the defining class, without the need to write a get-method or even have the text field declared as 010 * public.<br> 011 * Furthermore, a {@link ValueChecker} is assigned, so that the text field's content can be easily checked 012 * for validity. This results in a huge reduction of redundant code that would be necessary when checking 013 * every text field manually. 014 * A JTFCheckable, however, works only in connection with a {@link market.stdform.FSCheckable}. 015 */ 016 public class JTFCheckable extends JTextField { 017 018 /** 019 * ID for serialization. 020 */ 021 private static final long serialVersionUID = -499475729615879902L; 022 023 private ValueChecker chk; 024 private int id; 025 026 /** 027 * @param id the ID of the JTFCheckable. Every ID must be unique to make sure the {@link market.stdform.FSCheckable} 028 * works properly. 029 * @param chk the ValueChecker for this JTFCheckable. It must not be <code>null</code>. If no checking 030 * is desired, use {@link market.VCDummy} instead. 031 * @param size the size of the JTFCheckable. 032 */ 033 public JTFCheckable(int id, ValueChecker chk, int size) { 034 super(size); 035 this.id = id; 036 this.chk = chk; 037 } 038 039 /** 040 * @param id the ID of the JTFCheckable. Every ID must be unique to make sure the {@link market.stdform.FSCheckable} 041 * works properly. 042 * @param chk the ValueChecker for this JTFCheckable. 043 * @param str the content of the JTFCheckable right after creation. 044 * @param size the size of the JTFCheckable. 045 */ 046 public JTFCheckable(int id, ValueChecker chk, String str, int size) { 047 super(str, size); 048 this.id = id; 049 this.chk = chk; 050 } 051 052 /** 053 * Checks the text field's content for validity with the help of the assigned {@link ValueChecker}. 054 * 055 * @return <code>true</code> if the content is valid, otherwise <code>false</code>. 056 */ 057 public boolean hasValidValue() { 058 return chk.isValidValue(this.getText()); 059 } 060 061 /** 062 * Asks the {@link ValueChecker} for an error message according to the kind of error. 063 * @return the picked error message. 064 */ 065 public String getErrorMessage() { 066 return chk.getErrorString(); 067 } 068 069 /** 070 * @return the ID. 071 */ 072 public int getID() { 073 return id; 074 } 075 }