View | Details | Raw Unified | Return to bug 283244
Collapse All | Expand All

(-)src/org/eclipse/zest/tests/swt/PaintSnippet.java (-4 / +5 lines)
Lines 1-11 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
9
 ******************************************************************************/
11
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
12
package org.eclipse.zest.tests.swt;
11
13
Lines 13-19 Link Here
13
import org.eclipse.zest.core.widgets.Graph;
15
import org.eclipse.zest.core.widgets.Graph;
14
import org.eclipse.zest.core.widgets.GraphConnection;
16
import org.eclipse.zest.core.widgets.GraphConnection;
15
import org.eclipse.zest.core.widgets.GraphNode;
17
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.layouts.LayoutStyles;
17
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.PaintEvent;
20
import org.eclipse.swt.events.PaintEvent;
Lines 66-72 Link Here
66
		new GraphConnection(g, SWT.NONE, n, n2);
67
		new GraphConnection(g, SWT.NONE, n, n2);
67
		new GraphConnection(g, SWT.NONE, n2, n3);
68
		new GraphConnection(g, SWT.NONE, n2, n3);
68
		new GraphConnection(g, SWT.NONE, n3, n);
69
		new GraphConnection(g, SWT.NONE, n3, n);
69
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
70
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
70
71
71
		b.addSelectionListener(new SelectionListener() {
72
		b.addSelectionListener(new SelectionListener() {
72
73
(-)src/org/eclipse/zest/tests/swt/GraphSnippet7.java (-71 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.zest.core.widgets.Graph;
13
import org.eclipse.zest.core.widgets.GraphConnection;
14
import org.eclipse.zest.core.widgets.GraphNode;
15
import org.eclipse.zest.layouts.LayoutStyles;
16
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.MouseEvent;
19
import org.eclipse.swt.events.MouseMoveListener;
20
import org.eclipse.swt.layout.FillLayout;
21
import org.eclipse.swt.widgets.Display;
22
import org.eclipse.swt.widgets.Shell;
23
24
/**
25
 * This snippet shows how to use the findFigureAt to get the figure under the mouse
26
 * 
27
 * @author Ian Bull
28
 * 
29
 */
30
public class GraphSnippet7 {
31
32
	/**
33
	 * @param args
34
	 */
35
	public static void main(String[] args) {
36
		Display d = new Display();
37
		Shell shell = new Shell(d);
38
		shell.setText("GraphSnippet7");
39
		shell.setLayout(new FillLayout());
40
		shell.setSize(400, 400);
41
42
		final Graph g = new Graph(shell, SWT.NONE);
43
44
		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
45
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
46
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
47
		new GraphConnection(g, SWT.NONE, n, n2);
48
		new GraphConnection(g, SWT.NONE, n2, n3);
49
		new GraphConnection(g, SWT.NONE, n3, n);
50
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
51
		
52
		g.addMouseMoveListener(new MouseMoveListener() {
53
54
			public void mouseMove(MouseEvent e) {
55
				// Get the figure at the current mouse location 
56
				Object o = g.getFigureAt(e.x, e.y);
57
				if ( o != null ) {
58
					System.out.println(o + " is at: (" + e.x + "," + e.y + ")");
59
				}
60
			}
61
			
62
		});
63
64
		shell.open();
65
		while (!shell.isDisposed()) {
66
			while (!d.readAndDispatch()) {
67
				d.sleep();
68
			}
69
		}
70
	}
71
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet12.java (-164 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import java.util.Iterator;
13
14
import org.eclipse.draw2d.ColorConstants;
15
import org.eclipse.draw2d.Ellipse;
16
import org.eclipse.draw2d.Figure;
17
import org.eclipse.draw2d.FreeformLayout;
18
import org.eclipse.draw2d.IFigure;
19
import org.eclipse.draw2d.ImageFigure;
20
import org.eclipse.draw2d.PolylineShape;
21
import org.eclipse.draw2d.geometry.Point;
22
import org.eclipse.draw2d.geometry.Rectangle;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.events.SelectionListener;
26
import org.eclipse.swt.graphics.Image;
27
import org.eclipse.swt.layout.FillLayout;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Shell;
30
import org.eclipse.zest.core.widgets.CGraphNode;
31
import org.eclipse.zest.core.widgets.Graph;
32
import org.eclipse.zest.core.widgets.GraphConnection;
33
import org.eclipse.zest.core.widgets.GraphNode;
34
import org.eclipse.zest.layouts.LayoutStyles;
35
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
36
37
/**
38
 * 
39
 * This snippet shows how to create a curved connection using Zest.
40
 * 
41
 * @author Ian Bull
42
 * 
43
 */
44
public class GraphSnippet12 {
45
46
	public static IFigure createPersonFigure(Image headImage) {
47
		Figure person = new Figure();
48
		person.setLayoutManager(new FreeformLayout());
49
		IFigure head = null;
50
		if ( headImage != null ) {
51
			headImage = new Image(headImage.getDevice(), headImage.getImageData().scaledTo(40, 50));
52
			head = new ImageFigure(headImage);
53
		}
54
		else
55
			head = new Ellipse();
56
		head.setSize(40, 50);
57
		
58
		PolylineShape body = new PolylineShape();
59
		body.setLineWidth(1);
60
		body.setStart(new Point(20,40));
61
		body.setEnd(new Point(20,100));
62
		body.setBounds(new Rectangle(0,0,40,100));
63
		
64
		PolylineShape leftLeg = new PolylineShape();
65
		leftLeg.setLineWidth(1);
66
		leftLeg.setStart(new Point(20,100));
67
		leftLeg.setEnd(new Point(0,130));
68
		leftLeg.setBounds(new Rectangle(0,0,40,130));
69
		
70
		PolylineShape rightLeg = new PolylineShape();
71
		rightLeg.setLineWidth(1);
72
		rightLeg.setStart(new Point(20,100));
73
		rightLeg.setEnd(new Point(40,130));
74
		rightLeg.setBounds(new Rectangle(0,0,40,130));
75
		
76
		PolylineShape leftArm = new PolylineShape();
77
		leftArm.setLineWidth(1);
78
		leftArm.setStart(new Point(20,60));
79
		leftArm.setEnd(new Point(0,50));
80
		leftArm.setBounds(new Rectangle(0,0,40,130));
81
		
82
		PolylineShape rightArm = new PolylineShape();
83
		rightArm.setLineWidth(1);
84
		rightArm.setStart(new Point(20,60));
85
		rightArm.setEnd(new Point(40,50));
86
		rightArm.setBounds(new Rectangle(0,0,40,130));
87
		
88
		person.add(head);
89
		person.add(body);
90
		person.add(leftLeg);
91
		person.add(rightLeg);
92
		person.add(leftArm);
93
		person.add(rightArm);
94
		person.setSize(40,130);
95
		return person;
96
	}
97
98
	public static void main(String[] args) {
99
		final Display d = new Display();
100
		Shell shell = new Shell(d);
101
		shell.setText("GraphSnippet11");
102
		shell.setLayout(new FillLayout());
103
		shell.setSize(400, 400);
104
105
		
106
		final Graph g = new Graph(shell, SWT.NONE);
107
		g.addSelectionListener(new SelectionListener(){
108
		
109
			public void widgetSelected(SelectionEvent e) {
110
				Iterator iter = g.getSelection().iterator();
111
				while(iter.hasNext()) {
112
					Object o = iter.next();
113
					if ( o instanceof CGraphNode) {
114
						IFigure figure = ((CGraphNode)o).getFigure();
115
						figure.setBackgroundColor(ColorConstants.blue);
116
						figure.setForegroundColor(ColorConstants.blue);
117
					}
118
				}
119
				iter = g.getNodes().iterator();
120
				while ( iter.hasNext()) {
121
					Object o = iter.next();
122
					if ( o instanceof CGraphNode) {
123
						if ( !g.getSelection().contains(o)) {
124
							((CGraphNode)o).getFigure().setBackgroundColor(ColorConstants.black);
125
							((CGraphNode)o).getFigure().setForegroundColor(ColorConstants.black);
126
						}
127
					}
128
				}
129
			}
130
		
131
			public void widgetDefaultSelected(SelectionEvent e) {
132
				// TODO Auto-generated method stub
133
				
134
			}
135
		});
136
		
137
		Image zx = new Image(d, "zx.png");
138
		Image ibull = new Image(d, "ibull.jpg");
139
		CGraphNode n = new CGraphNode(g, SWT.NONE, createPersonFigure(zx));
140
		CGraphNode n2 = new CGraphNode(g, SWT.NONE,  createPersonFigure(ibull));
141
		GraphNode n3 = new GraphNode(g, SWT.NONE, "PDE");
142
		GraphNode n4 = new GraphNode(g, SWT.NONE, "Zest");
143
		GraphNode n5 = new GraphNode(g, SWT.NONE, "PDE Viz tool");
144
		
145
		new GraphConnection(g, SWT.NONE, n, n2);
146
		new GraphConnection(g, SWT.NONE, n, n3);
147
		new GraphConnection(g, SWT.NONE, n2, n4);
148
		new GraphConnection(g, SWT.NONE, n, n5);
149
		new GraphConnection(g, SWT.NONE, n2, n5);
150
		new GraphConnection(g, SWT.NONE, n3, n5);
151
		new GraphConnection(g, SWT.NONE, n4, n5);
152
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
153
154
		shell.open();
155
		while (!shell.isDisposed()) {
156
			while (!d.readAndDispatch()) {
157
				d.sleep();
158
			}
159
		}
160
		zx.dispose();
161
		ibull.dispose();
162
	}
163
164
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet10.java (-78 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionAdapter;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.layout.FillLayout;
16
import org.eclipse.swt.widgets.Button;
17
import org.eclipse.swt.widgets.Display;
18
import org.eclipse.swt.widgets.Shell;
19
import org.eclipse.zest.core.widgets.Graph;
20
import org.eclipse.zest.core.widgets.GraphConnection;
21
import org.eclipse.zest.core.widgets.GraphNode;
22
import org.eclipse.zest.layouts.LayoutStyles;
23
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
24
25
/**
26
 * 
27
 * This snippet shows how to create a curved connection using Zest.
28
 * Each time the Button is clicked, the curve changes.
29
 * 
30
 * @author Ian Bull
31
 * 
32
 */
33
public class GraphSnippet10 {
34
35
	public static void main(String[] args) {
36
		Display d = new Display();
37
		Shell shell = new Shell(d);
38
		shell.setText("GraphSnippet1");
39
		shell.setLayout(new FillLayout());
40
		shell.setSize(400, 400);
41
42
		final Graph g = new Graph(shell, SWT.NONE);
43
44
		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
45
		n.setBorderColor(org.eclipse.draw2d.ColorConstants.yellow);
46
		n.setBorderWidth(3);
47
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
48
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
49
		final GraphConnection connection = new GraphConnection(g, SWT.NONE, n, n2);
50
		connection.setLineWidth(3);
51
		new GraphConnection(g, SWT.NONE, n2, n3);
52
		new GraphConnection(g, SWT.NONE, n3, n);
53
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
54
		
55
		Button button = new Button(shell, SWT.PUSH);
56
		button.setText("Change Curve");
57
		button.addSelectionListener(new SelectionAdapter() {
58
			int count = 0;
59
			public void widgetSelected(SelectionEvent e) {
60
				count = ++count % 16;
61
				if ( count > 8 ) {
62
					connection.setCurveDepth((-16 + count) * 10);
63
				}
64
				else {
65
					connection.setCurveDepth(count * 10);
66
				}
67
			}
68
		});
69
70
		shell.open();
71
		while (!shell.isDisposed()) {
72
			while (!d.readAndDispatch()) {
73
				d.sleep();
74
			}
75
		}
76
	}
77
78
}
(-)src/org/eclipse/zest/tests/swt/ZoomSnippet.java (-151 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.zest.core.widgets.Graph;
13
import org.eclipse.zest.core.widgets.GraphConnection;
14
import org.eclipse.zest.core.widgets.GraphContainer;
15
import org.eclipse.zest.core.widgets.GraphItem;
16
import org.eclipse.zest.core.widgets.GraphNode;
17
import org.eclipse.zest.core.widgets.ZestStyles;
18
import org.eclipse.zest.layouts.LayoutAlgorithm;
19
import org.eclipse.zest.layouts.LayoutStyles;
20
import org.eclipse.zest.layouts.algorithms.CompositeLayoutAlgorithm;
21
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
22
import org.eclipse.zest.layouts.algorithms.HorizontalShift;
23
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
24
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.events.KeyEvent;
27
import org.eclipse.swt.events.KeyListener;
28
import org.eclipse.swt.graphics.Image;
29
import org.eclipse.swt.layout.FillLayout;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Shell;
32
33
public class ZoomSnippet {
34
35
	private static Image image1;
36
	private static Image classImage;
37
38
	public static void createContainer(Graph g) {
39
		GraphContainer a = new GraphContainer(g, SWT.NONE, "SomeClass.java", classImage);
40
		int r = (int) ((Math.random() * 3) + 1);
41
		r = 2;
42
		populateContainer(a, g, r, true);
43
		for (int i = 0; i < 4; i++) {
44
			GraphContainer b = new GraphContainer(g, SWT.NONE, "SomeNestedClass.java", classImage);
45
			r = (int) ((Math.random() * 3) + 1);
46
			r = 2;
47
			populateContainer(b, g, r, false);
48
			new GraphConnection(g, SWT.NONE, a, b);
49
			for (int j = 0; j < 4; j++) {
50
				GraphContainer c = new GraphContainer(g, SWT.NONE, "DefaultAction.java", classImage);
51
				r = (int) ((Math.random() * 3) + 1);
52
				r = 2;
53
				populateContainer(c, g, r, true);
54
				new GraphConnection(g, SWT.NONE, b, c);
55
			}
56
		}
57
	}
58
59
	public static void populateContainer(GraphContainer c, Graph g, int number, boolean radial) {
60
		GraphNode a = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "SomeClass.java", classImage);
61
		for (int i = 0; i < 4; i++) {
62
			GraphNode b = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "SomeNestedClass.java", classImage);
63
			new GraphConnection(g, SWT.NONE, a, b);
64
			for (int j = 0; j < 4; j++) {
65
				GraphNode d = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "DefaultAction.java", classImage);
66
				new GraphConnection(g, SWT.NONE, b, d);
67
				if (number > 2) {
68
					for (int k = 0; k < 4; k++) {
69
						GraphNode e = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "LastAction(Hero).java", classImage);
70
						new GraphConnection(g, SWT.NONE, d, e);
71
						if (number > 3) {
72
							for (int l = 0; l < 4; l++) {
73
								GraphNode f = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "LastAction(Hero).java", classImage);
74
								new GraphConnection(g, SWT.NONE, e, f);
75
							}
76
						}
77
					}
78
				}
79
			}
80
		}
81
		if (number == 1) {
82
			c.setScale(0.75);
83
		} else if (number == 2) {
84
			c.setScale(0.50);
85
		} else {
86
			c.setScale(0.25);
87
		}
88
		if (radial) {
89
			c.setLayoutAlgorithm(new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
90
		} else {
91
			c.setLayoutAlgorithm(new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
92
		}
93
	}
94
95
	/**
96
	 * @param args
97
	 */
98
	public static void main(String[] args) {
99
		// Create the shell
100
		Display d = new Display();
101
102
		image1 = new Image(Display.getDefault(), ZoomSnippet.class.getResourceAsStream("package_obj.gif"));
103
		classImage = new Image(Display.getDefault(), ZoomSnippet.class.getResourceAsStream("class_obj.gif"));
104
105
		Shell shell = new Shell(d);
106
		shell.setText("GraphSnippet1");
107
		shell.setLayout(new FillLayout());
108
		shell.setSize(500, 800);
109
110
		final Graph g = new Graph(shell, SWT.NONE);
111
		createContainer(g);
112
113
		CompositeLayoutAlgorithm compositeLayoutAlgorithm = new CompositeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING, new LayoutAlgorithm[] { new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), new HorizontalShift(LayoutStyles.NO_LAYOUT_NODE_RESIZING) });
114
		//g.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
115
		g.setLayoutAlgorithm(compositeLayoutAlgorithm, true);
116
117
		g.addKeyListener(new KeyListener() {
118
			boolean flip = true;
119
120
			public void keyPressed(KeyEvent e) {
121
122
				if (g.getSelection().size() == 1) {
123
					GraphNode item = (GraphNode) g.getSelection().get(0);
124
					if (item.getItemType() == GraphItem.CONTAINER) {
125
						if (flip) {
126
							(item).setSize(500, 100);
127
						} else {
128
							(item).setSize(0, 0);
129
						}
130
						flip = !flip;
131
					}
132
				}
133
134
			}
135
136
			public void keyReleased(KeyEvent e) {
137
				// TODO Auto-generated method stub
138
139
			}
140
141
		});
142
143
		shell.open();
144
		while (!shell.isDisposed()) {
145
			while (!d.readAndDispatch()) {
146
				d.sleep();
147
			}
148
		}
149
		image1.dispose();
150
	}
151
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet8.java (-120 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.draw2d.ColorConstants;
13
import org.eclipse.swt.SWT;
14
import org.eclipse.swt.layout.FillLayout;
15
import org.eclipse.swt.widgets.Display;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.zest.core.widgets.Graph;
18
import org.eclipse.zest.core.widgets.GraphConnection;
19
import org.eclipse.zest.core.widgets.GraphNode;
20
import org.eclipse.zest.core.widgets.ZestStyles;
21
import org.eclipse.zest.layouts.Filter;
22
import org.eclipse.zest.layouts.LayoutItem;
23
import org.eclipse.zest.layouts.LayoutStyles;
24
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
25
26
/**
27
 * This snippet shows how to filter elements in the layout.  The Data on the tree
28
 * connections are set to "False", meaning they won't be filtered.  
29
 * 
30
 * @author Ian Bull
31
 * 
32
 */
33
public class GraphSnippet8 {
34
35
	/**
36
	 * @param args
37
	 */
38
	public static void main(String[] args) {
39
		Display display = new Display();
40
		Shell shell = new Shell(display);
41
		shell.setText("GraphSnippet8");
42
		shell.setLayout(new FillLayout());
43
		shell.setSize(400, 400);
44
45
		final Graph graph = new Graph(shell, SWT.NONE);
46
47
		GraphNode a = new GraphNode(graph, SWT.NONE, "Root");
48
		GraphNode b = new GraphNode(graph, SWT.NONE, "B");
49
		GraphNode c = new GraphNode(graph, SWT.NONE, "C");
50
		GraphNode d = new GraphNode(graph, SWT.NONE, "D");
51
		GraphNode e = new GraphNode(graph, SWT.NONE, "E");
52
		GraphNode f = new GraphNode(graph, SWT.NONE, "F");
53
		GraphNode g = new GraphNode(graph, SWT.NONE, "G");
54
		GraphNode h = new GraphNode(graph, SWT.NONE, "H");
55
		GraphConnection connection = new GraphConnection(graph, SWT.NONE, a, b);
56
		connection.setData(Boolean.FALSE);
57
		connection = new GraphConnection(graph, SWT.NONE, a, c);
58
		connection.setData(Boolean.FALSE);
59
		connection = new GraphConnection(graph, SWT.NONE, a, c);
60
		connection.setData(Boolean.FALSE);
61
		connection = new GraphConnection(graph, SWT.NONE, a, d);
62
		connection.setData(Boolean.FALSE);
63
		connection = new GraphConnection(graph, SWT.NONE, b, e);
64
		connection.setData(Boolean.FALSE);
65
		connection = new GraphConnection(graph, SWT.NONE, b, f);
66
		connection.setData(Boolean.FALSE);
67
		connection = new GraphConnection(graph, SWT.NONE, c, g);
68
		connection.setData(Boolean.FALSE);
69
		connection = new GraphConnection(graph, SWT.NONE, d, h);
70
		connection.setData(Boolean.FALSE);
71
		
72
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, b, c);
73
		connection.setLineColor(ColorConstants.red);
74
		connection.setLineWidth(3);
75
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, c, d);
76
		connection.setLineColor(ColorConstants.red);
77
		connection.setLineWidth(3);
78
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, e, f);
79
		connection.setLineColor(ColorConstants.red);
80
		connection.setLineWidth(3);
81
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, f, g);
82
		connection.setLineColor(ColorConstants.red);
83
		connection.setLineWidth(3);
84
		
85
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, h, e);
86
		connection.setLineColor(ColorConstants.red);
87
		connection.setLineWidth(3);
88
		
89
		TreeLayoutAlgorithm treeLayoutAlgorithm = new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
90
		Filter filter = new Filter() {
91
			public boolean isObjectFiltered(LayoutItem item) {
92
93
				// Get the "Connection" from the Layout Item
94
				// and use this connection to get the "Graph Data"
95
				Object object = item.getGraphData();
96
				if  (object instanceof GraphConnection ) {
97
					GraphConnection connection = (GraphConnection) object;
98
					if ( connection.getData() != null && connection.getData() instanceof Boolean ) {
99
						// If the data is false, don't filter, otherwise, filter.
100
						return ((Boolean)connection.getData()).booleanValue();
101
					}
102
					return true;
103
				}
104
				return false;
105
			}
106
			
107
		};
108
		treeLayoutAlgorithm.setFilter(filter);
109
		graph.setLayoutAlgorithm(treeLayoutAlgorithm, true);
110
		
111
112
		shell.open();
113
		while (!shell.isDisposed()) {
114
			while (!display.readAndDispatch()) {
115
				display.sleep();
116
			}
117
		}
118
	}
119
120
}
(-)src/org/eclipse/zest/tests/swt/NestedGraphSnippet2.java (-87 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.zest.core.widgets.Graph;
13
import org.eclipse.zest.core.widgets.GraphConnection;
14
import org.eclipse.zest.core.widgets.GraphContainer;
15
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.core.widgets.ZestStyles;
17
import org.eclipse.zest.layouts.LayoutStyles;
18
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
19
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Shell;
24
25
public class NestedGraphSnippet2 {
26
27
	public static void main(String[] args) {
28
		// Create the shell
29
		Display d = new Display();
30
		Shell shell = new Shell(d);
31
		shell.setText("GraphSnippet1");
32
		shell.setLayout(new FillLayout());
33
		shell.setSize(400, 400);
34
35
		Graph g = new Graph(shell, SWT.NONE);
36
37
		/* Machines  */
38
		GraphContainer machine1 = new GraphContainer(g, SWT.NONE);
39
		machine1.setText("Machine 1 (prop:1)");
40
		GraphContainer machine2 = new GraphContainer(g, SWT.NONE);
41
		machine2.setText("Machine 2");
42
		GraphContainer machine3 = new GraphContainer(g, SWT.NONE);
43
		machine3.setText("Machine 3");
44
45
		/* Network */
46
		GraphConnection networkConnection = new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, machine1, machine2);
47
		networkConnection.setText("Network (bandwidth:1) ");
48
		new GraphConnection(g, SWT.NONE, machine2, machine3);
49
50
		/* Containers */
51
		GraphContainer container1 = new GraphContainer(machine1, SWT.NONE);
52
		container1.setText("Host 1");
53
		GraphContainer container2 = new GraphContainer(machine1, SWT.NONE);
54
		container2.setText("Host 2");
55
56
		GraphContainer container3 = new GraphContainer(machine2, SWT.NONE);
57
		container3.setText("Host 3");
58
		GraphContainer container4 = new GraphContainer(machine3, SWT.NONE);
59
		container4.setText("Host 4");
60
61
		/* Objects */
62
		GraphNode object1 = new GraphNode(container1, ZestStyles.NODES_FISHEYE, "JSP Object");
63
		GraphNode object2 = new GraphNode(container1, ZestStyles.NODES_FISHEYE, "JSP Object 2");
64
		GraphNode object3 = new GraphNode(container2, ZestStyles.NODES_FISHEYE, "JSP Object 3");
65
		GraphNode object4 = new GraphNode(container3, ZestStyles.NODES_FISHEYE, "JSP Object 4");
66
		GraphNode object5 = new GraphNode(container4, ZestStyles.NODES_FISHEYE, "JSP Object 5");
67
68
		/* Connections */
69
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object1, object2);
70
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object2, object3);
71
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object3, object4);
72
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object4, object5);
73
74
		container1.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
