⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardleveltagscellrenderer.java

📁 WAP ide 代码
💻 JAVA
字号:
package wapide;import javax.swing.*;import javax.swing.plaf.ColorUIResource;import javax.swing.plaf.FontUIResource;import java.awt.*;import java.awt.event.*;import java.beans.*;import java.io.*;import java.util.*;import javax.swing.tree.*;/** * This class was taken from the source code of Java, provided by Sun Microsystems. * It was distributed as part of the Java 1.3 SDK.  It has been slightly modified * by Mark Busman for use with the WAP.CardLevelTagsDesigner class so that * it will display the appropriate icon based on the control type of any * given node.  The only procedure modified was getTreeCellRendererComponent(). */public class CardLevelTagsCellRenderer extends JLabel implements TreeCellRenderer{    /** Is the value currently selected. */    protected boolean selected;    /** True if has focus. */    protected boolean hasFocus;    /** True if draws focus border around icon as well. */    private boolean drawsFocusBorderAroundIcon;    // Icons    /** Icon used to show non-leaf nodes that aren't expanded. */    transient protected Icon closedIcon;    /** Icon used to show leaf nodes. */    transient protected Icon leafIcon;    /** Icon used to show non-leaf nodes that are expanded. */    transient protected Icon openIcon;    // Colors    /** Color to use for the foreground for selected nodes. */    protected Color textSelectionColor;    /** Color to use for the foreground for non-selected nodes. */    protected Color textNonSelectionColor;    /** Color to use for the background when a node is selected. */    protected Color backgroundSelectionColor;    /** Color to use for the background when the node isn't selected. */    protected Color backgroundNonSelectionColor;    /** Color to use for the background when the node isn't selected. */    protected Color borderSelectionColor;    /**      * Returns a new instance of DefaultTreeCellRenderer.  Alignment is      * set to left aligned. Icons and text color are determined from the      * UIManager.      */    public CardLevelTagsCellRenderer() {	    setHorizontalAlignment(JLabel.LEFT);	    setLeafIcon(UIManager.getIcon("Tree.leafIcon"));	    setClosedIcon(UIManager.getIcon("Tree.closedIcon"));	    setOpenIcon(UIManager.getIcon("Tree.openIcon"));	    setTextSelectionColor(UIManager.getColor("Tree.selectionForeground"));	    setTextNonSelectionColor(UIManager.getColor("Tree.textForeground"));	    setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground"));	    setBackgroundNonSelectionColor(UIManager.getColor("Tree.textBackground"));	    setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor"));	    Object value = UIManager.get("Tree.drawsFocusBorderAroundIcon");	    drawsFocusBorderAroundIcon = (value != null && ((Boolean)value).				      booleanValue());    }    /**      * Returns the default icon, for the current laf, that is used to      * represent non-leaf nodes that are expanded.      */    private Icon getDefaultOpenIcon() {	    return UIManager.getIcon("Tree.openIcon");    }    /**      * Returns the default icon, for the current laf, that is used to      * represent non-leaf nodes that are not expanded.      */    private Icon getDefaultClosedIcon() {	    return UIManager.getIcon("Tree.closedIcon");    }    /**      * Returns the default icon, for the current laf, that is used to      * represent leaf nodes.      */    private Icon getDefaultLeafIcon() {	    return UIManager.getIcon("Tree.leafIcon");    }    /**      * Sets the icon used to represent non-leaf nodes that are expanded.      */    private void setOpenIcon(Icon newIcon) {	    openIcon = newIcon;    }    /**      * Returns the icon used to represent non-leaf nodes that are expanded.      */    private Icon getOpenIcon() {	    return openIcon;    }    /**      * Sets the icon used to represent non-leaf nodes that are not expanded.      */    private void setClosedIcon(Icon newIcon) {	    closedIcon = newIcon;    }    /**      * Returns the icon used to represent non-leaf nodes that are not      * expanded.      */    private Icon getClosedIcon() {	    return closedIcon;    }    /**      * Sets the icon used to represent leaf nodes.      */    private void setLeafIcon(Icon newIcon) {	    leafIcon = newIcon;    }    /**      * Returns the icon used to represent leaf nodes.      */    private Icon getLeafIcon() {	    return leafIcon;    }    /**      * Sets the color the text is drawn with when the node is selected.      */    public void setTextSelectionColor(Color newColor) {	    textSelectionColor = newColor;    }    /**      * Returns the color the text is drawn with when the node is selected.      */    public Color getTextSelectionColor() {	    return textSelectionColor;    }    /**      * Sets the color the text is drawn with when the node isn't selected.      */    public void setTextNonSelectionColor(Color newColor) {	    textNonSelectionColor = newColor;    }    /**      * Returns the color the text is drawn with when the node isn't selected.      */    public Color getTextNonSelectionColor() {	    return textNonSelectionColor;    }    /**      * Sets the color to use for the background if node is selected.      */    public void setBackgroundSelectionColor(Color newColor) {	    backgroundSelectionColor = newColor;    }    /**      * Returns the color to use for the background if node is selected.      */    public Color getBackgroundSelectionColor() {	    return backgroundSelectionColor;    }    /**      * Sets the background color to be used for non selected nodes.      */    public void setBackgroundNonSelectionColor(Color newColor) {	    backgroundNonSelectionColor = newColor;    }    /**      * Returns the background color to be used for non selected nodes.      */    public Color getBackgroundNonSelectionColor() {	    return backgroundNonSelectionColor;    }    /**      * Sets the color to use for the border.      */    public void setBorderSelectionColor(Color newColor) {	    borderSelectionColor = newColor;    }    /**      * Returns the color the border is drawn.      */    public Color getBorderSelectionColor() {	    return borderSelectionColor;    }    /**     * Subclassed to map <code>FontUIResource</code>s to null. If     * <code>font</code> is null, or a <code>FontUIResource</code>, this     * has the effect of letting the font of the JTree show     * through. On the other hand, if <code>font</code> is non-null, and not     * a <code>FontUIResource</code>, the font becomes <code>font</code>.     */    public void setFont(Font font) {	    if(font instanceof FontUIResource)	      font = null;	    super.setFont(font);    }    /**     * Subclassed to map <code>ColorUIResource</code>s to null. If     * <code>color</code> is null, or a <code>ColorUIResource</code>, this     * has the effect of letting the background color of the JTree show     * through. On the other hand, if <code>color</code> is non-null, and not     * a <code>ColorUIResource</code>, the background becomes     * <code>color</code>.     */    public void setBackground(Color color) {	    if(color instanceof ColorUIResource)	      color = null;	    super.setBackground(color);    }    /**      * Configures the renderer based on the passed in components.      * The value is set from messaging the tree with      * <code>convertValueToText</code>, which ultimately invokes      * <code>toString</code> on <code>value</code>.      * The foreground color is set based on the selection and the icon      * is set based on on leaf and expanded.      */    public Component getTreeCellRendererComponent(JTree tree, Object value,						  boolean sel,						  boolean expanded,						  boolean leaf, int row,						  boolean hasFocus) {	    String stringValue = tree.convertValueToText(value, sel,        				  expanded, leaf, row, hasFocus);      try {        EnhancedTreeNode node = (EnhancedTreeNode) value;        this.hasFocus = hasFocus;	      setText(stringValue);	      if(sel)	        setForeground(getTextSelectionColor());	      else	        setForeground(getTextNonSelectionColor());        setEnabled(true);        // render the icons accordingly        if (node.getNodeType().equals("do"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("doTag.gif")));        if (node.getNodeType().equals("go"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("goTag.gif")));        if (node.getNodeType().equals("noop"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("noopTag.gif")));        if (node.getNodeType().equals("prev"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("prevTag.gif")));        if (node.getNodeType().equals("refresh"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("refreshTag.gif")));        if (node.getNodeType().equals("timer"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("timerTag.gif")));        if (node.getNodeType().equals("postfield"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("postfieldTag.gif")));        if (node.getNodeType().equals("setvar"))          setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("setvarTag.gif")));        if (node.getNodeType().equals("onevent")) {          CardLevelTagsDesigner.oneventNode n = (CardLevelTagsDesigner.oneventNode) node;          String s = n.getType();          try {            setIcon(new ImageIcon(wapide.IDEFrame.class.getResource(s + ".gif")));          }          catch (NullPointerException locnullerr) {            setIcon(new ImageIcon(wapide.IDEFrame.class.getResource("dummy.gif")));          }        }        setComponentOrientation(tree.getComponentOrientation());	      selected = sel;      }      catch (NullPointerException nullerr) {}	    return this;    }    /**      * Paints the value.  The background is filled based on selected.      */    public void paint(Graphics g) {	    Color bColor;	    if(selected) {	      bColor = getBackgroundSelectionColor();	    } else {	      bColor = getBackgroundNonSelectionColor();	      if(bColor == null)		      bColor = getBackground();	    }	    int imageOffset = -1;	    if(bColor != null) {	      Icon currentI = getIcon();	      imageOffset = getLabelStart();	      g.setColor(bColor);	      if(getComponentOrientation().isLeftToRight()) {	        g.fillRect(imageOffset, 0, getWidth() - 1 - imageOffset,			      getHeight());	      } else {	        g.fillRect(0, 0, getWidth() - 1 - imageOffset,			      getHeight());	      }	    }	    if (hasFocus) {	      if (drawsFocusBorderAroundIcon) {		      imageOffset = 0;	      }	      else if (imageOffset == -1) {		      imageOffset = getLabelStart();	      }	      Color       bsColor = getBorderSelectionColor();	      if (bsColor != null) {		      g.setColor(bsColor);		      if(getComponentOrientation().isLeftToRight()) {		        g.drawRect(imageOffset, 0, getWidth() - 1 - imageOffset,			        getHeight() - 1);		      } else {		        g.drawRect(0, 0, getWidth() - 1 - imageOffset,			        getHeight() - 1);		      }	      }	    }	    super.paint(g);    }    private int getLabelStart() {	    Icon currentI = getIcon();	    if(currentI != null && getText() != null) {	      return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);	    }	    return 0;    }    /**     * Overrides <code>JComponent.getPreferredSize</code> to     * return slightly wider preferred size value.     */    public Dimension getPreferredSize() {	    Dimension        retDimension = super.getPreferredSize();	    if(retDimension != null)	      retDimension = new Dimension(retDimension.width + 3,					 retDimension.height);      return retDimension;    }   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void validate() {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void revalidate() {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void repaint(long tm, int x, int y, int width, int height) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void repaint(Rectangle r) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {	    // Strings get interned...	    if (propertyName=="text")	      super.firePropertyChange(propertyName, oldValue, newValue);      }   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, char oldValue, char newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, short oldValue, short newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, int oldValue, int newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, long oldValue, long newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, float oldValue, float newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, double oldValue, double newValue) {}   /**    * Overridden for performance reasons.    * See the <a href="#override">Implementation Note</a>    * for more information.    */    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -