1 15 16 18 36 package ixenon.free.swing; 37 38 import java.awt.*; 39 import java.awt.event.*; 40 import java.util.*; 41 import java.io.*; import java.net.*; 44 import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.text.*; 49 50 51 60 public class HTMLViewerPane extends JPanel 61 implements ActionListener, HyperlinkListener 62 { 63 public final static boolean debug=false; 64 65 66 public final static String DEFAULT_BACK_TEXT="Back"; 67 68 public final static String DEFAULT_BACK_TOOLTIP_TEXT="Go back"; 69 70 public final static String DEFAULT_NEXT_TEXT="Next"; 71 72 public final static String DEFAULT_NEXT_TOOLTIP_TEXT="Go next"; 73 74 public final static String DEFAULT_RELOAD_TEXT="Reload"; 75 76 public final static String DEFAULT_RELOAD_TOOLTIP_TEXT="Reload"; 77 78 79 protected JComboBox comboBox; 80 81 protected JButton bt_back; 82 83 protected JButton bt_next; 84 85 protected JButton bt_reload; 86 87 88 protected JEditorPane editorPane; 89 90 91 protected JLabel lb_status; 92 93 94 protected boolean browsable = true; 95 96 97 protected int urlStackIndex=-1; 98 99 100 protected Stack urlStack = new Stack(); 101 102 private final static byte GO_BACK_IMAGE_DATA[] = { 104 (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0, 105 (byte)0xa1, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x92, (byte)0xc4, (byte)0xdb, (byte)0x4f, 106 (byte)0x86, (byte)0xdd, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x21, (byte)0xfe, (byte)0xe, (byte)0x4d, (byte)0x61, 107 (byte)0x64, (byte)0x65, (byte)0x20, (byte)0x77, (byte)0x69, (byte)0x74, (byte)0x68, (byte)0x20, (byte)0x47, (byte)0x49, 108 (byte)0x4d, (byte)0x50, (byte)0x0, (byte)0x21, (byte)0xf9, (byte)0x4, (byte)0x1, (byte)0xa, (byte)0x0, (byte)0x3, 109 (byte)0x0, (byte)0x2c, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0, 110 (byte)0x0, (byte)0x2, (byte)0x50, (byte)0x9c, (byte)0x8f, (byte)0x6, (byte)0xc9, (byte)0x8d, (byte)0x10, (byte)0x96, 111 (byte)0x9b, (byte)0x2a, (byte)0x44, (byte)0x3a, (byte)0xa1, (byte)0xbd, (byte)0x38, (byte)0xe9, (byte)0x2d, (byte)0x75, 112 (byte)0xd5, (byte)0x6, (byte)0x8a, (byte)0xc3, (byte)0x47, (byte)0x72, (byte)0x18, (byte)0x9a, (byte)0x46, (byte)0xc0, 113 (byte)0x1b, (byte)0x3e, (byte)0xed, (byte)0x5c, (byte)0xca, (byte)0xf4, (byte)0x1d, (byte)0xb2, (byte)0x77, (byte)0x2a, 114 (byte)0x1, (byte)0xc2, (byte)0xf, (byte)0xc, (byte)0xa, (byte)0x87, (byte)0xbd, (byte)0xa1, (byte)0x91, (byte)0xf8, 115 (byte)0x38, (byte)0x2a, (byte)0x5, (byte)0xb1, (byte)0xd3, (byte)0x12, (byte)0xc9, (byte)0xf0, (byte)0x3d, (byte)0x81, 116 (byte)0xcd, (byte)0x83, (byte)0x94, (byte)0x8, (byte)0x7b, (byte)0x51, (byte)0xae, (byte)0x54, (byte)0x93, (byte)0x55, 117 (byte)0x58, (byte)0xed, (byte)0x5c, (byte)0xc3, (byte)0x22, (byte)0x29, (byte)0xb9, (byte)0x7c, (byte)0x36, (byte)0xa5, 118 (byte)0xbd, (byte)0x94, (byte)0x2, (byte)0x0, (byte)0x3b, 119 }; 120 121 private final byte GO_FORWARD_IMAGE_DATA[] = { 123 (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0, 124 (byte)0xa1, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x92, (byte)0xc4, (byte)0xdb, (byte)0x4f, 125 (byte)0x86, (byte)0xdd, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x21, (byte)0xfe, (byte)0xe, (byte)0x4d, (byte)0x61, 126 (byte)0x64, (byte)0x65, (byte)0x20, (byte)0x77, (byte)0x69, (byte)0x74, (byte)0x68, (byte)0x20, (byte)0x47, (byte)0x49, 127 (byte)0x4d, (byte)0x50, (byte)0x0, (byte)0x21, (byte)0xf9, (byte)0x4, (byte)0x1, (byte)0xa, (byte)0x0, (byte)0x3, 128 (byte)0x0, (byte)0x2c, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0, 129 (byte)0x0, (byte)0x2, (byte)0x51, (byte)0x9c, (byte)0x8f, (byte)0x3, (byte)0xc9, (byte)0x8d, (byte)0x10, (byte)0x96, 130 (byte)0x9b, (byte)0x7, (byte)0x46, (byte)0x3a, (byte)0x6d, (byte)0xb8, (byte)0x98, (byte)0xe9, (byte)0x2d, (byte)0x75, 131 (byte)0xc6, (byte)0xb7, (byte)0x71, (byte)0x22, (byte)0x59, (byte)0x86, (byte)0x1e, (byte)0xc0, (byte)0xa2, (byte)0xa5, 132 (byte)0xf9, (byte)0xbc, (byte)0xf2, (byte)0xc, (byte)0x2b, (byte)0xf4, (byte)0x9d, (byte)0xc6, (byte)0x38, (byte)0x2e, 133 (byte)0x1, (byte)0xc2, (byte)0xf, (byte)0xc, (byte)0xa, (byte)0x87, (byte)0xbd, (byte)0xa1, (byte)0x91, (byte)0x58, 134 (byte)0x39, (byte)0x2a, (byte)0x5, (byte)0x2a, (byte)0xc5, (byte)0x12, (byte)0x99, (byte)0xf0, (byte)0x3d, (byte)0x81, 135 (byte)0xcd, (byte)0xa, (byte)0xe0, (byte)0x7a, (byte)0x3d, (byte)0x56, (byte)0x33, (byte)0x50, (byte)0x91, (byte)0x33, 136 (byte)0xb8, (byte)0xc5, (byte)0x48, (byte)0x99, (byte)0xde, (byte)0xe8, (byte)0x2f, (byte)0x7c, (byte)0x42, (byte)0x9f, 137 (byte)0xca, (byte)0xec, (byte)0x44, (byte)0x1, (byte)0x0, (byte)0x3b, 138 }; 139 140 private final static byte RELOAD_IMAGE_DATA[] = { 142 (byte)0x47, (byte)0x49, (byte)0x46, (byte)0x38, (byte)0x39, (byte)0x61, (byte)0x18, (byte)0x0, (byte)0x18, (byte)0x0, 143 (byte)0xc2, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x92, (byte)0xc4, (byte)0xdb, (byte)0x4f, 144 (byte)0x86, (byte)0xdd, (byte)0x34, (byte)0x59, (byte)0x91, (byte)0x6f, (byte)0x95, (byte)0xa5, (byte)0xff, (byte)0xff, 145 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x21, (byte)0xfe, (byte)0xe, 146 (byte)0x4d, (byte)0x61, (byte)0x64, (byte)0x65, (byte)0x20, (byte)0x77, (byte)0x69, (byte)0x74, (byte)0x68, (byte)0x20, 147 (byte)0x47, (byte)0x49, (byte)0x4d, (byte)0x50, (byte)0x0, (byte)0x21, (byte)0xf9, (byte)0x4, (byte)0x1, (byte)0xa, 148 (byte)0x0, (byte)0x7, (byte)0x0, (byte)0x2c, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x18, (byte)0x0, 149 (byte)0x18, (byte)0x0, (byte)0x0, (byte)0x3, (byte)0x77, (byte)0x78, (byte)0xba, (byte)0xac, (byte)0xf0, (byte)0xb0, 150 (byte)0xc9, (byte)0xf6, (byte)0x82, (byte)0xbd, (byte)0x6f, (byte)0x52, (byte)0x70, (byte)0xaf, (byte)0xf8, (byte)0x2, 151 (byte)0xa0, (byte)0x1d, (byte)0x55, (byte)0x7, (byte)0x9e, (byte)0xe1, (byte)0xc4, (byte)0x99, (byte)0x28, (byte)0x98, 152 (byte)0x51, (byte)0x2c, (byte)0xa, (byte)0x1, (byte)0x83, (byte)0x28, (byte)0x71, (byte)0x2d, (byte)0x44, (byte)0xec, 153 (byte)0x43, (byte)0xad, (byte)0xca, (byte)0x36, (byte)0xc0, (byte)0x8e, (byte)0xd0, (byte)0xb3, (byte)0x31, (byte)0x44, 154 (byte)0x80, (byte)0xcf, (byte)0xcb, (byte)0xc1, (byte)0x2b, (byte)0xc2, (byte)0x90, (byte)0x46, (byte)0x66, (byte)0xd3, 155 (byte)0x77, (byte)0xc, (byte)0x44, (byte)0x1d, (byte)0xc2, (byte)0xe9, (byte)0x95, (byte)0x64, (byte)0x31, (byte)0x3e, 156 (byte)0x86, (byte)0x3d, (byte)0x27, (byte)0xac, (byte)0x4b, (byte)0xca, (byte)0x6a, (byte)0xb7, (byte)0x5c, (byte)0xcc, 157 (byte)0x90, (byte)0x18, (byte)0x46, (byte)0x3b, (byte)0x3a, (byte)0x81, (byte)0xa9, (byte)0x58, (byte)0xb3, (byte)0xea, 158 (byte)0x84, (byte)0x6b, (byte)0xee, (byte)0x45, (byte)0xdd, (byte)0xf2, (byte)0xc1, (byte)0x8f, (byte)0xaa, (byte)0x1e, 159 (byte)0x2e, (byte)0x79, (byte)0x7a, (byte)0x81, (byte)0x39, (byte)0x83, (byte)0x2b, (byte)0x2d, (byte)0x32, (byte)0x23, 160 (byte)0x38, (byte)0x86, (byte)0x11, (byte)0x8b, (byte)0x27, (byte)0x33, (byte)0x7f, (byte)0x1b, (byte)0x8e, (byte)0x92, 161 (byte)0x95, (byte)0x96, (byte)0xc, (byte)0x9, (byte)0x0, (byte)0x3b, 162 }; 163 164 167 public HTMLViewerPane() 168 { 169 initialise( DEFAULT_BACK_TEXT, DEFAULT_NEXT_TEXT, DEFAULT_RELOAD_TEXT, 170 DEFAULT_BACK_TOOLTIP_TEXT, DEFAULT_NEXT_TOOLTIP_TEXT, 171 DEFAULT_RELOAD_TOOLTIP_TEXT ); 172 } 173 174 178 public HTMLViewerPane( String url ) 179 throws IOException 180 { 181 this( url, DEFAULT_BACK_TEXT, DEFAULT_NEXT_TEXT, DEFAULT_RELOAD_TEXT ); 182 } 183 184 190 public HTMLViewerPane( String url, 191 String backText, 192 String nextText, 193 String reloadText) 194 throws IOException 195 { 196 this( url, backText, nextText, reloadText, 197 DEFAULT_BACK_TOOLTIP_TEXT, DEFAULT_NEXT_TOOLTIP_TEXT, 198 DEFAULT_RELOAD_TOOLTIP_TEXT ); 199 } 200 201 209 public HTMLViewerPane( String url, 210 String backText, 211 String nextText, 212 String reloadText, 213 String backToolTip, 214 String nextToolTip, 215 String reloadToolTip ) 216 throws IOException 217 { 218 initialise( backText, nextText, reloadText, 219 backToolTip, nextToolTip, reloadToolTip ); 220 setPage(url); 221 comboBox.addActionListener(this); 224 } 225 226 227 protected void initialise( String backText, 228 String nextText, 229 String reloadText, 230 String backToolTip, 231 String nextToolTip, 232 String reloadToolTip ) 233 { 234 setLayout( new BorderLayout( 2,2 )); 235 236 JPanel np = new JPanel(); 240 np.setLayout( new BorderLayout() ); 241 242 Font labelFont = new Font( "Courier", Font.BOLD, 10 ); 243 Font entryFont = new Font( "Courier", Font.PLAIN, 10 ); 244 245 JPanel anyPanel = new JPanel(); 246 anyPanel.setLayout( new FlowLayout( FlowLayout.LEFT )); 247 bt_back = new JButton( backText ); 248 bt_back.setToolTipText( backToolTip ); 249 ImageIcon img1 = 250 new ImageIcon( 251 Toolkit.getDefaultToolkit().createImage(GO_BACK_IMAGE_DATA) ); 252 bt_back.setIcon( img1 ); 253 bt_back.setHorizontalTextPosition( SwingConstants.CENTER ); 254 bt_back.setVerticalTextPosition( SwingConstants.BOTTOM ); 255 bt_back.setFont( labelFont ); 256 bt_back.addActionListener(this); 257 anyPanel.add(bt_back); 258 259 bt_next = new JButton( nextText ); 260 bt_next.setToolTipText( nextToolTip ); 261 ImageIcon img2 = 262 new ImageIcon( 263 Toolkit.getDefaultToolkit().createImage(GO_FORWARD_IMAGE_DATA) ); 264 bt_next.setIcon( img2 ); 265 bt_next.setHorizontalTextPosition( SwingConstants.CENTER ); 266 bt_next.setVerticalTextPosition( SwingConstants.BOTTOM ); 267 bt_next.setFont( labelFont ); 268 bt_next.addActionListener(this); 269 anyPanel.add(bt_next); 270 271 bt_reload = new JButton( reloadText ); 272 bt_reload.setToolTipText( reloadToolTip ); 273 ImageIcon img3 = 274 new ImageIcon( 275 Toolkit.getDefaultToolkit().createImage(RELOAD_IMAGE_DATA) ); 276 bt_reload.setIcon( img3 ); 277 bt_reload.setHorizontalTextPosition( SwingConstants.CENTER ); 278 bt_reload.setVerticalTextPosition( SwingConstants.BOTTOM ); 279 bt_reload.setFont( labelFont ); 280 bt_reload.addActionListener(this); 281 anyPanel.add(bt_reload); 282 np.add( anyPanel, BorderLayout.CENTER ); 283 284 comboBox = new JComboBox(); 285 comboBox.setFont( entryFont ); 286 np.add( comboBox, BorderLayout.SOUTH ); 290 291 add( np, BorderLayout.NORTH ); 292 293 JPanel sp = new JPanel(); 297 sp.setLayout( new BorderLayout() ); 298 299 301 lb_status = new JLabel( "-*- Status -*-" ); 302 lb_status.setFont( labelFont ); 303 sp.add( lb_status, BorderLayout.CENTER ); 304 305 add( sp, BorderLayout.SOUTH ); 306 307 editorPane = new JEditorPane(); 311 editorPane.setFont( new Font( "Courier", Font.PLAIN, 12 ) ); 312 editorPane.setEditable(false); 313 editorPane.addHyperlinkListener(this); 314 315 JScrollPane ta_scrollable_text = new JScrollPane( editorPane ) { 316 public Dimension getPreferredSize() { 317 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 318 Dimension bestSize; 319 if (screen.width > 950) 320 bestSize = new Dimension( 735, 765 ); 321 else 322 bestSize = new Dimension( 500, 415 ); 323 Dimension size = getSize(); 324 if (size.width < bestSize.width ) 325 size.width = bestSize.width; 326 if (size.height < bestSize.height ) 327 size.height = bestSize.height; 328 return (size); 329 } 330 }; 331 add("Center", ta_scrollable_text ); 332 } 333 334 335 protected void updateComponent() 336 { 337 if (browsable) { 338 bt_back.setEnabled( urlStackIndex > 0 ); 339 bt_next.setEnabled( urlStackIndex < urlStack.size()-1 ); 340 } 341 else { 342 bt_back.setEnabled(false); 343 bt_next.setEnabled(false); 344 } 345 346 repaint(); 347 } 348 349 353 public void setVisibleNavigation( boolean visible ) 354 { 355 bt_back.setVisible(visible); 356 bt_next.setVisible(visible); 357 bt_reload.setVisible(visible); 358 comboBox.setVisible(visible); 359 editorPane.getParent().invalidate(); 360 editorPane.getParent().repaint(); 361 } 362 363 364 public void goBack() 365 { 366 if ( urlStackIndex > 0 ) { 367 int newIndex = urlStackIndex - 1; 368 String url = (String )urlStack.elementAt(newIndex); 369 try { 370 editorPane.setPage( url ); 371 urlStackIndex = newIndex; 372 lb_status.setText( url ); 374 updateComponent(); 375 } 376 catch (IOException e) { 377 System.err.println( getClass().getName()+" going forward error url: " +url ); 378 } 379 } 380 } 381 382 383 public void goNext() 384 { 385 if ( urlStackIndex < urlStack.size()-1 ) { 386 int newIndex = urlStackIndex + 1; 387 String url = (String )urlStack.elementAt(newIndex); 388 try { 389 editorPane.setPage( url ); 390 urlStackIndex = newIndex; 391 lb_status.setText( url ); 393 updateComponent(); 394 } 395 catch (IOException e) { 396 System.err.println( getClass().getName()+" going forward error url: " +url ); 397 } 398 } 399 } 400 401 protected void pushNewURLonStack( String url ) 402 { 403 if (debug) { 404 System.out.println( "pushNewURLonStack(`"+url+"')" ); 405 System.out.println( "*V* urlStackIndex = "+urlStackIndex ); 406 System.out.println( "size = "+urlStack.size() ); 407 } 408 409 if ( urlStack.size() < 1 ) 410 urlStackIndex = 0; 411 if ( urlStackIndex < urlStack.size() - 1 ) { 412 for ( int j = urlStackIndex+1; j<urlStack.size(); ++j ) { 414 try { 415 urlStack.pop(); 416 } 417 catch (EmptyStackException e) { ; } 418 } 419 } 420 421 urlStack.push(url); 422 urlStackIndex = urlStack.size() - 1; 423 424 if (debug) { 425 System.out.println( "*N* urlStackIndex = "+urlStackIndex ); 426 System.out.println( "size = "+urlStack.size() ); 427 } 428 429 updateComponent(); 430 } 431 432 433 public void addURLToComboBox( String url ) 434 { 435 for (int j=0; j<comboBox.getItemCount(); ++j ) { 436 String refUrl = (String )comboBox.getItemAt(j); 437 if ( url.equals(refUrl) ) { 438 return; 440 } 441 } 442 443 comboBox.addItem(url); 444 int index = comboBox.getItemCount(); 445 comboBox.setSelectedIndex( index - 1 ); 446 } 447 448 449 public URL getPage() 450 { 451 return editorPane.getPage(); 452 } 453 454 457 public void setPage( URL url ) 458 throws IOException 459 { 460 setPage( true, url ); 461 } 462 463 467 protected void setPage( boolean addToStack, URL url ) 468 throws IOException 469 { 470 try { 471 String extURL = url.toExternalForm(); 472 editorPane.setPage( url ); 475 lb_status.setText( extURL ); 478 addURLToComboBox( extURL ); 479 if ( addToStack ) 480 pushNewURLonStack( extURL ); updateComponent(); 482 } 483 catch (IOException ie) { 484 throw ie; } 486 } 487 488 491 public void setPage( String url ) 492 throws IOException 493 { 494 try { 495 setPage( new URL(url) ); 496 } 497 catch (MalformedURLException ue ) { 498 System.err.println("Malformed URL Exception: "+ue ); 499 } 500 } 501 502 503 public boolean isBrowsable() 504 { 505 return browsable; 506 } 507 508 511 public void setBrowsable( boolean flag ) 512 { 513 browsable=flag; 514 updateComponent(); 515 } 516 517 518 public String getContentType() 519 { 520 return editorPane.getContentType(); 521 } 522 523 526 public void setContentType( String contentType ) 527 throws IOException 528 { 529 editorPane.setContentType( contentType ); 530 } 531 532 533 534 public JButton getBackButton() 535 { 536 return bt_back; 537 } 538 539 540 public String getBackButtonText() 541 { 542 return bt_back.getText(); 543 } 544 545 546 public void setBackButtonText( String text ) 547 { 548 bt_back.setText(text); 549 } 550 551 552 public String getBackButtonToolTipText() 553 { 554 return bt_back.getToolTipText(); 555 } 556 557 558 public void setBackButtonToolTipText( String tooltip_text ) 559 { 560 bt_back.setToolTipText(tooltip_text); 561 } 562 563 564 public Icon getBackButtonIcon() 565 { 566 return bt_back.getIcon(); 567 } 568 569 570 public void setBackButtonIcon( Icon icon ) 571 { 572 bt_back.setIcon(icon); 573 } 574 575 576 577 public JButton getNextButton() 578 { 579 return bt_next; 580 } 581 582 583 public String getNextButtonText() 584 { 585 return bt_next.getText(); 586 } 587 588 589 public void setNextButtonText( String text ) 590 { 591 bt_next.setText(text); 592 } 593 594 595 public String getNextButtonToolTipText() 596 { 597 return bt_next.getToolTipText(); 598 } 599 600 601 public void setNextButtonToolTipText( String tooltip_text ) 602 { 603 bt_next.setToolTipText(tooltip_text); 604 } 605 606 607 public Icon getNextButtonIcon() 608 { 609 return bt_next.getIcon(); 610 } 611 612 613 public void setNextButtonIcon( Icon icon ) 614 { 615 bt_next.setIcon(icon); 616 } 617 618 619 620 public JButton getReloadButton() 621 { 622 return bt_reload; 623 } 624 625 626 public String getReloadButtonText() 627 { 628 return bt_reload.getText(); 629 } 630 631 632 public void setReloadButtonText( String text ) 633 { 634 bt_reload.setText(text); 635 } 636 637 638 public String getReloadButtonToolTipText() 639 { 640 return bt_reload.getToolTipText(); 641 } 642 643 644 public void setReloadButtonToolTipText( String tooltip_text ) 645 { 646 bt_reload.setToolTipText(tooltip_text); 647 } 648 649 650 public Icon getReloadButtonIcon() 651 { 652 return bt_reload.getIcon(); 653 } 654 655 656 public void setReloadButtonIcon( Icon icon ) 657 { 658 bt_reload.setIcon(icon); 659 } 660 661 protected void enableBrowsing( boolean flag ) 662 { 663 comboBox.setEnabled(flag); 664 bt_reload.setEnabled(flag); 665 bt_back.setEnabled(flag); 666 bt_next.setEnabled(flag); 667 if (flag) 668 updateComponent(); 669 } 670 671 675 public void actionPerformed( ActionEvent e ) 676 { 677 Object src = e.getSource(); 678 if ( src == bt_back ) { 679 goBack(); 680 } 681 else if ( src == bt_next ) { 682 goNext(); 683 } 684 else if ( src == bt_reload ) { 685 Cursor c = editorPane.getCursor(); 686 Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); 687 editorPane.setCursor(waitCursor); 688 enableBrowsing(false); 689 try { editorPane.setPage(""); } catch (IOException xyz) { ; } 690 691 SwingUtilities.invokeLater( 692 new PageLoader( editorPane.getPage(), c, false)); 693 } 694 else if ( src == comboBox ) { 695 int index = comboBox.getSelectedIndex(); 696 if (index >= 0 ) { 697 String urlExt = (String )comboBox.getItemAt(index); 698 if (browsable) { 699 try { 700 Cursor c = editorPane.getCursor(); 702 Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); 703 editorPane.setCursor(waitCursor); 704 enableBrowsing(false); 705 SwingUtilities.invokeLater( 706 new PageLoader( new URL(urlExt), c, true)); 707 } 708 catch ( IOException ioe) { ; } 709 } 710 else 711 lb_status.setText( urlExt ); 712 } 713 } 714 } 715 716 public void hyperlinkUpdate( HyperlinkEvent evt ) 717 { 718 if ( evt.getEventType() == HyperlinkEvent.EventType.ENTERED ) 719 lb_status.setText( evt.getURL().toString() ); 720 else if ( evt.getEventType() == HyperlinkEvent.EventType.EXITED ) 721 lb_status.setText( "-*- Status -*-" ); 722 else if ( evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) { 723 724 if (browsable) { 725 Cursor c = editorPane.getCursor(); 726 Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); 727 editorPane.setCursor(waitCursor); 728 enableBrowsing(false); 729 SwingUtilities.invokeLater( 730 new PageLoader( evt.getURL(), c, true)); 731 } 732 else 733 lb_status.setText( evt.getURL().toString() ); 734 } 735 } 736 737 public static void showInfo( JEditorPane ep ) 738 { 739 System.out.println("Document type: " + ep.getContentType() ); 740 System.out.println("Editor Kit: " + ep.getEditorKit().getClass().getName() ); 741 System.out.println("Document Type: " + ep.getDocument().getClass().getName() ); 742 System.out.println("----------------------------------------"); 743 } 744 745 749 755 class PageLoader implements Runnable { 756 757 private URL url; 758 private Cursor cursor; 759 private boolean addToStack; 760 761 PageLoader(URL u, Cursor c, boolean a ) { 762 url = u; 763 cursor = c; 764 addToStack = a; 765 } 766 767 public void run() { 768 if (url == null) { 769 editorPane.setCursor(cursor); 771 772 Container parent = editorPane.getParent(); 775 parent.repaint(); 776 } 777 else { 778 String oldURL = lb_status.getText(); 779 Document doc = editorPane.getDocument(); try { 781 lb_status.setText( url.toExternalForm() ); 782 setPage(addToStack, url ); 783 } 784 catch (IOException ioe) { 785 lb_status.setText("Failed to load URL: "+url.toExternalForm() ); 786 editorPane.setDocument(doc); 787 getToolkit().beep(); 788 } 789 finally { 790 url = null; 793 enableBrowsing(true); 794 SwingUtilities.invokeLater(this); 795 } 796 } 797 } 798 799 } 800 801 802 803 public static void main( String [] args ) 804 { 805 JFrame frame = new JFrame("HTMLViewerPane Demonstration"); 806 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 807 if (args.length != 1) { 808 System.out.println("USAGE: java HTMLViewerPane <URL>"); 809 System.exit(69); 810 } 811 812 try { 813 String url = args[0]; 814 File file = new File(url); 815 if ( file.exists() ) 816 url = "file:"+file.getAbsolutePath(); 817 System.out.println("starting URL: `"+url+"'"); 818 HTMLViewerPane pane = new HTMLViewerPane( url ); 819 frame.getContentPane().add( pane ); 820 frame.pack(); 823 frame.setVisible(true); 824 } 825 catch (IOException e) { 826 System.out.println(e); 827 System.exit(69); 828 } 829 } 830 } 831 832 | Popular Tags |