KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > FrameView


1 /*
2  * @(#)FrameView.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text.html;
8
9 import java.awt.*;
10 import java.util.*;
11 import java.net.*;
12 import java.io.*;
13 import javax.swing.*;
14 import javax.swing.text.*;
15 import javax.swing.event.*;
16
17 /**
18  * Implements a FrameView, intended to support the HTML
19  * <FRAME> tag. Supports the frameborder, scrolling,
20  * marginwidth and marginheight attributes.
21  *
22  * @author Sunita Mani
23  * @version 1.28, 12/19/03
24  */

25
26 class FrameView extends ComponentView implements HyperlinkListener {
27
28
29     JEditorPane htmlPane;
30     JScrollPane scroller;
31     boolean editable;
32     float width;
33     float height;
34     URL src;
35     /** Set to true when the component has been created. */
36     private boolean createdComponent;
37
38     /**
39      * Creates a new Frame.
40      *
41      * @param elem the element to represent.
42      */

43     public FrameView(Element elem) {
44     super(elem);
45     }
46
47     protected Component createComponent() {
48
49     Element elem = getElement();
50     AttributeSet attributes = elem.getAttributes();
51     String JavaDoc srcAtt = (String JavaDoc)attributes.getAttribute(HTML.Attribute.SRC);
52
53     if ((srcAtt != null) && (!srcAtt.equals(""))) {
54         try {
55         URL base = ((HTMLDocument JavaDoc)elem.getDocument()).getBase();
56         src = new URL(base, srcAtt);
57                 htmlPane = new FrameEditorPane();
58                 htmlPane.addHyperlinkListener(this);
59                 JEditorPane host = getHostPane();
60                 boolean isAutoFormSubmission = true;
61                 if (host != null) {
62                     htmlPane.setEditable(host.isEditable());
63             String JavaDoc charset = (String JavaDoc) host.getClientProperty("charset");
64             if (charset != null) {
65             htmlPane.putClientProperty("charset", charset);
66             }
67                     HTMLEditorKit JavaDoc hostKit = (HTMLEditorKit JavaDoc)host.getEditorKit();
68             if (hostKit != null) {
69             isAutoFormSubmission = hostKit.isAutoFormSubmission();
70             }
71                 }
72                 htmlPane.setPage(src);
73                 HTMLEditorKit JavaDoc kit = (HTMLEditorKit JavaDoc)htmlPane.getEditorKit();
74         if (kit != null) {
75             kit.setAutoFormSubmission(isAutoFormSubmission);
76         }
77
78         Document doc = htmlPane.getDocument();
79         if (doc instanceof HTMLDocument JavaDoc) {
80             ((HTMLDocument JavaDoc)doc).setFrameDocumentState(true);
81         }
82         setMargin();
83         createScrollPane();
84         setBorder();
85         } catch (MalformedURLException e) {
86         e.printStackTrace();
87         } catch (IOException e1) {
88         e1.printStackTrace();
89         }
90     }
91     createdComponent = true;
92     return scroller;
93     }
94
95     JEditorPane getHostPane() {
96     Container c = getContainer();
97     while ((c != null) && ! (c instanceof JEditorPane)) {
98         c = c.getParent();
99     }
100     return (JEditorPane) c;
101     }
102
103
104     /**
105      * Sets the parent view for the FrameView.
106      * Also determines if the FrameView should be editable
107      * or not based on whether the JTextComponent that
108      * contains it is editable.
109      *
110      * @param parent View
111      */

112     public void setParent(View parent) {
113     if (parent != null) {
114         JTextComponent t = (JTextComponent)parent.getContainer();
115         editable = t.isEditable();
116     }
117     super.setParent(parent);
118     }
119     
120
121     /**
122      * Also determines if the FrameView should be editable
123      * or not based on whether the JTextComponent that
124      * contains it is editable. And then proceeds to call
125      * the superclass to do the paint().
126      *
127      * @param parent View
128      * @see text.ComponentView#paint
129      */

130     public void paint(Graphics g, Shape allocation) {
131
132     Container host = getContainer();
133     if (host != null && htmlPane != null &&
134         htmlPane.isEditable() != ((JTextComponent)host).isEditable()) {
135         editable = ((JTextComponent)host).isEditable();
136         htmlPane.setEditable(editable);
137     }
138         super.paint(g, allocation);
139     }
140
141
142     /**
143      * If the marginwidth or marginheight attributes have been specified,
144      * then the JEditorPane's margin's are set to the new values.
145      */

146     private void setMargin() {
147     int margin = 0;
148     Insets in = htmlPane.getMargin();
149     Insets newInsets;
150     boolean modified = false;
151     AttributeSet attributes = getElement().getAttributes();
152     String JavaDoc marginStr = (String JavaDoc)attributes.getAttribute(HTML.Attribute.MARGINWIDTH);
153     if ( in != null) {
154         newInsets = new Insets(in.top, in.left, in.right, in.bottom);
155     } else {
156         newInsets = new Insets(0,0,0,0);
157     }
158     if (marginStr != null) {
159         margin = Integer.parseInt(marginStr);
160         if (margin > 0) {
161         newInsets.left = margin;
162         newInsets.right = margin;
163         modified = true;
164         }
165     }
166     marginStr = (String JavaDoc)attributes.getAttribute(HTML.Attribute.MARGINHEIGHT);
167     if (marginStr != null) {
168         margin = Integer.parseInt(marginStr);
169         if (margin > 0) {
170         newInsets.top = margin;
171         newInsets.bottom = margin;
172         modified = true;
173         }
174     }
175     if (modified) {
176         htmlPane.setMargin(newInsets);
177     }
178     }
179
180     /**
181      * If the frameborder attribute has been specified, either in the frame,
182      * or by the frames enclosing frameset, the JScrollPane's setBorder()
183      * method is invoked to achieve the desired look.
184      */

185     private void setBorder() {
186
187     AttributeSet attributes = getElement().getAttributes();
188     String JavaDoc frameBorder = (String JavaDoc)attributes.getAttribute(HTML.Attribute.FRAMEBORDER);
189     if ((frameBorder != null) &&
190         (frameBorder.equals("no") || frameBorder.equals("0"))) {
191         // make invisible borders.
192
scroller.setBorder(null);
193     }
194     }
195
196
197     /**
198      * This method creates the JScrollPane. The scrollbar policy is determined by
199      * the scrolling attribute. If not defined, the default is "auto" which
200      * maps to the scrollbar's being displayed as needed.
201      */

202     private void createScrollPane() {
203     AttributeSet attributes = getElement().getAttributes();
204     String JavaDoc scrolling = (String JavaDoc)attributes.getAttribute(HTML.Attribute.SCROLLING);
205     if (scrolling == null) {
206         scrolling = "auto";
207     }
208     
209     if (!scrolling.equals("no")) {
210         if (scrolling.equals("yes")) {
211         scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
212                        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
213         } else {
214         // scrollbars will be displayed if needed
215
//
216
scroller = new JScrollPane();
217         }
218     } else {
219         scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER,
220                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
221     }
222
223     JViewport vp = scroller.getViewport();
224     vp.add(htmlPane);
225     vp.setBackingStoreEnabled(true);
226     scroller.setMinimumSize(new Dimension(5,5));
227     scroller.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
228     }
229
230
231     /**
232      * Finds the outermost FrameSetView. It then
233      * returns that FrameSetView's container.
234      */

235     private JEditorPane getOutermostJEditorPane() {
236
237     View parent = getParent();
238     FrameSetView JavaDoc frameSetView = null;
239     while (parent != null) {
240         if (parent instanceof FrameSetView JavaDoc) {
241         frameSetView = (FrameSetView JavaDoc)parent;
242         }
243         parent = parent.getParent();
244     }
245     if (frameSetView != null) {
246         return (JEditorPane)frameSetView.getContainer();
247     }
248     return null;
249     }
250
251
252     /**
253      * Returns true if this frame is contained within
254      * a nested frameset.
255      */

256     private boolean inNestedFrameSet() {
257     FrameSetView JavaDoc parent = (FrameSetView JavaDoc)getParent();
258     return (parent.getParent() instanceof FrameSetView JavaDoc);
259     }
260
261
262     /**
263      * Notification of a change relative to a
264      * hyperlink. This method searches for the outermost
265      * JEditorPane, and then fires an HTMLFrameHyperlinkEvent
266      * to that frame. In addition, if the target is _parent,
267      * and there is not nested framesets then the target is
268      * reset to _top. If the target is _top, in addition to
269      * firing the event to the outermost JEditorPane, this
270      * method also invokes the setPage() method and explicitly
271      * replaces the current document with the destination url.
272      *
273      * @param HyperlinkEvent
274      */

275     public void hyperlinkUpdate(HyperlinkEvent evt) {
276
277     JEditorPane c = getOutermostJEditorPane();
278     if (c == null) {
279         return;
280     }
281
282     if (!(evt instanceof HTMLFrameHyperlinkEvent JavaDoc)) {
283         c.fireHyperlinkUpdate(evt);
284         return;
285     }
286
287     HTMLFrameHyperlinkEvent JavaDoc e = (HTMLFrameHyperlinkEvent JavaDoc)evt;
288
289     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
290         String JavaDoc target = e.getTarget();
291
292         if (e.getTarget().equals("_parent") && !inNestedFrameSet()){
293         target = "_top";
294         }
295
296             if (evt instanceof FormSubmitEvent JavaDoc) {
297                 HTMLEditorKit JavaDoc kit = (HTMLEditorKit JavaDoc)c.getEditorKit();
298                 if (kit != null && kit.isAutoFormSubmission()) {
299             if (target.equals("_top")) {
300             try {
301                 c.setPage(e.getURL());
302             } catch (IOException ex) {
303                 // Need a way to handle exceptions
304
}
305             } else {
306             HTMLDocument JavaDoc doc = (HTMLDocument JavaDoc)c.getDocument();
307             doc.processHTMLFrameHyperlinkEvent(e);
308             }
309                 } else {
310                     c.fireHyperlinkUpdate(evt);
311                 }
312                 return;
313             }
314
315         if (target.equals("_top")) {
316         try {
317             c.setPage(e.getURL());
318         } catch (IOException ex) {
319             // Need a way to handle exceptions
320
// ex.printStackTrace();
321
}
322         }
323         if (!c.isEditable()) {
324         c.fireHyperlinkUpdate(new HTMLFrameHyperlinkEvent JavaDoc(c,
325                                   e.getEventType(),
326                                   e.getURL(),
327                                   e.getDescription(),
328                                   getElement(),
329                                   target));
330         }
331     }
332     }
333
334     /**
335      * Gives notification from the document that attributes were changed
336      * in a location that this view is responsible for. Currently this view
337      * handles changes to its SRC attribute.
338      *
339      * @param e the change information from the associated document
340      * @param a the current allocation of the view
341      * @param f the factory to use to rebuild if the view has children
342      *
343      */

