File: GraphEdMV.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 (144 lines) | stat: -rw-r--r-- 4,175 bytes parent folder | download | duplicates (4)
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
/*
 * @(#)GraphEdMV.java 3.3 23-APR-04
 * 
 * Copyright (c) 2001-2005, Gaudenz Alder All rights reserved.
 * 
 * See LICENSE file in distribution for licensing details of this source file
 */
package com.jgraph.example;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.UndoableEditEvent;

import org.jgraph.event.GraphSelectionListener;
import org.jgraph.example.GraphEd;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.GraphUndoManager;

public class GraphEdMV extends GraphEd
		implements
			GraphSelectionListener,
			KeyListener {

	// Shared Model
	protected static GraphModel model;

	// Undo Manager
	protected static GraphUndoManager undoManager = new GraphUndoManager() {

		public void undoableEditHappened(UndoableEditEvent e) {
			super.undoableEditHappened(e);
			// Then Update Undo/Redo Buttons
			updateAllHistoryButtons();
		}
	};

	protected static ArrayList instances = new ArrayList();

	// Actions which Change State
	protected Action undo, redo, remove, group, ungroup, tofront, toback, cut,
			copy, paste;

	// 
	// Main
	// 
	// Main Method
	public static void main(String[] args) {
		// Switch off D3D because of Sun XOR painting bug
		// See http://www.jgraph.com/forum/viewtopic.php?t=4066
		System.setProperty("sun.java2d.d3d", "false");
		model = new MyModel();
		// Register UndoManager with the Model
		model.addUndoableEditListener(undoManager);
		// Construct Frame
		JFrame frame = new JFrame("GraphEdMV");
		// Set Close Operation to Exit
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// Add an Editor Panel
		GraphEdMV e1 = new GraphEdMV();
		GraphEdMV e2 = new GraphEdMV();
		instances.add(e1);
		instances.add(e2);
		frame.getContentPane().add(
				new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, e1, e2));
		// Fetch URL to Icon Resource
		URL jgraphUrl = GraphEdMV.class.getClassLoader().getResource("org/jgraph/example/resources/jgraph.gif");
		// If Valid URL
		if (jgraphUrl != null) {
			// Load Icon
			ImageIcon jgraphIcon = new ImageIcon(jgraphUrl);
			// Use in Window
			frame.setIconImage(jgraphIcon.getImage());
		}
		// Set Default Size
		frame.pack(); //setSize(520, 390);
		frame.setSize(new Dimension(frame.getWidth(), 400));
		// Show Frame
		frame.setVisible(true);
	}

	// 
	// Editor Panel
	// 
	// Construct an Editor Panel
	public GraphEdMV() {
		// Use Border Layout
		getContentPane().setLayout(new BorderLayout());
		// Construct the Graph
		graph = new MyGraph(model);
		// Use a Custom Marquee Handler
		graph.setMarqueeHandler(new MyMarqueeHandler());
		
		// Define the set of "view-local" attributes
		Set localAttributes = new HashSet();
		localAttributes.add(GraphConstants.BOUNDS);
		localAttributes.add(GraphConstants.POINTS);
		localAttributes.add(GraphConstants.LABELPOSITION);
		localAttributes.add(GraphConstants.ROUTING);
		graph.getGraphLayoutCache().setLocalAttributes(localAttributes);
		// 
		// Add Listeners to Graph
		// 
		// Update ToolBar based on Selection Changes
		graph.getSelectionModel().addGraphSelectionListener(this);
		// Listen for Delete Keystroke when the Graph has Focus
		graph.addKeyListener(this);
		// Construct Panel
		// 
		// Add a ToolBar
		getContentPane().add(createToolBar(), BorderLayout.NORTH);
		// Add the Graph as Center Component
		getContentPane().add(new JScrollPane(graph), BorderLayout.CENTER);
	}

	// Create a Group that Contains the Cells
	public void group(Object[] cells) {
		// Invert Array for Model ordering
		Object[] tmp = new Object[cells.length];
		for (int i = 0; i < cells.length; i++)
			tmp[cells.length - i - 1] = cells[i];
		cells = tmp;
		super.group(tmp);
	}

	protected static void updateAllHistoryButtons() {
		Iterator it = instances.iterator();
		while (it.hasNext())
			((GraphEdMV) it.next()).updateHistoryButtons();
	}

}