1 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 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 36 private boolean createdComponent; 37 38 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 srcAtt = (String )attributes.getAttribute(HTML.Attribute.SRC); 52 53 if ((srcAtt != null) && (!srcAtt.equals(""))) { 54 try { 55 URL base = ((HTMLDocument )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 charset = (String ) host.getClientProperty("charset"); 64 if (charset != null) { 65 htmlPane.putClientProperty("charset", charset); 66 } 67 HTMLEditorKit hostKit = (HTMLEditorKit )host.getEditorKit(); 68 if (hostKit != null) { 69 isAutoFormSubmission = hostKit.isAutoFormSubmission(); 70 } 71 } 72 htmlPane.setPage(src); 73 HTMLEditorKit kit = (HTMLEditorKit )htmlPane.getEditorKit(); 74 if (kit != null) { 75 kit.setAutoFormSubmission(isAutoFormSubmission); 76 } 77 78 Document doc = htmlPane.getDocument(); 79 if (doc instanceof HTMLDocument ) { 80 ((HTMLDocument )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 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 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 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 marginStr = (String )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 )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 185 private void setBorder() { 186 187 AttributeSet attributes = getElement().getAttributes(); 188 String frameBorder = (String )attributes.getAttribute(HTML.Attribute.FRAMEBORDER); 189 if ((frameBorder != null) && 190 (frameBorder.equals("no") || frameBorder.equals("0"))) { 191 scroller.setBorder(null); 193 } 194 } 195 196 197 202 private void createScrollPane() { 203 AttributeSet attributes = getElement().getAttributes(); 204 String scrolling = (String )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 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 235 private JEditorPane getOutermostJEditorPane() { 236 237 View parent = getParent(); 238 FrameSetView frameSetView = null; 239 while (parent != null) { 240 if (parent instanceof FrameSetView ) { 241 frameSetView = (FrameSetView )parent; 242 } 243 parent = parent.getParent(); 244 } 245 if (frameSetView != null) { 246 return (JEditorPane)frameSetView.getContainer(); 247 } 248 return null; 249 } 250 251 252 256 private boolean inNestedFrameSet() { 257 FrameSetView parent = (FrameSetView )getParent(); 258 return (parent.getParent() instanceof FrameSetView ); 259 } 260 261 262 275 public void hyperlinkUpdate(HyperlinkEvent evt) { 276 277 JEditorPane c = getOutermostJEditorPane(); 278 if (c == null) { 279 return; 280 } 281 282 if (!(evt instanceof HTMLFrameHyperlinkEvent )) { 283 c.fireHyperlinkUpdate(evt); 284 return; 285 } 286 287 HTMLFrameHyperlinkEvent e = (HTMLFrameHyperlinkEvent )evt; 288 289 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 290 String target = e.getTarget(); 291 292 if (e.getTarget().equals("_parent") && !inNestedFrameSet()){ 293 target = "_top"; 294 } 295 296 if (evt instanceof FormSubmitEvent ) { 297 HTMLEditorKit kit = (HTMLEditorKit )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 } 305 } else { 306 HTMLDocument doc = (HTMLDocument )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 } 322 } 323 if (!c.isEditable()) { 324 c.fireHyperlinkUpdate(new HTMLFrameHyperlinkEvent (c, 325 e.getEventType(), 326 e.getURL(), 327 e.getDescription(), 328 getElement(), 329 target)); 330 } 331 } 332 } 333 334 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 srcAtt = (String )attributes.getAttribute(HTML.Attribute.SRC); 352 URL base = ((HTMLDocument )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 ) { 364 ((HTMLDocument )newDoc).setFrameDocumentState(true); 365 } 366 } catch (MalformedURLException e1) { 367 } catch (IOException e2) { 370 } 373 } 374 375 386 public float getMinimumSpan(int axis) { 387 return 5; 388 } 389 390 401 public float getMaximumSpan(int axis) { 402 return Integer.MAX_VALUE; 403 } 404 405 408 private class FrameEditorPane extends JEditorPane { 409 public EditorKit getEditorKitForContentType(String 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 |