1 21 24 package org.lobobrowser.html.renderer; 25 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.awt.Insets ; 29 import javax.swing.text.AttributeSet ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.JTextComponent ; 32 33 import org.lobobrowser.html.domimpl.*; 34 import org.lobobrowser.util.gui.WrapperLayout; 35 36 abstract class BaseInputTextControl extends BaseInputControl { 37 private static final float DEFAULT_FONT_SIZE = 14.0f; 38 protected final JTextComponent widget; 39 40 public BaseInputTextControl(final HTMLBaseInputElement modelNode) { 41 super(modelNode); 42 this.setLayout(WrapperLayout.getInstance()); 43 JTextComponent widget = this.createTextField(); 44 Font font = widget.getFont(); 45 widget.setFont(font.deriveFont(DEFAULT_FONT_SIZE)); 46 widget.setDocument(new LimitedDocument()); 47 this.widget = widget; 48 this.add(widget); 49 } 50 51 public void reset(int availWidth, int availHeight) { 52 super.reset(availWidth, availHeight); 53 ElementImpl element = this.controlElement; 54 String value = element.getAttribute("value"); 55 widget.setText(value); 56 String maxLengthText = this.controlElement.getAttribute("maxlength"); 57 if(maxLengthText != null) { 58 try { 59 this.maxLength = Integer.parseInt(maxLengthText); 60 } catch(NumberFormatException nfe) { 61 } 63 } 64 65 } 66 67 protected abstract JTextComponent createTextField(); 68 69 private int maxLength = -1; 70 71 74 public int getMaxLength() { 75 return this.maxLength; 76 } 77 78 81 public boolean getReadOnly() { 82 return !this.widget.isEditable(); 83 } 84 85 88 public String getValue() { 89 return this.widget.getText(); 90 } 91 92 95 public void select() { 96 this.widget.selectAll(); 97 } 98 99 102 public void setDisabled(boolean disabled) { 103 super.setDisabled(disabled); 104 this.widget.setEnabled(!disabled); 105 } 106 107 110 public void setMaxLength(int maxLength) { 111 this.maxLength = maxLength; 112 } 113 114 117 public void setReadOnly(boolean readOnly) { 118 this.widget.setEditable(!readOnly); 119 } 120 121 124 public void setValue(String value) { 125 this.widget.setText(value); 126 } 127 128 public java.awt.Dimension getPreferredSize() { 129 int size = this.size; 130 JTextComponent widget = this.widget; 131 FontMetrics fm = widget.getFontMetrics(widget.getFont()); 132 Insets insets = widget.getInsets(); 133 int pw, ph; 134 if(size == -1) { 135 pw = 100; 136 } 137 else { 138 pw = insets.left + insets.right + fm.charWidth('0') * size; 139 } 140 ph = fm.getHeight() + insets.top + insets.bottom; 141 return new java.awt.Dimension (pw, ph); 142 } 143 144 public void resetInput() { 145 this.widget.setText(""); 146 } 147 148 151 private class LimitedDocument extends javax.swing.text.PlainDocument { 152 155 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 156 int max = BaseInputTextControl.this.maxLength; 157 if(max != -1) { 158 int docLength = this.getLength(); 159 if(docLength >= max) { 160 return; 161 } 162 int strLen = str.length(); 163 if(docLength + strLen > max) { 164 String shorterStr = str.substring(0, max - docLength); 165 super.insertString(offs, shorterStr, a); 166 } 167 else { 168 super.insertString(offs, str, a); 169 } 170 } 171 else { 172 super.insertString(offs, str, a); 173 } 174 } 175 } 176 } 177 | Popular Tags |