75
		container2.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
76
		container3.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
77
		container3.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
78
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
79
80
		shell.open();
81
		while (!shell.isDisposed()) {
82
			while (!d.readAndDispatch()) {
83
				d.sleep();
84
			}
85
		}
86
	}
87
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet3.java (-73 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *******************************************************************************/
11
package org.eclipse.zest.tests.swt;
12
13
import org.eclipse.zest.core.widgets.Graph;
14
import org.eclipse.zest.core.widgets.GraphConnection;
15
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.core.widgets.ZestStyles;
17
import org.eclipse.zest.layouts.LayoutStyles;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.layout.FillLayout;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Shell;
26
27
/**
28
 * Adds a selection listener to the nodes to tell when a selection event has
29
 * happened.
30
 * 
31
 * @author Ian Bull
32
 * 
33
 */
34
public class GraphSnippet3 {
35
36
	public static void main(String[] args) {
37
		Display d = new Display();
38
		Shell shell = new Shell(d);
39
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
40
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
41
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
42
		shell.setLayout(new FillLayout());
43
		shell.setSize(400, 400);
44
45
		Graph g = new Graph(shell, SWT.NONE);
46
		g.addSelectionListener(new SelectionAdapter() {
47
			public void widgetSelected(SelectionEvent e) {
48
				System.out.println(((Graph) e.widget).getSelection());
49
			}
50
		});
51
		
52
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
53
		GraphNode n1 = new GraphNode(g, SWT.NONE, "Information", image1);
54
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Warning", image2);
55
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Error", image3);
56
57
		new GraphConnection(g, SWT.NONE, n1, n2);
58
		new GraphConnection(g, SWT.NONE, n2, n3);
59
60
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
61
62
		shell.open();
63
		while (!shell.isDisposed()) {
64
			while (!d.readAndDispatch()) {
65
				d.sleep();
66
			}
67
		}
68
		image1.dispose();
69
		image2.dispose();
70
		image3.dispose();
71
72
	}
73
}
(-)src/org/eclipse/zest/tests/swt/CustomLayout.java (-52 / +31 lines)
Lines 1-3 Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
1
package org.eclipse.zest.tests.swt;
12
package org.eclipse.zest.tests.swt;
2
13
3
import org.eclipse.swt.SWT;
14
import org.eclipse.swt.SWT;
Lines 7-16 Link Here
7
import org.eclipse.zest.core.widgets.Graph;
18
import org.eclipse.zest.core.widgets.Graph;
8
import org.eclipse.zest.core.widgets.GraphConnection;
19
import org.eclipse.zest.core.widgets.GraphConnection;
9
import org.eclipse.zest.core.widgets.GraphNode;
20
import org.eclipse.zest.core.widgets.GraphNode;
10
import org.eclipse.zest.layouts.LayoutEntity;
21
import org.eclipse.zest.layouts.EntityLayout;
11
import org.eclipse.zest.layouts.algorithms.AbstractLayoutAlgorithm;
22
import org.eclipse.zest.layouts.LayoutAlgorithm;
12
import org.eclipse.zest.layouts.dataStructures.InternalNode;
23
import org.eclipse.zest.layouts.LayoutContext;
13
import org.eclipse.zest.layouts.dataStructures.InternalRelationship;
14
24
15
/**
25
/**
16
 * This snippet shows how to create a custom layout. This layout simply lays the nodes out vertically
26
 * This snippet shows how to create a custom layout. This layout simply lays the nodes out vertically
Lines 36-94 Link Here
36
		new GraphConnection(g, SWT.NONE, n, n2);
46
		new GraphConnection(g, SWT.NONE, n, n2);
37
		new GraphConnection(g, SWT.NONE, n2, n3);
47
		new GraphConnection(g, SWT.NONE, n2, n3);
38
		new GraphConnection(g, SWT.NONE, n3, n);
48
		new GraphConnection(g, SWT.NONE, n3, n);
39
		g.setLayoutAlgorithm(new AbstractLayoutAlgorithm(SWT.NONE) {
49
		
40
50
		LayoutAlgorithm layoutAlgorithm = new LayoutAlgorithm() {
51
			private LayoutContext context;
52
			public void setLayoutContext(LayoutContext context) {
53
				this.context = context;
54
			}
41
			
55
			
42
			private int totalSteps;
56
			public void applyLayout() {
43
			private int currentStep;
57
				EntityLayout[] entitiesToLayout = context.getEntities();
44
58
				int totalSteps = entitiesToLayout.length;
45
			protected void applyLayoutInternal(InternalNode[] entitiesToLayout,
59
				double distance = context.getBounds().width / totalSteps;
46
					InternalRelationship[] relationshipsToConsider, double boundsX, double boundsY, double boundsWidth,
47
					double boundsHeight) {
48
				
49
				totalSteps = entitiesToLayout.length;
50
				double distance = boundsWidth / totalSteps;
51
				int xLocation = 0;
60
				int xLocation = 0;
52
			
61
53
				fireProgressStarted(totalSteps);
62
				for (int currentStep = 0; currentStep < entitiesToLayout.length; currentStep++) {
54
				
63
					EntityLayout layoutEntity = entitiesToLayout[currentStep];
55
				for (currentStep = 0; currentStep < entitiesToLayout.length; currentStep++) {
64
					layoutEntity.setLocation(xLocation, layoutEntity.getLocation().y);
56
					LayoutEntity layoutEntity = entitiesToLayout[currentStep].getLayoutEntity();
57
					layoutEntity.setLocationInLayout(xLocation, layoutEntity.getYInLayout());
58
					xLocation+= distance;
65
					xLocation+= distance;
59
					fireProgressEvent(currentStep, totalSteps);
60
				}
66
				}
61
				fireProgressEnded(totalSteps);
62
			}
63
64
			protected int getCurrentLayoutStep() {
65
				return 0;
66
			}
67
68
			protected int getTotalNumberOfLayoutSteps() {
69
				return totalSteps;
70
			}
67
			}
71
68
		};
72
			protected boolean isValidConfiguration(boolean asynchronous, boolean continuous) {
69
		g.setLayoutAlgorithm(layoutAlgorithm, true);
73
				return true;
70
		
74
			}
75
76
			protected void postLayoutAlgorithm(InternalNode[] entitiesToLayout,
77
					InternalRelationship[] relationshipsToConsider) {
78
				// Do nothing
79
			}
80
81
			protected void preLayoutAlgorithm(InternalNode[] entitiesToLayout,
82
					InternalRelationship[] relationshipsToConsider, double x, double y, double width, double height) {
83
				// do nothing
84
			}
85
86
			public void setLayoutArea(double x, double y, double width, double height) {
87
				// do nothing
88
			}
89
			
90
		}, true);
91
92
		shell.open();
71
		shell.open();
93
		while (!shell.isDisposed()) {
72
		while (!shell.isDisposed()) {
94
			while (!d.readAndDispatch()) {
73
			while (!d.readAndDispatch()) {
(-)src/org/eclipse/zest/tests/swt/GraphSnippet13.java (-182 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import java.util.Iterator;
13
14
import org.eclipse.draw2d.ColorConstants;
15
import org.eclipse.draw2d.Ellipse;
16
import org.eclipse.draw2d.Figure;
17
import org.eclipse.draw2d.FlowLayout;
18
import org.eclipse.draw2d.FreeformLayout;
19
import org.eclipse.draw2d.IFigure;
20
import org.eclipse.draw2d.ImageFigure;
21
import org.eclipse.draw2d.Label;
22
import org.eclipse.draw2d.MarginBorder;
23
import org.eclipse.draw2d.PolylineShape;
24
import org.eclipse.draw2d.geometry.Point;
25
import org.eclipse.draw2d.geometry.Rectangle;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.events.SelectionEvent;
28
import org.eclipse.swt.events.SelectionListener;
29
import org.eclipse.swt.graphics.Image;
30
import org.eclipse.swt.layout.FillLayout;
31
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.swt.widgets.Shell;
33
import org.eclipse.zest.core.widgets.CGraphNode;
34
import org.eclipse.zest.core.widgets.Graph;
35
import org.eclipse.zest.core.widgets.GraphConnection;
36
import org.eclipse.zest.core.widgets.GraphContainer;
37
import org.eclipse.zest.core.widgets.GraphNode;
38
import org.eclipse.zest.core.widgets.ZestStyles;
39
import org.eclipse.zest.layouts.LayoutStyles;
40
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
41
42
/**
43
 * 
44
 * This snippet shows how to create a curved connection using Zest.
45
 * 
46
 * @author Ian Bull
47
 * 
48
 */
49
public class GraphSnippet13 {
50
51
	public static IFigure createPersonFigure(Image headImage) {
52
		Figure person = new Figure();
53
		person.setLayoutManager(new FreeformLayout());
54
		IFigure head = null;
55
		if ( headImage != null ) {
56
			headImage = new Image(headImage.getDevice(), headImage.getImageData().scaledTo(40, 50));
57
			head = new ImageFigure(headImage);
58
		}
59
		else
60
			head = new Ellipse();
61
		head.setSize(40, 50);
62
		
63
		PolylineShape body = new PolylineShape();
64
		body.setLineWidth(1);
65
		body.setStart(new Point(20,40));
66
		body.setEnd(new Point(20,100));
67
		body.setBounds(new Rectangle(0,0,40,100));
68
		
69
		PolylineShape leftLeg = new PolylineShape();
70
		leftLeg.setLineWidth(1);
71
		leftLeg.setStart(new Point(20,100));
72
		leftLeg.setEnd(new Point(0,130));
73
		leftLeg.setBounds(new Rectangle(0,0,40,130));
74
		
75
		PolylineShape rightLeg = new PolylineShape();
76
		rightLeg.setLineWidth(1);
77
		rightLeg.setStart(new Point(20,100));
78
		rightLeg.setEnd(new Point(40,130));
79
		rightLeg.setBounds(new Rectangle(0,0,40,130));
80
		
81
		PolylineShape leftArm = new PolylineShape();
82
		leftArm.setLineWidth(1);
83
		leftArm.setStart(new Point(20,60));
84
		leftArm.setEnd(new Point(0,50));
85
		leftArm.setBounds(new Rectangle(0,0,40,130));
86
		
87
		PolylineShape rightArm = new PolylineShape();
88
		rightArm.setLineWidth(1);
89
		rightArm.setStart(new Point(20,60));
90
		rightArm.setEnd(new Point(40,50));
91
		rightArm.setBounds(new Rectangle(0,0,40,130));
92
		
93
		person.add(head);
94
		person.add(body);
95
		person.add(leftLeg);
96
		person.add(rightLeg);
97
		person.add(leftArm);
98
		person.add(rightArm);
99
		person.setSize(40,130);
100
		return person;
101
	}
102
103
	public static void main(String[] args) {
104
		final Display d = new Display();
105
		Shell shell = new Shell(d);
106
		shell.setText("GraphSnippet11");
107
		shell.setLayout(new FillLayout());
108
		shell.setSize(400, 400);
109
110
		
111
		final Graph g = new Graph(shell, SWT.NONE);
112
		g.addSelectionListener(new SelectionListener(){
113
		
114
			public void widgetSelected(SelectionEvent e) {
115
				Iterator iter = g.getSelection().iterator();
116
				while(iter.hasNext()) {
117
					Object o = iter.next();
118
					if ( o instanceof CGraphNode) {
119
						IFigure figure = ((CGraphNode)o).getFigure();
120
						figure.setBackgroundColor(ColorConstants.blue);
121
						figure.setForegroundColor(ColorConstants.blue);
122
					}
123
				}
124
				iter = g.getNodes().iterator();
125
				while ( iter.hasNext()) {
126
					Object o = iter.next();
127
					if ( o instanceof CGraphNode) {
128
						if ( !g.getSelection().contains(o)) {
129
							((CGraphNode)o).getFigure().setBackgroundColor(ColorConstants.black);
130
							((CGraphNode)o).getFigure().setForegroundColor(ColorConstants.black);
131
						}
132
					}
133
				}
134
			}
135
		
136
			public void widgetDefaultSelected(SelectionEvent e) {
137
				// TODO Auto-generated method stub
138
				
139
			}
140
		});
141
		
142
		Image zx = new Image(d, "zxsnow.png");
143
		IFigure tooltip = new Figure();
144
		tooltip.setBorder(new MarginBorder(5,5,5,5));
145
		FlowLayout layout = new FlowLayout(false);
146
		layout.setMajorSpacing(3);
147
		layout.setMinorAlignment(3);
148
		tooltip.setLayoutManager(new FlowLayout(false));
149
		tooltip.add(new ImageFigure(zx));
150
		tooltip.add(new Label("Name: " + "Chris Aniszczyk"));
151
		tooltip.add(new Label("Location: " + "Austin, Texas"));
152
		
153
		Image ibull = new Image(d, "ibull.jpg");
154
		GraphContainer c1 = new GraphContainer(g, SWT.NONE);
155
		c1.setText("Canada");
156
		GraphContainer c2 = new GraphContainer(g, SWT.NONE);
157
		c2.setText("USA");
158
		
159
		GraphNode n1 = new GraphNode(c1, SWT.NONE, "Ian B.");
160
		n1.setSize(200, 100);
161
		GraphNode n2 = new GraphNode(c2, SWT.NONE, "Chris A.");
162
		n2.setTooltip(tooltip);
163
		
164
		GraphConnection connection = new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, n1, n2);
165
		connection.setCurveDepth(-30);
166
		GraphConnection connection2 = new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, n2, n1);
167
		connection2.setCurveDepth(-30);
168
		
169
		
170
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
171
172
		shell.open();
173
		while (!shell.isDisposed()) {
174
			while (!d.readAndDispatch()) {
175
				d.sleep();
176
			}
177
		}
178
		zx.dispose();
179
		ibull.dispose();
180
	}
