1 7 package javax.swing.plaf.basic; 8 9 import java.beans.*; 10 import java.awt.*; 11 import java.awt.event.KeyEvent ; 12 import java.awt.event.InputEvent ; 13 import javax.swing.*; 14 import javax.swing.event.DocumentEvent ; 15 import javax.swing.text.*; 16 import javax.swing.plaf.*; 17 18 35 public class BasicTextAreaUI extends BasicTextUI { 36 37 43 public static ComponentUI createUI(JComponent ta) { 44 return new BasicTextAreaUI (); 45 } 46 47 50 public BasicTextAreaUI() { 51 super(); 52 } 53 54 61 protected String getPropertyPrefix() { 62 return "TextArea"; 63 } 64 65 protected void installDefaults() { 66 super.installDefaults(); 67 } 69 70 80 protected void propertyChange(PropertyChangeEvent evt) { 81 if (evt.getPropertyName().equals("lineWrap") || 82 evt.getPropertyName().equals("wrapStyleWord") || 83 evt.getPropertyName().equals("tabSize")) { 84 modelChanged(); 86 } else if ("editable".equals(evt.getPropertyName())) { 87 updateFocusTraversalKeys(); 88 } 89 } 90 91 92 101 public Dimension getPreferredSize(JComponent c) { 102 return super.getPreferredSize(c); 103 } 105 106 115 public Dimension getMinimumSize(JComponent c) { 116 return super.getMinimumSize(c); 117 } 119 120 127 public View create(Element elem) { 128 Document doc = elem.getDocument(); 129 Object i18nFlag = doc.getProperty("i18n"); 130 if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { 131 return createI18N(elem); 133 } else { 134 JTextComponent c = getComponent(); 135 if (c instanceof JTextArea) { 136 JTextArea area = (JTextArea) c; 137 View v; 138 if (area.getLineWrap()) { 139 v = new WrappedPlainView(elem, area.getWrapStyleWord()); 140 } else { 141 v = new PlainView(elem); 142 } 143 return v; 144 } 145 } 146 return null; 147 } 148 149 View createI18N(Element elem) { 150 String kind = elem.getName(); 151 if (kind != null) { 152 if (kind.equals(AbstractDocument.ContentElementName)) { 153 return new PlainParagraph(elem); 154 } else if (kind.equals(AbstractDocument.ParagraphElementName)) { 155 return new BoxView(elem, View.Y_AXIS); 156 } 157 } 158 return null; 159 } 160 161 165 static class PlainParagraph extends ParagraphView { 166 167 PlainParagraph(Element elem) { 168 super(elem); 169 layoutPool = new LogicalView(elem); 170 layoutPool.setParent(this); 171 } 172 173 public void setParent(View parent) { 174 super.setParent(parent); 175 if (parent != null) { 176 setPropertiesFromAttributes(); 177 } 178 } 179 180 protected void setPropertiesFromAttributes() { 181 Component c = getContainer(); 182 if ((c != null) && (! c.getComponentOrientation().isLeftToRight())) { 183 setJustification(StyleConstants.ALIGN_RIGHT); 184 } else { 185 setJustification(StyleConstants.ALIGN_LEFT); 186 } 187 } 188 189 193 public int getFlowSpan(int index) { 194 Component c = getContainer(); 195 if (c instanceof JTextArea) { 196 JTextArea area = (JTextArea) c; 197 if (! area.getLineWrap()) { 198 return Integer.MAX_VALUE; 200 } 201 } 202 return super.getFlowSpan(index); 203 } 204 205 protected SizeRequirements calculateMinorAxisRequirements(int axis, 206 SizeRequirements r) { 207 SizeRequirements req = super.calculateMinorAxisRequirements(axis, r); 208 Component c = getContainer(); 209 if (c instanceof JTextArea) { 210 JTextArea area = (JTextArea) c; 211 if (! area.getLineWrap()) { 212 req.minimum = req.preferred; 214 } else { 215 req.minimum = 0; 216 req.preferred = getWidth(); 217 if (req.preferred == Integer.MAX_VALUE) { 218 req.preferred = 100; 221 } 222 } 223 } 224 return req; 225 } 226 227 235 public void setSize(float width, float height) { 236 if ((int) width != getWidth()) { 237 preferenceChanged(null, true, true); 238 } 239 super.setSize(width, height); 240 } 241 242 249 static class LogicalView extends CompositeView { 250 251 LogicalView(Element elem) { 252 super(elem); 253 } 254 255 protected int getViewIndexAtPosition(int pos) { 256 Element elem = getElement(); 257 if (elem.getElementCount() > 0) { 258 return elem.getElementIndex(pos); 259 } 260 return 0; 261 } 262 263 protected boolean updateChildren(DocumentEvent.ElementChange ec, 264 DocumentEvent e, ViewFactory f) { 265 return false; 266 } 267 268 protected void loadChildren(ViewFactory f) { 269 Element elem = getElement(); 270 if (elem.getElementCount() > 0) { 271 super.loadChildren(f); 272 } else { 273 View v = new GlyphView(elem); 274 append(v); 275 } 276 } 277 278 public float getPreferredSpan(int axis) { 279 if( getViewCount() != 1 ) 280 throw new Error ("One child view is assumed."); 281 282 View v = getView(0); 283 return v.getPreferredSpan(axis); 284 } 285 286 300 protected void forwardUpdateToView(View v, DocumentEvent e, 301 Shape a, ViewFactory f) { 302 v.setParent(this); 303 super.forwardUpdateToView(v, e, a, f); 304 } 305 306 309 public void paint(Graphics g, Shape allocation) { 310 } 311 312 protected boolean isBefore(int x, int y, Rectangle alloc) { 313 return false; 314 } 315 316 protected boolean isAfter(int x, int y, Rectangle alloc) { 317 return false; 318 } 319 320 protected View getViewAtPoint(int x, int y, Rectangle alloc) { 321 return null; 322 } 323 324 protected void childAllocation(int index, Rectangle a) { 325 } 326 } 327 } 328 329 } 330 | Popular Tags |