コード例 #1
0
    @Override
    public Component getTreeCellRendererComponent(
        JTree tree,
        Object value,
        boolean sel,
        boolean expanded,
        boolean leaf,
        int row,
        boolean hasFocus) {
      super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

      if (!(value instanceof File)) {
        labelRenderer.getTreeCellRendererComponent(tree, value, sel, expanded, false, row, false);
        labelRenderer.setIcon(null);
        return labelRenderer;
      }

      File file = (File) value;
      checkBox.setText(RecursiveLibraryDirectoryPanel.this.getText(file));
      // setColors(compTemp, tree, tree.getPathForRow(row), checkBox, sel);
      checkBox.setBackground(this.getBackground());
      checkBox.setForeground(this.getForeground());

      if (isExcluded(file)) {
        checkBox.setSelected(false);
        checkBox.setIcon(null);
      } else if (isFullyIncluded(file)) {
        checkBox.setSelected(true);
        checkBox.setIcon(null);
      } else {
        checkBox.setSelected(true);
        checkBox.setIcon(partiallyIncludedIcon);
      }
      return checkBox;
    }
コード例 #2
0
ファイル: ExplorerNodeRenderer.java プロジェクト: r1c0/Nuclos
  @Override
  public Component getTreeCellRendererComponent(
      JTree tree,
      Object oValue,
      boolean bSelected,
      boolean bExpanded,
      boolean bLeaf,
      int iRow,
      boolean bHasFocus) {

    final JLabel lbl =
        (JLabel)
            super.getTreeCellRendererComponent(
                tree, oValue, bSelected, bExpanded, bLeaf, iRow, bHasFocus);

    String tmp = lbl.getText();

    final List<PaintImage> images = new LinkedList<PaintImage>();

    int idx = -1;
    int spaceX = SwingUtilities.computeStringWidth(lbl.getFontMetrics(lbl.getFont()), " ");

    while ((idx = tmp.indexOf("[$" + CollectableFieldFormat.class.getName() + ",")) != -1) {
      int formatEnd = tmp.indexOf("$]");
      String format = tmp.substring(idx, formatEnd);

      String[] formatDef = format.split(",");
      try {
        CollectableFieldFormat clctformat =
            CollectableFieldFormat.getInstance(Class.forName(formatDef[1]));

        int x =
            SwingUtilities.computeStringWidth(
                lbl.getFontMetrics(lbl.getFont()), tmp.substring(0, idx));

        NuclosImage img = (NuclosImage) clctformat.parse(null, formatDef[3]);

        String tmp1 = tmp.substring(0, idx);
        for (int i = 0; i < img.getWidth() / spaceX; i++) {
          tmp1 += " ";
        }
        tmp1 += tmp.substring(formatEnd + 2);
        tmp = tmp1;

        images.add(new PaintImage(x, img));
      } catch (CollectableFieldFormatException e) {
        LOG.error("format exception at " + formatDef[1], e);
      } catch (ClassNotFoundException e) {
        LOG.error("class not found for " + formatDef[1], e);
      }
    }

    final ExplorerNode<?> explorernode = (ExplorerNode<?>) oValue;

    DefaultTreeCellRenderer lbComp =
        new DefaultTreeCellRenderer() {

          private static final long serialVersionUID = 5423600039963175923L;

          public void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            for (PaintImage paintImage : images) {
              g2d.drawImage(
                  new ImageIcon(paintImage.image.getContent()).getImage(),
                  getIcon().getIconWidth() + getIconTextGap() + paintImage.x,
                  1,
                  null);
            }
          };
        };

    JComponent result =
        (JComponent)
            lbComp.getTreeCellRendererComponent(
                tree, oValue, bSelected, bExpanded, bLeaf, iRow, bHasFocus);

    lbComp.setText(tmp);

    // set tooltip text:
    final String sDescription =
        StringUtils.nullIfEmpty(explorernode.getTreeNode().getDescription());
    lbComp.setToolTipText(sDescription);

    // set icon:
    final Icon icon = explorernode.getIcon();
    // if (icon != null) {
    lbComp.setIcon(icon);
    // }

    if (explorernode instanceof GenericObjectExplorerNode) {
      final Icon iconRelation = ((GenericObjectExplorerNode) explorernode).getRelationIcon();
      if (iconRelation != null) {
        lbComp.setIcon(new CompositeIcon(iconRelation, icon));

        // result = new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 0));
        // result.add(new JLabel(iconRelation));
        // result.add(lbl);
        // result.setOpaque(false);
      }
    }

    return result;
  }