Пример #1
0
  public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    if (node == null) {
      return;
    }

    Object userObject = node.getUserObject();
    if (userObject instanceof TopicInfo) {
      TopicInfo topicInfo = (TopicInfo) userObject;
      try {
        if (topicInfo.subtopicCount > 0) {
          htmlPane.setText(
              "<div style='font-family:Verdana,Tahoma;font-size:10px;'>Please choose a topic</div>");
          return;
        }

        URL helpFileUrl = ResourceManager.getHelpFileUrl(topicInfo.id);
        if (helpFileUrl == null) {
          throw new RuntimeException();
        }
        String content = CommonUtil.readStringFromUrl(helpFileUrl);
        content =
            "<html><body style=\"font-family:Verdana,Tahoma;font-size:10px;\">"
                + "<h2>"
                + topicInfo.title
                + "</h2>"
                + content
                + "</body></html>";
        ((HTMLDocument) htmlPane.getDocument()).setBase(helpFileUrl);
        htmlPane.setText(content);
        htmlPane.setCaretPosition(0);
      } catch (Exception e1) {
        htmlPane.setText(
            "<div style='color:#FF0000;font-family:Verdana,Tahoma;font-size:10px;'>Cannot read help for \""
                + topicInfo.title
                + "\"!</div>");
      }
    }
  }
Пример #2
0
  /** Constructor - creates layout. */
  public HelpFrame() {
    setTitle("Web-Harvest Help");
    setIconImage(((ImageIcon) ResourceManager.HELP32_ICON).getImage());

    this.topNode = new DefaultMutableTreeNode();
    this.treeModel = new DefaultTreeModel(this.topNode);
    try {
      String helpContent = CommonUtil.readStringFromUrl(ResourceManager.getHelpContentUrl());
      XmlNode xmlNode = XmlParser.parse(new InputSource(new StringReader(helpContent)));
      createNodes(topNode, xmlNode);
    } catch (Exception e) {
      e.printStackTrace();
      GuiUtils.showErrorMessage("Error reading help content!");
    }

    tree = new JTree(topNode);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.setBorder(new EmptyBorder(5, 5, 5, 5));
    tree.setCellRenderer(
        new DefaultTreeCellRenderer() {
          public Component getTreeCellRendererComponent(
              JTree tree,
              Object value,
              boolean sel,
              boolean expanded,
              boolean leaf,
              int row,
              boolean hasFocus) {
            DefaultTreeCellRenderer renderer =
                (DefaultTreeCellRenderer)
                    super.getTreeCellRendererComponent(
                        tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof DefaultMutableTreeNode) {
              DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) value;
              Object userObject = defaultMutableTreeNode.getUserObject();
              if (userObject instanceof TopicInfo) {
                TopicInfo topicInfo = (TopicInfo) userObject;
                renderer.setIcon(
                    topicInfo.subtopicCount == 0
                        ? ResourceManager.HELPTOPIC_ICON
                        : ResourceManager.HELPDIR_ICON);
              }
            }
            return renderer;
          }
        });
    tree.addTreeSelectionListener(this);

    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    htmlPane.setContentType("text/html");
    htmlPane.setEditorKit(new HTMLEditorKit());
    htmlPane.setBorder(new EmptyBorder(5, 5, 5, 5));

    JSplitPane splitPane = new ProportionalSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setResizeWeight(0.0d);
    splitPane.setBorder(null);

    JScrollPane treeScrollPane = new WHScrollPane(tree);
    treeScrollPane.getViewport().setBackground(Color.white);
    treeScrollPane.setBackground(Color.white);
    splitPane.setLeftComponent(treeScrollPane);
    splitPane.setRightComponent(new WHScrollPane(htmlPane));
    splitPane.setDividerLocation(0.3d);

    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(splitPane, BorderLayout.CENTER);

    pack();
  }