File: FastGraphUI.java

package info (click to toggle)
libjgraph-java 5.12.4.2%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 7,888 kB
  • sloc: java: 20,619; xml: 135; makefile: 8
file content (128 lines) | stat: -rw-r--r-- 4,125 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2005, David Benson
 *
 * See LICENSE file in distribution for licensing details of this source file
 */
package com.jgraph.example.fastgraph;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.jgraph.graph.CellView;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphConstants;
import org.jgraph.plaf.basic.BasicGraphUI;

public class FastGraphUI extends BasicGraphUI {
	/**
	 * Paints the renderer of <code>view</code> to <code>g</code> at
	 * <code>bounds</code>. Recursive implementation that paints the children
	 * first.
	 * <p>
	 * The reciever should NOT modify <code>clipBounds</code>, or
	 * <code>insets</code>. The <code>preview</code> flag is passed to the
	 * renderer, and is not used here.
	 */
	public void paintCell(Graphics g, CellView view, Rectangle2D bounds,
			boolean preview) {
		if (view != null && view instanceof FastEdgeView) {
			// First Paint View
			if (bounds != null) {
				boolean selected = graph.isCellSelected(view.getCell());
				Graphics2D g2 = (Graphics2D) g;
				Stroke stroke = g2.getStroke();
				Object rendererHint = g2
						.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);

				g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
						RenderingHints.VALUE_STROKE_PURE);
				int c = BasicStroke.CAP_BUTT;
				int j = BasicStroke.JOIN_MITER;
				float lineWidth = 1.0f;
				g2.setStroke(new BasicStroke(lineWidth, c, j));
				// TODO
				g2.setColor(Color.BLACK);//(getForeground());
				int n = ((EdgeView)view).getPointCount();
				Point2D lastPoint = null;
				for (int i = 0; i < n; i++) {
					Point2D currentPoint = ((EdgeView) view).getPoint(i);
					if (i != 0) {
						g2.drawLine((int) lastPoint.getX(), (int) lastPoint.getY(),
								(int) currentPoint.getX(),
								(int) currentPoint.getY());
					}
					lastPoint = currentPoint;
				}
				if (selected) { // Paint Selected
					g2.setStroke(GraphConstants.SELECTION_STROKE);
					g2.setColor(graph.getHighlightColor());
					for (int i = 0; i < n; i++) {
						Point2D currentPoint = ((EdgeView) view).getPoint(i);
						if (i != 0) {
							g2.drawLine((int) lastPoint.getX(), (int) lastPoint.getY(),
									(int) currentPoint.getX(),
									(int) currentPoint.getY());
						}
						lastPoint = currentPoint;
					}
				}
				g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, rendererHint);
				g2.setStroke(stroke);
			}
		} else {
			super.paintCell(g, view, bounds, preview);
		}
	}

	/**
	 * Updates the <code>preferredSize</code> instance variable, which is
	 * returned from <code>getPreferredSize()</code>. Ignores edges for
	 * performance
	 */
	protected void updateCachedPreferredSize() {
		CellView[] views = graphLayoutCache.getRoots();
		Rectangle2D size = null;
		if (views != null && views.length > 0) {
			for (int i = 0; i < views.length; i++) {
				if (views[i] != null && !(views[i] instanceof FastEdgeView)) {
					Rectangle2D r = views[i].getBounds();
					if (r != null) {
						if (size == null)
							size = new Rectangle2D.Double(r.getX(), r.getY(), r
									.getWidth(), r.getHeight());
						else
							Rectangle2D.union(size, r, size);
					}
				}
			}
		}
		if (size == null)
			size = new Rectangle2D.Double();
		Point2D psize = new Point2D.Double(size.getX() + size.getWidth(), size
				.getY()
				+ size.getHeight());
		Dimension d = graph.getMinimumSize();
		Point2D min = (d != null) ? graph
				.toScreen(new Point(d.width, d.height)) : new Point(0, 0);
		Point2D scaled = graph.toScreen(psize);
		preferredSize = new Dimension(
				(int) Math.max(min.getX(), scaled.getX()), (int) Math.max(min
						.getY(), scaled.getY()));
		Insets in = graph.getInsets();
		if (in != null) {
			preferredSize.setSize(
					preferredSize.getWidth() + in.left + in.right,
					preferredSize.getHeight() + in.top + in.bottom);
		}
		validCachedPreferredSize = true;
	}
}