181
182
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet4.java (-103 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *******************************************************************************/
11
package org.eclipse.zest.tests.swt;
12
13
import org.eclipse.draw2d.IFigure;
14
import org.eclipse.draw2d.Label;
15
import org.eclipse.zest.core.widgets.Graph;
16
import org.eclipse.zest.core.widgets.GraphConnection;
17
import org.eclipse.zest.core.widgets.GraphNode;
18
import org.eclipse.zest.core.widgets.ZestStyles;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.graphics.GC;
21
import org.eclipse.swt.graphics.Image;
22
import org.eclipse.swt.layout.FillLayout;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Shell;
25
26
/**
27
 * This snippet shows how a custom figure can be used as a ToolTip for connections.
28
 * Let your mouse hover over an edge to see the custom tooltip.
29
 * 
30
 * @author Ian Bull
31
 * 
32
 */
33
public class GraphSnippet4 {
34
	
35
	
36
	/**
37
	 * Merges 2 images so they appear beside each other
38
	 * 
39
	 * You must dispose this image!
40
	 * @param image1
41
	 * @param image2
42
	 * @param result
43
	 * @return
44
	 */
45
	public static Image mergeImages(Image image1, Image image2) {
46
		Image mergedImage = new Image(Display.getDefault(), image1.getBounds().width + image2.getBounds().width, image1.getBounds().height);
47
		GC gc = new GC(mergedImage);
48
		gc.drawImage(image1, 0, 0);
49
		gc.drawImage(image2, image1.getBounds().width, 0);
50
		gc.dispose();
51
		return mergedImage;
52
	}
53
	
54
55
	/**
56
	 * @param args
57
	 */
58
	public static void main(String[] args) {
59
		Display d = new Display();
60
		Shell shell = new Shell(d);
61
		shell.setText("Graph Snippet 4");
62
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
63
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
64
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
65
		shell.setLayout(new FillLayout());
66
		shell.setSize(400, 400);
67
68
		Graph g = new Graph(shell, SWT.NONE);
69
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED );
70
		GraphNode n1 = new GraphNode(g, SWT.NONE, "Information", image1);
71
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Warning", image2);
72
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Error", image3);
73
		
74
		GraphConnection connection1 = new GraphConnection(g, SWT.NONE, n1, n2);
75
		GraphConnection connection2 = new GraphConnection(g, SWT.NONE, n2, n3);
76
		
77
		Image information2warningImage = mergeImages(image1, image2);
78
		Image warning2error = mergeImages(image2, image3);
79
		IFigure tooltip1 = new Label("Information to Warning", information2warningImage);
80
		IFigure tooltip2 = new Label("Warning to Error", warning2error);
81
		connection1.setTooltip(tooltip1);
82
		connection2.setTooltip(tooltip2);
83
		
84
		n1.setLocation(10, 10);
85
		n2.setLocation(200, 10);
86
		n3.setLocation(200, 200);
87
88
		shell.open();
89
		while (!shell.isDisposed()) {
90
			while (!d.readAndDispatch()) {
91
				d.sleep();
92
			}
93
		}
94
		
95
		image1.dispose();
96
		image2.dispose();
97
		image3.dispose();
98
		
99
		information2warningImage.dispose();
100
		warning2error.dispose();
101
102
	}
103
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet2.java (-66 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *******************************************************************************/
11
package org.eclipse.zest.tests.swt;
12
13
import org.eclipse.zest.core.widgets.Graph;
14
import org.eclipse.zest.core.widgets.GraphConnection;
15
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.core.widgets.ZestStyles;
17
import org.eclipse.zest.layouts.LayoutStyles;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Shell;
24
25
/**
26
 * This snippet creates a very simple graph with an Icon and Label.
27
 * 
28
 * This snippet shows how to use directed edges and self loops.
29
 * 
30
 * @author Ian Bull
31
 * 
32
 */
33
public class GraphSnippet2 {
34
35
	public static void main(String[] args) {
36
		Display d = new Display();
37
		Shell shell = new Shell(d);
38
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
39
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
40
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
41
		shell.setLayout(new FillLayout());
42
		shell.setSize(400, 400);
43
44
		Graph g = new Graph(shell, SWT.NONE);
45
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
46
		GraphNode n1 = new GraphNode(g, SWT.NONE, "Information", image1);
47
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Warning", image2);
48
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Error", image3);
49
50
		new GraphConnection(g, SWT.NONE, n1, n2);
51
		new GraphConnection(g, SWT.NONE, n2, n3);
52
		new GraphConnection(g, SWT.NONE, n3, n3);
53
54
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
55
56
		shell.open();
57
		while (!shell.isDisposed()) {
58
			while (!d.readAndDispatch()) {
59
				d.sleep();
60
			}
61
		}
62
		image1.dispose();
63
		image2.dispose();
64
		image3.dispose();
65
	}
66
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet5.java (-137 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Map;
17
18
import org.eclipse.draw2d.ColorConstants;
19
import org.eclipse.zest.core.widgets.Graph;
20
import org.eclipse.zest.core.widgets.GraphConnection;
21
import org.eclipse.zest.core.widgets.GraphItem;
22
import org.eclipse.zest.core.widgets.GraphNode;
23
import org.eclipse.zest.core.widgets.ZestStyles;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.events.KeyAdapter;
26
import org.eclipse.swt.events.KeyEvent;
27
import org.eclipse.swt.events.PaintEvent;
28
import org.eclipse.swt.events.PaintListener;
29
import org.eclipse.swt.graphics.Font;
30
import org.eclipse.swt.graphics.FontData;
31
import org.eclipse.swt.graphics.Image;
32
import org.eclipse.swt.graphics.Region;
33
import org.eclipse.swt.layout.FillLayout;
34
import org.eclipse.swt.widgets.Display;
35
import org.eclipse.swt.widgets.Shell;
36
37
/**
38
 * This snippet shows how you can add a paint listener to a Zest graph to paint on top of
39
 * the widget.  This snippet allows you to type and it selects all the nodes that match 
40
 * what you type.
41
 * 
42
 * @author Ian Bull
43
 * 
44
 */
45
public class GraphSnippet5 {
46
	public static final int BACKSPACE = 8;
47
	public static final int ENTER = 13;
48
49
	/**
50
	 * @param args
51
	 */
52
	public static void main(String[] args) {
53
		final Map figureListing = new HashMap();
54
		final StringBuffer stringBuffer = new StringBuffer();
55
		final Display d = new Display();
56
		FontData fontData = d.getSystemFont().getFontData()[0];
57
		fontData.height = 42;
58
59
		final Font font = new Font(d, fontData);
60
61
		Shell shell = new Shell(d);
62
		shell.setText("Graph Snippet 5");
63
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
64
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
65
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
66
		shell.setLayout(new FillLayout());
67
		shell.setSize(400, 400);
68
69
		final Graph g = new Graph(shell, SWT.NONE);
70
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
71
		GraphNode n1 = new GraphNode(g, SWT.NONE, "org.eclipse.Information", image1);
72
		GraphNode n2 = new GraphNode(g, SWT.NONE, "org.eclipse.Warning", image2);
73
		GraphNode n3 = new GraphNode(g, SWT.NONE, "org.eclipse.Error", image3);
74
		figureListing.put(n1.getText().toLowerCase(), n1);
75
		figureListing.put(n2.getText().toLowerCase(), n2);
76
		figureListing.put(n3.getText().toLowerCase(), n3);
77
78
		new GraphConnection(g, SWT.NONE, n1, n2);
79
		new GraphConnection(g, SWT.NONE, n2, n3);
80
		n1.setLocation(10, 10);
81
		n2.setLocation(200, 10);
82
		n3.setLocation(200, 200);
83
84
		g.addKeyListener(new KeyAdapter() {
85
86
			public void keyPressed(KeyEvent e) {
87
				boolean complete = false;
88
				if (e.keyCode == BACKSPACE) {
89
					if (stringBuffer.length() > 0) {
90
						stringBuffer.deleteCharAt(stringBuffer.length() - 1);
91
					}
92
				} else if (e.keyCode == ENTER) {
93
					complete = true;
94
				} else if ((e.character >= 'a' && e.character <= 'z') || (e.character >= 'A' && e.character <= 'Z') || (e.character == '.') || (e.character >= '0' && e.character <= '9')) {
95
					stringBuffer.append(e.character);
96
				}
97
				Iterator iterator = figureListing.keySet().iterator();
98
				List list = new ArrayList();
99
				if (stringBuffer.length() > 0) {
100
					while (iterator.hasNext()) {
101
						String string = (String) iterator.next();
102
						if (string.indexOf(stringBuffer.toString().toLowerCase()) >= 0) {
103
							list.add(figureListing.get(string));
104
						}
105
					}
106
				}
107
				g.setSelection((GraphItem[]) list.toArray(new GraphItem[list.size()]));
108
				if (complete && stringBuffer.length() > 0) {
109
					stringBuffer.delete(0, stringBuffer.length());
110
				}
111
112
				g.redraw();
113
			}
114
115
		});
116
117
		g.addPaintListener(new PaintListener() {
118
			public void paintControl(PaintEvent e) {
119
				e.gc.setFont(font);
120
				e.gc.setClipping((Region) null);
121
				e.gc.setForeground(ColorConstants.black);
122
				e.gc.drawText(stringBuffer.toString(), 50, 50, true);
123
			}
124
		});
125
126
		shell.open();
127
		while (!shell.isDisposed()) {
128
			while (!d.readAndDispatch()) {
129
				d.sleep();
130
			}
131
		}
132
		image1.dispose();
133
		image2.dispose();
134
		image3.dispose();
135
		font.dispose();
136
	}
137
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet1.java (-60 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.zest.core.widgets.Graph;
13
import org.eclipse.zest.core.widgets.GraphConnection;
14
import org.eclipse.zest.core.widgets.GraphNode;
15
import org.eclipse.zest.layouts.LayoutStyles;
16
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.layout.FillLayout;
19
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Shell;
21
22
/**
23
 * This snippet creates a very simple graph where Rock is connected to Paper
24
 * which is connected to scissors which is connected to rock.
25
 * 
26
 * The nodes a positioned using a SpringLayout Algorithm, and they can be moved
27
 * around.
28
 * 
29
 * 
30
 * @author Ian Bull
31
 * 
32
 */
33
public class GraphSnippet1 {
34
35
	public static void main(String[] args) {
36
		Display d = new Display();
37
		Shell shell = new Shell(d);
38
		shell.setText("GraphSnippet1");
39
		shell.setLayout(new FillLayout());
40
		shell.setSize(400, 400);
41
42
		Graph g = new Graph(shell, SWT.NONE);
43
44
		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
45
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
46
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
47
		new GraphConnection(g, SWT.NONE, n, n2);
48
		new GraphConnection(g, SWT.NONE, n2, n3);
49
		new GraphConnection(g, SWT.NONE, n3, n);
50
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
51
52
		shell.open();
53
		while (!shell.isDisposed()) {
54
			while (!d.readAndDispatch()) {
55
				d.sleep();
56
			}
57
		}
58
	}
59
60
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet9.java (-54 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.layout.FillLayout;
14
import org.eclipse.swt.widgets.Display;
15
import org.eclipse.swt.widgets.Shell;
16
import org.eclipse.zest.core.widgets.Graph;
17
import org.eclipse.zest.core.widgets.GraphConnection;
18
import org.eclipse.zest.core.widgets.GraphNode;
19
import org.eclipse.zest.core.widgets.ZestStyles;
20
21
/**
22
 * This snippet demonstrates a self loop with a label.
23
 * 
24
 * @author Ian Bull
25
 * 
26
 */
27
public class GraphSnippet9 {
28
29
	/**
30
	 * @param args
31
	 */
32
	public static void main(String[] args) {
33
		Display display = new Display();
34
		Shell shell = new Shell(display);
35
		shell.setText("GraphSnippet9");
36
		shell.setLayout(new FillLayout());
37
		shell.setSize(400, 400);
38
39
		final Graph graph = new Graph(shell, SWT.NONE);
40
41
		GraphNode a = new GraphNode(graph, ZestStyles.CONNECTIONS_DIRECTED, "Root");
42
		GraphConnection connection = new GraphConnection(graph, SWT.NONE, a, a);
43
		connection.setText("A to A");
44
		a.setLocation(100, 100);
45
46
		shell.open();
47
		while (!shell.isDisposed()) {
48
			while (!display.readAndDispatch()) {
49
				display.sleep();
50
			}
51
		}
52
	}
53
54
}
(-)src/org/eclipse/zest/tests/swt/LayoutExample.java (-86 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.zest.core.widgets.ConstraintAdapter;
13
import org.eclipse.zest.core.widgets.Graph;
14
import org.eclipse.zest.core.widgets.GraphConnection;
15
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.layouts.LayoutStyles;
17
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
18
import org.eclipse.zest.layouts.constraints.BasicEdgeConstraints;
19
import org.eclipse.zest.layouts.constraints.LayoutConstraint;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Shell;
24
25
/**
26
 * This snippet shows how to use the findFigureAt to get the figure under the
27
 * mouse
28
 * 
29
 * @author Ian Bull
30
 * 
31
 */
32
public class LayoutExample {
33
34
	/**
35
	 * @param args
36
	 */
37
	public static void main(String[] args) {
38
		// Create the shell
39
		Display d = new Display();
40
		Shell shell = new Shell(d);
41
		shell.setText("GraphSnippet1");
42
		shell.setLayout(new FillLayout());
43
		shell.setSize(400, 400);
44
45
		final Graph g = new Graph(shell, SWT.NONE);
46
		GraphNode root = new GraphNode(g, SWT.NONE, "Root");
47
		for (int i = 0; i < 3; i++) {
48
			GraphNode n = new GraphNode(g, SWT.NONE, "1 - " + i);
49
			for (int j = 0; j < 3; j++) {
50
				GraphNode n2 = new GraphNode(g, SWT.NONE, "2 - " + j);
51
				new GraphConnection(g, SWT.NONE, n, n2);
52
			}
53
			new GraphConnection(g, SWT.NONE, root, n);
54
		}
55
56
		SpringLayoutAlgorithm springLayoutAlgorithm = new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
57
58
		ConstraintAdapter constraintAdapters = new ConstraintAdapter() {
59
60
			public void populateConstraint(Object object, LayoutConstraint constraint) {
61
				if (constraint instanceof BasicEdgeConstraints) {
62
					BasicEdgeConstraints basicEdgeConstraints = (BasicEdgeConstraints) constraint;
63
					GraphConnection connection = (GraphConnection) object;
64
					if (connection.getSource().getText().equals("Root")) {
65
						basicEdgeConstraints.weight = 1;
66
					}
67
					else {
68
						basicEdgeConstraints.weight = -1;
69
					}
70
				}
71
72
			}
73
		};
74
75
		g.addConstraintAdapter(constraintAdapters);
76
		g.setLayoutAlgorithm(springLayoutAlgorithm, true);
77
78
		shell.open();
79
		while (!shell.isDisposed()) {
80
			while (!d.readAndDispatch()) {
81
				d.sleep();
82
			}
83
		}
84
	}
85
86
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet6.java (-71 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *******************************************************************************/
11
package org.eclipse.zest.tests.swt;
12
13
import org.eclipse.zest.core.widgets.Graph;
14
import org.eclipse.zest.core.widgets.GraphConnection;
15
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.core.widgets.ZestStyles;
17
import org.eclipse.zest.layouts.LayoutStyles;
18
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Shell;
24
25
/**
26
 * This snippet creates a graph with 80*3 nodes (240 nodes).  Only the icons are shown for the nodes, but if
27
 * you mouse over the node you get the entire text.
28
 * 
29
 * @author Ian Bull
30
 * 
31
 */
32
public class GraphSnippet6 {
33
34
	/**
35
	 * @param args
36
	 */
37
	public static void main(String[] args) {
38
		Display d = new Display();
39
		Shell shell = new Shell(d);
40
		shell.setText("GraphSnippet6");
41
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
42
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
43
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
44
		shell.setLayout(new FillLayout());
45
		shell.setSize(800, 800);
46
47
		Graph g = new Graph(shell, SWT.NONE);
48
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
49
		for (int i = 0; i < 80; i++) {
50
			GraphNode n1 = new GraphNode(g, ZestStyles.NODES_HIDE_TEXT | ZestStyles.NODES_FISHEYE, "Information", image1);
51
			GraphNode n2 = new GraphNode(g, ZestStyles.NODES_HIDE_TEXT | ZestStyles.NODES_FISHEYE, "Warning", image2);
52
			GraphNode n3 = new GraphNode(g, ZestStyles.NODES_HIDE_TEXT | ZestStyles.NODES_FISHEYE, "Error", image3);
53
			new GraphConnection(g, SWT.NONE, n1, n2);
54
			new GraphConnection(g, SWT.NONE, n2, n3);
55
			new GraphConnection(g, SWT.NONE, n3, n3);
56
		}
57
		g.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
58
59
		shell.open();
60
		while (!shell.isDisposed()) {
61
			while (!d.readAndDispatch()) {
62
				d.sleep();
63
			}
64
		}
65
		image1.dispose();
66
		image2.dispose();
67
		image3.dispose();
68
69
	}
70
71
}
(-)src/org/eclipse/zest/tests/swt/NestedGraphSnippet.java (-81 / +47 lines)
Lines 5-11 Link Here
5
 * accompanies this distribution, and is available at
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
9
 ******************************************************************************/
11
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
12
package org.eclipse.zest.tests.swt;
11
13
Lines 14-115 Link Here
14
import org.eclipse.zest.core.widgets.GraphContainer;
16
import org.eclipse.zest.core.widgets.GraphContainer;
15
import org.eclipse.zest.core.widgets.GraphNode;
17
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.core.widgets.ZestStyles;
18
import org.eclipse.zest.core.widgets.ZestStyles;
17
import org.eclipse.zest.layouts.LayoutAlgorithm;
18
import org.eclipse.zest.layouts.LayoutStyles;
19
import org.eclipse.zest.layouts.algorithms.CompositeLayoutAlgorithm;
20
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
19
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
21
import org.eclipse.zest.layouts.algorithms.HorizontalShift;
20
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
22
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
23
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
24
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.graphics.Image;
26
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.layout.FillLayout;
27
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Shell;
24
import org.eclipse.swt.widgets.Shell;
29
25
30
public class NestedGraphSnippet {
26
public class NestedGraphSnippet {
31
27
32
	private static Image image1;
33
	private static Image classImage;
34
35
	public static void createContainer(Graph g) {
36
		GraphContainer a = new GraphContainer(g, SWT.NONE, "SomeClass.java", classImage);
37
		int r = (int) ((Math.random() * 3) + 1);
38
		r = 2;
39
		populateContainer(a, g, r, true);
40
		for (int i = 0; i < 4; i++) {
41
			GraphContainer b = new GraphContainer(g, SWT.NONE, "SomeNestedClass.java", classImage);
42
			r = (int) ((Math.random() * 3) + 1);
43
			r = 2;
44
			populateContainer(b, g, r, false);
45
			new GraphConnection(g, SWT.NONE, a, b);
46
			for (int j = 0; j < 4; j++) {
47
				GraphContainer c = new GraphContainer(g, SWT.NONE, "DefaultAction.java", classImage);
48
				r = (int) ((Math.random() * 3) + 1);
49
				r = 2;
50
				populateContainer(c, g, r, true);
51
				new GraphConnection(g, SWT.NONE, b, c);
52
			}
53
		}
54
	}
55
56
	public static void populateContainer(GraphContainer c, Graph g, int number, boolean radial) {
57
		GraphNode a = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "SomeClass.java", classImage);
58
		for (int i = 0; i < 4; i++) {
59
			GraphNode b = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "SomeNestedClass.java", classImage);
60
			new GraphConnection(g, SWT.NONE, a, b);
61
			for (int j = 0; j < 4; j++) {
62
				GraphNode d = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "DefaultAction.java", classImage);
63
				new GraphConnection(g, SWT.NONE, b, d);
64
				if (number > 2) {
65
					for (int k = 0; k < 4; k++) {
66
						GraphNode e = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "LastAction(Hero).java", classImage);
67
						new GraphConnection(g, SWT.NONE, d, e);
68
						if (number > 3) {
69
							for (int l = 0; l < 4; l++) {
70
								GraphNode f = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "LastAction(Hero).java", classImage);
71
								new GraphConnection(g, SWT.NONE, e, f);
72
							}
73
						}
74
					}
75
				}
76
			}
77
		}
78
		if (number == 1) {
79
			c.setScale(0.75);
80
		} else if (number == 2) {
81
			c.setScale(0.50);
82
		} else {
83
			c.setScale(0.25);
84
		}
85
		if (radial) {
86
			c.setLayoutAlgorithm(new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
87
		} else {
88
			c.setLayoutAlgorithm(new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
89
		}
90
	}
91
92
	/**
93
	 * @param args
94
	 */
95
	public static void main(String[] args) {
28
	public static void main(String[] args) {
96
		// Create the shell
29
		// Create the shell
97
		Display d = new Display();
30
		Display d = new Display();
98
99
		image1 = new Image(Display.getDefault(), NestedGraphSnippet.class.getResourceAsStream("package_obj.gif"));
100
		classImage = new Image(Display.getDefault(), NestedGraphSnippet.class.getResourceAsStream("class_obj.gif"));
101
102
		Shell shell = new Shell(d);
31
		Shell shell = new Shell(d);
103
		shell.setText("GraphSnippet1");
32
		shell.setText("GraphSnippet1");
104
		shell.setLayout(new FillLayout());
33
		shell.setLayout(new FillLayout());
105
		shell.setSize(500, 800);
34
		shell.setSize(400, 400);
106
35
107
		Graph g = new Graph(shell, SWT.NONE);
36
		Graph g = new Graph(shell, SWT.NONE);
108
		createContainer(g);
109
37
110
		CompositeLayoutAlgorithm compositeLayoutAlgorithm = new CompositeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING, new LayoutAlgorithm[] { new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), new HorizontalShift(LayoutStyles.NO_LAYOUT_NODE_RESIZING) });
38
		/* Machines  */
111
		//g.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
39
		GraphContainer machine1 = new GraphContainer(g, SWT.NONE);
112
		g.setLayoutAlgorithm(compositeLayoutAlgorithm, true);
40
		machine1.setText("Machine 1 (prop:1)");
41
		GraphContainer machine2 = new GraphContainer(g, SWT.NONE);
42
		machine2.setText("Machine 2");
43
		GraphContainer machine3 = new GraphContainer(g, SWT.NONE);
44
		machine3.setText("Machine 3");
45
46
		/* Network */
47
		GraphConnection networkConnection = new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, machine1, machine2);
48
		networkConnection.setText("Network (bandwidth:1) ");
49
		new GraphConnection(g, SWT.NONE, machine2, machine3);
50
51
		/* Containers */
52
		GraphContainer container1 = new GraphContainer(machine1, SWT.NONE);
53
		container1.setText("Host 1");
54
		GraphContainer container2 = new GraphContainer(machine1, SWT.NONE);
55
		container2.setText("Host 2");
56
57
		GraphContainer container3 = new GraphContainer(machine2, SWT.NONE);
58
		container3.setText("Host 3");
59
		GraphContainer container4 = new GraphContainer(machine3, SWT.NONE);
60
		container4.setText("Host 4");
61
62
		/* Objects */
63
		GraphNode object1 = new GraphNode(container1, ZestStyles.NODES_FISHEYE, "JSP Object");
64
		GraphNode object2 = new GraphNode(container1, ZestStyles.NODES_FISHEYE, "JSP Object 2");
65
		GraphNode object3 = new GraphNode(container2, ZestStyles.NODES_FISHEYE, "JSP Object 3");
66
		GraphNode object4 = new GraphNode(container3, ZestStyles.NODES_FISHEYE, "JSP Object 4");
67
		GraphNode object5 = new GraphNode(container4, ZestStyles.NODES_FISHEYE, "JSP Object 5");
68
69
		/* Connections */
70
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object1, object2);
71
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object2, object3);
72
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object3, object4);
73
		new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, object4, object5);
74
75
		container1.setLayoutAlgorithm(new GridLayoutAlgorithm(), true);
76
		container2.setLayoutAlgorithm(new GridLayoutAlgorithm(), true);
77
		container3.setLayoutAlgorithm(new GridLayoutAlgorithm(), true);
78
		container3.setLayoutAlgorithm(new GridLayoutAlgorithm(), true);
79
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
113
80
114
		shell.open();
81
		shell.open();
115
		while (!shell.isDisposed()) {
82
		while (!shell.isDisposed()) {
Lines 117-122 Link Here
117
				d.sleep();
84
				d.sleep();
118
			}
85
			}
119
		}
86
		}
120
		image1.dispose();
121
	}
87
	}
122
}
88
}
(-)src/org/eclipse/zest/tests/swt/GraphSnippet11.java (-69 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.draw2d.ColorConstants;
13
import org.eclipse.swt.SWT;
14
import org.eclipse.swt.graphics.Color;
15
import org.eclipse.swt.layout.FillLayout;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.swt.widgets.Shell;
18
import org.eclipse.zest.core.widgets.Graph;
19
import org.eclipse.zest.core.widgets.GraphConnection;
20
import org.eclipse.zest.core.widgets.GraphNode;
21
import org.eclipse.zest.core.widgets.ZestStyles;
22
import org.eclipse.zest.layouts.LayoutStyles;
23
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
24
25
/**
26
 * 
27
 * This snippet shows how to create a curved connection using Zest.
28
 * 
29
 * @author Ian Bull
30
 * 
31
 */
32
public class GraphSnippet11 {
33
34
35
	public static void createConnection( Graph g, GraphNode n1, GraphNode n2, Color color, int curve) {
36
		GraphConnection connection = new GraphConnection(g, SWT.NONE, n1, n2);
37
		connection.setLineColor(color);
38
		connection.setCurveDepth(curve);
39
		connection.setLineWidth(1);
40
	}
41
	
42
	public static void main(String[] args) {
43
		Display d = new Display();
44
		Shell shell = new Shell(d);
45
		shell.setText("GraphSnippet11");
46
		shell.setLayout(new FillLayout());
47
		shell.setSize(400, 400);
48
49
		final Graph g = new Graph(shell, SWT.NONE);
50
		GraphNode n = new GraphNode(g, SWT.NONE, "Node 1");
51
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Node 2");
52
		createConnection(g, n, n2, ColorConstants.darkGreen, 20);
53
		createConnection(g, n, n2, ColorConstants.darkGreen, -20);
54
		createConnection(g, n, n2, ColorConstants.darkBlue, 40);
55
		createConnection(g, n, n2, ColorConstants.darkBlue, -40);
56
		createConnection(g, n, n2, ColorConstants.darkGray, 60);
57
		createConnection(g, n, n2, ColorConstants.darkGray, -60);
58
		createConnection(g, n, n2, ColorConstants.black, 0);
59
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
60
61
		shell.open();
62
		while (!shell.isDisposed()) {
63
			while (!d.readAndDispatch()) {
64
				d.sleep();
65
			}
66
		}
67
	}
68
69
}
(-)src/org/eclipse/zest/tests/swt/HelloWorld.java (-4 / +5 lines)
Lines 1-18 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
9
 ******************************************************************************/
11
 ******************************************************************************/
10
package org.eclipse.zest.tests.swt;
12
package org.eclipse.zest.tests.swt;
11
13
12
import org.eclipse.zest.core.widgets.Graph;
14
import org.eclipse.zest.core.widgets.Graph;
13
import org.eclipse.zest.core.widgets.GraphConnection;
15
import org.eclipse.zest.core.widgets.GraphConnection;
14
import org.eclipse.zest.core.widgets.GraphNode;
16
import org.eclipse.zest.core.widgets.GraphNode;
15
import org.eclipse.zest.layouts.LayoutStyles;
16
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
17
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.layout.FillLayout;
19
import org.eclipse.swt.layout.FillLayout;
Lines 41-47 Link Here
41
		GraphNode hello = new GraphNode(g, SWT.NONE, "Hello");
42
		GraphNode hello = new GraphNode(g, SWT.NONE, "Hello");
42
		GraphNode world = new GraphNode(g, SWT.NONE, "World");
43
		GraphNode world = new GraphNode(g, SWT.NONE, "World");
43
		new GraphConnection(g, SWT.NONE, hello, world);
44
		new GraphConnection(g, SWT.NONE, hello, world);
44
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
45
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
45
46
46
		shell.open();
47
		shell.open();
47
		while (!shell.isDisposed()) {
48
		while (!shell.isDisposed()) {
(-)src/org/eclipse/zest/tests/uml/UMLExample.java (-6 / +15 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-25 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.zest.tests.uml;
12
package org.eclipse.zest.tests.uml;
12
13
13
import org.eclipse.draw2d.IFigure;
14
import org.eclipse.draw2d.IFigure;
14
import org.eclipse.draw2d.Label;
15
import org.eclipse.draw2d.Label;
16
import org.eclipse.draw2d.geometry.Dimension;
15
import org.eclipse.zest.core.widgets.Graph;
17
import org.eclipse.zest.core.widgets.Graph;
16
import org.eclipse.zest.core.widgets.GraphConnection;
18
import org.eclipse.zest.core.widgets.GraphConnection;
17
import org.eclipse.zest.core.widgets.GraphContainer;
19
import org.eclipse.zest.core.widgets.GraphContainer;
18
import org.eclipse.zest.core.widgets.GraphNode;
20
import org.eclipse.zest.core.widgets.GraphNode;
19
import org.eclipse.zest.core.widgets.IContainer;
20
import org.eclipse.zest.core.widgets.ZestStyles;
21
import org.eclipse.zest.core.widgets.ZestStyles;
21
import org.eclipse.zest.layouts.LayoutStyles;
22
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
22
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
23
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.graphics.Color;
25
import org.eclipse.swt.graphics.Color;
25
import org.eclipse.swt.graphics.Font;
26
import org.eclipse.swt.graphics.Font;
Lines 82-94 Link Here
82
83
83
		IFigure customFigure = null;
84
		IFigure customFigure = null;
84
85
85
		public UMLNode(IContainer graphModel, int style, IFigure figure) {
86
		public UMLNode(Graph graphModel, int style, IFigure figure) {
87
			super(graphModel, style, figure);
88
		}
89
		
90
		public UMLNode(GraphContainer graphModel, int style, IFigure figure) {
86
			super(graphModel, style, figure);
91
			super(graphModel, style, figure);
87
		}
92
		}
88
93
89
		protected IFigure createFigureForModel() {
94
		protected IFigure createFigureForModel() {
90
			return (IFigure) this.getData();
95
			return (IFigure) this.getData();
91
		}
96
		}
97
		
98
		public Dimension getSize() {
99
			return ((IFigure) this.getData()).getPreferredSize();
100
		}
92
101
93
	}
102
	}
94
103
Lines 119-126 Link Here
119
		new GraphConnection(g, SWT.NONE, n1, n2);
128
		new GraphConnection(g, SWT.NONE, n1, n2);
120
		new GraphConnection(g, SWT.NONE, n, n1);
129
		new GraphConnection(g, SWT.NONE, n, n1);
121
130
122
		c.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
131
		c.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
123
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
132
		g.setLayoutAlgorithm(new TreeLayoutAlgorithm(), true);
124
133
125
		shell.open();
134
		shell.open();
126
		while (!shell.isDisposed()) {
135
		while (!shell.isDisposed()) {
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet3.java (-135 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.jface;
11
12
import java.io.BufferedReader;
13
import java.io.File;
14
import java.io.FileReader;
15
import java.io.IOException;
16
import java.util.ArrayList;
17
import java.util.StringTokenizer;
18
19
import org.eclipse.jface.viewers.LabelProvider;
20
import org.eclipse.jface.viewers.Viewer;
21
import org.eclipse.zest.core.viewers.GraphViewer;
22
import org.eclipse.zest.core.viewers.IGraphContentProvider;
23
import org.eclipse.zest.layouts.LayoutStyles;
24
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.layout.FillLayout;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.FileDialog;
29
import org.eclipse.swt.widgets.Shell;
30
31
/**
32
 * This snippet uses a very simple file format to read a graph. Edges are listed
33
 * on a new line in a file as such: 
34
 * a calls b 
35
 * b calls c 
36
 * c calld d
37
 * 
38
 * The content provider creates an edge for each line in the file and names the
39
 * sources and destination from the line.
40
 * 
41
 * 
42
 * @author Ian Bull
43
 * 
44
 */
45
public class GraphJFaceSnippet3 {
46
47
	public static final String graph = "a calls b\n" + "a calls c\n" + "b calld d\n" + "b calls e\n" + "c calls f\n" + "c calls g\n" + "d calls h\n" + "d calls i\n" + "e calls j\n" + "e calls k\n" + "f calls l\n" + "f calls m\n";
48
49
	static class SimpleGraphContentProvider implements IGraphContentProvider {
50
51
		private StringTokenizer graph;
52
53
		public Object getDestination(Object rel) {
54
			String string = (String) rel;
55
			String[] parts = string.split(" ");
56
			return parts[2];
57
		}
58
59
		public Object[] getElements(Object input) {
60
			ArrayList listOfEdges = new ArrayList();
61
			while (graph.hasMoreTokens()) {
62
				listOfEdges.add(graph.nextToken());
63
			}
64
			return listOfEdges.toArray();
65
		}
66
67
		public Object getSource(Object rel) {
68
			String string = (String) rel;
69
			String[] parts = string.split(" ");
70
			return parts[0];
71
		}
72
73
		public double getWeight(Object connection) {
74
			return 0;
75
		}
76
77
		public void dispose() {
78
79
		}
80
81
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
82
			if (newInput != null) {
83
				graph = new StringTokenizer((String) newInput, "\n");
84
			}
85
		}
86
87
	}
88
89
	public static void main(String[] args) throws IOException {
90
		Display display = new Display();
91
		Shell shell = new Shell(display);
92
		shell.setText("Simple Graph File Format");
93
94
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
95
		dialog.setFilterNames(new String[] { "Simple Graph Files (*.sgf)", "All Files (*.*)" });
96
		dialog.setFilterExtensions(new String[] { "*.sgf", "*.*" }); //Windows wild cards
97
98
		String directory = System.getProperty("user.dir") + "/src/org/eclipse/zest/tests/jface/SimpleGraph.sgf"; //eclipse/zest/examples/jface/";
99
		System.out.println(directory);
100
		dialog.setFilterPath(directory);
101
		//dialog.setFilterPath(System.getProperty("user.dir") + "src/org/eclipse/zest/examples/jface/"); //Windows path
102
103
		shell.setLayout(new FillLayout(SWT.VERTICAL));
104
		shell.setSize(400, 400);
105
		GraphViewer viewer = null;
106
107
		viewer = new GraphViewer(shell, SWT.NONE);
108
		viewer.setContentProvider(new SimpleGraphContentProvider());
109
		viewer.setLabelProvider(new LabelProvider());
110
		viewer.setLayoutAlgorithm(new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
111
112
		shell.open();
113
		String fileName = dialog.open();
114
115
		if (fileName == null) {
116
			// use the sample graph
117
			viewer.setInput(graph);
118
		} else {
119
			FileReader fileReader = new FileReader(new File(fileName));
120
			BufferedReader bufferedReader = new BufferedReader(fileReader);
121
			StringBuffer stringBuffer = new StringBuffer();
122
			while (bufferedReader.ready()) {
123
				stringBuffer.append(bufferedReader.readLine() + "\n");
124
			}
125
			viewer.setInput(stringBuffer.toString());
126
		}
127
128
		while (!shell.isDisposed()) {
129
			if (!display.readAndDispatch()) {
130
				display.sleep();
131
			}
132
		}
133
		display.dispose();
134
	}
135
}
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet1.java (-3 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.zest.tests.jface;
12
package org.eclipse.zest.tests.jface;
12
13
Lines 14-20 Link Here
14
import org.eclipse.jface.viewers.LabelProvider;
15
import org.eclipse.jface.viewers.LabelProvider;
15
import org.eclipse.jface.viewers.SelectionChangedEvent;
16
import org.eclipse.jface.viewers.SelectionChangedEvent;
16
import org.eclipse.jface.viewers.Viewer;
17
import org.eclipse.jface.viewers.Viewer;
17
import org.eclipse.zest.layouts.LayoutStyles;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
19
import org.eclipse.zest.core.viewers.GraphViewer;
19
import org.eclipse.zest.core.viewers.GraphViewer;
20
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
20
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
Lines 113-119 Link Here
113
113
114
		viewer.setContentProvider(new MyContentProvider());
114
		viewer.setContentProvider(new MyContentProvider());
115
		viewer.setLabelProvider(new MyLabelProvider());
115
		viewer.setLabelProvider(new MyLabelProvider());
116
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
116
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
117
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
117
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
118
			public void selectionChanged(SelectionChangedEvent event) {
118
			public void selectionChanged(SelectionChangedEvent event) {
119
				System.out.println("Selection changed: " + (event.getSelection()));
119
				System.out.println("Selection changed: " + (event.getSelection()));
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet7.java (-182 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.jface;
11
12
13
import org.eclipse.draw2d.IFigure;
14
import org.eclipse.draw2d.Label;
15
import org.eclipse.jface.viewers.LabelProvider;
16
import org.eclipse.jface.viewers.Viewer;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionEvent;
19
import org.eclipse.swt.events.SelectionListener;
20
import org.eclipse.swt.graphics.Font;
21
import org.eclipse.swt.graphics.Image;
22
import org.eclipse.swt.layout.FillLayout;
23
import org.eclipse.swt.widgets.Button;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.zest.core.viewers.EntityConnectionData;
27
import org.eclipse.zest.core.viewers.GraphViewer;
28
import org.eclipse.zest.core.viewers.IFigureProvider;
29
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
30
import org.eclipse.zest.core.viewers.INestedContentProvider;
31
import org.eclipse.zest.layouts.LayoutStyles;
32
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
33
import org.eclipse.zest.tests.uml.UMLClassFigure;
34
35
/**
36
 * This snippet shows how to use the INestedGraphContentProvider to create a graph
37
 * with Zest. In this example, getElements returns 3 edges: * Rock2Paper *
38
 * Paper2Scissors * Scissors2Rock
39
 * 
40
 * And for each of these, the source and destination are returned in getSource
41
 * and getDestination.
42
 * 
43
 * A label provider is also used to create the text and icons for the graph.
44
 * 
45
 * @author Ian Bull
46
 * 
47
 */
48
public class GraphJFaceSnippet7 {
49
50
	public static IFigure createClassFigure1(Font classFont, Image classImage, Image publicField, Image privateField) {
51
		Label classLabel1 = new Label("Table", classImage);
52
		classLabel1.setFont(classFont);
53
54
		UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
55
		Label attribute1 = new Label("columns: Column[]", privateField);
56
57
		Label attribute2 = new Label("rows: Row[]", privateField);
58
59
		Label method1 = new Label("getColumns(): Column[]", publicField);
60
		Label method2 = new Label("getRows(): Row[]", publicField);
61
		classFigure.getAttributesCompartment().add(attribute1);
62
		classFigure.getAttributesCompartment().add(attribute2);
63
		classFigure.getMethodsCompartment().add(method1);
64
		classFigure.getMethodsCompartment().add(method2);
65
		classFigure.setSize(-1, -1);
66
67
		return classFigure;
68
	}
69
	
70
	static class MyContentProvider implements IGraphEntityContentProvider, INestedContentProvider {
71
72
		public Object[] getConnectedTo(Object entity) {
73
			if (entity.equals("First")) {
74
				return new Object[] { "Second" };
75
			}
76
			if (entity.equals("Second")) {
77
				return new Object[] { "Third", "rock" };
78
			}
79
			if (entity.equals("Third")) {
80
				return new Object[] { "First" };
81
			}
82
			if ( entity.equals("rock")) {
83
				return new Object[] { "paper" };
84
			}
85
			return null;
86
		}
87
88
		public Object[] getElements(Object inputElement) {
89
			return new String[] { "First", "Second", "Third" };
90
		}
91
92
		public double getWeight(Object entity1, Object entity2) {
93
			return 0;
94
		}
95
96
		public void dispose() {
97
98
		}
99
100
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
101
102
		}
103
104
		public Object[] getChildren(Object element) {
105
			// TODO Auto-generated method stub
106
			return new Object[] {"rock", "paper", "scissors"};
107
		}
108
109
		public boolean hasChildren(Object element) {
110
			// TODO Auto-generated method stub
111
			if ( element.equals("First")) return true;
112
			return false;
113
		}
114
115
	}
116
117
118
	
119
	static class MyLabelProvider extends LabelProvider implements IFigureProvider {
120
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
121
122
		public Image getImage(Object element) {
123
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
124
				return image;
125
			}
126
			return null;
127
		}
128
129
		public String getText(Object element) {
130
			if ( element instanceof EntityConnectionData ) return "";
131
			return element.toString();
132
		}
133
		
134
		public IFigure getFigure(Object element) {
135
			Font classFont = new Font(null, "Arial", 12, SWT.BOLD);
136
			Image classImage = new Image(Display.getDefault(), UMLClassFigure.class.getResourceAsStream("class_obj.gif"));
137
			Image privateField = new Image(Display.getDefault(), UMLClassFigure.class.getResourceAsStream("field_private_obj.gif"));
138
			Image publicField= new Image(Display.getDefault(), UMLClassFigure.class.getResourceAsStream("methpub_obj.gif"));
139
			return createClassFigure1(classFont, classImage, publicField, privateField);
140
		}
141
142
	}
143
144
	static GraphViewer viewer = null;
145
146
	/**
147
	 * @param args
148
	 */
149
	public static void main(String[] args) {
150
		Display d = new Display();
151
		Shell shell = new Shell(d);
152
		shell.setText("GraphJFaceSnippet2");
153
		shell.setLayout(new FillLayout(SWT.VERTICAL));
154
		shell.setSize(400, 400);
155
		viewer = new GraphViewer(shell, SWT.NONE);
156
		viewer.setContentProvider(new MyContentProvider());
157
		viewer.setLabelProvider(new MyLabelProvider());
158
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
159
		viewer.setInput(new Object());
160
		
161
		Button button = new Button(shell, SWT.PUSH);
162
		button.setText("push");
163
		button.addSelectionListener(new SelectionListener() {
164
165
			public void widgetDefaultSelected(SelectionEvent e) {
166
			}
167
168
			public void widgetSelected(SelectionEvent e) {
169
				viewer.setInput(new Object());
170
			}
171
			
172
		});
173
		shell.open();
174
		while (!shell.isDisposed()) {
175
			while (!d.readAndDispatch()) {
176
				d.sleep();
177
			}
178
		}
179
180
	}
181
182
}
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet5.java (-141 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.jface;
11
12
import org.eclipse.jface.viewers.LabelProvider;
13
import org.eclipse.jface.viewers.Viewer;
14
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
15
import org.eclipse.zest.core.viewers.GraphViewer;
16
import org.eclipse.zest.core.viewers.IGraphContentProvider;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.layout.GridLayout;
24
import org.eclipse.swt.widgets.Button;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Shell;
28
29
/**
30
 * This snippet shows how the refresh works on a Zest viewer.
31
 */
32
public class GraphJFaceSnippet5 {
33
34
	static class MyContentProvider implements IGraphContentProvider {
35
36
		Object[] elements = new Object[] { "Rock2Paper", "Paper2Scissors", "Scissors2Rock" };
37
38
		public Object getDestination(Object rel) {
39
			if ("Rock2Paper".equals(rel)) {
40
				return "Rock";
41
			} else if ("Paper2Scissors".equals(rel) || "Scissors2Paper".equals(rel)) {
42
				return "Paper";
43
			} else if ("Scissors2Rock".equals(rel)) {
44
				return "Scissors";
45
			}
46
			return null;
47
		}
48
49
		public Object[] getElements(Object input) {
50
			return elements;
51
		}
52
53
		public Object getSource(Object rel) {
54
			if ("Rock2Paper".equals(rel)) {
55
				return "Paper";
56
			} else if ("Paper2Scissors".equals(rel) || "Scissors2Paper".equals(rel)) {
57
				return "Scissors";
58
			} else if ("Scissors2Rock".equals(rel)) {
59
				return "Rock";
60
			}
61
			return null;
62
		}
63
64
		public void setElements(Object[] elements) {
65
			this.elements = elements;
66
		}
67
68
		public double getWeight(Object connection) {
69
			return 0;
70
		}
71
72
		public void dispose() {
73
		}
74
75
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
76
		}
77
78
	}
79
80
	static class MyLabelProvider extends LabelProvider {
81
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);	
82
		
83
		public MyLabelProvider() {
84
			
85
		}
86
		
87
		public String getText(Object element) {
88
			return element.toString();
89
		}
90
		
91
		public Image getImage(Object element) {
92
			return image;
93
		}
94
	}
95
96
	static GraphViewer viewer = null;
97
	private static MyContentProvider contentProvider;
98
99
	/**
100
	 * @param args
101
	 */
102
	public static void main(String[] args) {
103
		Display d = new Display();
104
		Shell shell = new Shell(d);
105
		shell.setText("GraphJFaceSnippet2");
106
		shell.setLayout(new FillLayout(SWT.VERTICAL));
107
		shell.setSize(400, 400);
108
		Composite parent = new Composite(shell, SWT.NONE);
109
		parent.setLayout(new GridLayout(2, false));
110
		buildViewer(parent);
111
		buildButton(parent);
112
		shell.open();
113
		while (!shell.isDisposed()) {
114
			while (!d.readAndDispatch()) {
115
				d.sleep();
116
			}
117
		}
118
119
	}
120
121
	private static void buildButton(Composite parent) {
122
		Button button = new Button(parent, SWT.PUSH);
123
		button.setText("Refresh");
124
		button.addSelectionListener(new SelectionAdapter() {
125
			public void widgetSelected(SelectionEvent e) {
126
				contentProvider.setElements(new Object[] { "Rock2Paper", "Scissors2Paper", "Scissors2Rock" });
127
				viewer.refresh();
128
			}
129
		});
130
	}
131
132
	private static void buildViewer(Composite parent) {
133
		viewer = new GraphViewer(parent, SWT.NONE);
134
		viewer.getGraphControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
135
		contentProvider = new MyContentProvider();
136
		viewer.setContentProvider(contentProvider);
137
		viewer.setLabelProvider(new MyLabelProvider());
138
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
139
		viewer.setInput(new Object());
140
	}
141
}
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet2.java (-4 / +5 lines)
Lines 1-17 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
9
 ******************************************************************************/
11
 ******************************************************************************/
10
package org.eclipse.zest.tests.jface;
12
package org.eclipse.zest.tests.jface;
11
13
12
import org.eclipse.jface.viewers.LabelProvider;
14
import org.eclipse.jface.viewers.LabelProvider;
13
import org.eclipse.jface.viewers.Viewer;
15
import org.eclipse.jface.viewers.Viewer;
14
import org.eclipse.zest.layouts.LayoutStyles;
15
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
16
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
16
import org.eclipse.zest.core.viewers.GraphViewer;
17
import org.eclipse.zest.core.viewers.GraphViewer;
17
import org.eclipse.zest.core.viewers.IGraphContentProvider;
18
import org.eclipse.zest.core.viewers.IGraphContentProvider;
Lines 107-113 Link Here
107
		viewer = new GraphViewer(shell, SWT.NONE);
108
		viewer = new GraphViewer(shell, SWT.NONE);
108
		viewer.setContentProvider(new MyContentProvider());
109
		viewer.setContentProvider(new MyContentProvider());
109
		viewer.setLabelProvider(new MyLabelProvider());
110
		viewer.setLabelProvider(new MyLabelProvider());
110
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
111
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
111
		viewer.setInput(new Object());
112
		viewer.setInput(new Object());
112
		shell.open();
113
		shell.open();
113
		while (!shell.isDisposed()) {
114
		while (!shell.isDisposed()) {
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet4.java (-133 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.jface;
11
12
import java.util.Iterator;
13
14
import org.eclipse.jface.viewers.ISelectionChangedListener;
15
import org.eclipse.jface.viewers.LabelProvider;
16
import org.eclipse.jface.viewers.SelectionChangedEvent;
17
import org.eclipse.jface.viewers.StructuredSelection;
18
import org.eclipse.jface.viewers.Viewer;
19
import org.eclipse.zest.layouts.LayoutStyles;
20
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
21
import org.eclipse.zest.core.viewers.GraphViewer;
22
import org.eclipse.zest.core.viewers.IGraphContentProvider;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.graphics.Image;
25
import org.eclipse.swt.layout.FillLayout;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Shell;
28
29
public class GraphJFaceSnippet4 {
30
	static class MyContentProvider implements IGraphContentProvider {
31
32
		public Object getDestination(Object rel) {
33
			if ("Rock2Paper".equals(rel)) {
34
				return "Rock";
35
			} else if ("Paper2Scissors".equals(rel)) {
36
				return "Paper";
37
			} else if ("Scissors2Rock".equals(rel)) {
38
				return "Scissors";
39
			}
40
			return null;
41
		}
42
43
		public Object[] getElements(Object input) {
44
			return new Object[] { "Rock2Paper", "Paper2Scissors", "Scissors2Rock" };
45
		}
46
47
		public Object getSource(Object rel) {
48
			if ("Rock2Paper".equals(rel)) {
49
				return "Paper";
50
			} else if ("Paper2Scissors".equals(rel)) {
51
				return "Scissors";
52
			} else if ("Scissors2Rock".equals(rel)) {
53
				return "Rock";
54
			}
55
			return null;
56
		}
57
58
		public double getWeight(Object connection) {
59
			return 0;
60
		}
61
62
		public void dispose() {
63
		}
64
65
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
66
		}
67
68
	}
69
70
	static class MyLabelProvider extends LabelProvider {
71
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
72
73
		public Image getImage(Object element) {
74
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
75
				return image;
76
			}
77
			return null;
78
		}
79
80
		public String getText(Object element) {
81
			return element.toString();
82
		}
83
84
	}
85
86
	static GraphViewer viewer = null;
87
88
	/**
89
	 * @param args
90
	 */
91
	public static void main(String[] args) {
92
		Display d = new Display();
93
		Shell shell = new Shell(d);
94
		shell.setText("GraphJFaceSnippet2");
95
		shell.setLayout(new FillLayout(SWT.VERTICAL));
96
		shell.setSize(400, 400);
97
		viewer = new GraphViewer(shell, SWT.NONE);
98
		viewer.setContentProvider(new MyContentProvider());
99
		viewer.setLabelProvider(new MyLabelProvider());
100
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
101
		viewer.setInput(new Object());
102
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
103
104
			public void selectionChanged(SelectionChangedEvent event) {
105
				System.out.println("Selection Changed: " + selectionToString((StructuredSelection) event.getSelection()));
106
			}
107
108
			private String selectionToString(StructuredSelection selection) {
109
				StringBuffer stringBuffer = new StringBuffer();
110
				Iterator iterator = selection.iterator();
111
				boolean first = true;
112
				while (iterator.hasNext()) {
113
					if (first) {
114
						first = false;
115
					} else {
116
						stringBuffer.append(" : ");
117
					}
118
					stringBuffer.append(iterator.next());
119
				}
120
				return stringBuffer.toString();
121
			}
122
123
		});
124
		shell.open();
125
		while (!shell.isDisposed()) {
126
			while (!d.readAndDispatch()) {
127
				d.sleep();
128
			}
129
		}
130
131
	}
132
133
}
(-)src/org/eclipse/zest/tests/jface/GraphJFaceSnippet6.java (-147 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors: The Chisel Group, University of Victoria
9
 ******************************************************************************/
10
package org.eclipse.zest.tests.jface;
11
12
13
import org.eclipse.jface.viewers.LabelProvider;
14
import org.eclipse.jface.viewers.Viewer;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.events.SelectionListener;
18
import org.eclipse.swt.graphics.Image;
19
import org.eclipse.swt.layout.FillLayout;
20
import org.eclipse.swt.widgets.Button;
21
import org.eclipse.swt.widgets.Display;
22
import org.eclipse.swt.widgets.Shell;
23
import org.eclipse.zest.core.viewers.EntityConnectionData;
24
import org.eclipse.zest.core.viewers.GraphViewer;
25
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
26
import org.eclipse.zest.core.viewers.INestedContentProvider;
27
import org.eclipse.zest.layouts.LayoutStyles;
28
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
29
30
/**
31
 * This snippet shows how to use the INestedGraphContentProvider to create a graph
32
 * with Zest. In this example, getElements returns 3 edges: * Rock2Paper *
33
 * Paper2Scissors * Scissors2Rock
34
 * 
35
 * And for each of these, the source and destination are returned in getSource
36
 * and getDestination.
37
 * 
38
 * A label provider is also used to create the text and icons for the graph.
39
 * 
40
 * @author Ian Bull
41
 * 
42
 */
43
public class GraphJFaceSnippet6 {
44
45
	static class MyContentProvider implements IGraphEntityContentProvider, INestedContentProvider {
46
47
		public Object[] getConnectedTo(Object entity) {
48
			if (entity.equals("First")) {
49
				return new Object[] { "Second" };
50
			}
51
			if (entity.equals("Second")) {
52
				return new Object[] { "Third", "rock" };
53
			}
54
			if (entity.equals("Third")) {
55
				return new Object[] { "First" };
56
			}
57
			if ( entity.equals("rock")) {
58
				return new Object[] { "paper" };
59
			}
60
			return null;
61
		}
62
63
		public Object[] getElements(Object inputElement) {
64
			return new String[] { "First", "Second", "Third" };
65
		}
66
67
		public double getWeight(Object entity1, Object entity2) {
68
			return 0;
69
		}
70
71
		public void dispose() {
72
73
		}
74
75
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
76
77
		}
78
79
		public Object[] getChildren(Object element) {
80
			// TODO Auto-generated method stub
81
			return new Object[] {"rock", "paper", "scissors"};
82
		}
83
84
		public boolean hasChildren(Object element) {
85
			// TODO Auto-generated method stub
86
			if ( element.equals("First")) return true;
87
			return false;
88
		}
89
90
	}
91
92
	static class MyLabelProvider extends LabelProvider {
93
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
94
95
		public Image getImage(Object element) {
96
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
97
				return image;
98
			}
99
			return null;
100
		}
101
102
		public String getText(Object element) {
103
			if ( element instanceof EntityConnectionData ) return "";
104
			return element.toString();
105
		}
106
107
	}
108
109
	static GraphViewer viewer = null;
110
111
	/**
112
	 * @param args
113
	 */
114
	public static void main(String[] args) {
115
		Display d = new Display();
116
		Shell shell = new Shell(d);
117
		shell.setText("GraphJFaceSnippet2");
118
		shell.setLayout(new FillLayout(SWT.VERTICAL));
119
		shell.setSize(400, 400);
120
		viewer = new GraphViewer(shell, SWT.NONE);
121
		viewer.setContentProvider(new MyContentProvider());
122
		viewer.setLabelProvider(new MyLabelProvider());
123
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
124
		viewer.setInput(new Object());
125
		
126
		Button button = new Button(shell, SWT.PUSH);
127
		button.setText("push");
128
		button.addSelectionListener(new SelectionListener() {
129
130
			public void widgetDefaultSelected(SelectionEvent e) {
131
			}
132
133
			public void widgetSelected(SelectionEvent e) {
134
				viewer.setInput(new Object());
135
			}
136
			
137
		});
138
		shell.open();
139
		while (!shell.isDisposed()) {
140
			while (!d.readAndDispatch()) {
141
				d.sleep();
142
			}
143
		}
144
145
	}
146
147
}
(-)src/org/eclipse/zest/tests/swt/TreeLayoutExample.java (+106 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Mateusz Matela and others. All rights reserved. This
3
 * program and the accompanying materials are made available under the terms of
4
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
5
 * available at http://www.eclipse.org/legal/epl-v10.html
6
 * 
7
 * Contributors:
8
 * 	   Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
9
 *******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionAdapter;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.layout.GridData;
16
import org.eclipse.swt.layout.GridLayout;
17
import org.eclipse.swt.widgets.Button;
18
import org.eclipse.swt.widgets.Display;
19
import org.eclipse.swt.widgets.Shell;
20
import org.eclipse.zest.core.widgets.Graph;
21
import org.eclipse.zest.core.widgets.GraphConnection;
22
import org.eclipse.zest.core.widgets.GraphNode;
23
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
24
25
/**
26
 * This example shows the effects of TreeLayoutAlgorithm with different
27
 * directions.
28
 */
29
public class TreeLayoutExample {
30
	/**
31
	 * @param args
32
	 */
33
	public static void main(String[] args) {
34
		// Create the shell
35
		Display d = new Display();
36
		Shell shell = new Shell(d);
37
		shell.setText("GraphSnippet1");
38
		GridLayout gridLayout = new GridLayout();
39
		gridLayout.numColumns = 10;
40
		shell.setLayout(gridLayout);
41
		shell.setSize(500, 500);
42
43
		final Graph g = new Graph(shell, SWT.NONE);
44
		g.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 10, 10));
45
		g.setSize(500, 500);
46
		GraphNode root = new GraphNode(g, SWT.NONE, "Root");
47
		for (int i = 0; i < 3; i++) {
48
			GraphNode n = new GraphNode(g, SWT.NONE, "1 - " + i);
49
			for (int j = 0; j < 3; j++) {
50
				GraphNode n2 = new GraphNode(g, SWT.NONE, "2 - " + j);
51
				new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);
52
			}
53
			new GraphConnection(g, SWT.NONE, root, n);
54
		}
55
56
		final TreeLayoutAlgorithm algorithm = new TreeLayoutAlgorithm();
57
		g.setLayoutAlgorithm(algorithm, false);
58
59
		final Button buttonTopDown = new Button(shell, SWT.FLAT);
60
		buttonTopDown.setText("TOP_DOWN");
61
62
		final Button buttonBottomUp = new Button(shell, SWT.FLAT);
63
		buttonBottomUp.setText("BOTTOM_UP");
64
		buttonBottomUp.setLayoutData(new GridData());
65
66
		final Button buttonLeftRight = new Button(shell, SWT.FLAT);
67
		buttonLeftRight.setText("LEFT_RIGHT");
68
69
		final Button buttonRightLeft = new Button(shell, SWT.FLAT);
70
		buttonRightLeft.setText("RIGHT_LEFT");
71
72
		SelectionAdapter buttonListener = new SelectionAdapter() {
73
			public void widgetSelected(SelectionEvent e) {
74
				if (e.widget == buttonTopDown)
75
					algorithm.setDirection(TreeLayoutAlgorithm.TOP_DOWN);
76
				if (e.widget == buttonBottomUp)
77
					algorithm.setDirection(TreeLayoutAlgorithm.BOTTOM_UP);
78
				if (e.widget == buttonLeftRight)
79
					algorithm.setDirection(TreeLayoutAlgorithm.LEFT_RIGHT);
80
				if (e.widget == buttonRightLeft)
81
					algorithm.setDirection(TreeLayoutAlgorithm.RIGHT_LEFT);
82
83
				g.applyLayout();
84
			}
85
		};
86
		buttonTopDown.addSelectionListener(buttonListener);
87
		buttonBottomUp.addSelectionListener(buttonListener);
88
		buttonLeftRight.addSelectionListener(buttonListener);
89
		buttonRightLeft.addSelectionListener(buttonListener);
90
		
91
		final Button resizeButton = new Button(shell, SWT.CHECK);
92
		resizeButton.setText("Resize");
93
		resizeButton.addSelectionListener(new SelectionAdapter() {
94
			public void widgetSelected(SelectionEvent e) {
95
				algorithm.setResizing(resizeButton.getSelection());
96
			}
97
		});
98
99
		shell.open();
100
		while (!shell.isDisposed()) {
101
			while (!d.readAndDispatch()) {
102
				d.sleep();
103
			}
104
		}
105
	}
106
}
(-)src/org/eclipse/zest/tests/swt/IconsGraphSnippet.java (+66 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 *******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.zest.core.widgets.Graph;
15
import org.eclipse.zest.core.widgets.GraphConnection;
16
import org.eclipse.zest.core.widgets.GraphNode;
17
import org.eclipse.zest.core.widgets.ZestStyles;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Shell;
24
25
/**
26
 * This snippet creates a very simple graph with an Icon and Label.
27
 * 
28
 * This snippet shows how to use directed edges and self loops.
29
 * 
30
 * @author Ian Bull
31
 * 
32
 */
33
public class IconsGraphSnippet {
34
35
	public static void main(String[] args) {
36
		Display d = new Display();
37
		Shell shell = new Shell(d);
38
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
39
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
40
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
41
		shell.setLayout(new FillLayout());
42
		shell.setSize(400, 400);
43
44
		Graph g = new Graph(shell, SWT.NONE);
45
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
46
		GraphNode n1 = new GraphNode(g, SWT.NONE, "Information", image1);
47
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Warning", image2);
48
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Error", image3);
49
50
		new GraphConnection(g, SWT.NONE, n1, n2);
51
		new GraphConnection(g, SWT.NONE, n2, n3);
52
		new GraphConnection(g, SWT.NONE, n3, n3);
53
54
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
55
56
		shell.open();
57
		while (!shell.isDisposed()) {
58
			while (!d.readAndDispatch()) {
59
				d.sleep();
60
			}
61
		}
62
		image1.dispose();
63
		image2.dispose();
64
		image3.dispose();
65
	}
66
}
(-)src/org/eclipse/zest/tests/swt/SimpleGraphSnippet.java (+61 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.layout.FillLayout;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.swt.widgets.Shell;
18
import org.eclipse.zest.core.widgets.Graph;
19
import org.eclipse.zest.core.widgets.GraphConnection;
20
import org.eclipse.zest.core.widgets.GraphNode;
21
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
22
23
/**
24
 * This snippet creates a very simple graph where Rock is connected to Paper
25
 * which is connected to scissors which is connected to rock.
26
 * 
27
 * The nodes a positioned using a SpringLayout Algorithm, and they can be moved
28
 * around.
29
 * 
30
 * 
31
 * @author Ian Bull
32
 * 
33
 */
34
public class SimpleGraphSnippet {
35
36
	public static void main(String[] args) {
37
		Display d = new Display();
38
		Shell shell = new Shell(d);
39
		shell.setText("GraphSnippet1");
40
		shell.setLayout(new FillLayout());
41
		shell.setSize(400, 400);
42
43
		Graph g = new Graph(shell, SWT.NONE);
44
45
		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
46
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
47
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
48
		new GraphConnection(g, SWT.NONE, n, n2);
49
		new GraphConnection(g, SWT.NONE, n2, n3);
50
		//new GraphConnection(g, SWT.NONE, n3, n);
51
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
52
53
		shell.open();
54
		while (!shell.isDisposed()) {
55
			while (!d.readAndDispatch()) {
56
				d.sleep();
57
			}
58
		}
59
	}
60
61
}
(-)src/org/eclipse/zest/tests/swt/FindFigureGraphSnippet.java (+72 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.zest.core.widgets.Graph;
15
import org.eclipse.zest.core.widgets.GraphConnection;
16
import org.eclipse.zest.core.widgets.GraphNode;
17
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.MouseEvent;
20
import org.eclipse.swt.events.MouseMoveListener;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Shell;
24
25
/**
26
 * This snippet shows how to use the findFigureAt to get the figure under the mouse
27
 * 
28
 * @author Ian Bull
29
 * 
30
 */
31
public class FindFigureGraphSnippet {
32
33
	/**
34
	 * @param args
35
	 */
36
	public static void main(String[] args) {
37
		Display d = new Display();
38
		Shell shell = new Shell(d);
39
		shell.setText("GraphSnippet7");
40
		shell.setLayout(new FillLayout());
41
		shell.setSize(400, 400);
42
43
		final Graph g = new Graph(shell, SWT.NONE);
44
45
		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
46
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
47
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
48
		new GraphConnection(g, SWT.NONE, n, n2);
49
		new GraphConnection(g, SWT.NONE, n2, n3);
50
		new GraphConnection(g, SWT.NONE, n3, n);
51
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
52
		
53
		g.addMouseMoveListener(new MouseMoveListener() {
54
55
			public void mouseMove(MouseEvent e) {
56
				// Get the figure at the current mouse location 
57
				Object o = g.getFigureAt(e.x, e.y);
58
				if ( o != null ) {
59
					System.out.println(o + " is at: (" + e.x + "," + e.y + ")");
60
				}
61
			}
62
			
63
		});
64
65
		shell.open();
66
		while (!shell.isDisposed()) {
67
			while (!d.readAndDispatch()) {
68
				d.sleep();
69
			}
70
		}
71
	}
72
}
(-)src/org/eclipse/zest/tests/swt/FisheyeGraphSnippet.java (+71 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 *******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.graphics.Image;
16
import org.eclipse.swt.layout.FillLayout;
17
import org.eclipse.swt.widgets.Display;
18
import org.eclipse.swt.widgets.Shell;
19
import org.eclipse.zest.core.widgets.Graph;
20
import org.eclipse.zest.core.widgets.GraphConnection;
21
import org.eclipse.zest.core.widgets.GraphNode;
22
import org.eclipse.zest.core.widgets.ZestStyles;
23
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
24
25
/**
26
 * This snippet creates a graph with 80*3 nodes (240 nodes).  Only the icons are shown for the nodes, but if
27
 * you mouse over the node you get the entire text.
28
 * 
29
 * @author Ian Bull
30
 * 
31
 */
32
public class FisheyeGraphSnippet {
33
34
	/**
35
	 * @param args
36
	 */
37
	public static void main(String[] args) {
38
		Display d = new Display();
39
		Shell shell = new Shell(d);
40
		shell.setText("GraphSnippet6");
41
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
42
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
43
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
44
		shell.setLayout(new FillLayout());
45
		shell.setSize(800, 800);
46
47
		Graph g = new Graph(shell, SWT.NONE);
48
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
49
		for (int i = 0; i < 80; i++) {
50
			GraphNode n1 = new GraphNode(g, ZestStyles.NODES_HIDE_TEXT | ZestStyles.NODES_FISHEYE, "Information", image1);
51
			GraphNode n2 = new GraphNode(g, ZestStyles.NODES_HIDE_TEXT | ZestStyles.NODES_FISHEYE, "Warning", image2);
52
			GraphNode n3 = new GraphNode(g, ZestStyles.NODES_HIDE_TEXT | ZestStyles.NODES_FISHEYE, "Error", image3);
53
			new GraphConnection(g, SWT.NONE, n1, n2);
54
			new GraphConnection(g, SWT.NONE, n2, n3);
55
			new GraphConnection(g, SWT.NONE, n3, n3);
56
		}
57
		g.setLayoutAlgorithm(new GridLayoutAlgorithm(), true);
58
59
		shell.open();
60
		while (!shell.isDisposed()) {
61
			while (!d.readAndDispatch()) {
62
				d.sleep();
63
			}
64
		}
65
		image1.dispose();
66
		image2.dispose();
67
		image3.dispose();
68
69
	}
70
71
}
(-)src/org/eclipse/zest/tests/swt/CustomFigureGraphSnippet.java (+165 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import java.util.Iterator;
15
16
import org.eclipse.draw2d.ColorConstants;
17
import org.eclipse.draw2d.Ellipse;
18
import org.eclipse.draw2d.Figure;
19
import org.eclipse.draw2d.FreeformLayout;
20
import org.eclipse.draw2d.IFigure;
21
import org.eclipse.draw2d.ImageFigure;
22
import org.eclipse.draw2d.PolylineShape;
23
import org.eclipse.draw2d.geometry.Point;
24
import org.eclipse.draw2d.geometry.Rectangle;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.events.SelectionEvent;
27
import org.eclipse.swt.events.SelectionListener;
28
import org.eclipse.swt.graphics.Image;
29
import org.eclipse.swt.layout.FillLayout;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Shell;
32
import org.eclipse.zest.core.widgets.CGraphNode;
33
import org.eclipse.zest.core.widgets.Graph;
34
import org.eclipse.zest.core.widgets.GraphConnection;
35
import org.eclipse.zest.core.widgets.GraphNode;
36
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
37
38
/**
39
 * 
40
 * This snippet shows how to create a graph with custom figures as nodes.
41
 * 
42
 * @author Ian Bull
43
 * 
44
 */
45
public class CustomFigureGraphSnippet {
46
47
	public static IFigure createPersonFigure(Image headImage) {
48
		Figure person = new Figure();
49
		person.setLayoutManager(new FreeformLayout());
50
		IFigure head = null;
51
		if ( headImage != null ) {
52
			headImage = new Image(headImage.getDevice(), headImage.getImageData().scaledTo(40, 50));
53
			head = new ImageFigure(headImage);
54
		}
55
		else
56
			head = new Ellipse();
57
		head.setSize(40, 50);
58
		
59
		PolylineShape body = new PolylineShape();
60
		body.setLineWidth(1);
61
		body.setStart(new Point(20,40));
62
		body.setEnd(new Point(20,100));
63
		body.setBounds(new Rectangle(0,0,40,100));
64
		
65
		PolylineShape leftLeg = new PolylineShape();
66
		leftLeg.setLineWidth(1);
67
		leftLeg.setStart(new Point(20,100));
68
		leftLeg.setEnd(new Point(0,130));
69
		leftLeg.setBounds(new Rectangle(0,0,40,130));
70
		
71
		PolylineShape rightLeg = new PolylineShape();
72
		rightLeg.setLineWidth(1);
73
		rightLeg.setStart(new Point(20,100));
74
		rightLeg.setEnd(new Point(40,130));
75
		rightLeg.setBounds(new Rectangle(0,0,40,130));
76
		
77
		PolylineShape leftArm = new PolylineShape();
78
		leftArm.setLineWidth(1);
79
		leftArm.setStart(new Point(20,60));
80
		leftArm.setEnd(new Point(0,50));
81
		leftArm.setBounds(new Rectangle(0,0,40,130));
82
		
83
		PolylineShape rightArm = new PolylineShape();
84
		rightArm.setLineWidth(1);
85
		rightArm.setStart(new Point(20,60));
86
		rightArm.setEnd(new Point(40,50));
87
		rightArm.setBounds(new Rectangle(0,0,40,130));
88
		
89
		person.add(head);
90
		person.add(body);
91
		person.add(leftLeg);
92
		person.add(rightLeg);
93
		person.add(leftArm);
94
		person.add(rightArm);
95
		person.setSize(40,130);
96
		return person;
97
	}
98
99
	public static void main(String[] args) {
100
		final Display d = new Display();
101
		Shell shell = new Shell(d);
102
		shell.setText("GraphSnippet11");
103
		shell.setLayout(new FillLayout());
104
		shell.setSize(400, 400);
105
106
		
107
		final Graph g = new Graph(shell, SWT.NONE);
108
		g.addSelectionListener(new SelectionListener(){
109
		
110
			public void widgetSelected(SelectionEvent e) {
111
				Iterator iter = g.getSelection().iterator();
112
				while(iter.hasNext()) {
113
					Object o = iter.next();
114
					if ( o instanceof CGraphNode) {
115
						IFigure figure = ((CGraphNode)o).getFigure();
116
						figure.setBackgroundColor(ColorConstants.blue);
117
						figure.setForegroundColor(ColorConstants.blue);
118
					}
119
				}
120
				iter = g.getNodes().iterator();
121
				while ( iter.hasNext()) {
122
					Object o = iter.next();
123
					if ( o instanceof CGraphNode) {
124
						if ( !g.getSelection().contains(o)) {
125
							((CGraphNode)o).getFigure().setBackgroundColor(ColorConstants.black);
126
							((CGraphNode)o).getFigure().setForegroundColor(ColorConstants.black);
127
						}
128
					}
129
				}
130
			}
131
		
132
			public void widgetDefaultSelected(SelectionEvent e) {
133
				// TODO Auto-generated method stub
134
				
135
			}
136
		});
137
		
138
		Image zx = new Image(d, "zx.png");
139
		Image ibull = new Image(d, "ibull.jpg");
140
		CGraphNode n = new CGraphNode(g, SWT.NONE, createPersonFigure(zx));
141
		CGraphNode n2 = new CGraphNode(g, SWT.NONE,  createPersonFigure(ibull));
142
		GraphNode n3 = new GraphNode(g, SWT.NONE, "PDE");
143
		GraphNode n4 = new GraphNode(g, SWT.NONE, "Zest");
144
		GraphNode n5 = new GraphNode(g, SWT.NONE, "PDE Viz tool");
145
		
146
		new GraphConnection(g, SWT.NONE, n, n2);
147
		new GraphConnection(g, SWT.NONE, n, n3);
148
		new GraphConnection(g, SWT.NONE, n2, n4);
149
		new GraphConnection(g, SWT.NONE, n, n5);
150
		new GraphConnection(g, SWT.NONE, n2, n5);
151
		new GraphConnection(g, SWT.NONE, n3, n5);
152
		new GraphConnection(g, SWT.NONE, n4, n5);
153
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
154
155
		shell.open();
156
		while (!shell.isDisposed()) {
157
			while (!d.readAndDispatch()) {
158
				d.sleep();
159
			}
160
		}
161
		zx.dispose();
162
		ibull.dispose();
163
	}
164
165
}
(-)src/org/eclipse/zest/tests/swt/CurvedEdgeGraphSnippet2.java (+69 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.draw2d.ColorConstants;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.graphics.Color;
17
import org.eclipse.swt.layout.FillLayout;
18
import org.eclipse.swt.widgets.Display;
19
import org.eclipse.swt.widgets.Shell;
20
import org.eclipse.zest.core.widgets.Graph;
21
import org.eclipse.zest.core.widgets.GraphConnection;
22
import org.eclipse.zest.core.widgets.GraphNode;
23
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
24
25
/**
26
 * 
27
 * This snippet shows how to create a curved connection using Zest.
28
 * 
29
 * @author Ian Bull
30
 * 
31
 */
32
public class CurvedEdgeGraphSnippet2 {
33
34
35
	public static void createConnection( Graph g, GraphNode n1, GraphNode n2, Color color, int curve) {
36
		GraphConnection connection = new GraphConnection(g, SWT.NONE, n1, n2);
37
		connection.setLineColor(color);
38
		connection.setCurveDepth(curve);
39
		connection.setLineWidth(1);
40
	}
41
	
42
	public static void main(String[] args) {
43
		Display d = new Display();
44
		Shell shell = new Shell(d);
45
		shell.setText("GraphSnippet11");
46
		shell.setLayout(new FillLayout());
47
		shell.setSize(400, 400);
48
49
		final Graph g = new Graph(shell, SWT.NONE);
50
		GraphNode n = new GraphNode(g, SWT.NONE, "Node 1");
51
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Node 2");
52
		createConnection(g, n, n2, ColorConstants.darkGreen, 20);
53
		createConnection(g, n, n2, ColorConstants.darkGreen, -20);
54
		createConnection(g, n, n2, ColorConstants.darkBlue, 40);
55
		createConnection(g, n, n2, ColorConstants.darkBlue, -40);
56
		createConnection(g, n, n2, ColorConstants.darkGray, 60);
57
		createConnection(g, n, n2, ColorConstants.darkGray, -60);
58
		createConnection(g, n, n2, ColorConstants.black, 0);
59
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
60
61
		shell.open();
62
		while (!shell.isDisposed()) {
63
			while (!d.readAndDispatch()) {
64
				d.sleep();
65
			}
66
		}
67
	}
68
69
}
(-)src/org/eclipse/zest/tests/swt/CustomPaintingGraphSnippet.java (+139 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Map;
19
20
import org.eclipse.draw2d.ColorConstants;
21
import org.eclipse.zest.core.widgets.Graph;
22
import org.eclipse.zest.core.widgets.GraphConnection;
23
import org.eclipse.zest.core.widgets.GraphItem;
24
import org.eclipse.zest.core.widgets.GraphNode;
25
import org.eclipse.zest.core.widgets.ZestStyles;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.events.KeyAdapter;
28
import org.eclipse.swt.events.KeyEvent;
29
import org.eclipse.swt.events.PaintEvent;
30
import org.eclipse.swt.events.PaintListener;
31
import org.eclipse.swt.graphics.Font;
32
import org.eclipse.swt.graphics.FontData;
33
import org.eclipse.swt.graphics.Image;
34
import org.eclipse.swt.graphics.Region;
35
import org.eclipse.swt.layout.FillLayout;
36
import org.eclipse.swt.widgets.Display;
37
import org.eclipse.swt.widgets.Shell;
38
39
/**
40
 * This snippet shows how you can add a paint listener to a Zest graph to paint on top of
41
 * the widget.  This snippet allows you to type and it selects all the nodes that match 
42
 * what you type.
43
 * 
44
 * @author Ian Bull
45
 * 
46
 */
47
public class CustomPaintingGraphSnippet {
48
	public static final int BACKSPACE = 8;
49
	public static final int ENTER = 13;
50
51
	/**
52
	 * @param args
53
	 */
54
	public static void main(String[] args) {
55
		final Map figureListing = new HashMap();
56
		final StringBuffer stringBuffer = new StringBuffer();
57
		final Display d = new Display();
58
		FontData fontData = d.getSystemFont().getFontData()[0];
59
		fontData.height = 42;
60
61
		final Font font = new Font(d, fontData);
62
63
		Shell shell = new Shell(d);
64
		shell.setText("Graph Snippet 5");
65
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
66
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
67
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
68
		shell.setLayout(new FillLayout());
69
		shell.setSize(400, 400);
70
71
		final Graph g = new Graph(shell, SWT.NONE);
72
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
73
		GraphNode n1 = new GraphNode(g, SWT.NONE, "org.eclipse.Information", image1);
74
		GraphNode n2 = new GraphNode(g, SWT.NONE, "org.eclipse.Warning", image2);
75
		GraphNode n3 = new GraphNode(g, SWT.NONE, "org.eclipse.Error", image3);
76
		figureListing.put(n1.getText().toLowerCase(), n1);
77
		figureListing.put(n2.getText().toLowerCase(), n2);
78
		figureListing.put(n3.getText().toLowerCase(), n3);
79
80
		new GraphConnection(g, SWT.NONE, n1, n2);
81
		new GraphConnection(g, SWT.NONE, n2, n3);
82
		n1.setLocation(10, 10);
83
		n2.setLocation(200, 10);
84
		n3.setLocation(200, 200);
85
86
		g.addKeyListener(new KeyAdapter() {
87
88
			public void keyPressed(KeyEvent e) {
89
				boolean complete = false;
90
				if (e.keyCode == BACKSPACE) {
91
					if (stringBuffer.length() > 0) {
92
						stringBuffer.deleteCharAt(stringBuffer.length() - 1);
93
					}
94
				} else if (e.keyCode == ENTER) {
95
					complete = true;
96
				} else if ((e.character >= 'a' && e.character <= 'z') || (e.character >= 'A' && e.character <= 'Z') || (e.character == '.') || (e.character >= '0' && e.character <= '9')) {
97
					stringBuffer.append(e.character);
98
				}
99
				Iterator iterator = figureListing.keySet().iterator();
100
				List list = new ArrayList();
101
				if (stringBuffer.length() > 0) {
102
					while (iterator.hasNext()) {
103
						String string = (String) iterator.next();
104
						if (string.indexOf(stringBuffer.toString().toLowerCase()) >= 0) {
105
							list.add(figureListing.get(string));
106
						}
107
					}
108
				}
109
				g.setSelection((GraphItem[]) list.toArray(new GraphItem[list.size()]));
110
				if (complete && stringBuffer.length() > 0) {
111
					stringBuffer.delete(0, stringBuffer.length());
112
				}
113
114
				g.redraw();
115
			}
116
117
		});
118
119
		g.addPaintListener(new PaintListener() {
120
			public void paintControl(PaintEvent e) {
121
				e.gc.setFont(font);
122
				e.gc.setClipping((Region) null);
123
				e.gc.setForeground(ColorConstants.black);
124
				e.gc.drawText(stringBuffer.toString(), 50, 50, true);
125
			}
126
		});
127
128
		shell.open();
129
		while (!shell.isDisposed()) {
130
			while (!d.readAndDispatch()) {
131
				d.sleep();
132
			}
133
		}
134
		image1.dispose();
135
		image2.dispose();
136
		image3.dispose();
137
		font.dispose();
138
	}
139
}
(-)src/org/eclipse/zest/tests/swt/ContainerResizeGraphSnippet.java (+152 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.KeyEvent;
16
import org.eclipse.swt.events.KeyListener;
17
import org.eclipse.swt.graphics.Image;
18
import org.eclipse.swt.layout.FillLayout;
19
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.zest.core.widgets.Graph;
22
import org.eclipse.zest.core.widgets.GraphConnection;
23
import org.eclipse.zest.core.widgets.GraphContainer;
24
import org.eclipse.zest.core.widgets.GraphItem;
25
import org.eclipse.zest.core.widgets.GraphNode;
26
import org.eclipse.zest.core.widgets.ZestStyles;
27
import org.eclipse.zest.layouts.LayoutAlgorithm;
28
import org.eclipse.zest.layouts.algorithms.CompositeLayoutAlgorithm;
29
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
30
import org.eclipse.zest.layouts.algorithms.HorizontalShiftAlgorithm;
31
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
32
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
33
34
public class ContainerResizeGraphSnippet {
35
36
	private static Image image1;
37
	private static Image classImage;
38
39
	public static void createContainer(Graph g) {
40
		GraphContainer a = new GraphContainer(g, SWT.NONE, "SomeClass.java", classImage);
41
		int r = (int) ((Math.random() * 3) + 1);
42
		r = 2;
43
		populateContainer(a, g, r, true);
44
		for (int i = 0; i < 4; i++) {
45
			GraphContainer b = new GraphContainer(g, SWT.NONE, "SomeNestedClass.java", classImage);
46
			r = (int) ((Math.random() * 3) + 1);
47
			r = 2;
48
			populateContainer(b, g, r, false);
49
			new GraphConnection(g, SWT.NONE, a, b);
50
			for (int j = 0; j < 4; j++) {
51
				GraphContainer c = new GraphContainer(g, SWT.NONE, "DefaultAction.java", classImage);
52
				r = (int) ((Math.random() * 3) + 1);
53
				r = 2;
54
				populateContainer(c, g, r, true);
55
				new GraphConnection(g, SWT.NONE, b, c);
56
			}
57
		}
58
	}
59
60
	public static void populateContainer(GraphContainer c, Graph g, int number, boolean radial) {
61
		GraphNode a = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "SomeClass.java", classImage);
62
		for (int i = 0; i < 4; i++) {
63
			GraphNode b = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "SomeNestedClass.java", classImage);
64
			new GraphConnection(g, SWT.NONE, a, b);
65
			for (int j = 0; j < 4; j++) {
66
				GraphNode d = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "DefaultAction.java", classImage);
67
				new GraphConnection(g, SWT.NONE, b, d);
68
				if (number > 2) {
69
					for (int k = 0; k < 4; k++) {
70
						GraphNode e = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "LastAction(Hero).java", classImage);
71
						new GraphConnection(g, SWT.NONE, d, e);
72
						if (number > 3) {
73
							for (int l = 0; l < 4; l++) {
74
								GraphNode f = new GraphNode(c, ZestStyles.NODES_FISHEYE | ZestStyles.NODES_HIDE_TEXT, "LastAction(Hero).java", classImage);
75
								new GraphConnection(g, SWT.NONE, e, f);
76
							}
77
						}
78
					}
79
				}
80
			}
81
		}
82
		if (number == 1) {
83
			c.setScale(0.75);
84
		} else if (number == 2) {
85
			c.setScale(0.50);
86
		} else {
87
			c.setScale(0.25);
88
		}
89
		if (radial) {
90
			c.setLayoutAlgorithm(new RadialLayoutAlgorithm(), true);
91
		} else {
92
			c.setLayoutAlgorithm(new TreeLayoutAlgorithm(), true);
93
		}
94
	}
95
96
	/**
97
	 * @param args
98
	 */
99
	public static void main(String[] args) {
100
		// Create the shell
101
		Display d = new Display();
102
103
		image1 = new Image(Display.getDefault(), ContainerResizeGraphSnippet.class.getResourceAsStream("package_obj.gif"));
104
		classImage = new Image(Display.getDefault(), ContainerResizeGraphSnippet.class.getResourceAsStream("class_obj.gif"));
105
106
		Shell shell = new Shell(d);
107
		shell.setText("GraphSnippet1");
108
		shell.setLayout(new FillLayout());
109
		shell.setSize(500, 800);
110
111
		final Graph g = new Graph(shell, SWT.NONE);
112
		createContainer(g);
113
114
		CompositeLayoutAlgorithm compositeLayoutAlgorithm = new CompositeLayoutAlgorithm(new LayoutAlgorithm[] { new GridLayoutAlgorithm(), new HorizontalShiftAlgorithm() });
115
		//g.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
116
		g.setLayoutAlgorithm(compositeLayoutAlgorithm, true);
117
118
		g.addKeyListener(new KeyListener() {
119
			boolean flip = true;
120
121
			public void keyPressed(KeyEvent e) {
122
123
				if (g.getSelection().size() == 1) {
124
					GraphNode item = (GraphNode) g.getSelection().get(0);
125
					if (item.getItemType() == GraphItem.CONTAINER) {
126
						if (flip) {
127
							(item).setSize(500, 100);
128
						} else {
129
							(item).setSize(0, 0);
130
						}
131
						flip = !flip;
132
					}
133
				}
134
135
			}
136
137
			public void keyReleased(KeyEvent e) {
138
				// TODO Auto-generated method stub
139
140
			}
141
142
		});
143
144
		shell.open();
145
		while (!shell.isDisposed()) {
146
			while (!d.readAndDispatch()) {
147
				d.sleep();
148
			}
149
		}
150
		image1.dispose();
151
	}
152
}
(-)src/org/eclipse/zest/tests/swt/ToolTipGraphSnippet.java (+104 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 *******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.draw2d.IFigure;
15
import org.eclipse.draw2d.Label;
16
import org.eclipse.zest.core.widgets.Graph;
17
import org.eclipse.zest.core.widgets.GraphConnection;
18
import org.eclipse.zest.core.widgets.GraphNode;
19
import org.eclipse.zest.core.widgets.ZestStyles;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.graphics.GC;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.layout.FillLayout;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Shell;
26
27
/**
28
 * This snippet shows how a custom figure can be used as a ToolTip for connections.
29
 * Let your mouse hover over an edge to see the custom tooltip.
30
 * 
31
 * @author Ian Bull
32
 * 
33
 */
34
public class ToolTipGraphSnippet {
35
	
36
	
37
	/**
38
	 * Merges 2 images so they appear beside each other
39
	 * 
40
	 * You must dispose this image!
41
	 * @param image1
42
	 * @param image2
43
	 * @param result
44
	 * @return
45
	 */
46
	public static Image mergeImages(Image image1, Image image2) {
47
		Image mergedImage = new Image(Display.getDefault(), image1.getBounds().width + image2.getBounds().width, image1.getBounds().height);
48
		GC gc = new GC(mergedImage);
49
		gc.drawImage(image1, 0, 0);
50
		gc.drawImage(image2, image1.getBounds().width, 0);
51
		gc.dispose();
52
		return mergedImage;
53
	}
54
	
55
56
	/**
57
	 * @param args
58
	 */
59
	public static void main(String[] args) {
60
		Display d = new Display();
61
		Shell shell = new Shell(d);
62
		shell.setText("Graph Snippet 4");
63
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
64
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
65
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
66
		shell.setLayout(new FillLayout());
67
		shell.setSize(400, 400);
68
69
		Graph g = new Graph(shell, SWT.NONE);
70
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED );
71
		GraphNode n1 = new GraphNode(g, SWT.NONE, "Information", image1);
72
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Warning", image2);
73
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Error", image3);
74
		
75
		GraphConnection connection1 = new GraphConnection(g, SWT.NONE, n1, n2);
76
		GraphConnection connection2 = new GraphConnection(g, SWT.NONE, n2, n3);
77
		
78
		Image information2warningImage = mergeImages(image1, image2);
79
		Image warning2error = mergeImages(image2, image3);
80
		IFigure tooltip1 = new Label("Information to Warning", information2warningImage);
81
		IFigure tooltip2 = new Label("Warning to Error", warning2error);
82
		connection1.setTooltip(tooltip1);
83
		connection2.setTooltip(tooltip2);
84
		
85
		n1.setLocation(10, 10);
86
		n2.setLocation(200, 10);
87
		n3.setLocation(200, 200);
88
89
		shell.open();
90
		while (!shell.isDisposed()) {
91
			while (!d.readAndDispatch()) {
92
				d.sleep();
93
			}
94
		}
95
		
96
		image1.dispose();
97
		image2.dispose();
98
		image3.dispose();
99
		
100
		information2warningImage.dispose();
101
		warning2error.dispose();
102
103
	}
104
}
(-)src/org/eclipse/zest/tests/jface/NestedGraphJFaceSnippet.java (+148 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.jface;
13
14
15
import org.eclipse.jface.viewers.LabelProvider;
16
import org.eclipse.jface.viewers.Viewer;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.events.SelectionEvent;
19
import org.eclipse.swt.events.SelectionListener;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.FillLayout;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Shell;
25
import org.eclipse.zest.core.viewers.EntityConnectionData;
26
import org.eclipse.zest.core.viewers.GraphViewer;
27
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
28
import org.eclipse.zest.core.viewers.INestedContentProvider;
29
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
30
31
/**
32
 * This snippet shows how to use the INestedGraphContentProvider to create a graph
33
 * with Zest. In this example, getElements returns 3 edges: * Rock2Paper *
34
 * Paper2Scissors * Scissors2Rock
35
 * 
36
 * And for each of these, the source and destination are returned in getSource
37
 * and getDestination.
38
 * 
39
 * A label provider is also used to create the text and icons for the graph.
40
 * 
41
 * @author Ian Bull
42
 * 
43
 */
44
public class NestedGraphJFaceSnippet {
45
46
	static class MyContentProvider implements IGraphEntityContentProvider, INestedContentProvider {
47
48
		public Object[] getConnectedTo(Object entity) {
49
			if (entity.equals("First")) {
50
				return new Object[] { "Second" };
51
			}
52
			if (entity.equals("Second")) {
53
				return new Object[] { "Third", "rock" };
54
			}
55
			if (entity.equals("Third")) {
56
				return new Object[] { "First" };
57
			}
58
			if ( entity.equals("rock")) {
59
				return new Object[] { "paper" };
60
			}
61
			return null;
62
		}
63
64
		public Object[] getElements(Object inputElement) {
65
			return new String[] { "First", "Second", "Third" };
66
		}
67
68
		public double getWeight(Object entity1, Object entity2) {
69
			return 0;
70
		}
71
72
		public void dispose() {
73
74
		}
75
76
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
77
78
		}
79
80
		public Object[] getChildren(Object element) {
81
			// TODO Auto-generated method stub
82
			return new Object[] {"rock", "paper", "scissors"};
83
		}
84
85
		public boolean hasChildren(Object element) {
86
			// TODO Auto-generated method stub
87
			if ( element.equals("First")) return true;
88
			return false;
89
		}
90
91
	}
92
93
	static class MyLabelProvider extends LabelProvider {
94
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
95
96
		public Image getImage(Object element) {
97
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
98
				return image;
99
			}
100
			return null;
101
		}
102
103
		public String getText(Object element) {
104
			if ( element instanceof EntityConnectionData ) return "";
105
			return element.toString();
106
		}
107
108
	}
109
110
	static GraphViewer viewer = null;
111
112
	/**
113
	 * @param args
114
	 */
115
	public static void main(String[] args) {
116
		Display d = new Display();
117
		Shell shell = new Shell(d);
118
		shell.setText("GraphJFaceSnippet2");
119
		shell.setLayout(new FillLayout(SWT.VERTICAL));
120
		shell.setSize(400, 400);
121
		viewer = new GraphViewer(shell, SWT.NONE);
122
		viewer.setContentProvider(new MyContentProvider());
123
		viewer.setLabelProvider(new MyLabelProvider());
124
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
125
		viewer.setInput(new Object());
126
		
127
		Button button = new Button(shell, SWT.PUSH);
128
		button.setText("push");
129
		button.addSelectionListener(new SelectionListener() {
130
131
			public void widgetDefaultSelected(SelectionEvent e) {
132
			}
133
134
			public void widgetSelected(SelectionEvent e) {
135
				viewer.setInput(new Object());
136
			}
137
			
138
		});
139
		shell.open();
140
		while (!shell.isDisposed()) {
141
			while (!d.readAndDispatch()) {
142
				d.sleep();
143
			}
144
		}
145
146
	}
147
148
}
(-)src/org/eclipse/zest/tests/swt/FilterGraphSnippet.java (+117 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.draw2d.ColorConstants;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.layout.FillLayout;
17
import org.eclipse.swt.widgets.Display;
18
import org.eclipse.swt.widgets.Shell;
19
import org.eclipse.zest.core.widgets.Graph;
20
import org.eclipse.zest.core.widgets.GraphConnection;
21
import org.eclipse.zest.core.widgets.GraphItem;
22
import org.eclipse.zest.core.widgets.GraphNode;
23
import org.eclipse.zest.core.widgets.LayoutFilter;
24
import org.eclipse.zest.core.widgets.ZestStyles;
25
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
26
27
/**
28
 * This snippet shows how to filter elements in the layout.  The Data on the tree
29
 * connections are set to "False", meaning they won't be filtered.  
30
 * 
31
 * @author Ian Bull
32
 * 
33
 */
34
public class FilterGraphSnippet {
35
36
	/**
37
	 * @param args
38
	 */
39
	public static void main(String[] args) {
40
		Display display = new Display();
41
		Shell shell = new Shell(display);
42
		shell.setText("GraphSnippet8");
43
		shell.setLayout(new FillLayout());
44
		shell.setSize(400, 400);
45
46
		final Graph graph = new Graph(shell, SWT.NONE);
47
48
		GraphNode a = new GraphNode(graph, SWT.NONE, "Root");
49
		GraphNode b = new GraphNode(graph, SWT.NONE, "B");
50
		GraphNode c = new GraphNode(graph, SWT.NONE, "C");
51
		GraphNode d = new GraphNode(graph, SWT.NONE, "D");
52
		GraphNode e = new GraphNode(graph, SWT.NONE, "E");
53
		GraphNode f = new GraphNode(graph, SWT.NONE, "F");
54
		GraphNode g = new GraphNode(graph, SWT.NONE, "G");
55
		GraphNode h = new GraphNode(graph, SWT.NONE, "H");
56
		GraphConnection connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, a, b);
57
		connection.setData(Boolean.TRUE);
58
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, a, c);
59
		connection.setData(Boolean.TRUE);
60
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, a, c);
61
		connection.setData(Boolean.TRUE);
62
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, a, d);
63
		connection.setData(Boolean.TRUE);
64
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, b, e);
65
		connection.setData(Boolean.FALSE);
66
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, b, f);
67
		connection.setData(Boolean.FALSE);
68
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, c, g);
69
		connection.setData(Boolean.FALSE);
70
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, d, h);
71
		connection.setData(Boolean.FALSE);
