File: FastGraphModel.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 (106 lines) | stat: -rw-r--r-- 3,287 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
/*
 * Copyright (c) 2001-2005, Gaudenz Alder
 * Copyright (c) 2005, David Benson
 * 
 * See LICENSE file in distribution for licensing details of this source file
 */
package com.jgraph.example.fastgraph;

import java.util.Iterator;
import java.util.Map;

import javax.swing.undo.UndoableEdit;

import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.Edge;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.ParentMap;

// A Custom Model that does not allow Self-References
public class FastGraphModel extends DefaultGraphModel {
	
	/**
	 * A flag indicating whether or not undos are currently disabled
	 */
	protected boolean undoDisabled = true;
	
	// Override Superclass Method
	public boolean acceptsSource(Object edge, Object port) {
		// Source only Valid if not Equal Target
		return (((Edge) edge).getTarget() != port);
	}

	// Override Superclass Method
	public boolean acceptsTarget(Object edge, Object port) {
		// Target only Valid if not Equal Source
		return (((Edge) edge).getSource() != port);
	}

	/**
	 * Extends DefaultGraphModel insert method to check for large changes and
	 * sets the <code>undoDisabled</code> flag to true. Disabling the undo
	 * provides large performance and memory footprint advantages on huge
	 * inserts.
	 */
	public void insert(Object[] roots, Map attributes, ConnectionSet cs,
			ParentMap pm, UndoableEdit[] edits) {
//		System.out.println("Model insert starting, timestamp="
//				+ System.currentTimeMillis() + "ms");
		GraphModelEdit edit = createEdit(roots, null, attributes, cs, pm, edits);
		if (edit != null) {
			edit.execute(); // fires graphChangeEvent
			if (edits != null) {
				for (int i = 0; i < edits.length; i++)
					if (edits[i] instanceof GraphLayoutCache.GraphLayoutCacheEdit)
						((GraphLayoutCache.GraphLayoutCacheEdit) edits[i])
								.execute();
			}
			if (!undoDisabled) {
				postEdit(edit); // fires undoableedithappened
			}
		}
//		System.out.println("Model insert finished, timestamp="
//				+ System.currentTimeMillis() + "ms");
	}

	public void edit(Map attributes, ConnectionSet cs, ParentMap pm,
			UndoableEdit[] edits) {
//		System.out.println("Model edit starting, timestamp="
//				+ System.currentTimeMillis() + "ms");
		super.edit(attributes, cs, pm, edits);
//		System.out.println("Model edit finished, timestamp="
//				+ System.currentTimeMillis() + "ms");
	}

	/**
	 * Applies <code>attributes</code> to the cells specified as keys. Returns
	 * the <code>attributes</code> to undo the change.
	 */
	protected Map handleAttributes(Map attributes) {
		if (!undoDisabled) {
			return super.handleAttributes(attributes);
		}
		if (attributes != null) {
			Iterator it = attributes.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				Object cell = entry.getKey();
				Map deltaNew = (Map) entry.getValue();
				AttributeMap attr = getAttributes(cell);
				
				if (attr != null) {
					attr.applyMap(deltaNew);
				}
				// Handle new values
				Object newValue = deltaNew.get(GraphConstants.VALUE);
				if (newValue != null) {
					valueForCellChanged(cell, newValue);
				}
			}
		}
		return null;
	}
}