1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
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
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d;
|
12
|
|
13
|
|
14
|
import org.eclipse.draw2d.geometry.Dimension;
|
15
|
import org.eclipse.draw2d.geometry.Rectangle;
|
16
|
import org.eclipse.draw2d.geometry.Translatable;
|
17
|
import org.eclipse.draw2d.rap.swt.SWT;
|
18
|
|
19
|
/**
|
20
|
* A non-freeform, scalable layered pane.
|
21
|
*
|
22
|
* @author Eric Bordeau
|
23
|
* @since 2.1.1
|
24
|
*/
|
25
|
public class ScalableLayeredPane extends LayeredPane implements ScalableFigure {
|
26
|
|
27
|
private double scale = 1.0;
|
28
|
|
29
|
/**
|
30
|
* @see IFigure#getClientArea(Rectangle)
|
31
|
*/
|
32
|
public Rectangle getClientArea(Rectangle rect) {
|
33
|
super.getClientArea(rect);
|
34
|
rect.width /= scale;
|
35
|
rect.height /= scale;
|
36
|
rect.x /= scale;
|
37
|
rect.y /= scale;
|
38
|
return rect;
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* @see Figure#getPreferredSize(int, int)
|
43
|
*/
|
44
|
public Dimension getMinimumSize(int wHint, int hHint) {
|
45
|
Dimension d = super
|
46
|
.getMinimumSize(
|
47
|
wHint != SWT.DEFAULT ? (int) (wHint / getScale())
|
48
|
: SWT.DEFAULT,
|
49
|
hHint != SWT.DEFAULT ? (int) (hHint / getScale())
|
50
|
: SWT.DEFAULT);
|
51
|
int w = getInsets().getWidth();
|
52
|
int h = getInsets().getHeight();
|
53
|
return d.getExpanded(-w, -h).scale(scale).expand(w, h);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* @see Figure#getPreferredSize(int, int)
|
58
|
*/
|
59
|
public Dimension getPreferredSize(int wHint, int hHint) {
|
60
|
Dimension d = super
|
61
|
.getPreferredSize(
|
62
|
wHint != SWT.DEFAULT ? (int) (wHint / getScale())
|
63
|
: SWT.DEFAULT,
|
64
|
hHint != SWT.DEFAULT ? (int) (hHint / getScale())
|
65
|
: SWT.DEFAULT);
|
66
|
int w = getInsets().getWidth();
|
67
|
int h = getInsets().getHeight();
|
68
|
return d.getExpanded(-w, -h).scale(scale).expand(w, h);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Returns the scale level, default is 1.0.
|
73
|
*
|
74
|
* @return the scale level
|
75
|
*/
|
76
|
public double getScale() {
|
77
|
return scale;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @see org.eclipse.draw2d.IFigure#isCoordinateSystem()
|
82
|
*/
|
83
|
public boolean isCoordinateSystem() {
|
84
|
return true;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @see org.eclipse.draw2d.Figure#paintClientArea(Graphics)
|
89
|
*/
|
90
|
protected void paintClientArea(Graphics graphics) {
|
91
|
if (getChildren().isEmpty())
|
92
|
return;
|
93
|
if (scale == 1.0) {
|
94
|
super.paintClientArea(graphics);
|
95
|
} else {
|
96
|
ScaledGraphics g = new ScaledGraphics(graphics);
|
97
|
boolean optimizeClip = getBorder() == null
|
98
|
|| getBorder().isOpaque();
|
99
|
if (!optimizeClip)
|
100
|
g.clipRect(getBounds().getCropped(getInsets()));
|
101
|
g.scale(scale);
|
102
|
g.pushState();
|
103
|
paintChildren(g);
|
104
|
g.dispose();
|
105
|
graphics.restoreState();
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Sets the zoom level
|
111
|
*
|
112
|
* @param newZoom
|
113
|
* The new zoom level
|
114
|
*/
|
115
|
public void setScale(double newZoom) {
|
116
|
if (scale == newZoom)
|
117
|
return;
|
118
|
scale = newZoom;
|
119
|
fireMoved(); // for AncestorListener compatibility
|
120
|
revalidate();
|
121
|
repaint();
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* @see org.eclipse.draw2d.Figure#translateFromParent(Translatable)
|
126
|
*/
|
127
|
public void translateFromParent(Translatable t) {
|
128
|
t.performScale(1 / scale);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* @see org.eclipse.draw2d.Figure#translateToParent(Translatable)
|
133
|
*/
|
134
|
public void translateToParent(Translatable t) {
|
135
|
t.performScale(scale);
|
136
|
}
|
137
|
|
138
|
}
|