1 27 package org.htmlparser.lexerapplications.thumbelina; 28 29 import java.awt.Component ; 30 import java.awt.Dimension ; 31 import java.awt.Graphics ; 32 import java.awt.Image ; 33 import java.awt.Insets ; 34 import java.awt.Point ; 35 import java.awt.Rectangle ; 36 import java.awt.event.ComponentEvent ; 37 import java.awt.event.ComponentListener ; 38 import java.awt.event.HierarchyEvent ; 39 import java.awt.event.HierarchyListener ; 40 import java.awt.event.MouseEvent ; 41 import java.awt.event.MouseListener ; 42 import java.util.Enumeration ; 43 import java.util.HashSet ; 44 import javax.swing.JPanel ; 45 import javax.swing.JViewport ; 46 import javax.swing.Scrollable ; 47 import javax.swing.border.BevelBorder ; 48 49 53 public class PicturePanel 54 extends 55 JPanel 56 implements 57 MouseListener , 58 Scrollable , 59 ComponentListener , 60 HierarchyListener 61 { 62 65 protected static final int UNIT_INCREMENT = 10; 66 67 70 protected static final int BLOCK_INCREMENT = 100; 71 72 75 protected Thumbelina mThumbelina; 76 77 80 protected TileSet mMosaic; 81 82 87 protected Dimension mPreferredSize; 88 89 94 public PicturePanel (final Thumbelina thumbelina) 95 { 96 mThumbelina = thumbelina; 97 mMosaic = new TileSet (); 98 mPreferredSize = null; 99 setBorder (new BevelBorder (BevelBorder.LOWERED)); 100 addMouseListener (this); 101 addHierarchyListener (this); 102 } 103 104 107 public void reset () 108 { 109 mMosaic = new TileSet (); 110 repaint (); 111 } 112 113 119 public void bringToTop (final Picture picture) 120 { 121 picture.reset (); 122 mMosaic.bringToTop (picture); 123 repaint (picture.x, picture.y, picture.width, picture.height); 124 mThumbelina.mUrlText.setText (picture.getURL ().toExternalForm ()); 125 } 126 127 138 public Picture find (final String url) 139 { 140 Enumeration enumeration; 141 Picture picture; 142 Picture ret; 143 144 ret = null; 145 enumeration = mMosaic.getPictures (); 146 while ((null == ret) && enumeration.hasMoreElements ()) 147 { 148 picture = (Picture)enumeration.nextElement (); 149 if (url.equals (picture.getURL ().toExternalForm ())) 150 ret = picture; 151 } 152 153 return (ret); 154 } 155 156 161 protected void draw (final Picture picture, final boolean add) 162 { 163 Component parent; 164 boolean dolayout; 165 Dimension before; 166 Dimension after; 167 168 parent = getParent (); 169 dolayout = false; 170 synchronized (mMosaic) 171 { 172 if (parent instanceof JViewport ) 173 { 174 before = getPreferredSize (); 175 mMosaic.add (picture); 176 after = calculatePreferredSize (); 177 if (after.width > before.width) 178 dolayout = true; 179 else 180 after.width = before.width; 181 if (after.height > before.height) 182 dolayout = true; 183 else 184 after.height = before.height; 185 if (dolayout) 186 mPreferredSize = after; 187 } 188 else 189 mMosaic.add (picture); 190 } 191 if (dolayout) 192 revalidate (); 193 repaint (picture.x, picture.y, picture.width, picture.height); 194 if (add) 195 mThumbelina.addHistory (picture.getURL ().toExternalForm ()); 196 } 197 198 202 public void update (final Graphics graphics) 203 { 204 paint (graphics); 205 } 206 207 211 public void adjustClipForInsets (final Graphics graphics) 212 { 213 Dimension dim; 214 Insets insets; 215 Rectangle clip; 216 217 dim = getSize (); 218 insets = getInsets (); 219 clip = graphics.getClipBounds (); 220 if (clip.x < insets.left) 221 clip.x = insets.left; 222 if (clip.y < insets.top) 223 clip.y = insets.top; 224 if (clip.x + clip.width > dim.width - insets.right) 225 clip.width = dim.width - insets.right - clip.x; 226 if (clip.y + clip.height > dim.height - insets.bottom) 227 clip.height = dim.height - insets.bottom - clip.y; 228 graphics.setClip (clip.x, clip.y, clip.width, clip.height); 229 } 230 231 236 public void paint (final Graphics graphics) 237 { 238 Rectangle clip; 239 Enumeration enumeration; 240 HashSet set; Picture picture; 242 Image image; 243 Point origin; 244 int width; 245 int height; 246 247 adjustClipForInsets (graphics); 248 clip = graphics.getClipBounds (); 249 synchronized (mMosaic) 250 { 251 if (0 == mMosaic.getSize ()) 252 super.paint (graphics); 253 else 254 { 255 super.paint (graphics); 256 enumeration = mMosaic.getPictures (); 257 set = new HashSet (); 258 while (enumeration.hasMoreElements ()) 259 { 260 picture = (Picture)enumeration.nextElement (); 261 if ((null == clip) || (clip.intersects (picture))) 262 { 263 image = picture.getImage (); 264 if (!set.contains (image)) 265 { 266 origin = picture.getOrigin (); 267 width = image.getWidth (this); 268 height = image.getHeight (this); 269 graphics.drawImage (picture.getImage (), 270 origin.x, origin.y, 271 origin.x + width, origin.y + height, 272 0, 0, width, height, 273 this); 274 set.add (image); 275 } 276 } 277 } 278 } 279 } 280 } 281 282 283 287 public Dimension getPreferredSize () 288 { 289 if (null == mPreferredSize) 290 setPreferredSize (calculatePreferredSize ()); 291 else 292 if ((0 == mPreferredSize.width) || (0 == mPreferredSize.height)) 293 setPreferredSize (calculatePreferredSize ()); 294 return (mPreferredSize); 295 } 296 297 302 public void setPreferredSize (final Dimension dimension) 303 { 304 mPreferredSize = dimension; 305 } 306 307 315 protected Dimension calculatePreferredSize () 316 { 317 Enumeration enumeration; 318 int x; 319 int y; 320 Picture picture; 321 Component parent; 322 Insets insets; 323 Dimension ret; 324 325 enumeration = mMosaic.getPictures (); 326 x = 0; 327 y = 0; 328 picture = null; 329 while (enumeration.hasMoreElements ()) 330 { 331 picture = (Picture)enumeration.nextElement (); 332 if (picture.x + picture.width > x) 333 x = picture.x + picture.width; 334 if (picture.y + picture.height > y) 335 y = picture.y + picture.height; 336 } 337 parent = getParent (); 338 if (parent instanceof JViewport ) 339 { 340 ret = parent.getSize (); 341 insets = ((JViewport )parent).getInsets (); 342 ret.width -= insets.left + insets.right; 343 ret.height -= insets.top + insets.bottom; 344 if ((0 != ret.width) || (0 != ret.height)) 345 ret.width -= 2; if (ret.width < x) 347 ret.width = x; 348 if (ret.height < y) 349 ret.height = y; 350 } 351 else 352 { 353 insets = getInsets (); 354 x += insets.left + insets.right; 355 y += insets.top + insets.bottom; 356 ret = new Dimension (x, y); 357 } 358 359 return (ret); 360 } 361 362 366 372 public void mouseClicked (final MouseEvent event) 373 { 374 } 375 376 381 public void mouseReleased (final MouseEvent event) 382 { 383 } 384 385 390 public void mouseEntered (final MouseEvent event) 391 { 392 } 393 394 399 public void mouseExited (final MouseEvent event) 400 { 401 } 402 403 407 public void mousePressed (final MouseEvent event) 408 { 409 Picture picture; 410 411 if (!event.isMetaDown ()) 412 { 413 picture = mMosaic.pictureAt (event.getX (), event.getY ()); 414 if (null != picture) 415 bringToTop (picture); 416 } 417 } 418 419 423 435 public Dimension getPreferredScrollableViewportSize () 436 { 437 return (getPreferredSize ()); 438 } 439 440 441 459 public int getScrollableUnitIncrement ( 460 final Rectangle visibleRect, 461 final int orientation, 462 final int direction) 463 { 464 return (UNIT_INCREMENT); 465 } 466 467 468 484 public int getScrollableBlockIncrement ( 485 final Rectangle visibleRect, 486 final int orientation, 487 final int direction) 488 { 489 return (BLOCK_INCREMENT); 490 } 491 492 493 509 public boolean getScrollableTracksViewportWidth () 510 { 511 return (false); 512 } 513 514 527 public boolean getScrollableTracksViewportHeight () 528 { 529 return (false); 530 } 531 532 536 541 public void componentResized (final ComponentEvent event) 542 { 543 setPreferredSize (null); 544 } 545 546 551 public void componentMoved (final ComponentEvent event) 552 { 553 } 554 555 560 public void componentShown (final ComponentEvent event) 561 { 562 } 563 564 569 public void componentHidden (final ComponentEvent event) 570 { 571 } 572 573 577 584 public void hierarchyChanged (final HierarchyEvent event) 585 { 586 if (0 != (event.getChangeFlags () & HierarchyEvent.PARENT_CHANGED)) 587 { 588 Component dad = event.getChanged (); 589 Component parent = getParent (); 590 if ((null != parent) && (parent.getParent () == dad)) 591 dad.addComponentListener (this); 592 } 593 } 594 } 595 596 609 | Popular Tags |