Reputation: 4353
I have an RCP view with a GeoMap widget inside it. When the view is created, the map is centred on a position.
The problem I'm having is that when GeoMap calls getSize()
it is wrong. It is returning (0,0)
.
I've found one method of fixing this but it required changing code in GeoMap
even though I believe the problem is to do with the code in the view. If I change the setCenterPosition
method for GeoMap
to use the parents client area it works.
View
@PostConstruct
public void createPartControl(final Composite parent) {
Display.getDefault().asyncExec(() -> setInitialPosition());
}
private void setInitialPosition() {
widget.setCenterPosition(new PointD(longitude, latitude));
widget.redraw();
}
GeoMap
public void setCenterPosition(final Point p) {
// `parent.getClientArea()` instead of `getSize()`
final Rectangle size = getParent().getClientArea();
setMapPosition(p.x - (size.width / 2), p.y - (size.height / 2));
}
The problem with this is that I've had to change code in GeoMap
to fix a problem that is likely to do with getting the size of the view. The view hasn't had the size set by the time I call to centre the map.
I've also tried adding a resize listener to the view. This wouldn't always produce the same results when getting the size of the GeoMap
widget.
parent.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(final org.eclipse.swt.widgets.Event e) {
if (firstResize) {
setInitialPosition();
firstResize = false;
}
}
});
Here mentions that a resize listener should be used, and getClientArea
. The problem is that GeoMap
doesn't use getClientArea
but I don't know if it should be.
Is this a problem with how I'm calling setCenterPosition
or a bug in GeoMap
?
Update
I've also tried adding a resize listener to the widget.
widget.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(final org.eclipse.swt.widgets.Event e) {
if (firstResize) {
setInitialPosition();
firstResize = false;
}
}
});
For some reason the size the widget gets is too small. It is getting a size of (615, 279)
when it should be (768, 388)
.
By adding a button to call setCenterPosition
again it gets the correct size of (768, 388)
.
I can fix this if I add it inside an async
block but this seems more like a workaround than an actual fix.
widget.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(final org.eclipse.swt.widgets.Event e) {
if (firstResize) {
Display.getDefault().asyncExec(() -> {
setInitialPosition();
firstResize = false;
});
}
}
});
Upvotes: 2
Views: 63