Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Is there a zoom to selection action somewhere?
Is there a zoom to selection action somewhere? [message #229736] Wed, 24 January 2007 13:48 Go to next message
Steinar Bang is currently offline Steinar BangFriend
Messages: 108
Registered: July 2009
Senior Member
Platform: Intel Pentium M, Ubuntu Dapper,
Sun Java SDK 1.5.0_06,
eclipse 3.2.1
GEF 3.2.1

Two very useful actions in graphical editor, are zoom to fit/zoom all,
which shows all of a model, and "zoom selection", which lets you
select a bunch of objects, and then invoke the "zoom selection" to
center on the selection, and zoom to make the selection fill all of
the available view port.

Zoom all was easy, since it was built-in
(ZoomManager#setZoomAsText(ZoomManager.FIT_ALL) for the casual
reader).

But I haven't found anything like "zoom selection", neither by
browsing through the GEF source and examples, nor by googling.

Does anyone know of an implementation of a "zoom selection" action?
Failing that, does anyone have any ideas how to go about to implement
such an action?

Thanx!


- Steinar
Re: Is there a zoom to selection action somewhere? [message #229781 is a reply to message #229736] Wed, 24 January 2007 18:59 Go to previous messageGo to next message
Steinar Bang is currently offline Steinar BangFriend
Messages: 108
Registered: July 2009
Senior Member
>>>>> Steinar Bang <sb@dod.no>:

> Does anyone know of an implementation of a "zoom selection" action?
> Failing that, does anyone have any ideas how to go about to
> implement such an action?

I started by calculating the bounding box of the selection, which
proved to be easy (see below). Then I looked at the operations of the
ZoomManager and found that there is a ZoomManager#zoomTo(Rectangle)
operation, and exclaimed "Yes!!"... and then I saw the implementation:

/**
* Currently does nothing.
* @param rect a rectangle
*/
public void zoomTo(Rectangle rect) { }

Oh well...! :-/


Calculating the bounding box, looks like this:

public class ZoomSelectionEditorActionDelegate implements IEditorActionDelegate {
public final static String ID = ZoomSelectionEditorActionDelegate.class.getCanonicalName();
private GraphicalEditor editor;

/**
* @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.ecl ipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
*/
public void setActiveEditor(IAction action, IEditorPart targetEditor)
{
editor = (GraphicalEditor) targetEditor.getAdapter(GraphicalEditor.class);
}

/**
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action. IAction)
*/
public void run(IAction action)
{
EditPartViewer viewer = editor.getRoot().getViewer();
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
System.out.println("Calculating bounding box:");
Rectangle boundingBox = null;
for (Object selObj : selection.toList()) {
GraphicalEditPart editPart = (GraphicalEditPart) selObj;
IFigure figure = editPart.getFigure();
Rectangle bounds = figure.getBounds();
if (boundingBox == null) {
boundingBox = new Rectangle(bounds);
} else {
boundingBox.union(bounds);
}
System.out.println(" Figure bounds: "+bounds);
}
System.out.println(" Bounding box: "+boundingBox);
}

/**
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse. jface.action.IAction, org.eclipse.jface.viewers.ISelection)
*/
public void selectionChanged(IAction action, ISelection selection)
{
// TODO Auto-generated method stub

}

}
Re: Is there a zoom to selection action somewhere? [message #229789 is a reply to message #229781] Wed, 24 January 2007 20:04 Go to previous messageGo to next message
Steinar Bang is currently offline Steinar BangFriend
Messages: 108
Registered: July 2009
Senior Member
>>>>> Steinar Bang <sb@dod.no>:

>>>>> Steinar Bang <sb@dod.no>:
>> Does anyone know of an implementation of a "zoom selection" action?
>> Failing that, does anyone have any ideas how to go about to
>> implement such an action?

> I started by calculating the bounding box of the selection, which
> proved to be easy (see below). Then I looked at the operations of the
> ZoomManager and found that there is a ZoomManager#zoomTo(Rectangle)
> operation, and exclaimed "Yes!!"... and then I saw the implementation:

> /**
> * Currently does nothing.
> * @param rect a rectangle
> */
> public void zoomTo(Rectangle rect) { }

> Oh well...! :-/

Turns out this wasn't too hard, with a bit of creative cut'n paste
from ZoomManager#getFitXZoomLevel(int). An IEditorActionDelegate that
implements this command is below.

But that isn't really the right home for the algorithm. Is it
possible to replace the ZoomManager, with one that uses this code in a
replaced zoomTo(Rectangle) method, I wonder...?

The IEditorActionDelegate that zooms to selection, follows:

public class ZoomSelectionEditorActionDelegate implements IEditorActionDelegate {
public final static String ID = ZoomSelectionEditorActionDelegate.class.getCanonicalName();
private GraphicalEditor editor;

/**
* @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.ecl ipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
*/
public void setActiveEditor(IAction action, IEditorPart targetEditor)
{
editor = (GraphicalEditor) targetEditor.getAdapter(GraphicalEditor.class);
}

/**
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action. IAction)
*/
public void run(IAction action)
{
GraphicalViewer viewer = (GraphicalViewer) editor.getAdapter(GraphicalViewer.class);
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
System.out.println("Calculating bounding box:");
Rectangle boundingBox = null;
for (Object selObj : selection.toList()) {
GraphicalEditPart editPart = (GraphicalEditPart) selObj;
IFigure figure = editPart.getFigure();
Rectangle bounds = figure.getBounds();
if (boundingBox == null) {
boundingBox = new Rectangle(bounds);
} else {
boundingBox.union(bounds);
}
System.out.println(" Figure bounds: "+bounds);
}
System.out.println(" Bounding box: "+boundingBox);
ZoomManager zoomer = (ZoomManager) editor.getAdapter(ZoomManager.class);
zoomer.zoomTo(boundingBox); // If this method hadn't been empty, this would have been enough...
// The rest of this method becomes unneccessary, once ZoomManager#zoomTo(Rectangle) is implemented
Dimension available = zoomer.getViewport().getClientArea().getSize();
Dimension desired = boundingBox.getSize().getCopy();
double zoom = zoomer.getZoom();
double scaleX = Math.min(available.width * zoom / desired.width, zoomer.getMaxZoom());
double scaleY = Math.min(available.height * zoom / desired.height, zoomer.getMaxZoom());
System.out.println(" scaleX: "+scaleX+" scaleY: "+scaleY);
double scale = Math.min(scaleX, scaleY);
zoomer.setZoom(scale);
zoomer.setViewLocation(boundingBox.getTopLeft());
}

/**
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse. jface.action.IAction, org.eclipse.jface.viewers.ISelection)
*/
public void selectionChanged(IAction action, ISelection selection)
{
// TODO Auto-generated method stub

}

}
Re: Is there a zoom to selection action somewhere? [message #229797 is a reply to message #229789] Wed, 24 January 2007 22:02 Go to previous message
Steinar Bang is currently offline Steinar BangFriend
Messages: 108
Registered: July 2009
Senior Member
>>>>> Steinar Bang <sb@dod.no>:

> But that isn't really the right home for the algorithm. Is it
> possible to replace the ZoomManager, with one that uses this code in
> a replaced zoomTo(Rectangle) method, I wonder...?

Not easily... :-/

The ZoomManager is a private field in ScalableRootEditPart, with no
setter method. So it isn't easily replacable in a subclass, or from
the viewer user.

So... it _is_ possible to subclass ScalableRootEditPart, replace
#getZoomManager() method and the #register() and #unregister()
#methods, and disregard the private field (though wasteful and ugly).

But my earlier attempts at explicitly setting the root edit part
(based an GEF example or other) were unsuccessful, while leaving the
one GraphicalEditor set in place, worked...

We'll see what tomorrow brings. In the meantime, a completely
untested ZoomManagerNG (a better name escaped me), with a filled in
#zoomTo() method, and a brand new #zoomToSelection() method.

Enjoy!


- Steinar

/**
* Replaces the empty {@link #zoomTo(org.eclipse.draw2d.geometry.Rectangle)}
* method of the base class. Also adds a
* {@link #zoomToSelection(IStructuredSelection)} method that can zoom to
* the best fit around the bounding box of the currently selected edit parts.
*/
public class ZoomManagerNG extends ZoomManager {

/**
* Only reimplement the non-deprecated constructor in the base class.
*
* @param pane
* @param viewport
*/
public ZoomManagerNG(ScalableFigure pane, Viewport viewport)
{
super(pane, viewport);
// TODO Auto-generated constructor stub
}

/**
* The base class implementation of this method is empty. This one isn't.
* What this method is to zoom so that the Rectangle argument fills the
* viewport.
*
* @see org.eclipse.gef.editparts.ZoomManager#zoomTo(org.eclipse.dra w2d.geometry.Rectangle)
*/
@Override
public void zoomTo(Rectangle rect)
{
Dimension available = getViewport().getClientArea().getSize();
Dimension desired = rect.getSize().getCopy();
double zoom = getZoom();
double scaleX = Math.min(available.width * zoom / desired.width, getMaxZoom());
double scaleY = Math.min(available.height * zoom / desired.height, getMaxZoom());
System.out.println(" scaleX: "+scaleX+" scaleY: "+scaleY);
double scale = Math.min(scaleX, scaleY);
setZoom(scale);
setViewLocation(rect.getTopLeft());
}

/**
* Find the bounding box of the figures of the edit parts in
* the selection argument, and call {@link #zoomTo(Rectangle)}
* on this bounding box.
*
* @param selection a selection of edit parts
*/
public void zoomToSelection(IStructuredSelection selection)
{
System.out.println("Calculating bounding box:");
Rectangle boundingBox = null;
for (Object selObj : selection.toList()) {
GraphicalEditPart editPart = (GraphicalEditPart) selObj;
IFigure figure = editPart.getFigure();
Rectangle bounds = figure.getBounds();
if (boundingBox == null) {
boundingBox = new Rectangle(bounds);
} else {
boundingBox.union(bounds);
}
System.out.println(" Figure bounds: "+bounds);
}
System.out.println(" Bounding box: "+boundingBox);
zoomTo(boundingBox);
}
}
Previous Topic:resizing
Next Topic:scrolling problem
Goto Forum:
  


Current Time: Thu Mar 28 23:30:18 GMT 2024

Powered by FUDForum. Page generated in 0.02023 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top