Ejemplo n.º 1
0
  public TestFrame(String title) throws HeadlessException {
    super(title);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    contentPane.setLayout(new BorderLayout());
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());
    final JTextField textField = new JTextField();
    this.addressField = textField;
    JButton button = new JButton("Parse & Render");
    final JTabbedPane tabbedPane = new JTabbedPane();
    final JTree tree = new JTree();
    final JScrollPane scrollPane = new JScrollPane(tree);

    this.tree = tree;

    contentPane.add(topPanel, BorderLayout.NORTH);
    contentPane.add(bottomPanel, BorderLayout.CENTER);

    topPanel.add(new JLabel("URL: "), BorderLayout.WEST);
    topPanel.add(textField, BorderLayout.CENTER);
    topPanel.add(button, BorderLayout.EAST);

    bottomPanel.add(tabbedPane, BorderLayout.CENTER);

    final HtmlPanel panel = new HtmlPanel();
    panel.addSelectionChangeListener(
        new SelectionChangeListener() {
          public void selectionChanged(SelectionChangeEvent event) {
            if (logger.isLoggable(Level.INFO)) {
              logger.info("selectionChanged(): selection node: " + panel.getSelectionNode());
            }
          }
        });
    this.htmlPanel = panel;
    UserAgentContext ucontext = new SimpleUserAgentContext();
    this.rcontext = new LocalHtmlRendererContext(panel, ucontext);

    final JTextArea textArea = new JTextArea();
    this.textArea = textArea;
    textArea.setEditable(false);
    final JScrollPane textAreaSp = new JScrollPane(textArea);

    tabbedPane.addTab("HTML", panel);
    tabbedPane.addTab("Tree", scrollPane);
    tabbedPane.addTab("Source", textAreaSp);
    tabbedPane.addChangeListener(
        new ChangeListener() {
          public void stateChanged(ChangeEvent e) {
            Component component = tabbedPane.getSelectedComponent();
            if (component == scrollPane) {
              tree.setModel(new NodeTreeModel(panel.getRootNode()));
            } else if (component == textAreaSp) {
              textArea.setText(rcontext.getSourceCode());
            }
          }
        });

    button.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            process(textField.getText());
          }
        });
  }