OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / text / html / FrameView.java
1 /* FrameView.java -- Renders HTML frame tags
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38
39 package javax.swing.text.html;
40
41 import java.awt.Component;
42 import java.io.IOException;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45
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;
53
54 /**
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.
58  */
59 class FrameView
60   extends ComponentView
61   implements HyperlinkListener
62 {
63
64   /**
65    * Creates a new FrameView for the specified element.
66    *
67    * @param el the element for the view
68    */
69   FrameView(Element el)
70   {
71     super(el);
72   }
73
74   /**
75    * Creates the element that will be embedded in the view.
76    * This will be a JEditorPane with the appropriate content set.
77    *
78    * @return the element that will be embedded in the view
79    */
80   protected Component createComponent()
81   {
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(""))
89       {
90         try
91           {
92             URL page = new URL(base, srcAtt);
93             html.setPage(page);
94             ((HTMLDocument) html.getDocument()).setFrameDocument(true);
95           }
96         catch (MalformedURLException ex)
97           {
98             // Leave page empty.
99           }
100         catch (IOException ex)
101           {
102             // Leave page empty.
103           }
104       }
105     return html;
106   }
107
108   /**
109    * Catches hyperlink events on that frame's editor and forwards it to
110    * the outermost editorpane.
111    */
112   public void hyperlinkUpdate(HyperlinkEvent event)
113   {
114     JEditorPane outer = getTopEditorPane();
115     if (outer != null)
116       {
117         if (event instanceof HTMLFrameHyperlinkEvent)
118           {
119             HTMLFrameHyperlinkEvent hfhe = (HTMLFrameHyperlinkEvent) event;
120             if (hfhe.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
121               {
122                 String target = hfhe.getTarget();
123                 if (event instanceof FormSubmitEvent)
124                   {
125                     handleFormSubmitEvent(hfhe, outer, target);
126                   }
127                 else // No FormSubmitEvent.
128                   {
129                     handleHyperlinkEvent(hfhe, outer, target);
130                   }
131               }
132           }
133         else
134           {
135             // Simply forward this event.
136             outer.fireHyperlinkUpdate(event);
137           }
138       }
139   }
140
141   /**
142    * Handles normal hyperlink events.
143    *
144    * @param event the event
145    * @param outer the top editor
146    * @param target the target
147    */
148   private void handleHyperlinkEvent(HyperlinkEvent event,
149                                     JEditorPane outer, String target)
150   {
151     if (target.equals("_top"))
152       {
153         try
154           {
155             outer.setPage(event.getURL());
156           }
157         catch (IOException ex)
158           {
159             // Well...
160             ex.printStackTrace();
161           }
162       }
163     if (! outer.isEditable())
164       {
165         outer.fireHyperlinkUpdate
166           (new HTMLFrameHyperlinkEvent(outer,
167                                        event.getEventType(),
168                                        event.getURL(),
169                                        event.getDescription(),
170                                        getElement(),
171                                        target));
172       }
173   }
174
175   /**
176    * Handles form submit events.
177    *
178    * @param event the event
179    * @param outer the top editor
180    * @param target the target
181    */
182   private void handleFormSubmitEvent(HTMLFrameHyperlinkEvent event,
183                                      JEditorPane outer,
184                                      String target)
185   {
186     HTMLEditorKit kit = (HTMLEditorKit) outer.getEditorKit();
187     if (kit != null && kit.isAutoFormSubmission())
188       {
189         if (target.equals("_top"))
190           {
191             try
192               {
193                 outer.setPage(event.getURL());
194               }
195             catch (IOException ex)
196               {
197                 // Well...
198                 ex.printStackTrace();
199               }
200           }
201         else
202           {
203             HTMLDocument doc =
204               (HTMLDocument) outer.getDocument();
205             doc.processHTMLFrameHyperlinkEvent(event);
206           }
207       }
208     else
209       {
210         outer.fireHyperlinkUpdate(event);
211       }
212   }
213
214   /**
215    * Determines the topmost editor in a nested frameset.
216    *
217    * @return the topmost editor in a nested frameset
218    */
219   private JEditorPane getTopEditorPane()
220   {
221     View parent = getParent();
222     View top = null;
223     while (parent != null)
224       {
225         if (parent instanceof FrameSetView)
226           top = parent;
227       }
228     JEditorPane editor = null;
229     if (top != null)
230       editor = (JEditorPane) top.getContainer();
231     return editor;
232   }
233 }