72
		
73
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, b, c);
74
		connection.setLineColor(ColorConstants.red);
75
		connection.setLineWidth(3);
76
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, c, d);
77
		connection.setLineColor(ColorConstants.red);
78
		connection.setLineWidth(3);
79
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, e, f);
80
		connection.setLineColor(ColorConstants.red);
81
		connection.setLineWidth(3);
82
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, f, g);
83
		connection.setLineColor(ColorConstants.red);
84
		connection.setLineWidth(3);
85
		
86
		connection = new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, h, e);
87
		connection.setLineColor(ColorConstants.red);
88
		connection.setLineWidth(3);
89
		
90
		TreeLayoutAlgorithm treeLayoutAlgorithm = new TreeLayoutAlgorithm();
91
		LayoutFilter filter = new LayoutFilter() {
92
			public boolean isObjectFiltered(GraphItem item) {
93
				if  (item instanceof GraphConnection ) {
94
					GraphConnection connection = (GraphConnection) item;
95
					Object data = connection.getData();
96
					if ( data != null && data instanceof Boolean ) {
97
						// If the data is false, don't filter, otherwise, filter.
98
						return ((Boolean)data).booleanValue();
99
					}
100
					return true;
101
				}
102
				return false;
103
			}
104
		};
105
		graph.addLayoutFilter(filter);
