1 26 27 package org.nightlabs.editor2d.viewer.action; 28 29 import java.awt.Rectangle ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import org.nightlabs.editor2d.DrawComponent; 34 import org.nightlabs.editor2d.viewer.IViewer; 35 import org.nightlabs.editor2d.viewer.ViewerPlugin; 36 import org.nightlabs.editor2d.viewer.event.ISelectionChangedListener; 37 import org.nightlabs.editor2d.viewer.event.SelectionEvent; 38 39 public class ZoomSelectionAction 40 extends ZoomAction 41 { 42 public static final String ID = ZoomSelectionAction.class.getName(); 43 44 public ZoomSelectionAction(IViewer viewer) 45 { 46 super(); 47 this.viewer = viewer; 48 viewer.getSelectionManager().addSelectionChangedListener(selectionListener); 49 } 50 51 protected IViewer viewer = null; 52 53 public void init() 54 { 55 setId(ID); 56 setText(ViewerPlugin.getResourceString("action.zoomSelecion.text")); 57 setToolTipText(ViewerPlugin.getResourceString("action.zoomSelecion.tooltip")); 58 } 59 60 public void zoomChanged(double zoom) { 61 62 } 63 64 public void run() 65 { 66 Rectangle bounds = getSelectionBounds(); 67 if (bounds != null) 68 viewer.getZoomSupport().zoomTo(bounds); 69 } 70 71 protected Rectangle getSelectionBounds() 72 { 73 List <DrawComponent> selectedDrawComponents = viewer.getSelectionManager().getSelectedDrawComponents(); 74 Rectangle totalBounds = null; 75 for (Iterator <DrawComponent> it = selectedDrawComponents.iterator(); it.hasNext(); ) { 76 DrawComponent dc = it.next(); 77 Rectangle bounds = dc.getBounds(); 78 if (totalBounds == null) 79 totalBounds = bounds; 80 else 81 totalBounds = totalBounds.union(bounds); 82 } 83 return totalBounds; 84 } 85 86 protected ISelectionChangedListener selectionListener = new ISelectionChangedListener() 87 { 88 public void selectionChanged(SelectionEvent e) 89 { 90 setEnabled(!e.isEmpty()); 91 } 92 }; 93 94 } 95 | Popular Tags |