344     public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
345
346     Element elem = getElement();
347     AttributeSet attributes = elem.getAttributes();
348
349     URL oldPage = src;
350
351     String JavaDoc srcAtt = (String JavaDoc)attributes.getAttribute(HTML.Attribute.SRC);
352     URL base = ((HTMLDocument JavaDoc)elem.getDocument()).getBase();
353     try {
354         src = new URL(base, srcAtt);
355         if (!createdComponent) {
356         return;
357         }
358         if (oldPage.equals(src) && (src.getRef() == null)) {
359         return;
360         }
361         htmlPane.setPage(src);
362         Document newDoc = htmlPane.getDocument();
363         if (newDoc instanceof HTMLDocument JavaDoc) {
364         ((HTMLDocument JavaDoc)newDoc).setFrameDocumentState(true);
365         }
366     } catch (MalformedURLException e1) {
367         // Need a way to handle exceptions
368
//e1.printStackTrace();
369
} catch (IOException e2) {
370         // Need a way to handle exceptions
371
//e2.printStackTrace();
372
}
373     }
374
375     /**
376      * Determines the minimum span for this view along an
377      * axis.
378      *
379      * @param axis may be either <code>View.X_AXIS</code> or
380      * <code>View.Y_AXIS</code>
381      * @return the preferred span; given that we do not
382      * support resizing of frames, the minimum span returned
383      * is the same as the preferred span
384      *
385      */

