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 private ValueChecker chk; 019 private int id; 020 021 /** 022 * @param id the ID of the JTFCheckable. Every ID must be unique to make sure the {@link market.stdform.FSCheckable} 023 * works properly. 024 * @param chk the ValueChecker for this JTFCheckable. It must not be <code>null</code>. If no checking 025 * is desired, use {@link market.VCDummy} instead. 026 * @param size the size of the JTFCheckable. 027 */ 028 public JTFCheckable(int id, ValueChecker chk, int size) { 029 super(size); 030 this.id = id; 031 this.chk = chk; 032 } 033 034 /** 035 * @param id the ID of the JTFCheckable. Every ID must be unique to make sure the {@link market.stdform.FSCheckable} 036 * works properly. 037 * @param chk the ValueChecker for this JTFCheckable. 038 * @param str the content of the JTFCheckable right after creation. 039 * @param size the size of the JTFCheckable. 040 */ 041 public JTFCheckable(int id, ValueChecker chk, String str, int size) { 042 super(str, size); 043 this.id = id; 044 this.chk = chk; 045 } 046 047 /** 048 * Checks the text field's content for validity with the help of the assigned {@link ValueChecker}. 049 * 050 * @return <code>true</code> if the content is valid, otherwise <code>false</code>. 051 */ 052 public boolean hasValidValue() { 053 return chk.isValidValue(this.getText()); 054 } 055 056 /** 057 * Asks the {@link ValueChecker} for an error message according to the kind of error. 058 * @return the picked error message. 059 */ 060 public String getErrorMessage() { 061 return chk.getErrorString(); 062 } 063 064 /** 065 * @return the ID. 066 */ 067 public int getID() { 068 return id; 069 } 070 }