View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    *
8    * Created on Apr 14, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.renderers;
12  
13  import java.awt.Color;
14  import java.awt.Component;
15  import java.awt.Font;
16  import java.awt.Rectangle;
17  import java.io.Serializable;
18  
19  import javax.swing.JComponent;
20  import javax.swing.JLabel;
21  import javax.swing.border.Border;
22  import javax.swing.border.EmptyBorder;
23  
24  /**
25   * DefaultEdgeLabelRenderer is similar to the cell renderers
26   * used by the JTable and JTree jfc classes.
27   * 
28   * @author Tom Nelson 
29   *
30   * 
31   */
32  @SuppressWarnings("serial")
33  public class DefaultEdgeLabelRenderer extends JLabel implements
34          EdgeLabelRenderer, Serializable {
35  
36       protected static Border noFocusBorder = new EmptyBorder(0,0,0,0); 
37      
38       protected Color pickedEdgeLabelColor = Color.black;
39       protected boolean rotateEdgeLabels;
40       
41       public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor) {
42           this(pickedEdgeLabelColor, true);
43       }
44       
45      /**
46       * Creates a default table cell renderer.
47       */
48      public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor, boolean rotateEdgeLabels) {
49          super();
50          this.pickedEdgeLabelColor = pickedEdgeLabelColor;
51          this.rotateEdgeLabels = rotateEdgeLabels;
52          setOpaque(true);
53          setBorder(noFocusBorder);
54      }
55  
56      /**
57       * @return Returns the rotateEdgeLabels.
58       */
59      public boolean isRotateEdgeLabels() {
60          return rotateEdgeLabels;
61      }
62      /**
63       * @param rotateEdgeLabels The rotateEdgeLabels to set.
64       */
65      public void setRotateEdgeLabels(boolean rotateEdgeLabels) {
66          this.rotateEdgeLabels = rotateEdgeLabels;
67      }
68      /**
69       * Overrides <code>JComponent.setForeground</code> to assign
70       * the unselected-foreground color to the specified color.
71       * 
72       * @param c set the foreground color to this value
73       */
74      @Override
75      public void setForeground(Color c) {
76          super.setForeground(c); 
77      }
78      
79      /**
80       * Overrides <code>JComponent.setBackground</code> to assign
81       * the unselected-background color to the specified color.
82       *
83       * @param c set the background color to this value
84       */
85      @Override
86      public void setBackground(Color c) {
87          super.setBackground(c); 
88      }
89  
90      /**
91       * Notification from the <code>UIManager</code> that the look and feel
92       * [L&F] has changed.
93       * Replaces the current UI object with the latest version from the 
94       * <code>UIManager</code>.
95       *
96       * @see JComponent#updateUI
97       */
98      @Override
99      public void updateUI() {
100         super.updateUI(); 
101         setForeground(null);
102         setBackground(null);
103     }
104     
105     /**
106     *
107     * Returns the default label renderer for an Edge
108     *
109     * @param vv  the <code>VisualizationViewer</code> to render on
110     * @param value  the value to assign to the label for
111     *			<code>Edge</code>
112     * @param edge  the <code>Edge</code>
113     * @return the default label renderer
114     */
115     public <E> Component getEdgeLabelRendererComponent(JComponent vv, Object value,
116             Font font, boolean isSelected, E edge) {
117         
118         super.setForeground(vv.getForeground());
119         if(isSelected) setForeground(pickedEdgeLabelColor);
120         super.setBackground(vv.getBackground());
121         
122         if(font != null) {
123             setFont(font);
124         } else {
125             setFont(vv.getFont());
126         }
127         setIcon(null);
128         setBorder(noFocusBorder);
129         setValue(value); 
130         return this;
131     }
132     
133     /*
134      * The following methods are overridden as a performance measure to 
135      * to prune code-paths are often called in the case of renders
136      * but which we know are unnecessary.  Great care should be taken
137      * when writing your own renderer to weigh the benefits and 
138      * drawbacks of overriding methods like these.
139      */
140 
141     /**
142      * Overridden for performance reasons.
143      * See the <a href="#override">Implementation Note</a> 
144      * for more information.
145      */
146     @Override
147     public boolean isOpaque() { 
148         Color back = getBackground();
149         Component p = getParent(); 
150         if (p != null) { 
151             p = p.getParent(); 
152         }
153         boolean colorMatch = (back != null) && (p != null) && 
154         back.equals(p.getBackground()) && 
155         p.isOpaque();
156         return !colorMatch && super.isOpaque(); 
157     }
158 
159     /**
160      * Overridden for performance reasons.
161      * See the <a href="#override">Implementation Note</a> 
162      * for more information.
163      */
164     @Override
165     public void validate() {}
166 
167     /**
168      * Overridden for performance reasons.
169      * See the <a href="#override">Implementation Note</a> 
170      * for more information.
171      */
172     @Override
173     public void revalidate() {}
174 
175     /**
176      * Overridden for performance reasons.
177      * See the <a href="#override">Implementation Note</a> 
178      * for more information.
179      */
180     @Override
181     public void repaint(long tm, int x, int y, int width, int height) {}
182 
183     /**
184      * Overridden for performance reasons.
185      * See the <a href="#override">Implementation Note</a> 
186      * for more information.
187      */
188     @Override
189     public void repaint(Rectangle r) { }
190 
191     /**
192      * Overridden for performance reasons.
193      * See the <a href="#override">Implementation Note</a> 
194      * for more information.
195      */
196     @Override
197     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {	
198         // Strings get interned...
199         if (propertyName=="text") {
200             super.firePropertyChange(propertyName, oldValue, newValue);
201         }
202     }
203 
204     /**
205      * Overridden for performance reasons.
206      * See the <a href="#override">Implementation Note</a> 
207      * for more information.
208      */
209     @Override
210     public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
211 
212     /**
213      * Sets the <code>String</code> object for the cell being rendered to
214      * <code>value</code>.
215      * 
216      * @param value  the string value for this cell; if value is
217      *		<code>null</code> it sets the text value to an empty string
218      * @see JLabel#setText
219      * 
220      */
221     protected void setValue(Object value) {
222         setText((value == null) ? "" : value.toString());
223     }
224 }