386     public float getMinimumSpan(int axis) {
387       return 5;
388     }
389
390     /**
391      * Determines the maximum span for this view along an
392      * axis.
393      *
394      * @param axis may be either <code>View.X_AXIS</code> or
395      * <code>View.Y_AXIS</code>
396      * @return the preferred span; given that we do not
397      * support resizing of frames, the maximum span returned
398      * is the same as the preferred span
399      *
400      */

401     public float getMaximumSpan(int axis) {
402     return Integer.MAX_VALUE;
403     }
404
405     /** Editor pane rendering frame of HTML document
406      * It uses the same editor kits classes as outermost JEditorPane
407      */

408     private class FrameEditorPane extends JEditorPane {
409         public EditorKit getEditorKitForContentType(String JavaDoc type) {
410             EditorKit editorKit = super.getEditorKitForContentType(type);
411             JEditorPane outerMostJEditorPane = null;
412             if ((outerMostJEditorPane = getOutermostJEditorPane()) != null) {
413                 EditorKit inheritedEditorKit = outerMostJEditorPane.getEditorKitForContentType(type);
414                 if (! editorKit.getClass().equals(inheritedEditorKit.getClass())) {
415                     editorKit = (EditorKit) inheritedEditorKit.clone();
416                     setEditorKitForContentType(type, editorKit);
417                 }
418             }
419             return editorKit;
420         }
421     }
422 }
423
424
Popular Tags