Path: blob/master/src/demo/share/jfc/SwingSet2/HtmlDemo.java
41152 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.text.html.*;37import javax.swing.border.*;38import javax.swing.colorchooser.*;39import javax.swing.filechooser.*;40import javax.accessibility.*;4142import java.awt.*;43import java.awt.event.*;44import java.beans.*;45import java.util.*;46import java.io.*;47import java.applet.*;48import java.net.*;4950/**51* Html Demo52*53* @author Jeff Dinkins54*/55public class HtmlDemo extends DemoModule {5657JEditorPane html;5859/**60* main method allows us to run as a standalone demo.61*/62public static void main(String[] args) {63HtmlDemo demo = new HtmlDemo(null);64demo.mainImpl();65}6667/**68* HtmlDemo Constructor69*/70public HtmlDemo(SwingSet2 swingset) {71// Set the title for this demo, and an icon used to represent this72// demo inside the SwingSet2 app.73super(swingset, "HtmlDemo", "toolbar/JEditorPane.gif");7475try {76URL url = null;77// System.getProperty("user.dir") +78// System.getProperty("file.separator");79String path = null;80try {81path = "/resources/index.html";82url = getClass().getResource(path);83} catch (Exception e) {84System.err.println("Failed to open " + path);85url = null;86}8788if(url != null) {89html = new JEditorPane(url);90html.setEditable(false);91html.addHyperlinkListener(createHyperLinkListener());9293JScrollPane scroller = new JScrollPane();94JViewport vp = scroller.getViewport();95vp.add(html);96getDemoPanel().add(scroller, BorderLayout.CENTER);97}98} catch (MalformedURLException e) {99System.out.println("Malformed URL: " + e);100} catch (IOException e) {101System.out.println("IOException: " + e);102}103}104105public HyperlinkListener createHyperLinkListener() {106return new HyperlinkListener() {107public void hyperlinkUpdate(HyperlinkEvent e) {108if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {109if (e instanceof HTMLFrameHyperlinkEvent) {110((HTMLDocument)html.getDocument()).processHTMLFrameHyperlinkEvent(111(HTMLFrameHyperlinkEvent)e);112} else {113try {114html.setPage(e.getURL());115} catch (IOException ioe) {116System.out.println("IOE: " + ioe);117}118}119}120}121};122}123124void updateDragEnabled(boolean dragEnabled) {125html.setDragEnabled(dragEnabled);126}127128}129130131