File: PortLabelVertexView.java

package info (click to toggle)
libjgraph-java 5.12.4.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 7,884 kB
  • ctags: 7,321
  • sloc: java: 20,619; xml: 135; sh: 51; makefile: 10
file content (394 lines) | stat: -rw-r--r-- 12,748 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Copyright (c) 2005-2006, David Benson
 * 
 * See LICENSE file in distribution for licensing details of this source file
 */
package com.jgraph.example.portlabels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;

/**
 * 
 */
public class PortLabelVertexView extends VertexView {

	/**
	 * the renderer for this view
	 */
	protected static WrapperPortLabelRenderer renderer = new WrapperPortLabelRenderer();

	protected CellView[] ports;

	/**
	 * Creates new instance of <code>InstanceView</code>.
	 */
	public PortLabelVertexView() {
		super();
	}

	/**
	 * Creates new instance of <code>InstanceView</code> for the specified
	 * graph cell.
	 * 
	 * @param arg0
	 *            a graph cell to create view for
	 */
	public PortLabelVertexView(Object arg0) {
		super(arg0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jgraph.graph.AbstractCellView#getRenderer()
	 */
	public CellViewRenderer getRenderer() {
		return renderer;
	}

	public static class WrapperPortLabelRenderer extends JPanel implements
			CellViewRenderer {

		private transient static JLabel label = new JLabel();

		private transient static PortLabelVertexRenderer portLabelRenderer = new PortLabelVertexRenderer();
		
		/**
		 * Cache the current graph for drawing
		 */
		protected transient JGraph graph = null;

		transient protected Color gradientColor = null;
		
		/** Cached hasFocus and selected value. */
		transient protected boolean hasFocus, selected, preview;

		/** Cached default foreground and default background. */
		transient protected Color defaultForeground, defaultBackground;

		/**
		 * Constructs a renderer that may be used to render vertices.
		 */
		public WrapperPortLabelRenderer() {
			super(new BorderLayout());
			this.add(portLabelRenderer, BorderLayout.CENTER);
			this.add(label, BorderLayout.SOUTH);
			defaultForeground = UIManager.getColor("Tree.textForeground");
			defaultBackground = UIManager.getColor("Tree.textBackground");
			label.setOpaque(false);

		}

		public Component getRendererComponent(JGraph graph, CellView view,
				boolean sel, boolean focus, boolean preview) {
			portLabelRenderer.getRendererComponent(graph, view, sel, focus,
					preview);
			label.setText(view.getCell().toString());
			this.graph = graph;
			this.selected = sel;
			this.preview = preview;
			this.hasFocus = focus;
			installAttributes(view);
			return this;
		}

		public void paint(Graphics g) {
			if (gradientColor != null && !preview) {
				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);
		}

		public Point2D getPerimeterPoint(VertexView view, Point2D source, Point2D p) {
			return portLabelRenderer.getPerimeterPoint(view, source, p);
		}

		protected void installAttributes(CellView view) {
			Map attributes = view.getAllAttributes();
			label.setVerticalAlignment(GraphConstants.getVerticalAlignment(attributes));
			label.setHorizontalAlignment(GraphConstants.getHorizontalAlignment(attributes));
			label.setVerticalTextPosition(GraphConstants.getVerticalTextPosition(attributes));
			label.setHorizontalTextPosition(GraphConstants.getHorizontalTextPosition(attributes));
			label.setFont(GraphConstants.getFont(attributes));
			Color foreground = GraphConstants.getForeground(attributes);
			label.setForeground((foreground != null) ? foreground : defaultForeground);
			portLabelRenderer.installAttributes(view);
			Color background = null;
			if (graph != null) {
				background = graph.getBackground();
			}
			setBackground((background != null) ? background : defaultBackground);
		}
}

	/**
	 * The renderer class for instance view.
	 */
	public static class PortLabelVertexRenderer extends VertexRenderer {

		protected CellView[] ports = null;

		protected FontMetrics fontMetrics = null;

		public static transient int PORTLABELSPACING = 8;

		public static transient int MINIMUMHORIZONTALSPACING = 60;

		public static transient int MINIMUMVERTICALSPACING = 40;

		/**
		 * The vertical distance added to the label height to make the color backdrop extend
		 * beyond the label text
		 */
		public static transient int COLORHEIGHTBUFFER = 4;

		/**
		 * The maximum width of a label, any label more than this value in
		 * width  
		 */
		public static transient int MAXLABELWIDTH = 150;
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see java.awt.Component#paint(java.awt.Graphics)
		 */
		public void paint(Graphics g) {
			super.paint(g);
			g.setColor(getForeground());
			paintPortLabels(g);
		}
		
		public Component getRendererComponent(JGraph graph, CellView view,
				boolean sel, boolean focus, boolean preview) {
			// Do not display the label, the wrapper does this
			setText(null);
			// Finds the ports and install them into the renderer
			Graphics2D g = (Graphics2D) graph.getGraphics();
			fontMetrics = g.getFontMetrics();
			Object cell = view.getCell();
			GraphModel model = graph.getModel();
			int childCount = model.getChildCount(cell);
			List result = new ArrayList(childCount);
			for (int i = 0; i < childCount; i++) {
				Object child = model.getChild(cell, i);
				if (model.isPort(child)) {
					CellView portView = graph.getGraphLayoutCache().getMapping(
							child, false);
					if (portView != null) {
						result.add(portView);
					}
				}
			}

			ports = new CellView[result.size()];
			result.toArray(ports);
			Component c = super.getRendererComponent(graph, view, sel, focus, preview);
			// Do not display the label, the wrapper does this
			setText(null);
			return c;
		}

		/**
		 * 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 view
		 *            the cell view to retrieve the attribute values from.
		 */
		protected void installAttributes(CellView view) {
			Map map = view.getAllAttributes();
			setIcon(GraphConstants.getIcon(map));
			setOpaque(GraphConstants.isOpaque(map));
			setBorder(GraphConstants.getBorder(map));
			setVerticalAlignment(GraphConstants.getVerticalAlignment(map));
			setHorizontalAlignment(GraphConstants.getHorizontalAlignment(map));
			bordercolor = GraphConstants.getBorderColor(map);
			borderWidth = Math.max(1, Math.round(GraphConstants.getLineWidth(map)));
			if (getBorder() == null && bordercolor != null)
				setBorder(BorderFactory.createLineBorder(bordercolor, borderWidth));
			Color gradientColor = GraphConstants.getGradientColor(map);
			setGradientColor(gradientColor);
			Color background = GraphConstants.getBackground(map);
			setBackground((background != null) ? background : defaultBackground);
			setFont(GraphConstants.getFont(map));
		}

		/**
		 * Draws a <code>String</code>. Its horizontal position
		 * <code>x</code> is given and its vertical position is centered on
		 * given <code>y</code>.
		 * 
		 * @param g
		 *            a <code>Graphics2D</code> to draw with
		 * @param label
		 *            a <code>String</code> to draw
		 * @param x
		 *            an offset to left edge of the bounding box of vertex
		 * @param y
		 *            an offset to center of the string
		 * @param background
		 *            the background color, if any, behind the text
		 * @param isLeftLabel
		 *            whether or not this label is on the left-hand side of the vertex
		 */
		public static void drawPortLabel(Graphics g, String label, double x,
				double y, Color background, boolean isLeftLabel) {
			FontMetrics metrics = g.getFontMetrics();
			int sw = metrics.stringWidth(label);
			// If string is longer than allowed, concat it, replacing excess
			// with "..."
			int tries = 0;
			if (sw > MAXLABELWIDTH) {
				int dotLength = metrics.stringWidth("...");
				while (sw > MAXLABELWIDTH && tries < 5) {
					int characters = label.length();
					double stringRatio = (double)(MAXLABELWIDTH-dotLength) / (double)sw;
					int newStringLength = (int)((double)characters * stringRatio);
					// Shorten string with each try
					newStringLength -= tries;
					if (newStringLength < characters) {
						String newLabel = label.substring(0, newStringLength);
						newLabel += "...";
						int newStringWidth = metrics.stringWidth(newLabel);
						if (newStringWidth < MAXLABELWIDTH) {
							label = newLabel;
							sw = newStringWidth;
						}
					}
					tries++;
				}
			}
			int sh = metrics.getHeight();
			int offsetX = 0;
			if (isLeftLabel) {
				offsetX = PORTLABELSPACING;
			} else {
				offsetX = -sw - PORTLABELSPACING;
			}
			// Draw coloured rectangle is colour is set
			if (background != null) {
				Color oldColor = g.getColor();
				g.setColor(background);
				g.fillRect((int) x + offsetX - PORTLABELSPACING,
						(int) (y - COLORHEIGHTBUFFER*2), sw + (PORTLABELSPACING * 2), sh + COLORHEIGHTBUFFER*2);
				g.setColor(oldColor);
			}
//			int stringWidth = Math.min((int)x + offsetX, MAXLABELWIDTH);
			g.drawString(label, (int) x + offsetX, (int) (y + sh / 2));
		}

		/**
		 * Paints port labels the view.
		 * 
		 * @param g
		 *            a <code>Graphics2D</code> to draw with
		 */
		public void paintPortLabels(Graphics g) {
			if (ports != null && ports.length > 0) {
				CellView parentView = ports[0].getParentView();
				Rectangle2D bounds = GraphConstants.getBounds(parentView
						.getAllAttributes());
				// Get the bounds of the vertex and deduct twice the cell label
				// height plus the vertical buffer distance from it.
				double height = bounds.getHeight();
				Color oldBackground = getBackground();
				for (int i = 0; i < ports.length; i++) {
					String labelValue = ports[i].getCell().toString();
					if (labelValue == null) {
						labelValue = new String();
					}
					Point2D portOffset = GraphConstants.getOffset(ports[i]
							.getAllAttributes());
					Color portBackground = GraphConstants.getBackground(ports[i]
							.getAllAttributes());
					double x = 0;
					double y = 0;
					if (portOffset != null) {
						if (bounds != null) {
							// By x position should be 0 or the width of the
							// vertex
							x = portOffset.getX() * bounds.getWidth()
									/ GraphConstants.PERMILLE;
							// The y position is the proportion of the vertex
							// height available for port label. Remember a bit
							// is reserved either end for the vertex label.
							y = portOffset.getY() * height
									/ GraphConstants.PERMILLE;
							if (portOffset.getX() == 0) {
								drawPortLabel(g, labelValue, x, y, portBackground, true);
							} else if (portOffset.getX() == GraphConstants.PERMILLE) {
								drawPortLabel(g, labelValue, x, y, portBackground, false);
							}
						}
					}
				}
				setBackground(oldBackground);
			}
		}

		public Dimension getPreferredSize() {
			Dimension vertexPreferredSize = super.getPreferredSize();
			if (ports != null && fontMetrics != null) {
				int heightPerLabel = fontMetrics.getHeight()
						+ MINIMUMVERTICALSPACING;
				double left = 0, right = 0;
				double maxLeft = 0, maxRight = 0;
				for (int i = 0; i < ports.length; i++) {
					String labelValue = ports[i].getCell().toString();
					if (labelValue == null) {
						labelValue = new String();
					}
					Point2D portOffset = GraphConstants.getOffset(ports[i]
							.getAllAttributes());
					int sw = fontMetrics.stringWidth(labelValue);
					// Limit the size of labels to MAXLABELWIDTH
					if (sw > MAXLABELWIDTH) sw = MAXLABELWIDTH;
					if (portOffset != null) {
						if (portOffset.getX() == 0) {
							left += heightPerLabel;
							maxLeft = Math.max(maxLeft, sw);
						} else if (portOffset.getX() == GraphConstants.PERMILLE) {
							right += heightPerLabel;
							maxRight = Math.max(maxRight, sw);
						}
					}
				}
				int maxX = Math.max((int) vertexPreferredSize.getWidth(),
						(int) (maxLeft + maxRight + MINIMUMHORIZONTALSPACING));
				return new Dimension(maxX, (int) Math.max(left, right));
			}
			return vertexPreferredSize;
		}
	}
}