1 7 package javax.swing.text; 8 9 import java.awt.*; 10 import javax.swing.SwingUtilities ; 11 import javax.swing.event.*; 12 13 50 public class ComponentView extends View { 51 52 57 public ComponentView(Element elem) { 58 super(elem); 59 } 60 61 69 protected Component createComponent() { 70 AttributeSet attr = getElement().getAttributes(); 71 Component comp = StyleConstants.getComponent(attr); 72 return comp; 73 } 74 75 78 public final Component getComponent() { 79 return createdC; 80 } 81 82 84 93 public void paint(Graphics g, Shape a) { 94 if (c != null) { 95 Rectangle alloc = (a instanceof Rectangle) ? 96 (Rectangle) a : a.getBounds(); 97 c.setBounds(alloc.x, alloc.y, alloc.width, alloc.height); 98 } 99 } 100 101 114 public float getPreferredSpan(int axis) { 115 if ((axis != X_AXIS) && (axis != Y_AXIS)) { 116 throw new IllegalArgumentException ("Invalid axis: " + axis); 117 } 118 if (c != null) { 119 Dimension size = c.getPreferredSize(); 120 if (axis == View.X_AXIS) { 121 return size.width; 122 } else { 123 return size.height; 124 } 125 } 126 return 0; 127 } 128 129 142 public float getMinimumSpan(int axis) { 143 if ((axis != X_AXIS) && (axis != Y_AXIS)) { 144 throw new IllegalArgumentException ("Invalid axis: " + axis); 145 } 146 if (c != null) { 147 Dimension size = c.getMinimumSize(); 148 if (axis == View.X_AXIS) { 149 return size.width; 150 } else { 151 return size.height; 152 } 153 } 154 return 0; 155 } 156 157 170 public float getMaximumSpan(int axis) { 171 if ((axis != X_AXIS) && (axis != Y_AXIS)) { 172 throw new IllegalArgumentException ("Invalid axis: " + axis); 173 } 174 if (c != null) { 175 Dimension size = c.getMaximumSize(); 176 if (axis == View.X_AXIS) { 177 return size.width; 178 } else { 179 return size.height; 180 } 181 } 182 return 0; 183 } 184 185 197 public float getAlignment(int axis) { 198 if (c != null) { 199 switch (axis) { 200 case View.X_AXIS: 201 return c.getAlignmentX(); 202 case View.Y_AXIS: 203 return c.getAlignmentY(); 204 } 205 } 206 return super.getAlignment(axis); 207 } 208 209 231 public void setParent(View p) { 232 super.setParent(p); 233 if (SwingUtilities.isEventDispatchThread()) { 234 setComponentParent(); 235 } else { 236 Runnable callSetComponentParent = new Runnable () { 237 public void run() { 238 Document doc = getDocument(); 239 try { 240 if (doc instanceof AbstractDocument ) { 241 ((AbstractDocument )doc).readLock(); 242 } 243 setComponentParent(); 244 Container host = getContainer(); 245 if (host != null) { 246 preferenceChanged(null, true, true); 247 host.repaint(); 248 } 249 } finally { 250 if (doc instanceof AbstractDocument ) { 251 ((AbstractDocument )doc).readUnlock(); 252 } 253 } 254 } 255 }; 256 SwingUtilities.invokeLater(callSetComponentParent); 257 } 258 } 259 260 264 void setComponentParent() { 265 View p = getParent(); 266 if (p != null) { 267 Container parent = getContainer(); 268 if (parent != null) { 269 if (c == null) { 270 Component comp = createComponent(); 272 if (comp != null) { 273 createdC = comp; 274 c = new Invalidator(comp); 275 } 276 } 277 if (c != null) { 278 if (c.getParent() == null) { 279 parent.add(c, this); 282 } 283 } 284 } 285 } else { 286 if (c != null) { 287 Container parent = c.getParent(); 288 if (parent != null) { 289 parent.remove(c); 291 } 292 } 293 } 294 } 295 296 307 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 308 int p0 = getStartOffset(); 309 int p1 = getEndOffset(); 310 if ((pos >= p0) && (pos <= p1)) { 311 Rectangle r = a.getBounds(); 312 if (pos == p1) { 313 r.x += r.width; 314 } 315 r.width = 0; 316 return r; 317 } 318 throw new BadLocationException (pos + " not in range " + p0 + "," + p1, pos); 319 } 320 321 332 public int viewToModel(float x, float y, Shape a, Position.Bias [] bias) { 333 Rectangle alloc = (Rectangle) a; 334 if (x < alloc.x + (alloc.width / 2)) { 335 bias[0] = Position.Bias.Forward; 336 return getStartOffset(); 337 } 338 bias[0] = Position.Bias.Backward; 339 return getEndOffset(); 340 } 341 342 344 private Component createdC; 345 private Component c; 346 347 355 class Invalidator extends Container { 356 357 363 Invalidator(Component child) { 364 setLayout(null); 365 add(child); 366 cacheChildSizes(); 367 } 368 369 375 public void invalidate() { 376 super.invalidate(); 377 if (getParent() != null) { 378 preferenceChanged(null, true, true); 379 } 380 } 381 382 public void doLayout() { 383 cacheChildSizes(); 384 } 385 386 public void setBounds(int x, int y, int w, int h) { 387 super.setBounds(x, y, w, h); 388 if (getComponentCount() > 0) { 389 getComponent(0).setSize(w, h); 390 } 391 cacheChildSizes(); 392 } 393 394 public void validateIfNecessary() { 395 if (!isValid()) { 396 validate(); 397 } 398 } 399 400 private void cacheChildSizes() { 401 if (getComponentCount() > 0) { 402 Component child = getComponent(0); 403 min = child.getMinimumSize(); 404 pref = child.getPreferredSize(); 405 max = child.getMaximumSize(); 406 yalign = child.getAlignmentY(); 407 xalign = child.getAlignmentX(); 408 } else { 409 min = pref = max = new Dimension(0, 0); 410 } 411 } 412 413 421 public void setVisible(boolean b) { 422 super.setVisible(b); 423 if (getComponentCount() > 0) { 424 getComponent(0).setVisible(b); 425 } 426 } 427 428 433 public boolean isShowing() { 434 return true; 435 } 436 437 public Dimension getMinimumSize() { 438 validateIfNecessary(); 439 return min; 440 } 441 442 public Dimension getPreferredSize() { 443 validateIfNecessary(); 444 return pref; 445 } 446 447 public Dimension getMaximumSize() { 448 validateIfNecessary(); 449 return max; 450 } 451 452 public float getAlignmentX() { 453 validateIfNecessary(); 454 return xalign; 455 } 456 457 public float getAlignmentY() { 458 validateIfNecessary(); 459 return yalign; 460 } 461 462 public java.util.Set getFocusTraversalKeys(int id) { 463 return KeyboardFocusManager.getCurrentKeyboardFocusManager(). 464 getDefaultFocusTraversalKeys(id); 465 } 466 467 Dimension min; 468 Dimension pref; 469 Dimension max; 470 float yalign; 471 float xalign; 472 473 } 474 475 } 476 477 | Popular Tags |