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 30 import javax.swing.*; 31 import javax.swing.text.JTextComponent ; 32 33 import org.lobobrowser.html.domimpl.*; 34 import org.lobobrowser.util.gui.WrapperLayout; 35 36 class InputTextAreaControl extends BaseInputControl { 37 private final JTextComponent widget; 38 39 public InputTextAreaControl(HTMLBaseInputElement modelNode) { 40 super(modelNode); 41 this.setLayout(WrapperLayout.getInstance()); 42 JTextComponent widget = this.createTextField(); 43 this.widget = widget; 44 this.add(new JScrollPane(widget)); 45 } 46 47 public void reset(int availWidth, int availHeight) { 48 super.reset(availWidth, availHeight); 49 ElementImpl element = this.controlElement; 50 String colsStr = element.getAttribute("cols"); 51 if(colsStr != null) { 52 try { 53 this.setCols(Integer.parseInt(colsStr)); 54 } catch(NumberFormatException nfe) { 55 } 57 } 58 String rowsStr = element.getAttribute("rows"); 59 if(rowsStr != null) { 60 try { 61 this.setRows(Integer.parseInt(rowsStr)); 62 } catch(NumberFormatException nfe) { 63 } 65 } 66 String value = element.getTextContent(); 67 this.widget.setText(value); 68 } 69 70 protected JTextComponent createTextField() { 71 return new JTextArea(); 72 } 73 74 77 public int getCols() { 78 return this.cols; 79 } 80 81 84 public int getRows() { 85 return this.rows; 86 } 87 88 private int cols = -1; 89 private int rows = -1; 90 91 94 public void setCols(int cols) { 95 this.cols = cols; 96 this.invalidate(); 97 } 98 99 102 public void setRows(int rows) { 103 this.rows = rows; 104 this.invalidate(); 105 } 106 107 public java.awt.Dimension getPreferredSize() { 108 int pw; 109 int cols = this.cols; 110 if(cols == -1) { 111 pw = 100; 112 } 113 else { 114 Font f = this.widget.getFont(); 115 FontMetrics fm = this.widget.getFontMetrics(f); 116 Insets insets = this.widget.getInsets(); 117 pw = insets.left + insets.right + fm.charWidth('*') * cols; 118 } 119 int ph; 120 int rows = this.rows; 121 if(rows == -1) { 122 ph = 100; 123 } 124 else { 125 Font f = this.widget.getFont(); 126 FontMetrics fm = this.widget.getFontMetrics(f); 127 Insets insets = this.widget.getInsets(); 128 ph = insets.top + insets.bottom + fm.getHeight() * rows; 129 } 130 return new java.awt.Dimension (pw, ph); 131 132 } 133 134 137 public boolean getReadOnly() { 138 return !this.widget.isEditable(); 139 } 140 141 144 public String getValue() { 145 return this.widget.getText(); 146 } 147 148 151 public void setReadOnly(boolean readOnly) { 152 this.widget.setEditable(readOnly); 153 } 154 155 158 public void setValue(String value) { 159 this.widget.setText(value); 160 } 161 162 public void resetInput() { 163 this.widget.setText(""); 164 } 165 } 166 | Popular Tags |