Пример #1
0
 public HtmlRendererContext open(
     URL url, String windowName, String windowFeatures, boolean replace) {
   TestFrame frame = new TestFrame("Cobra Test Tool");
   frame.setSize(600, 400);
   frame.setExtendedState(TestFrame.NORMAL);
   frame.setVisible(true);
   HtmlRendererContext ctx = frame.getHtmlRendererContext();
   ctx.setOpener(this);
   frame.navigate(url.toExternalForm());
   return ctx;
 }
Пример #2
0
 public HtmlRendererContext open(
     URL url, String windowName, String windowFeatures, boolean replace) {
   // This is called on window.open().
   HtmlPanel newPanel = new HtmlPanel();
   JFrame frame = new JFrame();
   frame.setSize(600, 400);
   frame.getContentPane().add(newPanel);
   HtmlRendererContext newCtx =
       new LocalHtmlRendererContext(newPanel, this.getUserAgentContext());
   newCtx.navigate(url, "_this");
   return newCtx;
 }
Пример #3
0
  private void initComponents() {
    setLayout(new BorderLayout());
    final JPanel containerPanel = new JPanel();
    containerPanel.setLayout(new BorderLayout());

    theRendererContext = new SimpleHtmlRendererContext(theSheetPanel);

    theDocBuilder =
        new DocumentBuilderImpl(theRendererContext.getUserAgentContext(), theRendererContext);

    theSheetPanel = new HtmlPanel();

    containerPanel.add(theSheetPanel, BorderLayout.CENTER);

    theSelectPanel = new SelectPanel(this);
    theSplitPane =
        new FlippingSplitPane(JSplitPane.HORIZONTAL_SPLIT, containerPanel, theSelectPanel);
    theSplitPane.setOneTouchExpandable(true);
    theSplitPane.setDividerSize(10);
    add(theSplitPane, BorderLayout.CENTER);

    theSheetPanel.setPreferredWidth(this.getWidth());

    addComponentListener(
        new ComponentAdapter() {
          @Override
          public void componentShown(@SuppressWarnings("unused") ComponentEvent evt) {
            formComponentShown();
          }
        });
  }
Пример #4
0
 /**
  * Renders HTML given as a string.
  *
  * @param htmlSource The HTML source code.
  * @param uri A base URI used to resolve item URIs.
  * @param rcontext The {@link HtmlRendererContext} instance.
  * @see org.lobobrowser.html.test.SimpleHtmlRendererContext
  * @see #setDocument(Document, HtmlRendererContext)
  */
 public void setHtml(
     final String htmlSource, final String uri, final HtmlRendererContext rcontext) {
   try {
     final DocumentBuilderImpl builder =
         new DocumentBuilderImpl(rcontext.getUserAgentContext(), rcontext);
     try (final Reader reader = new StringReader(htmlSource)) {
       final InputSourceImpl is = new InputSourceImpl(reader, uri);
       final Document document = builder.parse(is);
       this.setDocument(document, rcontext);
     }
   } catch (final java.io.IOException ioe) {
     throw new IllegalStateException("Unexpected condition.", ioe);
   } catch (final org.xml.sax.SAXException se) {
     throw new IllegalStateException("Unexpected condition.", se);
   }
 }
Пример #5
0
  private void setDocumentImpl(final Document node, final HtmlRendererContext rcontext) {
    // Expected to be called in the GUI thread.
    /*
    if (!(node instanceof HTMLDocumentImpl)) {
      throw new IllegalArgumentException("Only nodes of type HTMLDocumentImpl are currently supported. Use DocumentBuilderImpl.");
    }
    */

    if (this.rootNode instanceof HTMLDocumentImpl) {
      final HTMLDocumentImpl prevDocument = (HTMLDocumentImpl) this.rootNode;
      prevDocument.removeDocumentNotificationListener(this.notificationListener);
    }
    if (node instanceof HTMLDocumentImpl) {
      final HTMLDocumentImpl nodeImpl = (HTMLDocumentImpl) node;
      nodeImpl.addDocumentNotificationListener(this.notificationListener);
    }

    if (node instanceof NodeImpl) {
      final NodeImpl nodeImpl = (NodeImpl) node;
      this.rootNode = nodeImpl;
      final NodeImpl fsrn = this.getFrameSetRootNode(nodeImpl);
      final boolean newIfs = fsrn != null;
      if ((newIfs != this.isFrameSet) || (this.getComponentCount() == 0)) {
        this.isFrameSet = newIfs;
        if (newIfs) {
          this.setUpFrameSet(fsrn);
        } else {
          this.setUpAsBlock(rcontext.getUserAgentContext(), rcontext);
        }
      }
      final NodeRenderer nr = this.nodeRenderer;
      if (nr != null) {
        // These subcomponents should take care
        // of revalidation.
        if (newIfs) {
          nr.setRootNode(fsrn);
        } else {
          nr.setRootNode(nodeImpl);
        }
      } else {
        this.invalidate();
        this.validate();
        this.repaint();
      }
    }
  }
Пример #6
0
  public static void main(String[] args) throws Exception {
    // Initialize logging so only Cobra warnings are seen.
    Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING);

    // Open a connection on the URL we want to render first.
    String uri = "http://www.google.com";
    URL url = new URL(uri);
    URLConnection connection = url.openConnection();
    InputStream in = connection.getInputStream();

    // A Reader should be created with the correct charset,
    // which may be obtained from the Content-Type header
    // of an HTTP response.
    Reader reader = new InputStreamReader(in);

    // InputSourceImpl constructor with URI recommended
    // so the renderer can resolve page component URLs.
    InputSource is = new InputSourceImpl(reader, uri);
    HtmlPanel htmlPanel = new HtmlPanel();
    UserAgentContext ucontext = new LocalUserAgentContext();
    HtmlRendererContext rendererContext = new LocalHtmlRendererContext(htmlPanel, ucontext);

    // Set a preferred width for the HtmlPanel,
    // which will allow getPreferredSize() to
    // be calculated according to block content.
    // We do this here to illustrate the
    // feature, but is generally not
    // recommended for performance reasons.
    htmlPanel.setPreferredWidth(800);

    // Note: This example does not perform incremental
    // rendering while loading the initial document.
    DocumentBuilderImpl builder =
        new DocumentBuilderImpl(rendererContext.getUserAgentContext(), rendererContext);

    Document document = builder.parse(is);
    in.close();

    // Set the document in the HtmlPanel. This method
    // schedules the document to be rendered in the
    // GUI thread.
    htmlPanel.setDocument(document, rendererContext);

    // Create a JFrame and add the HtmlPanel to it.
    final JFrame frame = new JFrame();
    frame.getContentPane().add(htmlPanel);

    // We pack the JFrame to demonstrate the
    // validity of HtmlPanel's preferred size.
    // Normally you would want to set a specific
    // JFrame size instead.

    // pack() should be called in the GUI dispatch
    // thread since the document is scheduled to
    // be rendered in that thread, and is required
    // for the preferred size determination.
    EventQueue.invokeLater(
        new Runnable() {
          public void run() {
            frame.pack();
            frame.setVisible(true);
          }
        });
  }