Java tutorial
/* Copyright (C) 2015, University of Kansas Center for Research * * Specify Software Project, specify@ku.edu, Biodiversity Institute, * 1345 Jayhawk Boulevard, Lawrence, Kansas, 66045, USA * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package edu.ku.brc.af.ui.forms.validation; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.event.DocumentEvent; import javax.swing.text.Document; import org.apache.commons.lang.StringUtils; import edu.ku.brc.af.prefs.AppPrefsCache; import edu.ku.brc.af.prefs.AppPrefsChangeEvent; import edu.ku.brc.af.prefs.AppPrefsChangeListener; import edu.ku.brc.ui.ColorWrapper; import edu.ku.brc.ui.DocumentAdaptor; import edu.ku.brc.ui.GetSetValueIFace; import edu.ku.brc.ui.UIHelper; /** * A JTextArea that implements UIValidatable for participating in validation * * @code_status Beta * * @author rods * */ @SuppressWarnings("serial") public class ValTextArea extends JTextArea implements UIValidatable, GetSetValueIFace, AppPrefsChangeListener { protected UIValidatable.ErrorType valState = UIValidatable.ErrorType.Valid; protected boolean isRequired = false; protected boolean isChanged = false; protected boolean isNew = false; protected Color bgColor = null; protected static ColorWrapper valTextColor = null; protected static ColorWrapper requiredFieldColor = null; protected String defaultValue = null; /** * Constructor */ public ValTextArea() { super(); init(); } /** * Constructor * @param text initial value */ public ValTextArea(String text) { super(text); init(); } public ValTextArea(int rows, int cols) { super(rows, cols); init(); } public ValTextArea(String text, int rows, int cols) { super(text, rows, cols); init(); } public ValTextArea(Document doc) { super(doc); init(); } public ValTextArea(Document doc, String text, int rows, int cols) { super(doc, text, rows, cols); init(); } /* (non-Javadoc) * @see edu.ku.brc.ui.db.JAutoCompTextField#init() */ public void init() { bgColor = getBackground(); if (valTextColor == null || requiredFieldColor == null) { valTextColor = AppPrefsCache.getColorWrapper("ui", "formatting", "valtextcolor"); requiredFieldColor = AppPrefsCache.getColorWrapper("ui", "formatting", "requiredfieldcolor"); } AppPrefsCache.addChangeListener("ui.formatting.requiredfieldcolor", this); initDocument(); addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { isNew = false; repaint(); } }); // Enable being able to TAB out of TextArea getInputMap().put(KeyStroke.getKeyStroke("TAB"), "none"); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_TAB) { if (event.isShiftDown()) { transferFocusBackward(); } else { transferFocus(); } } } }); } /* (non-Javadoc) * @see edu.ku.brc.ui.db.JAutoCompTextField#init() */ public void initDocument() { getDocument().addDocumentListener(new DocumentAdaptor() { @Override protected void changed(DocumentEvent e) { isChanged = true; } }); } /* (non-Javadoc) * @see javax.swing.text.JTextComponent#setDocument(javax.swing.text.Document) */ @Override public void setDocument(final Document doc) { super.setDocument(doc); initDocument(); } /* (non-Javadoc) * @see edu.ku.brc.af.ui.forms.validation.UIValidatable#isNotEmpty() */ public boolean isNotEmpty() { return getText().length() > 0; } /* (non-Javadoc) * @see java.awt.Component#paint(java.awt.Graphics) */ @Override public void paint(Graphics g) { super.paint(g); if (!isNew && valState == UIValidatable.ErrorType.Error && isEnabled()) { UIHelper.drawRoundedRect((Graphics2D) g, valTextColor.getColor(), getSize(), 1); } } /* (non-Javadoc) * @see java.awt.Component#setEnabled(boolean) */ @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); setBackground(isRequired && isEnabled() ? requiredFieldColor.getColor() : bgColor); } //-------------------------------------------------- //-- UIValidatable Interface //-------------------------------------------------- /* (non-Javadoc) * @see edu.kui.brc.ui.validation.UIValidatable#isInError() */ public boolean isInError() { return valState != UIValidatable.ErrorType.Valid; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#getState() */ public ErrorType getState() { return valState; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#setState(edu.ku.brc.ui.forms.validation.UIValidatable.ErrorType) */ public void setState(ErrorType state) { this.valState = state; } /* (non-Javadoc) * @see edu.kui.brc.ui.validation.UIValidatable#isRequired() */ public boolean isRequired() { return isRequired; } /* (non-Javadoc) * @see edu.kui.brc.ui.validation.UIValidatable#setRequired(boolean) */ public void setRequired(boolean isRequired) { setBackground(isRequired && isEnabled() ? requiredFieldColor.getColor() : bgColor); this.isRequired = isRequired; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#isChanged() */ public boolean isChanged() { return isChanged; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#setChanged(boolean) */ public void setChanged(boolean isChanged) { this.isChanged = isChanged; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#setAsNew(boolean) */ public void setAsNew(boolean isNew) { this.isNew = isRequired ? isNew : false; } /* (non-Javadoc) * @see java.awt.Component#validate() */ public UIValidatable.ErrorType validateState() { valState = isRequired && getText().length() == 0 ? UIValidatable.ErrorType.Incomplete : UIValidatable.ErrorType.Valid; return valState; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#reset() */ public void reset() { setText(StringUtils.isNotEmpty(defaultValue) ? defaultValue : ""); valState = isRequired ? UIValidatable.ErrorType.Incomplete : UIValidatable.ErrorType.Valid; repaint(); } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#getValidatableUIComp() */ public Component getValidatableUIComp() { return this; } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#cleanUp() */ public void cleanUp() { UIHelper.removeFocusListeners(this); UIHelper.removeKeyListeners(this); AppPrefsCache.removeChangeListener("ui.formatting.requiredfieldcolor", this); } /* (non-Javadoc) * @see edu.ku.brc.ui.forms.validation.UIValidatable#getReason() */ public String getReason() { return null; } //-------------------------------------------------------- // GetSetValueIFace //-------------------------------------------------------- /* (non-Javadoc) * @see edu.ku.brc.af.ui.GetSetValueIFace#setValue(java.lang.Object, java.lang.String) */ public void setValue(final Object value, final String defaultValue) { this.defaultValue = defaultValue; setText(value != null ? value.toString() : StringUtils.isNotEmpty(defaultValue) ? defaultValue : ""); setCaretPosition(0); } /* (non-Javadoc) * @see edu.ku.brc.af.ui.GetSetValueIFace#getValue() */ public Object getValue() { return getText(); } //------------------------------------------------- // AppPrefsChangeListener //------------------------------------------------- public void preferenceChange(AppPrefsChangeEvent evt) { if (evt.getKey().equals("requiredfieldcolor")) { setBackground(isRequired && isEnabled() ? requiredFieldColor.getColor() : bgColor); } } }