File: GraphEdXMenuBar.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 (110 lines) | stat: -rw-r--r-- 3,059 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
/*
 * $Id: GraphEdXMenuBar.java,v 1.5 2005/12/07 19:44:09 gaudenz Exp $
 * Copyright (c) 2001-2005, Gaudenz Alder
 * 
 * All rights reserved. 
 * 
 * This file is licensed under the JGraph software license, a copy of which
 * will have been provided to you in the file LICENSE at the root of your
 * installation directory. If you are unable to locate this file please
 * contact JGraph sales for another copy.
 */
package com.jgraph.example;

import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

/**
 * A simple menu bar
 */
public class GraphEdXMenuBar extends JMenuBar {

	/**
	 * JGraph Factory instance for random new graphs
	 */
	protected JGraphGraphFactory graphFactory = null;

	public GraphEdXMenuBar(final GraphEdX app, JGraphGraphFactory factory) {
		graphFactory = factory;

		// Sample data menu
		JMenu fileMenu = new JMenu("File");
		fileMenu.add(new JMenuItem(new AbstractAction("Open") {
			public void actionPerformed(ActionEvent e) {
				app.openFile();
			}
		}));

		fileMenu.add(new JMenuItem(new AbstractAction("Save") {
			public void actionPerformed(ActionEvent e) {
				app.saveFile();
			}
		}));

		fileMenu.addSeparator();
		fileMenu.add(new JMenuItem(new AbstractAction("Deserialize") {
			public void actionPerformed(ActionEvent e) {
				app.deserializeGraph();
			}
		}));

		fileMenu.add(new JMenuItem(new AbstractAction("Serialize") {
			public void actionPerformed(ActionEvent e) {
				app.serializeGraph();
			}
		}));

		add(fileMenu);

		// Sample data menu
		JMenu sampleMenu = new JMenu("Sample Data");
		sampleMenu.add(new JMenuItem(new AbstractAction("Insert Random Tree") {
			public void actionPerformed(ActionEvent e) {
				graphFactory.insertGraph(app.getGraph(),
						JGraphGraphFactory.TREE,
						app.createCellAttributes(new Point2D.Double(0, 0)), app
								.createEdgeAttributes());
			}
		}));

		sampleMenu.add(new JMenuItem(new AbstractAction("Insert Random Graph") {
			public void actionPerformed(ActionEvent e) {
				graphFactory.insertGraph(app.getGraph(),
						JGraphGraphFactory.RANDOM_CONNECTED,
						app.createCellAttributes(new Point2D.Double(0, 0)), app
								.createEdgeAttributes());
			}
		}));

		add(sampleMenu);
	}

	/**
	 * helper for creating radio button menu items
	 * 
	 * @param group
	 *            the <code>ButtonGroup</code> of the item
	 * @param action
	 *            the <code>Action</code> associated with the item
	 * @return the menu item
	 */
	public JRadioButtonMenuItem createRadioMenuItem(ButtonGroup group,
			Action action) {
		JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(action);
		menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl shift "
				+ String.valueOf(action.getValue("shortcut")).substring(0, 1)
						.toUpperCase()));
		group.add(menuItem);
		return menuItem;
	}

}