KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > action > ZoomSelectionAction


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

26
27 package org.nightlabs.editor2d.viewer.action;
28
29 import java.awt.Rectangle JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc bounds = getSelectionBounds();
67         if (bounds != null)
68             viewer.getZoomSupport().zoomTo(bounds);
69     }
70     
71     protected Rectangle JavaDoc getSelectionBounds()
72     {
73         List JavaDoc<DrawComponent> selectedDrawComponents = viewer.getSelectionManager().getSelectedDrawComponents();
74         Rectangle JavaDoc totalBounds = null;
75         for (Iterator JavaDoc<DrawComponent> it = selectedDrawComponents.iterator(); it.hasNext(); ) {
76             DrawComponent dc = it.next();
77             Rectangle JavaDoc 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