1 8 package com.nightlabs.editor2d.actions; 9 10 import java.util.Iterator ; 11 12 import org.eclipse.draw2d.IFigure; 13 import org.eclipse.draw2d.geometry.Rectangle; 14 import org.eclipse.gef.EditPart; 15 import org.eclipse.gef.GraphicalEditPart; 16 import org.eclipse.gef.editparts.ZoomManager; 17 import org.eclipse.gef.ui.actions.SelectionAction; 18 import org.eclipse.ui.IWorkbenchPart; 19 20 import com.nightlabs.editor2d.EditorPlugin; 21 import com.nightlabs.editor2d.custom.EditorImages; 22 import com.nightlabs.editor2d.util.EditorUtil; 23 24 25 public class ZoomSelectionAction 26 extends SelectionAction 27 { 28 public static final String ID = ZoomSelectionAction.class.getName(); 29 30 protected final Rectangle EMPTY_RECTANGLE = new Rectangle(); 31 34 public ZoomSelectionAction(IWorkbenchPart part) { 35 super(part); 36 } 37 38 protected void init() 39 { 40 super.init(); 41 setText(EditorPlugin.getResourceString("action.zoom.selection.label")); 42 setToolTipText(EditorPlugin.getResourceString("action.zoom.selection.tooltip")); 43 setId(ID); 44 setImageDescriptor(EditorImages.ZOOM_SELECTION_16); 45 } 46 47 50 protected boolean calculateEnabled() 51 { 52 if (getSelectedObjects() != null && !getSelectedObjects().isEmpty()) 53 { 54 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) { 55 Object o = it.next(); 56 if (o instanceof GraphicalEditPart) { 57 GraphicalEditPart editPart = (GraphicalEditPart) o; 58 IFigure f = editPart.getFigure(); 59 if (!f.getBounds().equals(EMPTY_RECTANGLE)) 60 return true; 61 } 62 } 63 } 64 return false; 65 } 66 67 public void run() 68 { 69 Rectangle totalBounds = null; 70 GraphicalEditPart editPart = null; 71 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) 72 { 73 Object o = it.next(); 74 if (o instanceof GraphicalEditPart) { 75 editPart = (GraphicalEditPart) o; 76 Rectangle bounds = editPart.getFigure().getBounds(); 77 if (totalBounds == null) { 78 totalBounds = bounds.getCopy(); 79 } 80 totalBounds.union(bounds); 81 } 82 } 83 if (totalBounds != null & editPart != null) { 84 ZoomManager zoomManager = EditorUtil.getZoomManager(editPart); 85 if (zoomManager != null) 86 EditorUtil.zoomToRelativeRect(totalBounds, zoomManager); 87 } 88 } 89 90 } 91 | Popular Tags |