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.algorithms.internal;
|
11
|
|
12
|
import java.util.Comparator;
|
13
|
import java.util.TreeSet;
|
14
|
|
15
|
import org.eclipse.zest.layouts.dataStructures.DisplayIndependentPoint;
|
16
|
import org.eclipse.zest.layouts.dataStructures.DisplayIndependentRectangle;
|
17
|
import org.eclipse.zest.layouts.dataStructures.InternalNode;
|
18
|
|
19
|
public class DynamicScreen {
|
20
|
|
21
|
private TreeSet XCoords = null;
|
22
|
private TreeSet YCoords = null;
|
23
|
|
24
|
private DisplayIndependentRectangle screenBounds = null;
|
25
|
|
26
|
double minX = 0.0;
|
27
|
double minY = 0.0;
|
28
|
double maxX = 0.0;
|
29
|
double maxY = 0.0;
|
30
|
|
31
|
public void cleanScreen() {
|
32
|
minX = 0.0;
|
33
|
minY = 0.0;
|
34
|
maxX = 0.0;
|
35
|
maxY = 0.0;
|
36
|
}
|
37
|
|
38
|
class XComparator implements Comparator {
|
39
|
public int compare(Object arg0, Object arg1) {
|
40
|
InternalNode n1 = (InternalNode) arg0;
|
41
|
InternalNode n2 = (InternalNode) arg1;
|
42
|
if (n1.getInternalX() > n2.getInternalX())
|
43
|
return +1;
|
44
|
else if (n1.getInternalX() < n2.getInternalX())
|
45
|
return -1;
|
46
|
else {
|
47
|
return n1.toString().compareTo(n2.toString());
|
48
|
}
|
49
|
|
50
|
}
|
51
|
}
|
52
|
|
53
|
class YComparator implements Comparator {
|
54
|
public int compare(Object arg0, Object arg1) {
|
55
|
InternalNode n1 = (InternalNode) arg0;
|
56
|
InternalNode n2 = (InternalNode) arg1;
|
57
|
if (n1.getInternalY() > n2.getInternalY())
|
58
|
return +1;
|
59
|
else if (n1.getInternalY() < n2.getInternalY())
|
60
|
return -1;
|
61
|
else {
|
62
|
return n1.toString().compareTo(n2.toString());
|
63
|
}
|
64
|
|
65
|
}
|
66
|
}
|
67
|
|
68
|
public DynamicScreen(int x, int y, int width, int height) {
|
69
|
XCoords = new TreeSet(new XComparator());
|
70
|
YCoords = new TreeSet(new YComparator());
|
71
|
|
72
|
this.screenBounds = new DisplayIndependentRectangle(x, y, width, height);
|
73
|
}
|
74
|
|
75
|
public void removeNode(InternalNode node) {
|
76
|
XCoords.remove(node);
|
77
|
YCoords.remove(node);
|
78
|
}
|
79
|
|
80
|
public void addNode(InternalNode node) {
|
81
|
XCoords.add(node);
|
82
|
YCoords.add(node);
|
83
|
}
|
84
|
|
85
|
public DisplayIndependentPoint getScreenLocation(InternalNode node) {
|
86
|
|
87
|
DisplayIndependentRectangle layoutBounds = calculateBounds();
|
88
|
|
89
|
double x = (layoutBounds.width == 0) ? 0 : (node.getInternalX() - layoutBounds.x) / layoutBounds.width;
|
90
|
double y = (layoutBounds.height == 0) ? 0 : (node.getInternalY() - layoutBounds.y) / layoutBounds.height;
|
91
|
|
92
|
x = screenBounds.x + x * screenBounds.width;
|
93
|
y = screenBounds.y + y * screenBounds.height;
|
94
|
|
95
|
return new DisplayIndependentPoint(x, y);
|
96
|
}
|
97
|
|
98
|
public DisplayIndependentPoint getVirtualLocation(DisplayIndependentPoint point) {
|
99
|
|
100
|
DisplayIndependentRectangle layoutBounds = calculateBounds();
|
101
|
|
102
|
double x = (point.x / screenBounds.width) * layoutBounds.width + layoutBounds.x;
|
103
|
double y = (point.y / screenBounds.height) * layoutBounds.height + layoutBounds.y;
|
104
|
|
105
|
return new DisplayIndependentPoint(x, y);
|
106
|
}
|
107
|
|
108
|
private DisplayIndependentRectangle calculateBounds() {
|
109
|
InternalNode n1 = (InternalNode) XCoords.first();
|
110
|
InternalNode n2 = (InternalNode) XCoords.last();
|
111
|
InternalNode n3 = (InternalNode) YCoords.first();
|
112
|
InternalNode n4 = (InternalNode) YCoords.last();
|
113
|
double x = n1.getInternalX();
|
114
|
double width = n2.getInternalX();
|
115
|
double y = n3.getInternalY();
|
116
|
double height = n4.getInternalY();
|
117
|
return new DisplayIndependentRectangle(x, y, width - x, height - y);
|
118
|
}
|
119
|
|
120
|
}
|