View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors
3    * All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE 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 an instance with the specified properties.
47       * 
48       * @param pickedEdgeLabelColor the color to use for rendering the labels of picked edges
49       * @param rotateEdgeLabels whether the 
50       */
51      public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor, boolean rotateEdgeLabels) {
52          super();
53          this.pickedEdgeLabelColor = pickedEdgeLabelColor;
54          this.rotateEdgeLabels = rotateEdgeLabels;
55          setOpaque(true);
56          setBorder(noFocusBorder);
57      }
58  
59      /**
60       * @return Returns the rotateEdgeLabels.
61       */
62      public boolean isRotateEdgeLabels() {
63          return rotateEdgeLabels;
64      }
65      /**
66       * @param rotateEdgeLabels The rotateEdgeLabels to set.
67       */
68      public void setRotateEdgeLabels(boolean rotateEdgeLabels) {
69          this.rotateEdgeLabels = rotateEdgeLabels;
70      }
71      /**
72       * Overrides <code>JComponent.setForeground</code> to assign
73       * the unselected-foreground color to the specified color.
74       * 
75       * @param c set the foreground color to this value
76       */
77      @Override
78      public void setForeground(Color c) {
79          super.setForeground(c); 
80      }
81      
82      /**
83       * Overrides <code>JComponent.setBackground</code> to assign
84       * the unselected-background color to the specified color.
85       *
86       * @param c set the background color to this value
87       */
88      @Override
89      public void setBackground(Color c) {
90          super.setBackground(c); 
91      }
92  
93      /**
94       * Notification from the <code>UIManager</code> that the look and feel
95       * has changed.
96       * Replaces the current UI object with the latest version from the 
97       * <code>UIManager</code>.
98       *
99       * @see JComponent#updateUI
100      */
101     @Override
102     public void updateUI() {
103         super.updateUI(); 
104         setForeground(null);
105         setBackground(null);
106     }
107     
108     /**
109     *
110     * Returns the default label renderer for an Edge
111     *
112     * @param vv  the <code>VisualizationViewer</code> to render on
113     * @param value  the value to assign to the label for
114     *			<code>Edge</code>
115     * @param edge  the <code>Edge</code>
116     * @return the default label renderer
117     */
118     public <E> Component getEdgeLabelRendererComponent(JComponent vv, Object value,
119             Font font, boolean isSelected, E edge) {
120         
121         super.setForeground(vv.getForeground());
122         if(isSelected) setForeground(pickedEdgeLabelColor);
123         super.setBackground(vv.getBackground());
124         
125         if(font != null) {
126             setFont(font);
127         } else {
128             setFont(vv.getFont());
129         }
130         setIcon(null);
131         setBorder(noFocusBorder);
132         setValue(value); 
133         return this;
134     }
135     
136     /*
137      * <bold id="override">Implementation Note</bold>
138      * The following methods are overridden as a performance measure to 
139      * prune code-paths that are often called in the case of renders
140      * but which we know are unnecessary.  Great care should be taken
141      * when writing your own renderer to weigh the benefits and 
142      * drawbacks of overriding methods like these.
143      */
144 
145     /**
146      * Overridden for performance reasons.
147      * See the <a href="#override">Implementation Note</a> 
148      * for more information.
149      */
150     @Override
151     public boolean isOpaque() { 
152         Color back = getBackground();
153         Component p = getParent(); 
154         if (p != null) { 
155             p = p.getParent(); 
156         }
157         boolean colorMatch =
158         		(back != null) && (p != null)
159         		&& back.equals(p.getBackground()) && p.isOpaque();
160         return !colorMatch && super.isOpaque(); 
161     }
162 
163     /**
164      * Overridden for performance reasons.
165      * See the <a href="#override">Implementation Note</a> 
166      * for more information.
167      */
168     @Override
169     public void validate() {}
170 
171     /**
172      * Overridden for performance reasons.
173      * See the <a href="#override">Implementation Note</a> 
174      * for more information.
175      */
176     @Override
177     public void revalidate() {}
178 
179     /**
180      * Overridden for performance reasons.
181      * See the <a href="#override">Implementation Note</a> 
182      * for more information.
183      */
184     @Override
185     public void repaint(long tm, int x, int y, int width, int height) {}
186 
187     /**
188      * Overridden for performance reasons.
189      * See the <a href="#override">Implementation Note</a> 
190      * for more information.
191      */
192     @Override
193     public void repaint(Rectangle r) { }
194 
195     /**
196      * Overridden for performance reasons.
197      * See the <a href="#override">Implementation Note</a> 
198      * for more information.
199      */
200     @Override
201     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {	
202         // Strings get interned...
203         if (propertyName=="text") {
204             super.firePropertyChange(propertyName, oldValue, newValue);
205         }
206     }
207 
208     /**
209      * Overridden for performance reasons.
210      * See the <a href="#override">Implementation Note</a> 
211      * for more information.
212      */
213     @Override
214     public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
215 
216     /**
217      * Sets the <code>String</code> object for the cell being rendered to
218      * <code>value</code>.
219      * 
220      * @param value  the string value for this cell; if value is
221      *		<code>null</code> it sets the text value to an empty string
222      * @see JLabel#setText
223      * 
224      */
225     protected void setValue(Object value) {
226         setText((value == null) ? "" : value.toString());
227     }
228 }