106
		graph.setLayoutAlgorithm(treeLayoutAlgorithm, true);
107
		
108
109
		shell.open();
110
		while (!shell.isDisposed()) {
111
			while (!display.readAndDispatch()) {
112
				display.sleep();
113
			}
114
		}
115
	}
116
117
}
(-)src/org/eclipse/zest/tests/jface/CustomFigureGraphJFaceSnippet.java (+177 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.jface;
13
14
15
import org.eclipse.draw2d.IFigure;
16
import org.eclipse.draw2d.Label;
17
import org.eclipse.jface.viewers.LabelProvider;
18
import org.eclipse.jface.viewers.Viewer;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.events.SelectionListener;
22
import org.eclipse.swt.graphics.Font;
23
import org.eclipse.swt.graphics.Image;
24
import org.eclipse.swt.layout.FillLayout;
25
import org.eclipse.swt.widgets.Button;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.zest.core.viewers.EntityConnectionData;
29
import org.eclipse.zest.core.viewers.GraphViewer;
30
import org.eclipse.zest.core.viewers.IFigureProvider;
31
import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
32
import org.eclipse.zest.core.viewers.INestedContentProvider;
33
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
34
import org.eclipse.zest.tests.uml.UMLClassFigure;
35
36
/**
37
 * This snippet shows how to use Zest viewers with custom figures.
38
 * It creates a figure containing UML representation of Table class. 
39
 * 
40
 * @author Ian Bull
41
 * 
42
 */
43
public class CustomFigureGraphJFaceSnippet {
44
45
	public static IFigure createClassFigure1(Font classFont, Image classImage, Image publicField, Image privateField) {
46
		Label classLabel1 = new Label("Table", classImage);
47
		classLabel1.setFont(classFont);
48
49
		UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
50
		Label attribute1 = new Label("columns: Column[]", privateField);
51
52
		Label attribute2 = new Label("rows: Row[]", privateField);
53
54
		Label method1 = new Label("getColumns(): Column[]", publicField);
55
		Label method2 = new Label("getRows(): Row[]", publicField);
56
		classFigure.getAttributesCompartment().add(attribute1);
57
		classFigure.getAttributesCompartment().add(attribute2);
58
		classFigure.getMethodsCompartment().add(method1);
59
		classFigure.getMethodsCompartment().add(method2);
60
		classFigure.setSize(-1, -1);
61
62
		return classFigure;
63
	}
64
	
65
	static class MyContentProvider implements IGraphEntityContentProvider, INestedContentProvider {
66
67
		public Object[] getConnectedTo(Object entity) {
68
			if (entity.equals("First")) {
69
				return new Object[] { "Second" };
70
			}
71
			if (entity.equals("Second")) {
72
				return new Object[] { "Third", "rock" };
73
			}
74
			if (entity.equals("Third")) {
75
				return new Object[] { "First" };
76
			}
77
			if ( entity.equals("rock")) {
78
				return new Object[] { "paper" };
79
			}
80
			return null;
81
		}
82
83
		public Object[] getElements(Object inputElement) {
84
			return new String[] { "First", "Second", "Third" };
85
		}
86
87
		public double getWeight(Object entity1, Object entity2) {
88
			return 0;
89
		}
90
91
		public void dispose() {
92
93
		}
94
95
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
96
97
		}
98
99
		public Object[] getChildren(Object element) {
100
			// TODO Auto-generated method stub
101
			return new Object[] {"rock", "paper", "scissors"};
102
		}
103
104
		public boolean hasChildren(Object element) {
105
			// TODO Auto-generated method stub
106
			if ( element.equals("First")) return true;
107
			return false;
108
		}
109
110
	}
111
112
113
	
114
	static class MyLabelProvider extends LabelProvider implements IFigureProvider {
115
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
116
117
		public Image getImage(Object element) {
118
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
119
				return image;
120
			}
121
			return null;
122
		}
123
124
		public String getText(Object element) {
125
			if ( element instanceof EntityConnectionData ) return "";
126
			return element.toString();
127
		}
128
		
129
		public IFigure getFigure(Object element) {
130
			Font classFont = new Font(null, "Arial", 12, SWT.BOLD);
131
			Image classImage = new Image(Display.getDefault(), UMLClassFigure.class.getResourceAsStream("class_obj.gif"));
132
			Image privateField = new Image(Display.getDefault(), UMLClassFigure.class.getResourceAsStream("field_private_obj.gif"));
133
			Image publicField= new Image(Display.getDefault(), UMLClassFigure.class.getResourceAsStream("methpub_obj.gif"));
134
			return createClassFigure1(classFont, classImage, publicField, privateField);
135
		}
136
137
	}
