I Try to make a composite that I can resize using the SWT.Tracker, here my sample code
final Composite b = new Composite(parent, SWT.NONE);
b.setBounds(20, 20, 80, 80);
b.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
b.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE);
tracker.setStippled(true);
Rectangle rect = b.getBounds();
tracker.setRectangles(new Rectangle[] { rect });
if (tracker.open()) {
Rectangle after = tracker.getRectangles()[0];
b.setBounds(after);
}
tracker.dispose();
}
});
When I start dragging on the left towards the right, the cursor jumps to the right edge of the composite. When I start dragging on the top towards the bottom, the cursor jumps to the bottom edge of the composite. I looked into the documentation of the class, but couldn't find any settings to fix this issue. Someone has a pointer?
Here is the solution : Track mouse position when it reaches to the edges of the control and show the proper cursor and then take decision accordingly which style to set in the Tracker
constructor. Maintain a trim area for any edge or corner so that user feels easy to enter your Tracker
.
For example on X-axis and Y-axis you can call the trims xTrim=2
and yTrim=2
respectively so that, on X-axis it will be a 2 pixel wide and in Y-axis it will be 3 pixels wide lines to enter into the Tracker
;
Declare a listener , change cursor and call Tracker
accordingly:
Listener resizeAndMoveListener = new Listener()
{
Point point = null;
public void handleEvent(Event event)
{
mousePoint = MouseInfo.getPointerInfo().getLocation();
sRect = shell.getBounds();
compoRect = composite.getBounds();
sRect = shell.getBounds();
compoRect = composite.getBounds();
topLeftX = sRect.x + compoRect.x + 8;
topLeftY = sRect.y + compoRect.y + 31;
topRightX = sRect.x + compoRect.x + compoRect.width + 7;
topRightY = sRect.y + compoRect.y + 31;
bottomLeftX = sRect.x + compoRect.x + 8;
bottomLeftY = sRect.y + compoRect.y + 31 + compoRect.height;
bottomRightX = sRect.x + compoRect.x + compoRect.width + 8;
bottomRightY = sRect.y + compoRect.y + 31 + compoRect.height;
xTrim = 2;
yTrim = 3;
switch (event.type)
{
case SWT.MouseDown:
// Top Left corner point
if (mousePoint.x >= topLeftX && mousePoint.x <= topLeftX + xTrim
&& mousePoint.y >= topLeftY && mousePoint.y <= topLeftY + xTrim)
{
shell.setCursor(CURSOR_SIZENE);
resizeAction = 1;
resizeStyle = SWT.TOP | SWT.LEFT;
}
// Top Edge
else if (mousePoint.x > topLeftX + xTrim && mousePoint.x < topRightX - xTrim && mousePoint.y >= topRightY && mousePoint.y <= topRightY + xTrim)
{
shell.setCursor(CURSOR_SIZEN);
resizeAction = 1;
resizeStyle = SWT.TOP;
}
// Top Right corner Point
else if (mousePoint.x >= topRightX - xTrim && mousePoint.x <= topRightX && mousePoint.y >= topRightY && mousePoint.y <= topRightY + xTrim)
{
shell.setCursor(CURSOR_SIZESE);
resizeAction = 1;
resizeStyle = SWT.TOP | SWT.RIGHT;
}
// Right edge
else if (mousePoint.x >= topRightX - xTrim && mousePoint.x <= topRightX
&& mousePoint.y > topRightY + xTrim && mousePoint.y < bottomRightY - xTrim)
{
shell.setCursor(CURSOR_SIZEE);
resizeAction = 1;
resizeStyle = SWT.RIGHT;
}
// Bottom Left corner
else if (mousePoint.x >= bottomLeftX && mousePoint.x <= bottomLeftX + xTrim
&& mousePoint.y >= bottomLeftY - xTrim && mousePoint.y <= bottomLeftY)
{
shell.setCursor(CURSOR_SIZESE);
resizeAction = 1;
resizeStyle = SWT.BOTTOM | SWT.LEFT;
}
// Left Edge
else if (mousePoint.x >= topLeftX && mousePoint.x <= topLeftX + xTrim
&& mousePoint.y > topLeftY + xTrim && mousePoint.y < bottomLeftY - xTrim)
{
shell.setCursor(CURSOR_SIZEE);
resizeAction = 1;
resizeStyle = SWT.LEFT;
}
// Bottom Right corner
else if (mousePoint.x >= bottomRightX - xTrim && mousePoint.x <= bottomRightX
&& mousePoint.y >= bottomRightY - xTrim && mousePoint.y <= bottomRightY)
{
shell.setCursor(CURSOR_SIZESE);
resizeAction = 1;
resizeStyle = SWT.BOTTOM | SWT.RIGHT;
}
// Bottom Edge
else if (mousePoint.x > bottomLeftX + xTrim && mousePoint.x < bottomRightX - xTrim
&& mousePoint.y > bottomRightY - yTrim && mousePoint.y < bottomRightY)
{
shell.setCursor(CURSOR_SIZEN);
resizeAction = 1;
resizeStyle = SWT.BOTTOM;
}
else
{
shell.setCursor(CURSOR_ARROW);
resizeAction = 0;
}
if (resizeAction == 1)
resize(event, resizeStyle);
else if (event.button == 1)
{
// prepare for moving when mouse button is down and resizeAction ==0
point = new Point(event.x, event.y);
}
break;
case SWT.MouseMove:
if (point == null)
break;
int x = point.x - event.x;
int y = point.y - event.y;
Control control = (Control) event.widget;
move(shell, control, x, y);
point = null;
break;
}
}
};
Define Resize ie the Tracker
:
void resize(Event event, final int STYLE)
{
Control control = (Control) event.widget;
tracker = new Tracker(control.getParent(), SWT.RESIZE | STYLE);
tracker.setStippled(true);
Rectangle rect = control.getBounds();
tracker.setRectangles(new Rectangle[] { rect });
if (tracker.open())
{
Rectangle after = tracker.getRectangles()[0];
control.setBounds(after);
}
tracker.dispose();
resizeAction = 0;
}
Don't forget to release Tracker
on mouse up :
Listener releaseTrackerListener = new Listener()
{
@Override
public void handleEvent(Event even)
{
resizeAction = 0;
tracker.dispose();
}
};
If you want to always extend towards right-down, change the line where you create the tracker to:
Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE | SWT.RIGHT | SWT.DOWN);
You can use SWT.UP, SWT.DOWN, SWT.LEFT and SWT.RIGHT.
AppWorks' solution works excellently. It's also a good idea to add a listener on shell to set the cursor as it exits from the control boundary so that the cursor returns its original Arrow.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.