home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2001 October / PCpro_2001_10.ISO / xml / xmlpro / data1.cab / Program_Executable_FILES / xmlpro.jar / XMLTreeCellRenderer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-09-15  |  4.1 KB  |  168 lines

  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import javax.swing.Icon;
  5. import javax.swing.ImageIcon;
  6. import javax.swing.JComponent;
  7. import javax.swing.JLabel;
  8. import javax.swing.JTree;
  9. import javax.swing.tree.TreeCellRenderer;
  10. import org.w3c.dom.Attr;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.NamedNodeMap;
  13. import org.w3c.dom.Node;
  14.  
  15. public class XMLTreeCellRenderer extends JLabel implements TreeCellRenderer {
  16.    public static boolean selected;
  17.    public static ImageIcon elemIcon;
  18.    public static ImageIcon entityIcon;
  19.    public static ImageIcon cdataIcon;
  20.    public static ImageIcon commentIcon;
  21.    public static ImageIcon pcdataIcon;
  22.    public static Color textSelectionColor;
  23.    public static Color textNonSelectionColor;
  24.    public static Color backgroundSelectionColor;
  25.    public static Color backgroundNonSelectionColor;
  26.    public static Color borderSelectionColor;
  27.    ElemPanel elemPanel;
  28.    XMLPro theApp;
  29.    static int lineBreak = 36;
  30.  
  31.    public XMLTreeCellRenderer(ElemPanel var1, XMLPro var2) {
  32.       this.theApp = var2;
  33.       this.elemPanel = var1;
  34.       textSelectionColor = Color.white;
  35.       backgroundSelectionColor = Color.blue;
  36.       textNonSelectionColor = Color.black;
  37.       backgroundNonSelectionColor = Color.white;
  38.    }
  39.  
  40.    String breakUp(String var1, int var2) {
  41.       int var3 = 0;
  42.       int var5 = var1.length();
  43.       StringBuffer var7 = new StringBuffer(var5);
  44.  
  45.       while(var3 < var5) {
  46.          if (var5 - var3 > var2) {
  47.             int var4 = var3 + var2;
  48.  
  49.             while(var1.charAt(var4) != ' ') {
  50.                --var4;
  51.                if (var4 == var3) {
  52.                   return var1;
  53.                }
  54.             }
  55.  
  56.             var7.append(var1.substring(var3, var4));
  57.             var7.append("\n");
  58.             var3 = var4 + 1;
  59.          } else {
  60.             var7.append(var1.substring(var3));
  61.             var3 = var5;
  62.          }
  63.       }
  64.  
  65.       return var7.toString();
  66.    }
  67.  
  68.    public Component getTreeCellRendererComponent(JTree var1, Object var2, boolean var3, boolean var4, boolean var5, int var6, boolean var7) {
  69.       Node var8 = ((ElemNode)var2).e;
  70.       if (var8.getNodeType() == 3 && this.theApp.viewText) {
  71.          String var12 = var8.getNodeValue();
  72.          if (var12.length() > 0) {
  73.             var12 = StringUtils.replaceStr(var12, "<", '<');
  74.             var12 = this.breakUp(var12, lineBreak);
  75.          } else {
  76.             var12 = "<New PCDATA>";
  77.          }
  78.  
  79.          KTextArea var15 = new KTextArea(var12);
  80.          selected = var3;
  81.          if (var3) {
  82.             var15.text.setForeground(textSelectionColor);
  83.             var15.text.setBackground(backgroundSelectionColor);
  84.          } else {
  85.             var15.text.setForeground(textNonSelectionColor);
  86.             var15.text.setBackground(backgroundNonSelectionColor);
  87.          }
  88.  
  89.          return var15;
  90.       } else {
  91.          if (var8.getNodeType() == 1) {
  92.             String var9 = ((Element)var8).getTagName().toString();
  93.             ((JLabel)this).setIcon(elemIcon);
  94.             if (this.theApp.viewAttributes) {
  95.                NamedNodeMap var10 = var8.getAttributes();
  96.                int var11 = var10.getLength();
  97.                if (var11 == 0) {
  98.                   ((JLabel)this).setText(var9);
  99.                } else {
  100.                   ((JLabel)this).setText(var9 + "     " + this.listAttributes(var10));
  101.                }
  102.             } else {
  103.                ((JLabel)this).setText(var9);
  104.             }
  105.          } else if (var8.getNodeType() == 5) {
  106.             ((JLabel)this).setIcon(entityIcon);
  107.             ((JLabel)this).setText(var8.getNodeName());
  108.          } else if (var8.getNodeType() == 3) {
  109.             ((JLabel)this).setIcon(pcdataIcon);
  110.             ((JLabel)this).setText("PCDATA");
  111.          } else if (var8.getNodeType() == 4) {
  112.             ((JLabel)this).setIcon(cdataIcon);
  113.             ((JLabel)this).setText("CDATA");
  114.          } else if (var8.getNodeType() == 8) {
  115.             ((JLabel)this).setIcon(commentIcon);
  116.             ((JLabel)this).setText("COMMENT");
  117.          }
  118.  
  119.          selected = var3;
  120.          if (var3) {
  121.             ((JComponent)this).setForeground(textSelectionColor);
  122.             ((JComponent)this).setBackground(backgroundSelectionColor);
  123.          } else {
  124.             ((JComponent)this).setForeground(textNonSelectionColor);
  125.             ((JComponent)this).setBackground(backgroundNonSelectionColor);
  126.          }
  127.  
  128.          return this;
  129.       }
  130.    }
  131.  
  132.    String listAttributes(NamedNodeMap var1) {
  133.       String var2 = "[";
  134.       int var3 = var1.getLength();
  135.  
  136.       for(int var4 = 0; var4 < var3; ++var4) {
  137.          Attr var5 = (Attr)var1.item(var4);
  138.          var2 = var2 + var5.getName() + "=" + var5.getValue();
  139.          if (var4 != var3 - 1) {
  140.             var2 = var2 + ", ";
  141.          }
  142.       }
  143.  
  144.       var2 = var2 + "]";
  145.       return var2;
  146.    }
  147.  
  148.    public void paint(Graphics var1) {
  149.       Icon var3 = ((JLabel)this).getIcon();
  150.       Color var2;
  151.       if (selected) {
  152.          var2 = backgroundSelectionColor;
  153.       } else {
  154.          var2 = backgroundNonSelectionColor;
  155.       }
  156.  
  157.       var1.setColor(var2);
  158.       if (var3 != null && ((JLabel)this).getText() != null) {
  159.          int var4 = var3.getIconWidth() + ((JLabel)this).getIconTextGap();
  160.          var1.fillRect(var4, 0, ((JComponent)this).getWidth() - 1 - var4, ((JComponent)this).getHeight() - 1);
  161.       } else {
  162.          var1.fillRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
  163.       }
  164.  
  165.       super.paint(var1);
  166.    }
  167. }
  168.