138
139
	static GraphViewer viewer = null;
140
141
	/**
142
	 * @param args
143
	 */
144
	public static void main(String[] args) {
145
		Display d = new Display();
146
		Shell shell = new Shell(d);
147
		shell.setText("GraphJFaceSnippet2");
148
		shell.setLayout(new FillLayout(SWT.VERTICAL));
149
		shell.setSize(400, 400);
150
		viewer = new GraphViewer(shell, SWT.NONE);
151
		viewer.setContentProvider(new MyContentProvider());
152
		viewer.setLabelProvider(new MyLabelProvider());
153
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
154
		viewer.setInput(new Object());
155
		
156
		Button button = new Button(shell, SWT.PUSH);
157
		button.setText("push");
158
		button.addSelectionListener(new SelectionListener() {
159
160
			public void widgetDefaultSelected(SelectionEvent e) {
161
			}
162
163
			public void widgetSelected(SelectionEvent e) {
164
				viewer.setInput(new Object());
165
			}
166
			
167
		});
168
		shell.open();
169
		while (!shell.isDisposed()) {
170
			while (!d.readAndDispatch()) {
171
				d.sleep();
172
			}
173
		}
174
175
	}
176
177
}
(-)src/org/eclipse/zest/tests/swt/RadialLayoutSnippet.java (+58 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 *******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.layout.FillLayout;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.swt.widgets.Shell;
18
import org.eclipse.zest.core.widgets.Graph;
19
import org.eclipse.zest.core.widgets.GraphConnection;
20
import org.eclipse.zest.core.widgets.GraphNode;
21
import org.eclipse.zest.layouts.LayoutAlgorithm;
22
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
23
24
/**
25
 * This example shows how {@link RadialLayoutAlgorithm} lays out a simple tree.
26
 */
27
public class RadialLayoutSnippet {
28
	public static void main(String[] args) {
29
		// Create the shell
30
		Display d = new Display();
31
		Shell shell = new Shell(d);
32
		shell.setText("GraphSnippet1");
33
		shell.setLayout(new FillLayout());
34
		shell.setSize(500, 500);
35
36
		final Graph g = new Graph(shell, SWT.NONE);
37
		g.setSize(500, 500);
38
		GraphNode root = new GraphNode(g, SWT.NONE, "Root");
39
		for (int i = 0; i < 3; i++) {
40
			GraphNode n = new GraphNode(g, SWT.NONE, "1 - " + i);
41
			for (int j = 0; j < 3; j++) {
42
				GraphNode n2 = new GraphNode(g, SWT.NONE, "2 - " + j);
43
				new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);
44
			}
45
			new GraphConnection(g, SWT.NONE, root, n);
46
		}
47
48
		final LayoutAlgorithm layoutAlgorithm = new RadialLayoutAlgorithm();
49
50
		g.setLayoutAlgorithm(layoutAlgorithm, true);
51
		shell.open();
52
		while (!shell.isDisposed()) {
53
			while (!d.readAndDispatch()) {
54
				d.sleep();
55
			}
56
		}
57
	}
58
}
(-)src/org/eclipse/zest/tests/swt/CurvedEdgeGraphSnippet.java (+79 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.SelectionAdapter;
16
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.layout.FillLayout;
18
import org.eclipse.swt.widgets.Button;
19
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.zest.core.widgets.Graph;
22
import org.eclipse.zest.core.widgets.GraphConnection;
23
import org.eclipse.zest.core.widgets.GraphNode;
24
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
25
26
/**
27
 * 
28
 * This snippet shows how to create a curved connection using Zest.
29
 * Each time the Button is clicked, the curve changes.
30
 * 
31
 * @author Ian Bull
32
 * 
33
 */
34
public class CurvedEdgeGraphSnippet {
35
36
	public static void main(String[] args) {
37
		Display d = new Display();
38
		Shell shell = new Shell(d);
39
		shell.setText("GraphSnippet1");
40
		shell.setLayout(new FillLayout());
41
		shell.setSize(400, 400);
42
43
		final Graph g = new Graph(shell, SWT.NONE);
44
45
		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
46
		n.setBorderColor(org.eclipse.draw2d.ColorConstants.yellow);
47
		n.setBorderWidth(3);
48
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
49
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
50
		final GraphConnection connection = new GraphConnection(g, SWT.NONE, n, n2);
51
		connection.setLineWidth(3);
52
		new GraphConnection(g, SWT.NONE, n2, n3);
53
		new GraphConnection(g, SWT.NONE, n3, n);
54
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
55
		
56
		Button button = new Button(shell, SWT.PUSH);
57
		button.setText("Change Curve");
58
		button.addSelectionListener(new SelectionAdapter() {
59
			int count = 0;
60
			public void widgetSelected(SelectionEvent e) {
61
				count = ++count % 16;
62
				if ( count > 8 ) {
63
					connection.setCurveDepth((-16 + count) * 10);
64
				}
65
				else {
66
					connection.setCurveDepth(count * 10);
67
				}
68
			}
69
		});
70
71
		shell.open();
72
		while (!shell.isDisposed()) {
73
			while (!d.readAndDispatch()) {
74
				d.sleep();
75
			}
76
		}
77
	}
78
79
}
(-)src/org/eclipse/zest/tests/swt/SpringLayoutProgressSnippet.java (+87 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Mateusz Matela and others. All rights reserved. This
3
 * program and the accompanying materials are made available under the terms of
4
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
5
 * available at http://www.eclipse.org/legal/epl-v10.html
6
 * 
7
 * Contributors:
8
 * 	   Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
9
 *******************************************************************************/
10
package org.eclipse.zest.tests.swt;
11
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.SelectionAdapter;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.layout.GridData;
16
import org.eclipse.swt.layout.GridLayout;
17
import org.eclipse.swt.widgets.Button;
18
import org.eclipse.swt.widgets.Display;
19
import org.eclipse.swt.widgets.Label;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.zest.core.widgets.Graph;
22
import org.eclipse.zest.core.widgets.GraphConnection;
23
import org.eclipse.zest.core.widgets.GraphNode;
24
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
25
26
/**
27
 * This example allows to see how SpringLayoutAlgorithm changes the position
28
 * of entities in each step.
29
 */
30
public class SpringLayoutProgressSnippet {
31
32
	/**
33
	 * @param args
34
	 */
35
	public static void main(String[] args) {
36
		// Create the shell
37
		Display d = new Display();
38
		Shell shell = new Shell(d);
39
		shell.setText("GraphSnippet1");
40
		GridLayout gridLayout = new GridLayout();
41
		gridLayout.numColumns = 5;
42
		shell.setLayout(gridLayout);
43
		shell.setSize(500, 500);
44
45
		final Graph g = new Graph(shell, SWT.NONE);
46
		g.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5, 5));
47
		g.setSize(500, 500);
48
		GraphNode root = new GraphNode(g, SWT.NONE, "Root");
49
		for (int i = 0; i < 3; i++) {
50
			GraphNode n = new GraphNode(g, SWT.NONE, "1 - " + i);
51
			for (int j = 0; j < 3; j++) {
52
				GraphNode n2 = new GraphNode(g, SWT.NONE, "2 - " + j);
53
				new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);
54
			}
55
			new GraphConnection(g, SWT.NONE, root, n);
56
		}
57
58
59
		final SpringLayoutAlgorithm springLayoutAlgorithm = new SpringLayoutAlgorithm();
60
61
		g.setLayoutAlgorithm(springLayoutAlgorithm, false);
62
63
		Button b = new Button(shell, SWT.FLAT);
64
		b.setText("step");
65
66
		final Label label = new Label(shell, SWT.LEFT);
67
		label.setText("<--click");
68
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
69
70
		b.addSelectionListener(new SelectionAdapter() {
71
			int steps = 0;
72
			public void widgetSelected(SelectionEvent e) {
73
				springLayoutAlgorithm.performOneIteration();
74
				g.redraw();
75
				label.setText("steps: " + ++steps);
76
			}
77
		});
78
79
		shell.open();
80
		while (!shell.isDisposed()) {
81
			while (!d.readAndDispatch()) {
82
				d.sleep();
83
			}
84
		}
85
	}
