Project

General

Profile

Download (26.7 KB) Statistics
| Branch: | Tag: | Revision:
1
// /*******************************************************************************
2
// * Copyright 2005, CHISEL Group, University of Victoria, Victoria, BC, Canada.
3
// * All rights reserved. This program and the accompanying materials are made
4
// * 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.layouts.exampleUses;
11
//
12
// import java.util.ArrayList;
13
// import java.util.Date;
14
// import java.util.Iterator;
15
// import java.util.List;
16
//
17
// import org.eclipse.core.runtime.IProgressMonitor;
18
// import org.eclipse.jface.dialogs.ProgressMonitorDialog;
19
// import org.eclipse.swt.SWT;
20
// import org.eclipse.swt.SWTError;
21
// import org.eclipse.swt.events.ControlEvent;
22
// import org.eclipse.swt.events.ControlListener;
23
// import org.eclipse.swt.events.DisposeEvent;
24
// import org.eclipse.swt.events.DisposeListener;
25
// import org.eclipse.swt.events.MouseEvent;
26
// import org.eclipse.swt.events.MouseListener;
27
// import org.eclipse.swt.events.MouseMoveListener;
28
// import org.eclipse.swt.events.PaintEvent;
29
// import org.eclipse.swt.events.PaintListener;
30
// import org.eclipse.swt.events.SelectionEvent;
31
// import org.eclipse.swt.events.SelectionListener;
32
// import org.eclipse.swt.graphics.Color;
33
// import org.eclipse.swt.graphics.GC;
34
// import org.eclipse.swt.graphics.Image;
35
// import org.eclipse.swt.graphics.Point;
36
// import org.eclipse.swt.graphics.Rectangle;
37
// import org.eclipse.swt.layout.FillLayout;
38
// import org.eclipse.swt.layout.GridData;
39
// import org.eclipse.swt.layout.GridLayout;
40
// import org.eclipse.swt.widgets.Canvas;
41
// import org.eclipse.swt.widgets.Composite;
42
// import org.eclipse.swt.widgets.Display;
43
// import org.eclipse.swt.widgets.Label;
44
// import org.eclipse.swt.widgets.Shell;
45
// import org.eclipse.swt.widgets.ToolBar;
46
// import org.eclipse.swt.widgets.ToolItem;
47
// import org.eclipse.zest.layouts.InvalidLayoutConfiguration;
48
// import org.eclipse.zest.layouts.LayoutAlgorithm;
49
// import org.eclipse.zest.layouts.LayoutBendPoint;
50
// import org.eclipse.zest.layouts.LayoutEntity;
51
// import org.eclipse.zest.layouts.LayoutRelationship;
52
// import org.eclipse.zest.layouts.LayoutStyles;
53
// import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
54
// import org.eclipse.zest.layouts.algorithms.HorizontalLayoutAlgorithm;
55
// import org.eclipse.zest.layouts.algorithms.HorizontalTreeLayoutAlgorithm;
56
// import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
57
// import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
58
// import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
59
// import org.eclipse.zest.layouts.algorithms.VerticalLayoutAlgorithm;
60
// import org.eclipse.zest.layouts.exampleStructures.SimpleNode;
61
// import org.eclipse.zest.layouts.exampleStructures.SimpleRelationship;
62
// import org.eclipse.zest.layouts.progress.ProgressEvent;
63
// import org.eclipse.zest.layouts.progress.ProgressListener;
64
//
65
// /**
66
// * @author Rob Lintern
67
// * @author Ian Bull
68
// * A simple example of using layout algorithms with a SWT application.
69
// */
70
// public class SimpleSWTExample {
71
//
72
// private static final Color BLACK = new Color(Display.getDefault(), 0, 0, 0);
73
// private static final Color NODE_NORMAL_COLOR = new
74
// Color(Display.getDefault(), 225, 225, 255);
75
// private static final Color NODE_SELECTED_COLOR = new
76
// Color(Display.getDefault(), 255, 125, 125);
77
// private static final Color NODE_ADJACENT_COLOR = new
78
// Color(Display.getDefault(), 255, 200, 125);
79
// private static final Color BORDER_NORMAL_COLOR = new
80
// Color(Display.getDefault(), 0, 0, 0);
81
// private static final Color BORDER_SELECTED_COLOR = new
82
// Color(Display.getDefault(), 255, 0, 0);
83
// private static final Color BORDER_ADJACENT_COLOR = new
84
// Color(Display.getDefault(), 255, 128, 0);
85
// private static final Color RELATIONSHIP_COLOR = new
86
// Color(Display.getDefault(), 192, 192, 225);
87
// private static final Color RELATIONSHIP_HIGHLIGHT_COLOR = new
88
// Color(Display.getDefault(), 255, 200, 125);
89
//
90
// private static final String[] NAMES = new String[] { "Peggy", "Rob", "Ian",
91
// "Chris", "Simon", "Wendy", "Steven", "Kim", "Neil", "Dave", "John",
92
// "Suzanne", "Jody", "Casey", "Bjorn", "Peter", "Erin", "Lisa", "Jennie",
93
// "Liz", "Bert", "Ryan", "Nick", "Amy", "Lee", "Me", "You", "Max", "NCI",
94
// "OWL",
95
// "Ed", "Jamie", "Protege", "Matt", "Bryan", "Pete", "Sam", "Bob", "Katie",
96
// "Bill", "Josh", "Davor", "Ken", "Jacob", "Norm", "Jim", "Maya", "Jill",
97
// "Kit", "Jo", "Joe", "Andrew", "Charles", "Pat", "Patrick", "Jeremy", "Mike",
98
// "Michael", "Patricia", "Marg", "Terry", "Emily", "Ben", "Holly",
99
// "Joanna", "Joanne", "Evan", "Tom", "Dan", "Eric", "Corey", "Meghan", "Kevin",
100
// "Nina", "Ron", "Daniel", "David", "Jeff", "Nathan", "Amanda", "Phil",
101
// "Tricia", "Steph", "Stewart", "Stuart", "Bull", "Lintern", "Callendar",
102
// "Thompson", "Rigby", "Adam", "Judith", "Cynthia", "Sarah", "Sara",
103
// "Roger", "Andy", "Kris", "Mark", "Shane", "Spence", "Ivy", "Ivanna", "Julie",
104
// "Justin", "Emile", "Toby", "Robin", "Rich", "Kathy", "Cathy", "Nicky",
105
// "Ricky", "Danny", "Anne", "Ann", "Jen", "Robert", "Calvin", "Alvin", "Scott",
106
// "Kumar" };
107
//
108
// //private static final boolean RENDER_HIGH_QUALITY = true;
109
//
110
// private static final int INITIAL_PANEL_WIDTH = 800;
111
// private static final int INITIAL_PANEL_HEIGHT = 600;
112
// private static final double INITIAL_NODE_WIDTH = 20;
113
// private static final double INITIAL_NODE_HEIGHT = 15;
114
//
115
// protected static ArrayList algorithms = new ArrayList();
116
// {
117
// algorithms.add(new
118
// SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
119
// algorithms.add(new TreeLayoutAlgorithm(LayoutStyles.NONE));
120
// algorithms.add(new HorizontalTreeLayoutAlgorithm(LayoutStyles.NONE));
121
// algorithms.add(new RadialLayoutAlgorithm(LayoutStyles.NONE));
122
// algorithms.add(new GridLayoutAlgorithm(LayoutStyles.NONE));
123
// algorithms.add(new HorizontalLayoutAlgorithm(LayoutStyles.NONE));
124
// algorithms.add(new VerticalLayoutAlgorithm(LayoutStyles.NONE));
125
// }
126
//
127
// protected static ArrayList algorithmNames = new ArrayList();
128
// {
129
// algorithmNames.add("Spring");
130
// algorithmNames.add("Fade");
131
// algorithmNames.add("Tree - V");
132
// algorithmNames.add("Tree - H");
133
// algorithmNames.add("Radial");
134
// algorithmNames.add("Grid");
135
// algorithmNames.add("Horizontal");
136
// algorithmNames.add("Vertical");
137
// }
138
//
139
// protected static ArrayList algorithmAnimates = new ArrayList();
140
// {
141
// algorithmAnimates.add(Boolean.TRUE);
142
// algorithmAnimates.add(Boolean.TRUE);
143
// algorithmAnimates.add(Boolean.FALSE);
144
// algorithmAnimates.add(Boolean.FALSE);
145
// algorithmAnimates.add(Boolean.FALSE);
146
// algorithmAnimates.add(Boolean.FALSE);
147
// algorithmAnimates.add(Boolean.FALSE);
148
// algorithmAnimates.add(Boolean.FALSE);
149
// }
150
//
151
// //private long updateGUICount = 0;
152
// private boolean animate = true;
153
// private static boolean continuous = false;
154
// private static boolean asynchronously = false;
155
//
156
// private Shell mainShell;
157
// private Composite mainComposite;
158
// private List entities;
159
// private List relationships;
160
//
161
// private ToolBar toolBar;
162
// private Label lblProgress;
163
//
164
// private LayoutAlgorithm currentLayoutAlgorithm;
165
// protected SimpleNode selectedEntity;
166
// protected SimpleNode hoverEntity;
167
//
168
// protected Point mouseDownPoint;
169
// protected Point selectedEntityPositionAtMouseDown;
170
// private long idCount = 0;
171
//
172
// public SimpleSWTExample(Display display) {
173
// mainShell = new Shell(display);
174
// mainShell.addControlListener(new ControlListener() {
175
// public void controlMoved(ControlEvent e) {
176
// }
177
//
178
// public void controlResized(ControlEvent e) {
179
// mainShell.layout(true);
180
// }
181
// });
182
// GridLayout gridLayout = new GridLayout(1, true);
183
// mainShell.setLayout(gridLayout);
184
// GridData toolbarGridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER,
185
// GridData.VERTICAL_ALIGN_BEGINNING, true, true);
186
// toolBar = new ToolBar(mainShell, SWT.HORIZONTAL);
187
// toolBar.setLayoutData(toolbarGridData);
188
// toolBar.setLayout(new FillLayout(SWT.HORIZONTAL));
189
//
190
// GridData progressGridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER,
191
// GridData.VERTICAL_ALIGN_END, true, false);
192
// progressGridData.widthHint = 300;
193
// lblProgress = new Label(mainShell, SWT.NONE);
194
// lblProgress.setLayoutData(progressGridData);
195
// lblProgress.setText("Progress: ");
196
//
197
// for (int i = 0; i < algorithms.size(); i++) {
198
// final LayoutAlgorithm algorithm = (LayoutAlgorithm) algorithms.get(i);
199
// String algorithmName = (String) algorithmNames.get(i);
200
// final boolean algorithmAnimate = ((Boolean)
201
// algorithmAnimates.get(i)).booleanValue();
202
// ToolItem algorithmButton = new ToolItem(toolBar, SWT.PUSH);
203
// algorithmButton.setText(algorithmName);
204
//
205
// new ToolItem(toolBar, SWT.SEPARATOR);
206
//
207
// algorithmButton.addSelectionListener(new SelectionListener() {
208
// public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
209
// currentLayoutAlgorithm = algorithm;
210
// algorithm.setEntityAspectRatio((double) mainComposite.getClientArea().width /
211
// (double) mainComposite.getClientArea().height);
212
// animate = algorithmAnimate;
213
// performLayout(false);
214
// }
215
//
216
// public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
217
// }
218
// });
219
// }
220
//
221
// ToolItem redrawButton = new ToolItem(toolBar, SWT.PUSH);
222
// redrawButton.setText("Redraw");
223
// redrawButton.addSelectionListener(new SelectionListener() {
224
// public void widgetSelected(SelectionEvent e) {
225
// mainComposite.redraw();
226
// }
227
//
228
// public void widgetDefaultSelected(SelectionEvent e) {
229
// }
230
// });
231
//
232
// ToolItem stopButton = new ToolItem(toolBar, SWT.PUSH);
233
// stopButton.setText("Stop");
234
// stopButton.addSelectionListener(new SelectionListener() {
235
// public void widgetSelected(SelectionEvent e) {
236
// currentLayoutAlgorithm.stop();
237
// }
238
//
239
// public void widgetDefaultSelected(SelectionEvent e) {
240
// }
241
// });
242
//
243
// ToolItem continuousButton = new ToolItem(toolBar, SWT.CHECK);
244
// continuousButton.setText("Continuous");
245
// continuousButton.addSelectionListener(new SelectionListener() {
246
// public void widgetSelected(SelectionEvent e) {
247
// setContinuous();
248
// }
249
//
250
// public void widgetDefaultSelected(SelectionEvent e) {
251
// }
252
// });
253
//
254
// ToolItem asynchronousButton = new ToolItem(toolBar, SWT.CHECK);
255
// asynchronousButton.setText("Asynchronous");
256
// asynchronousButton.addSelectionListener(new SelectionListener() {
257
// public void widgetSelected(SelectionEvent e) {
258
// setAsynchronously();
259
// }
260
//
261
// public void widgetDefaultSelected(SelectionEvent e) {
262
// }
263
// });
264
//
265
// createMainPanel();
266
// SimpleNode.setNodeColors(NODE_NORMAL_COLOR, BORDER_NORMAL_COLOR,
267
// NODE_SELECTED_COLOR, NODE_ADJACENT_COLOR, BORDER_SELECTED_COLOR,
268
// BORDER_ADJACENT_COLOR);
269
// SimpleRelationship.setDefaultColor(RELATIONSHIP_COLOR);
270
// SimpleRelationship.setDefaultHighlightColor(RELATIONSHIP_HIGHLIGHT_COLOR);
271
// createTreeGraph(4, 3, false);
272
// mainShell.pack();
273
// //mainShell.setSize(INITIAL_PANEL_WIDTH + 100, INITIAL_PANEL_HEIGHT + 200);
274
// }
275
//
276
// public void setAsynchronously() {
277
// if (asynchronously) {
278
// asynchronously = false;
279
// } else {
280
// asynchronously = true;
281
// }
282
//
283
// }
284
//
285
// public void setContinuous() {
286
// if (continuous) {
287
// continuous = false;
288
// } else {
289
// continuous = true;
290
// }
291
// }
292
//
293
// IProgressMonitor progressMonitor = null;
294
// ProgressMonitorDialog pmd = null;
295
// boolean GUI_UPDATING = false;
296
//
297
// private void performLayout(boolean placeRandomly) {
298
//
299
// if (!continuous) {
300
// }
301
//
302
// if (currentLayoutAlgorithm.isRunning()) {
303
// throw new RuntimeException("Layout is already running");
304
// }
305
// if (placeRandomly) {
306
// placeRandomly();
307
// }
308
// ProgressListener progressListener = new ProgressListener() {
309
//
310
// int lastStep = 0;
311
//
312
// class progressSync implements Runnable {
313
//
314
// public static final int PROGRESS_UPDATED = 1;
315
// public static final int PROGRESS_STARTED = 2;
316
// public static final int PROGRESS_ENDED = 3;
317
// public static final int UPDATE_GUI = 4;
318
//
319
// private int progressState = -1;
320
// private ProgressEvent e;
321
//
322
// public progressSync(int progressState, final ProgressEvent e) {
323
// this.progressState = progressState;
324
// this.e = e;
325
//
326
// }
327
//
328
// public void run() {
329
//
330
// switch (progressState) {
331
// case PROGRESS_STARTED:
332
// if (!continuous) {
333
// pmd = new ProgressMonitorDialog(getShell());
334
// progressMonitor = pmd.getProgressMonitor();
335
// pmd.open();
336
// progressMonitor.beginTask("Layout Running...", e.getTotalNumberOfSteps());
337
// }
338
// break;
339
//
340
// case PROGRESS_UPDATED:
341
// if (!continuous) {
342
// progressMonitor.worked(e.getStepsCompleted() - lastStep);
343
// lastStep = e.getStepsCompleted();
344
// }
345
// break;
346
//
347
// case PROGRESS_ENDED:
348
// if (!continuous) {
349
// progressMonitor.done();
350
// pmd.close();
351
// }
352
// updateGUI();
353
// mainShell.redraw();
354
// break;
355
// case UPDATE_GUI:
356
// updateGUI();
357
// GUI_UPDATING = false;
358
// break;
359
// }
360
// mainComposite.redraw();
361
//
362
// }
363
//
364
// }
365
//
366
// public void progressUpdated(final ProgressEvent e) {
367
// if (asynchronously) {
368
// if (!mainComposite.isDisposed()) {
369
// Display.getDefault().asyncExec(new
370
// progressSync(progressSync.PROGRESS_UPDATED, e));
371
// if (!GUI_UPDATING) {
372
// GUI_UPDATING = true;
373
// Display.getDefault().asyncExec(new progressSync(progressSync.UPDATE_GUI, e));
374
// }
375
// }
376
// } else {
377
// if (!mainComposite.isDisposed()) {
378
// new progressSync(progressSync.PROGRESS_UPDATED, e).run();
379
// }
380
// }
381
//
382
// }
383
//
384
// public void progressStarted(ProgressEvent e) {
385
// if (asynchronously) {
386
// if (!mainComposite.isDisposed()) {
387
// Display.getDefault().asyncExec(new
388
// progressSync(progressSync.PROGRESS_STARTED, e));
389
// }
390
// } else {
391
// if (!mainComposite.isDisposed()) {
392
// new progressSync(progressSync.PROGRESS_STARTED, e).run();
393
// }
394
// }
395
//
396
// }
397
//
398
// public void progressEnded(ProgressEvent e) {
399
// if (asynchronously) {
400
// if (!mainComposite.isDisposed()) {
401
// Display.getDefault().asyncExec(new progressSync(progressSync.PROGRESS_ENDED,
402
// e));
403
// }
404
// } else {
405
// if (!mainComposite.isDisposed()) {
406
// new progressSync(progressSync.PROGRESS_ENDED, e).run();
407
// }
408
// }
409
// currentLayoutAlgorithm.removeProgressListener(this);
410
// Display.getDefault().asyncExec(new
411
// progressSync(progressSync.PROGRESS_UPDATED, e));
412
// }
413
// };
414
// currentLayoutAlgorithm.addProgressListener(progressListener);
415
//
416
// try {
417
// LayoutEntity[] layoutEntities = new LayoutEntity[entities.size()];
418
// entities.toArray(layoutEntities);
419
// LayoutRelationship[] layoutRelationships = new
420
// LayoutRelationship[relationships.size()];
421
// relationships.toArray(layoutRelationships);
422
// currentLayoutAlgorithm.applyLayout(layoutEntities, layoutRelationships, 0, 0,
423
// mainComposite.getClientArea().width - 30,
424
// mainComposite.getClientArea().height - 17, asynchronously, continuous);
425
// if (!animate) {
426
// updateGUI();
427
// }
428
// } catch (InvalidLayoutConfiguration e) {
429
// e.printStackTrace();
430
// }
431
// }
432
//
433
// private Shell getShell() {
434
// return mainShell;
435
// }
436
//
437
// private void createMainPanel() {
438
// mainComposite = new Canvas(mainShell, SWT.NO_BACKGROUND);
439
// GridData mainGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL,
440
// GridData.VERTICAL_ALIGN_FILL, true, true);
441
// mainGridData.widthHint = INITIAL_PANEL_WIDTH;
442
// mainGridData.heightHint = INITIAL_PANEL_HEIGHT;
443
// mainComposite.setLayoutData(mainGridData);
444
// mainComposite.addPaintListener(new GraphPaintListener());
445
//
446
// mainComposite.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
447
// mainComposite.setLayout(null);
448
//
449
// mainComposite.addMouseMoveListener(new MouseMoveListener() {
450
//
451
// public void mouseMove(MouseEvent e) {
452
//
453
// if (selectedEntity == null) {
454
// // Nothing selected, lets use a mouse hover
455
// SimpleNode oldEntity = hoverEntity;
456
// hoverEntity = null;
457
//
458
// for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity ==
459
// null;) {
460
// SimpleNode entity = (SimpleNode) iter.next();
461
// double x = entity.getX();
462
// double y = entity.getY();
463
// double w = entity.getWidth();
464
// double h = entity.getHeight();
465
// Rectangle rect = new Rectangle((int) x, (int) y, (int) w, (int) h);
466
// if (rect.contains(e.x, e.y)) {
467
// hoverEntity = entity;
468
// hoverEntity.ignoreInLayout(true);
469
// hoverEntity.setSelected();
470
// break;
471
// }
472
// }
473
// if (oldEntity != null && oldEntity != hoverEntity) {
474
// oldEntity.ignoreInLayout(false);
475
// oldEntity.setUnSelected();
476
// }
477
// }
478
//
479
// }
480
//
481
// });
482
// mainComposite.addMouseListener(new MouseListener() {
483
//
484
// public void mouseDoubleClick(MouseEvent e) {
485
// }
486
//
487
// public void mouseDown(MouseEvent e) {
488
// selectedEntity = null;
489
// hoverEntity = null;
490
// for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity ==
491
// null;) {
492
// SimpleNode entity = (SimpleNode) iter.next();
493
// double x = entity.getX();
494
// double y = entity.getY();
495
// double w = entity.getWidth();
496
// double h = entity.getHeight();
497
// Rectangle rect = new Rectangle((int) x, (int) y, (int) w, (int) h);
498
// if (rect.contains(e.x, e.y)) {
499
// selectedEntity = entity;
500
// }
501
// }
502
// if (selectedEntity != null) {
503
// mouseDownPoint = new Point(e.x, e.y);
504
// selectedEntityPositionAtMouseDown = new Point((int) selectedEntity.getX(),
505
// (int) selectedEntity.getY());
506
// selectedEntity.ignoreInLayout(true);
507
// selectedEntity.setSelected();
508
// } else {
509
// mouseDownPoint = null;
510
// selectedEntityPositionAtMouseDown = null;
511
// }
512
// }
513
//
514
// public void mouseUp(MouseEvent e) {
515
// if (selectedEntity != null) {
516
// selectedEntity.ignoreInLayout(false);
517
// selectedEntity.setUnSelected();
518
// List relatedNodes = selectedEntity.getRelatedEntities();
519
// for (Iterator iter = relatedNodes.iterator(); iter.hasNext();) {
520
// SimpleNode element = (SimpleNode) iter.next();
521
// element.setUnSelected();
522
// }
523
// SimpleRelationship[] rels = selectedEntity.getRelationships();
524
// for (int i = 0; i < rels.length; i++) {
525
// rels[i].resetLineWidth();
526
// }
527
// }
528
// selectedEntity = null;
529
// mouseDownPoint = null;
530
// selectedEntityPositionAtMouseDown = null;
531
// }
532
// });
533
//
534
// // stops the algorithm when the window is closed
535
// mainComposite.addDisposeListener(new DisposeListener() {
536
// public void widgetDisposed(DisposeEvent e) {
537
// if (currentLayoutAlgorithm != null) {
538
// currentLayoutAlgorithm.stop();
539
// }
540
// }
541
// });
542
//
543
// mainComposite.addMouseMoveListener(new MouseMoveListener() {
544
//
545
// public void mouseMove(MouseEvent e) {
546
// if (selectedEntity != null && mouseDownPoint != null) {
547
// double dx = e.x - mouseDownPoint.x;
548
// double dy = e.y - mouseDownPoint.y;
549
//
550
// selectedEntity.setLocation(selectedEntityPositionAtMouseDown.x + dx,
551
// selectedEntityPositionAtMouseDown.y + dy);
552
// mainComposite.redraw();
553
// }
554
// }
555
// });
556
// }
557
//
558
// static int lastUpdateCall = 0;
559
//
560
// /**
561
// *
562
// * @param maxLevels Max number of levels wanted in tree
563
// * @param maxChildren Max number of children for each node in the tree
564
// * @param random Whether or not to pick random number of levels (from 1 to
565
// maxLevels) and
566
// * random number of children (from 1 to maxChildren)
567
// */
568
// private void createTreeGraph(int maxLevels, int maxChildren, boolean random)
569
// {
570
// entities = new ArrayList();
571
// relationships = new ArrayList();
572
//
573
// // ccallendar - testing out having 2 roots
574
// SimpleNode root = createSimpleNode(getNextID());
575
// entities.add(root);
576
// SimpleNode root2 = createSimpleNode(getNextID());
577
// entities.add(root2);
578
// // end
579
//
580
// SimpleNode currentParent = createSimpleNode(getNextID());
581
// entities.add(currentParent);
582
//
583
// // ccallendar - adding relationships from the parent to the 2 roots
584
// SimpleRelationship rel = new SimpleRelationship(root, currentParent, false);
585
// root.addRelationship(rel);
586
// currentParent.addRelationship(rel);
587
// relationships.add(rel);
588
// rel = new SimpleRelationship(root2, currentParent, false);
589
// root2.addRelationship(rel);
590
// currentParent.addRelationship(rel);
591
// relationships.add(rel);
592
// // end
593
//
594
// int levels = random ? (int) (Math.random() * maxLevels + 1) : maxLevels;
595
// createTreeGraphRecursive(currentParent, maxChildren, levels, 1, random);
596
// }
597
//
598
// private void createTreeGraphRecursive(SimpleNode currentParentNode, int
599
// maxChildren, int maxLevel, int level, boolean random) {
600
// if (level > maxLevel) {
601
// return;
602
// }
603
//
604
// int numChildren = random ? (int) (Math.random() * maxChildren + 1) :
605
// maxChildren;
606
// for (int child = 0; child < numChildren; child++) {
607
// SimpleNode childNode = createSimpleNode(getNextID());
608
// entities.add(childNode);
609
// SimpleRelationship rel = new SimpleRelationship(currentParentNode, childNode,
610
// false);
611
// childNode.addRelationship(rel);
612
// currentParentNode.addRelationship(rel);
613
// relationships.add(rel);
614
// SimpleRelationship.setDefaultSize(2);
615
// createTreeGraphRecursive(childNode, maxChildren, maxLevel, level + 1,
616
// random);
617
// }
618
// }
619
//
620
// private int repeats = 0;
621
//
622
// /**
623
// * Gets the next name from the names list.
624
// * Once all the names have been used up the names are
625
// * repeated with a '1' after the name.
626
// * @return String name
627
// */
628
// private String getNextID() {
629
// if (idCount >= NAMES.length) {
630
// idCount = 0;
631
// repeats++;
632
// }
633
// String id = NAMES[(int) idCount];
634
// if (repeats > 0) {
635
// id += "_" + repeats;
636
// }
637
// idCount++;
638
// return id;
639
// }
640
//
641
// /** Places nodes randomly on the screen **/
642
// private void placeRandomly() {
643
// for (Iterator iter = entities.iterator(); iter.hasNext();) {
644
// SimpleNode simpleNode = (SimpleNode) iter.next();
645
// double x = Math.random() * INITIAL_PANEL_WIDTH - INITIAL_NODE_WIDTH;
646
// double y = Math.random() * INITIAL_PANEL_HEIGHT - INITIAL_NODE_HEIGHT;
647
// simpleNode.setLocationInLayout(x, y);
648
// }
649
// }
650
//
651
// /**
652
// * Creates a SimpleNode
653
// * @param name
654
// * @return SimpleNode
655
// */
656
// private SimpleNode createSimpleNode(String name) {
657
// SimpleNode simpleNode = new SimpleNode(name);
658
// int w = name.length() * 8; // an initial approximation of the width
659
// simpleNode.setSizeInLayout(Math.max(w, INITIAL_NODE_WIDTH),
660
// INITIAL_NODE_HEIGHT);
661
// return simpleNode;
662
// }
663
//
664
// private void updateGUI() {
665
// if (!mainComposite.isDisposed()) {
666
// mainComposite.redraw();
667
// //mainComposite.update();
668
// }
669
// }
670
//
671
// static Display display = null;
672
//
673
// public static void main(String[] args) {
674
// display = Display.getDefault();
675
// SimpleSWTExample simpleSWTExample = new SimpleSWTExample(display);
676
// Shell shell = simpleSWTExample.getShell();
677
// //shell.pack();
678
// shell.open();
679
// while (!shell.isDisposed()) {
680
// if (!display.readAndDispatch()) {
681
// display.sleep();
682
// }
683
// }
684
// display.dispose();
685
// }
686
//
687
// /**
688
// * Implements a paint listener to display nodes and edges
689
// */
690
// private class GraphPaintListener implements PaintListener {
691
//
692
// long lastPaint;
693
//
694
// public void paintControl(PaintEvent e) {
695
// Date date = new Date();
696
// long currentTime = date.getTime();
697
// if (currentTime - lastPaint < 40) {
698
// return;
699
// } else {
700
// lastPaint = currentTime;
701
// }
702
// if (Display.getDefault() == null || e.width == 0 || e.height == 0) {
703
// return;
704
// }
705
// long startTime = date.getTime();
706
//
707
// // do a bit of our own double-buffering to stop flickering
708
// Image imageBuffer;
709
//
710
// try {
711
// imageBuffer = new Image(Display.getDefault(), e.width, e.height);
712
// } catch (SWTError noMoreHandles) {
713
// imageBuffer = null;
714
// noMoreHandles.printStackTrace();
715
// return;
716
// } catch (IllegalArgumentException tooBig) {
717
// imageBuffer = null;
718
// tooBig.printStackTrace();
719
// return;
720
// }
721
//
722
// GC gcBuffer = new GC(imageBuffer);
723
//
724
// // paint the relationships
725
// for (Iterator iter = relationships.iterator(); iter.hasNext();) {
726
// SimpleRelationship rel = (SimpleRelationship) iter.next();
727
// SimpleNode src = (SimpleNode) rel.getSourceInLayout();
728
// SimpleNode dest = (SimpleNode) rel.getDestinationInLayout();
729
//
730
// // highlight the adjacent nodes if one of the nodes is selected
731
// if (src.equals(selectedEntity)) {
732
// dest.setAdjacent();
733
// rel.setSelected();
734
// } else if (dest.equals(selectedEntity)) {
735
// src.setAdjacent();
736
// rel.setSelected();
737
// } else {
738
// rel.setUnSelected();
739
// }
740
//
741
// // Add bend points if required
742
// if ((rel).getBendPoints() != null && (rel).getBendPoints().length > 0) {
743
// src = drawBendPoints(rel, gcBuffer); // change source to last bendpoint
744
// }
745
//
746
// double srcX = src.getX() + src.getWidth() / 2.0;
747
// double srcY = src.getY() + src.getHeight() / 2.0;
748
// double destX = dest.getX() + dest.getWidth() / 2.0;
749
// double destY = dest.getY() + dest.getHeight() / 2.0;
750
// drawEdge(srcX, srcY, destX, destY, rel, gcBuffer);
751
//
752
// }
753
//
754
// // paint the nodes
755
// for (Iterator iter = entities.iterator(); iter.hasNext();) {
756
// SimpleNode entity = (SimpleNode) iter.next();
757
//
758
// String name = entity.toString();
759
// Point textSize = gcBuffer.stringExtent(name);
760
// int entityX = (int) entity.getX();
761
// int entityY = (int) entity.getY();
762
// //TODO: What about resize from the layout algorithm
763
// int entityWidth = Math.max((int) entity.getWidth(), textSize.x + 8);
764
// int entityHeight = Math.max((int) entity.getHeight(), textSize.y + 2);
765
//
766
// gcBuffer.setBackground((Color) entity.getColor());
767
// gcBuffer.fillRoundRectangle(entityX, entityY, entityWidth, entityHeight, 8,
768
// 8);
769
//
770
// // position the text in the middle of the node
771
// int x = (int) (entityX + (entityWidth / 2.0)) - (textSize.x / 2);
772
// gcBuffer.setForeground(BLACK);
773
// gcBuffer.drawString(name, x, entityY);
774
// gcBuffer.setForeground((Color) entity.getBorderColor());
775
// gcBuffer.setLineWidth(entity.getBorderWidth());
776
// gcBuffer.drawRoundRectangle(entityX, entityY, entityWidth, entityHeight, 8,
777
// 8);
778
// }
779
//
780
// e.gc.drawImage(imageBuffer, 0, 0);
781
// imageBuffer.dispose();
782
// gcBuffer.dispose();
783
//
784
// long time = date.getTime() - startTime;
785
// if (time > 200) {
786
// }
787
// }
788
//
789
// /**
790
// * Draw an edge
791
// * @param gcBuffer
792
// * @param srcX
793
// * @param srcY
794
// * @param destX
795
// * @param destY
796
// * @param rel
797
// */
798
// private void drawEdge(double srcX, double srcY, double destX, double destY,
799
// SimpleRelationship rel, GC gcBuffer) {
800
// gcBuffer.setForeground((Color) rel.getColor());
801
// gcBuffer.setLineWidth(rel.getLineWidth());
802
// gcBuffer.drawLine((int) srcX, (int) srcY, (int) destX, (int) destY);
803
// }
804
//
805
// /**
806
// * Draws a set of lines between bendpoints
807
// * TODO - This does not always draw outside the node.
808
// * @param relationship
809
// * @param bendNodes
810
// * @param bendEdges
811
// * @return the last bendpoint entity or null if there are no bendpoints
812
// */
813
// private SimpleNode drawBendPoints(SimpleRelationship rel, GC gcBuffer) {
814
// final String DUMMY_TITLE = "dummy";
815
// LayoutBendPoint[] bendPoints = (rel).getBendPoints();
816
// LayoutBendPoint bp;
817
// SimpleNode startEntity = (SimpleNode) rel.getSourceInLayout();
818
// SimpleNode destEntity = null;
819
//
820
// double srcX = startEntity.getX() + startEntity.getWidth() / 2.0;
821
// double srcY = startEntity.getY() + startEntity.getHeight() / 2.0;
822
// for (int i = 1; i < bendPoints.length - 1; i++) {
823
// bp = bendPoints[i];
824
// destEntity = new SimpleNode(DUMMY_TITLE, bp.getX(), bp.getY(), 0.01, 0.01);
825
// drawEdge(srcX, srcY, bp.getX(), bp.getY(), rel, gcBuffer);
826
// startEntity = destEntity;
827
// srcX = startEntity.getX();
828
// srcY = startEntity.getY();
829
// }
830
// return destEntity;
831
// }
832
//
833
// }
834
//
835
//}
(1-1/2)