KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > actions > zoom > ZoomSelectionAction


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.actions.zoom;
29
30 import java.util.Iterator JavaDoc;
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 JavaDoc ID = ZoomSelectionAction.class.getName();
48   
49   protected final Rectangle EMPTY_RECTANGLE = new Rectangle();
50   /**
51    * @param part
52    */

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(EditorImages.ZOOM_SELECTION_16);
64
setImageDescriptor(SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), ZoomSelectionAction.class));
65     setActionDefinitionId(ID);
66   }
67       
68   /* (non-Javadoc)
69    * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
70    */

71   protected boolean calculateEnabled()
72   {
73     if (getSelectedObjects() != null && !getSelectedObjects().isEmpty())
74     {
75         for (Iterator JavaDoc it = getSelectedObjects().iterator(); it.hasNext(); ) {
76             Object JavaDoc 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 JavaDoc it = getSelectedObjects().iterator(); it.hasNext(); )
93     {
94       Object JavaDoc 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