86
87
}
(-)src/org/eclipse/zest/tests/jface/RefreshGraphJFaceSnippet.java (+143 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.jface;
13
14
import org.eclipse.jface.viewers.LabelProvider;
15
import org.eclipse.jface.viewers.Viewer;
16
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
17
import org.eclipse.zest.core.viewers.GraphViewer;
18
import org.eclipse.zest.core.viewers.IGraphContentProvider;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.layout.FillLayout;
24
import org.eclipse.swt.layout.GridData;
25
import org.eclipse.swt.layout.GridLayout;
26
import org.eclipse.swt.widgets.Button;
27
import org.eclipse.swt.widgets.Composite;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Shell;
30
31
/**
32
 * This snippet shows how the refresh works on a Zest viewer.
33
 */
34
public class RefreshGraphJFaceSnippet {
35
36
	static class MyContentProvider implements IGraphContentProvider {
37
38
		Object[] elements = new Object[] { "Rock2Paper", "Paper2Scissors", "Scissors2Rock" };
39
40
		public Object getDestination(Object rel) {
41
			if ("Rock2Paper".equals(rel)) {
42
				return "Rock";
43
			} else if ("Paper2Scissors".equals(rel) || "Scissors2Paper".equals(rel)) {
44
				return "Paper";
45
			} else if ("Scissors2Rock".equals(rel)) {
46
				return "Scissors";
47
			}
48
			return null;
49
		}
50
51
		public Object[] getElements(Object input) {
52
			return elements;
53
		}
54
55
		public Object getSource(Object rel) {
56
			if ("Rock2Paper".equals(rel)) {
57
				return "Paper";
58
			} else if ("Paper2Scissors".equals(rel) || "Scissors2Paper".equals(rel)) {
59
				return "Scissors";
60
			} else if ("Scissors2Rock".equals(rel)) {
61
				return "Rock";
62
			}
63
			return null;
64
		}
65
66
		public void setElements(Object[] elements) {
67
			this.elements = elements;
68
		}
69
70
		public double getWeight(Object connection) {
71
			return 0;
72
		}
73
74
		public void dispose() {
75
		}
76
77
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
78
		}
79
80
	}
81
82
	static class MyLabelProvider extends LabelProvider {
83
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);	
84
		
85
		public MyLabelProvider() {
86
			
87
		}
88
		
89
		public String getText(Object element) {
90
			return element.toString();
91
		}
92
		
93
		public Image getImage(Object element) {
94
			return image;
95
		}
96
	}
