📄 jpanelvertexrenderer.java
字号:
/**
*
*/
package flow.graph.test;
/**
* @author Administrator
*
*/
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.Serializable;
import java.util.*;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
//import org.joc.gui.graph.*;
/** * This is a JPanel version of CellViewRenderer, instead of the default
*
JLabel * version. The code is borrowed and modified as a seperate extension.
* * This renderer displays entries that implement the CellView interface
* * and supports the following attributes. If the cell view is not a leaf,
* * this object is only visible if it is selected.
* * <li>
* * GraphConstants.BOUNDS
* * GraphConstants.ICON
* * GraphConstants.FONT
* * GraphConstants.OPAQUE
* * GraphConstants.BORDER
* * GraphConstants.BORDERCOLOR
* * GraphConstants.LINEWIDTH
* * GraphConstants.FOREGROUND
* * GraphConstants.BACKGROUND
* * GraphConstants.VERTICAL_ALIGNMENT
* * GraphConstants.HORIZONTAL_ALIGNMENT
* * GraphConstants.VERTICAL_TEXT_POSITION
* * GraphConstants.HORIZONTAL_TEXT_POSITION
* * </li>
* * * @version 1.0 1/1/02 * @author Gaudenz Alder
* */
public class JPanelVertexRenderer extends JPanel implements CellViewRenderer, Serializable{
/** Cache the current graph for drawing. */
transient protected JGraph graph;
transient protected VertexView view;
transient protected boolean hasFocus, selected, preview, opaque, childrenSelected;
transient protected Color defaultForeground, defaultBackground, bordercolor;
transient protected int borderWidth;
transient protected boolean isDoubleBuffered = false;
transient protected Color gradientColor = null;
/** * Constructs a renderer that may be used to render vertices. */
public JPanelVertexRenderer() {
super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
defaultForeground = UIManager.getColor("Tree.textForeground");
defaultBackground = UIManager.getColor("Tree.textBackground");
}
/** * Configure and return the renderer based on the passed in
* * components. The value is typically set from messaging the
*
* * graph with <code>convertValueToString</code>.
* * We recommend you check the value's class and throw an
* * illegal argument exception if it's not correct. *
* * @param graph the graph that that defines the rendering context.
* * @param value the object that should be rendered.
* * @param selected whether the object is selected.
* * @param hasFocus whether the object has the focus.
* * @param isPreview whether we are drawing a preview.
* * @return the component used to render the value. */
public Component getRendererComponent(JGraph graph, CellView view,
boolean sel, boolean focus, boolean preview) {
this.graph = graph;
isDoubleBuffered = graph.isDoubleBuffered();
if (view instanceof VertexView){
this.view = (VertexView)view;
setComponentOrientation(graph.getComponentOrientation());
this.removeAll();
if (graph.getEditingCell() != view.getCell()){
//Object label = new String[]{"11111111111111","2222222222222","333333333333"};//view.toString();//((JGraphForTreeModel)graph).retrieveObject(view);
Object label = view.toString();//((JGraphForTreeModel)graph).retrieveObject(view);
decorateData(label);
}
else{
this.add(new JLabel("Cell is not Editable!"));
}
this.graph = graph;
this.hasFocus = focus;
this.childrenSelected = graph.getSelectionModel().isChildrenSelected(view.getCell());
this.selected = sel;
this.preview = preview;
if (this.view.isLeaf() || GraphConstants.isOpaque(view.getAllAttributes())){
installAttributes(view);
}
else{
setBorder(null);
setOpaque(false);
setGradientColor(null);
}
this.validateTree();
return this;
}
return null;
}
/** * To decorate the userObject data in the JComponent GUI. Subclasses
* * may overwrite this method to customize the GUI.
* * @param jcp JComponent
* * @param userObject Object
* */
public void decorateData(Object userObject) {
String[] obj = (String[])userObject;
if (userObject != null){
JLabel j = new JLabel(obj[0]);
JLabel k = new JLabel(obj[1]);
this.add(k);
this.add(j);
System.out.println("I am here 3333");
//this.add(k);
//setText(label.toString());
}else{
//setText(null);
JLabel j = new JLabel("Nothing");
JLabel k = new JLabel("second");
this.add(k);
this.add(j);
System.out.println("I am here 222");
}
}
/** * Install the attributes of specified cell in this
* * renderer instance. This means, retrieve every published
* * key from the cells hashtable and set global variables
* * or superclass properties accordingly. *
* * @param cell to retrieve the attribute values from.
* */
protected void installAttributes(CellView view) {
Map map = view.getAllAttributes();
setOpaque(GraphConstants.isOpaque(map));
setBorder(GraphConstants.getBorder(map));
bordercolor = GraphConstants.getBorderColor(map);
borderWidth = Math.max(1, Math.round(GraphConstants.getLineWidth(map)));
if (getBorder() == null && bordercolor != null){
setBorder(BorderFactory.createLineBorder(bordercolor, borderWidth));
//setBorder(BorderFactory.createRaisedBevelBorder());
}
Color foreground = GraphConstants.getForeground(map);
foreground = (foreground != null) ? foreground : defaultForeground;
foreground = Color.decode("#000000");
setForeground(foreground);
Color background = GraphConstants.getBackground(map);
background = (background != null) ? background : defaultBackground;
background = Color.decode("#88ff88");
setBackground(background);
Component[] comps = this.getComponents();
if (comps.length > 0){
for (int i=0; i<comps.length; i++){
comps[i].setBackground(background);
}
}
Color gradientColor = GraphConstants.getGradientColor(map);
setGradientColor(gradientColor);
setFont(GraphConstants.getFont(map));
}
/** * Paint the renderer. Overrides superclass paint
* * to add specific painting.
* */
public void paint(Graphics g) {
try
{
if (gradientColor != null && !preview && isOpaque())
{
setOpaque(false);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), getHeight(), gradientColor, true));
g2d.fillRect(0, 0, getWidth(), getHeight());
}
super.paint(g);
paintSelectionBorder(g);
}
catch (IllegalArgumentException e)
{
// JDK Bug: Zero length string passed to TextLayout constructor
}
}
/** * Provided for subclassers to paint a selection border. */
protected void paintSelectionBorder(Graphics g) {
((Graphics2D)g).setStroke(GraphConstants.SELECTION_STROKE);
if (childrenSelected)
{
g.setColor(graph.getGridColor());
}
else if (hasFocus && selected)
{
g.setColor(graph.getLockedHandleColor());
}
else if (selected)
{
g.setColor(graph.getHighlightColor());
}
if (childrenSelected || selected)
{
Dimension d = getSize();
g.drawRect(0, 0, d.width - 1, d.height - 1);
}
}
/** * Returns the intersection of the bounding rectangle and the
* * straight line between the source and the specified point p.
* * The specified point is expected not to intersect the bounds.
* */
public Point2D getPerimeterPoint(VertexView view, Point2D source, Point2D p) {
Rectangle2D bounds = view.getBounds();
double x = bounds.getX();
double y = bounds.getY();
double width = bounds.getWidth();
double height = bounds.getHeight();
double xCenter = x + width / 2;
double yCenter = y + height / 2;
double dx = p.getX() - xCenter;
// Compute Angle
double dy = p.getY() - yCenter;
double alpha = Math.atan2(dy, dx);
double xout = 0, yout = 0;
double pi = Math.PI;
double pi2 = Math.PI / 2.0;
double beta = pi2 - alpha;
double t = Math.atan2(height, width);
if (alpha < -pi + t || alpha > pi - t)
{
// Left edge
xout = x;
yout = yCenter - width * Math.tan(alpha) / 2;
}
else if (alpha < -t)
{
// Top Edge
yout = y;
xout = xCenter - height * Math.tan(beta) / 2;
}
else if (alpha < t)
{
// Right Edge
xout = x + width;
yout = yCenter + width * Math.tan(alpha) / 2;
}
else
{
// Bottom Edge
yout = y + height;
xout = xCenter + height * Math.tan(beta) / 2;
}
return view.getAttributes().createPoint(xout, yout);
}
/** * Overridden for performance reasons.
* * See the <a href="#override">Implementation Note</a>
* * for more information.
* */
// comment out this one, otherwise the jpanel won't be painted
//public void validate() { }
//public void revalidate() { }
public void repaint(long tm, int x, int y, int width, int height) { }
public void repaint(Rectangle r) { }
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
if (propertyName == "text") // Strings get interned...
{
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
public void firePropertyChange(String propertyName, byte oldValue, byte newValue) { }
public void firePropertyChange(String propertyName, char oldValue, char newValue) { }
public void firePropertyChange(String propertyName, short oldValue, short newValue) { }
public void firePropertyChange(String propertyName, int oldValue, int newValue) { }
public void firePropertyChange(String propertyName, long oldValue, long newValue) { }
public void firePropertyChange(String propertyName, float oldValue, float newValue) { }
public void firePropertyChange(String propertyName, double oldValue, double newValue) { }
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
public Color getGradientColor() { return gradientColor; }
public void setGradientColor(Color gradientColor) { this.gradientColor = gradientColor; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -