1 /* FrameView.java -- Renders HTML frame tags
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text.html;
41 import java.awt.Component;
42 import java.io.IOException;
43 import java.net.MalformedURLException;
46 import javax.swing.JEditorPane;
47 import javax.swing.event.HyperlinkEvent;
48 import javax.swing.event.HyperlinkListener;
49 import javax.swing.text.AttributeSet;
50 import javax.swing.text.ComponentView;
51 import javax.swing.text.Element;
52 import javax.swing.text.View;
55 * A view that is responsible for rendering HTML frame tags.
56 * This is accomplished by a specialized {@link ComponentView}
57 * that embeds a JEditorPane with an own document.
61 implements HyperlinkListener
65 * Creates a new FrameView for the specified element.
67 * @param el the element for the view
75 * Creates the element that will be embedded in the view.
76 * This will be a JEditorPane with the appropriate content set.
78 * @return the element that will be embedded in the view
80 protected Component createComponent()
82 Element el = getElement();
83 AttributeSet atts = el.getAttributes();
84 JEditorPane html = new JEditorPane();
85 html.addHyperlinkListener(this);
86 URL base = ((HTMLDocument) el.getDocument()).getBase();
87 String srcAtt = (String) atts.getAttribute(HTML.Attribute.SRC);
88 if (srcAtt != null && ! srcAtt.equals(""))
92 URL page = new URL(base, srcAtt);
94 ((HTMLDocument) html.getDocument()).setFrameDocument(true);
96 catch (MalformedURLException ex)
100 catch (IOException ex)
109 * Catches hyperlink events on that frame's editor and forwards it to
110 * the outermost editorpane.
112 public void hyperlinkUpdate(HyperlinkEvent event)
114 JEditorPane outer = getTopEditorPane();
117 if (event instanceof HTMLFrameHyperlinkEvent)
119 HTMLFrameHyperlinkEvent hfhe = (HTMLFrameHyperlinkEvent) event;
120 if (hfhe.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
122 String target = hfhe.getTarget();
123 if (event instanceof FormSubmitEvent)
125 handleFormSubmitEvent(hfhe, outer, target);
127 else // No FormSubmitEvent.
129 handleHyperlinkEvent(hfhe, outer, target);
135 // Simply forward this event.
136 outer.fireHyperlinkUpdate(event);
142 * Handles normal hyperlink events.
144 * @param event the event
145 * @param outer the top editor
146 * @param target the target
148 private void handleHyperlinkEvent(HyperlinkEvent event,
149 JEditorPane outer, String target)
151 if (target.equals("_top"))
155 outer.setPage(event.getURL());
157 catch (IOException ex)
160 ex.printStackTrace();
163 if (! outer.isEditable())
165 outer.fireHyperlinkUpdate
166 (new HTMLFrameHyperlinkEvent(outer,
167 event.getEventType(),
169 event.getDescription(),
176 * Handles form submit events.
178 * @param event the event
179 * @param outer the top editor
180 * @param target the target
182 private void handleFormSubmitEvent(HTMLFrameHyperlinkEvent event,
186 HTMLEditorKit kit = (HTMLEditorKit) outer.getEditorKit();
187 if (kit != null && kit.isAutoFormSubmission())
189 if (target.equals("_top"))
193 outer.setPage(event.getURL());
195 catch (IOException ex)
198 ex.printStackTrace();
204 (HTMLDocument) outer.getDocument();
205 doc.processHTMLFrameHyperlinkEvent(event);
210 outer.fireHyperlinkUpdate(event);
215 * Determines the topmost editor in a nested frameset.
217 * @return the topmost editor in a nested frameset
219 private JEditorPane getTopEditorPane()
221 View parent = getParent();
223 while (parent != null)
225 if (parent instanceof FrameSetView)
228 JEditorPane editor = null;
230 editor = (JEditorPane) top.getContainer();