97
98
	static GraphViewer viewer = null;
99
	private static MyContentProvider contentProvider;
100
101
	/**
102
	 * @param args
103
	 */
104
	public static void main(String[] args) {
105
		Display d = new Display();
106
		Shell shell = new Shell(d);
107
		shell.setText("GraphJFaceSnippet2");
108
		shell.setLayout(new FillLayout(SWT.VERTICAL));
109
		shell.setSize(400, 400);
110
		Composite parent = new Composite(shell, SWT.NONE);
111
		parent.setLayout(new GridLayout(2, false));
112
		buildViewer(parent);
113
		buildButton(parent);
114
		shell.open();
115
		while (!shell.isDisposed()) {
116
			while (!d.readAndDispatch()) {
117
				d.sleep();
118
			}
119
		}
120
121
	}
122
123
	private static void buildButton(Composite parent) {
124
		Button button = new Button(parent, SWT.PUSH);
125
		button.setText("Refresh");
126
		button.addSelectionListener(new SelectionAdapter() {
127
			public void widgetSelected(SelectionEvent e) {
128
				contentProvider.setElements(new Object[] { "Rock2Paper", "Scissors2Paper", "Scissors2Rock" });
129
				viewer.refresh();
130
			}
131
		});
132
	}
133
134
	private static void buildViewer(Composite parent) {
135
		viewer = new GraphViewer(parent, SWT.NONE);
136
		viewer.getGraphControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
137
		contentProvider = new MyContentProvider();
138
		viewer.setContentProvider(contentProvider);
139
		viewer.setLabelProvider(new MyLabelProvider());
140
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
141
		viewer.setInput(new Object());
142
	}
143
}
(-)src/org/eclipse/zest/tests/swt/SelfLoopGraphSnippet.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.layout.FillLayout;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.swt.widgets.Shell;
18
import org.eclipse.zest.core.widgets.Graph;
19
import org.eclipse.zest.core.widgets.GraphConnection;
20
import org.eclipse.zest.core.widgets.GraphNode;
21
import org.eclipse.zest.core.widgets.ZestStyles;
22
23
/**
24
 * This snippet demonstrates a self loop with a label.
25
 * 
26
 * @author Ian Bull
27
 * 
28
 */
29
public class SelfLoopGraphSnippet {
30
31
	/**
32
	 * @param args
33
	 */
34
	public static void main(String[] args) {
35
		Display display = new Display();
36
		Shell shell = new Shell(display);
37
		shell.setText("GraphSnippet9");
38
		shell.setLayout(new FillLayout());
39
		shell.setSize(400, 400);
40
41
		final Graph graph = new Graph(shell, SWT.NONE);
42
43
		GraphNode a = new GraphNode(graph, ZestStyles.CONNECTIONS_DIRECTED, "Root");
44
		GraphConnection connection = new GraphConnection(graph, SWT.NONE, a, a);
45
		connection.setText("A to A");
46
		a.setLocation(100, 100);
47
48
		shell.open();
49
		while (!shell.isDisposed()) {
50
			while (!display.readAndDispatch()) {
51
				display.sleep();
52
			}
53
		}
54
	}
55
56
}
(-)src/org/eclipse/zest/tests/swt/SelectionGraphSnippet.java (+73 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 *******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.zest.core.widgets.Graph;
15
import org.eclipse.zest.core.widgets.GraphConnection;
16
import org.eclipse.zest.core.widgets.GraphNode;
17
import org.eclipse.zest.core.widgets.ZestStyles;
18
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.layout.FillLayout;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Shell;
26
27
/**
28
 * Adds a selection listener to the nodes to tell when a selection event has
29
 * happened.
30
 * 
31
 * @author Ian Bull
32
 * 
33
 */
34
public class SelectionGraphSnippet {
35
36
	public static void main(String[] args) {
37
		Display d = new Display();
38
		Shell shell = new Shell(d);
39
		Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION);
40
		Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
41
		Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR);
42
		shell.setLayout(new FillLayout());
43
		shell.setSize(400, 400);
44
45
		Graph g = new Graph(shell, SWT.NONE);
46
		g.addSelectionListener(new SelectionAdapter() {
47
			public void widgetSelected(SelectionEvent e) {
48
				System.out.println(((Graph) e.widget).getSelection());
49
			}
50
		});
51
		
52
		g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED);
53
		GraphNode n1 = new GraphNode(g, SWT.NONE, "Information", image1);
54
		GraphNode n2 = new GraphNode(g, SWT.NONE, "Warning", image2);
55
		GraphNode n3 = new GraphNode(g, SWT.NONE, "Error", image3);
56
57
		new GraphConnection(g, SWT.NONE, n1, n2);
58
		new GraphConnection(g, SWT.NONE, n2, n3);
59
60
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
61
62
		shell.open();
63
		while (!shell.isDisposed()) {
64
			while (!d.readAndDispatch()) {
65
				d.sleep();
66
			}
67
		}
68
		image1.dispose();
69
		image2.dispose();
70
		image3.dispose();
71
72
	}
73
}
(-)src/org/eclipse/zest/tests/swt/SpringLayoutSnippet.java (+64 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.layout.FillLayout;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.swt.widgets.Shell;
18
import org.eclipse.zest.core.widgets.Graph;
19
import org.eclipse.zest.core.widgets.GraphConnection;
20
import org.eclipse.zest.core.widgets.GraphNode;
21
import org.eclipse.zest.layouts.LayoutAlgorithm;
22
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
23
24
/**
25
 * This example shows how SpringLayoutSnippet layouts a simple graph 
26
 * and how it reacts to different weights of edges.
27
 */
28
public class SpringLayoutSnippet {
29
30
	/**
31
	 * @param args
32
	 */
33
	public static void main(String[] args) {
34
		// Create the shell
35
		Display d = new Display();
36
		Shell shell = new Shell(d);
37
		shell.setText("GraphSnippet1");
38
		shell.setLayout(new FillLayout());
39
		shell.setSize(500, 500);
40
41
		final Graph g = new Graph(shell, SWT.NONE);
42
		g.setSize(500, 500);
43
		GraphNode root = new GraphNode(g, SWT.NONE, "Root");
44
		for (int i = 0; i < 3; i++) {
45
			GraphNode n = new GraphNode(g, SWT.NONE, "1 - " + i);
46
			for (int j = 0; j < 3; j++) {
47
				GraphNode n2 = new GraphNode(g, SWT.NONE, "2 - " + j);
48
				new GraphConnection(g, SWT.NONE, n, n2).setWeight(-1);
49
			}
50
			new GraphConnection(g, SWT.NONE, root, n);
51
		}
52
53
		final LayoutAlgorithm layoutAlgorithm = new SpringLayoutAlgorithm();
54
55
		g.setLayoutAlgorithm(layoutAlgorithm, true);
56
		shell.open();
57
		while (!shell.isDisposed()) {
58
			while (!d.readAndDispatch()) {
59
				d.sleep();
60
			}
61
		}
62
	}
63
64
}
(-)src/org/eclipse/zest/tests/swt/ContainersGraphSnippet.java (+88 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.swt;
13
14
import org.eclipse.draw2d.Figure;
15
import org.eclipse.draw2d.FlowLayout;
16
import org.eclipse.draw2d.IFigure;
17
import org.eclipse.draw2d.ImageFigure;
18
import org.eclipse.draw2d.Label;
19
import org.eclipse.draw2d.MarginBorder;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.graphics.Image;
22
import org.eclipse.swt.layout.FillLayout;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Shell;
25
import org.eclipse.zest.core.widgets.Graph;
26
import org.eclipse.zest.core.widgets.GraphConnection;
27
import org.eclipse.zest.core.widgets.GraphContainer;
28
import org.eclipse.zest.core.widgets.GraphNode;
29
import org.eclipse.zest.core.widgets.ZestStyles;
30
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
31
32
/**
33
 * 
34
 * This snippet shows how to use graph containers.
35
 * 
36
 * @author Ian Bull
37
 * 
38
 */
39
public class ContainersGraphSnippet {
40
41
	public static void main(String[] args) {
42
		final Display d = new Display();
43
		Shell shell = new Shell(d);
44
		shell.setText("GraphSnippet11");
45
		shell.setLayout(new FillLayout());
46
		shell.setSize(400, 400);
47
		
48
		final Graph g = new Graph(shell, SWT.NONE);
49
		
50
		Image zx = new Image(d, "zxsnow.png");
51
		IFigure tooltip = new Figure();
52
		tooltip.setBorder(new MarginBorder(5,5,5,5));
53
		FlowLayout layout = new FlowLayout(false);
54
		layout.setMajorSpacing(3);
55
		layout.setMinorAlignment(3);
56
		tooltip.setLayoutManager(new FlowLayout(false));
57
		tooltip.add(new ImageFigure(zx));
58
		tooltip.add(new Label("Name: Chris Aniszczyk"));
59
		tooltip.add(new Label("Location: Austin, Texas"));
60
		
61
		GraphContainer c1 = new GraphContainer(g, SWT.NONE);
62
		c1.setText("Canada");
63
		GraphContainer c2 = new GraphContainer(g, SWT.NONE);
64
		c2.setText("USA");
65
		
66
		GraphNode n1 = new GraphNode(c1, SWT.NONE, "Ian B.");
67
		n1.setSize(200, 100);
68
		GraphNode n2 = new GraphNode(c2, SWT.NONE, "Chris A.");
69
		n2.setTooltip(tooltip);
70
		
71
		GraphConnection connection = new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, n1, n2);
72
		connection.setCurveDepth(-30);
73
		GraphConnection connection2 = new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, n2, n1);
74
		connection2.setCurveDepth(-30);
75
		
76
		
77
		g.setLayoutAlgorithm(new SpringLayoutAlgorithm(), true);
78
79
		shell.open();
80
		while (!shell.isDisposed()) {
81
			while (!d.readAndDispatch()) {
82
				d.sleep();
83
			}
84
		}
85
		zx.dispose();
86
	}
87
88
}
(-)src/org/eclipse/zest/tests/jface/SelectionListenerGraphJFaceSnippet.java (+134 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.jface;
13
14
import java.util.Iterator;
15
16
import org.eclipse.jface.viewers.ISelectionChangedListener;
17
import org.eclipse.jface.viewers.LabelProvider;
18
import org.eclipse.jface.viewers.SelectionChangedEvent;
19
import org.eclipse.jface.viewers.StructuredSelection;
20
import org.eclipse.jface.viewers.Viewer;
21
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
22
import org.eclipse.zest.core.viewers.GraphViewer;
23
import org.eclipse.zest.core.viewers.IGraphContentProvider;
24
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.graphics.Image;
26
import org.eclipse.swt.layout.FillLayout;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Shell;
29
30
public class SelectionListenerGraphJFaceSnippet {
31
	static class MyContentProvider implements IGraphContentProvider {
32
33
		public Object getDestination(Object rel) {
34
			if ("Rock2Paper".equals(rel)) {
35
				return "Rock";
36
			} else if ("Paper2Scissors".equals(rel)) {
37
				return "Paper";
38
			} else if ("Scissors2Rock".equals(rel)) {
39
				return "Scissors";
40
			}
41
			return null;
42
		}
43
44
		public Object[] getElements(Object input) {
45
			return new Object[] { "Rock2Paper", "Paper2Scissors", "Scissors2Rock" };
46
		}
47
48
		public Object getSource(Object rel) {
49
			if ("Rock2Paper".equals(rel)) {
50
				return "Paper";
51
			} else if ("Paper2Scissors".equals(rel)) {
52
				return "Scissors";
53
			} else if ("Scissors2Rock".equals(rel)) {
54
				return "Rock";
55
			}
56
			return null;
57
		}
58
59
		public double getWeight(Object connection) {
60
			return 0;
61
		}
62
63
		public void dispose() {
64
		}
65
66
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
67
		}
68
69
	}
70
71
	static class MyLabelProvider extends LabelProvider {
72
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);
73
74
		public Image getImage(Object element) {
75
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
76
				return image;
77
			}
78
			return null;
79
		}
80
81
		public String getText(Object element) {
82
			return element.toString();
83
		}
84
85
	}
86
87
	static GraphViewer viewer = null;
88
89
	/**
90
	 * @param args
91
	 */
92
	public static void main(String[] args) {
93
		Display d = new Display();
94
		Shell shell = new Shell(d);
95
		shell.setText("GraphJFaceSnippet2");
96
		shell.setLayout(new FillLayout(SWT.VERTICAL));
97
		shell.setSize(400, 400);
98
		viewer = new GraphViewer(shell, SWT.NONE);
99
		viewer.setContentProvider(new MyContentProvider());
100
		viewer.setLabelProvider(new MyLabelProvider());
101
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm());
102
		viewer.setInput(new Object());
103
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
104
105
			public void selectionChanged(SelectionChangedEvent event) {
106
				System.out.println("Selection Changed: " + selectionToString((StructuredSelection) event.getSelection()));
107
			}
108
109
			private String selectionToString(StructuredSelection selection) {
110
				StringBuffer stringBuffer = new StringBuffer();
111
				Iterator iterator = selection.iterator();
112
				boolean first = true;
113
				while (iterator.hasNext()) {
114
					if (first) {
115
						first = false;
116
					} else {
117
						stringBuffer.append(" : ");
118
					}
119
					stringBuffer.append(iterator.next());
120
				}
121
				return stringBuffer.toString();
122
			}
123
124
		});
125
		shell.open();
126
		while (!shell.isDisposed()) {
127
			while (!d.readAndDispatch()) {
128
				d.sleep();
129
			}
130
		}
131
132
	}
133
134
}
(-)src/org/eclipse/zest/tests/jface/FileProviderJFaceSnippet.java (+136 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright 2005-2009, CHISEL Group, University of Victoria, Victoria, BC,
3
 * Canada. All rights reserved. This program and the accompanying materials are
4
 * made available under the terms of the Eclipse Public License v1.0 which
5
 * accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     The Chisel Group, University of Victoria
10
 *     Mateusz Matela <mateusz.matela@gmail.com> - Adapt snippets to new layout API - https://bugs.eclipse.org/bugs/show_bug.cgi?id=283244
11
 ******************************************************************************/
12
package org.eclipse.zest.tests.jface;
13
14
import java.io.BufferedReader;
15
import java.io.File;
16
import java.io.FileReader;
17
import java.io.IOException;
18
import java.util.ArrayList;
19
import java.util.StringTokenizer;
20
21
import org.eclipse.jface.viewers.LabelProvider;
22
import org.eclipse.jface.viewers.Viewer;
23
import org.eclipse.zest.core.viewers.GraphViewer;
24
import org.eclipse.zest.core.viewers.IGraphContentProvider;
25
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.layout.FillLayout;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.FileDialog;
30
import org.eclipse.swt.widgets.Shell;
31
32
/**
33
 * This snippet uses a very simple file format to read a graph. Edges are listed
34
 * on a new line in a file as such: 
35
 * a calls b 
36
 * b calls c 
37
 * c calld d
38
 * 
39
 * The content provider creates an edge for each line in the file and names the
40
 * sources and destination from the line.
41
 * 
42
 * 
43
 * @author Ian Bull
44
 * 
45
 */
46
public class FileProviderJFaceSnippet {
47
48
	public static final String graph = "a calls b\n" + "a calls c\n" + "b calld d\n" + "b calls e\n" + "c calls f\n" + "c calls g\n" + "d calls h\n" + "d calls i\n" + "e calls j\n" + "e calls k\n" + "f calls l\n" + "f calls m\n";
49
50
	static class SimpleGraphContentProvider implements IGraphContentProvider {
51
52
		private StringTokenizer graph;
53
54
		public Object getDestination(Object rel) {
55
			String string = (String) rel;
56
			String[] parts = string.split(" ");
57
			return parts[2];
58
		}
59
60
		public Object[] getElements(Object input) {
61
			ArrayList listOfEdges = new ArrayList();
62
			while (graph.hasMoreTokens()) {
63
				listOfEdges.add(graph.nextToken());
64
			}
65
			return listOfEdges.toArray();
66
		}
67
68
		public Object getSource(Object rel) {
69
			String string = (String) rel;
70
			String[] parts = string.split(" ");
71
			return parts[0];
72
		}
73
74
		public double getWeight(Object connection) {
75
			return 0;
76
		}
77
78
		public void dispose() {
79
80
		}
81
82
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
83
			if (newInput != null) {
84
				graph = new StringTokenizer((String) newInput, "\n");
85
			}
86
		}
87
88
	}
89
90
	public static void main(String[] args) throws IOException {
91
		Display display = new Display();
92
		Shell shell = new Shell(display);
93
		shell.setText("Simple Graph File Format");
94
95
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
96
		dialog.setFilterNames(new String[] { "Simple Graph Files (*.sgf)", "All Files (*.*)" });
97
		dialog.setFilterExtensions(new String[] { "*.sgf", "*.*" }); //Windows wild cards
98
99
		String directory = System.getProperty("user.dir") + "/src/org/eclipse/zest/tests/jface/SimpleGraph.sgf"; //eclipse/zest/examples/jface/";
100
		System.out.println(directory);
101
		dialog.setFilterPath(directory);
102
		//dialog.setFilterPath(System.getProperty("user.dir") + "src/org/eclipse/zest/examples/jface/"); //Windows path
103
104
		shell.setLayout(new FillLayout(SWT.VERTICAL));
105
		shell.setSize(400, 400);
106
		GraphViewer viewer = null;
107
108
		viewer = new GraphViewer(shell, SWT.NONE);
109
		viewer.setContentProvider(new SimpleGraphContentProvider());
110
		viewer.setLabelProvider(new LabelProvider());
111
		viewer.setLayoutAlgorithm(new RadialLayoutAlgorithm());
112
113
		shell.open();
114
		String fileName = dialog.open();
115
116
		if (fileName == null) {
117
			// use the sample graph
118
			viewer.setInput(graph);
119
		} else {
120
			FileReader fileReader = new FileReader(new File(fileName));
121
			BufferedReader bufferedReader = new BufferedReader(fileReader);
122
			StringBuffer stringBuffer = new StringBuffer();
123
			while (bufferedReader.ready()) {
124
				stringBuffer.append(bufferedReader.readLine() + "\n");
125
			}
126
			viewer.setInput(stringBuffer.toString());
127
		}
128
129
		while (!shell.isDisposed()) {
130
			if (!display.readAndDispatch()) {
131
				display.sleep();
132
			}
133
		}
134
		display.dispose();
135
	}
136
}

Return to bug 283244