1 27 28 package org.nightlabs.editor2d.actions.zoom; 29 30 import java.util.Iterator ; 31 32 import org.eclipse.draw2d.IFigure; 33 import org.eclipse.draw2d.geometry.Rectangle; 34 import org.eclipse.gef.GraphicalEditPart; 35 import org.eclipse.gef.editparts.ZoomManager; 36 import org.eclipse.gef.ui.actions.SelectionAction; 37 import org.eclipse.ui.IWorkbenchPart; 38 import org.nightlabs.base.resource.SharedImages; 39 import org.nightlabs.editor2d.EditorPlugin; 40 import org.nightlabs.editor2d.custom.EditorImages; 41 import org.nightlabs.editor2d.util.EditorUtil; 42 43 44 public class ZoomSelectionAction 45 extends SelectionAction 46 { 47 public static final String ID = ZoomSelectionAction.class.getName(); 48 49 protected final Rectangle EMPTY_RECTANGLE = new Rectangle(); 50 53 public ZoomSelectionAction(IWorkbenchPart part) { 54 super(part); 55 } 56 57 protected void init() 58 { 59 super.init(); 60 setText(EditorPlugin.getResourceString("action.zoom.selection.label")); 61 setToolTipText(EditorPlugin.getResourceString("action.zoom.selection.tooltip")); 62 setId(ID); 63 setImageDescriptor(SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), ZoomSelectionAction.class)); 65 setActionDefinitionId(ID); 66 } 67 68 71 protected boolean calculateEnabled() 72 { 73 if (getSelectedObjects() != null && !getSelectedObjects().isEmpty()) 74 { 75 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) { 76 Object o = it.next(); 77 if (o instanceof GraphicalEditPart) { 78 GraphicalEditPart editPart = (GraphicalEditPart) o; 79 IFigure f = editPart.getFigure(); 80 if (!f.getBounds().equals(EMPTY_RECTANGLE)) 81 return true; 82 } 83 } 84 } 85 return false; 86 } 87 88 public void run() 89 { 90 Rectangle totalBounds = null; 91 GraphicalEditPart editPart = null; 92 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) 93 { 94 Object o = it.next(); 95 if (o instanceof GraphicalEditPart) { 96 editPart = (GraphicalEditPart) o; 97 Rectangle bounds = editPart.getFigure().getBounds(); 98 if (totalBounds == null) { 99 totalBounds = bounds.getCopy(); 100 } 101 totalBounds.union(bounds); 102 } 103 } 104 if (totalBounds != null & editPart != null) { 105 ZoomManager zoomManager = EditorUtil.getZoomManager(editPart); 106 if (zoomManager != null) 107 EditorUtil.zoomToRelativeRect(totalBounds, zoomManager); 108 } 109 } 110 111 } 112 | Popular Tags |