KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.DisposeEvent;
34 import org.eclipse.swt.events.DisposeListener;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.nightlabs.base.action.AbstractContributionItem;
41 import org.nightlabs.base.custom.ColorCombo;
42 import org.nightlabs.editor2d.viewer.IZoomListener;
43 import org.nightlabs.editor2d.viewer.IZoomSupport;
44 import org.nightlabs.editor2d.viewer.ViewerPlugin;
45
46 public class ZoomContributionItem
47 extends AbstractContributionItem
48 {
49     public static final String JavaDoc ID = ZoomContributionItem.class.getName();
50
51     public ZoomContributionItem(IZoomSupport zoomSupport)
52     {
53         super(ID, ViewerPlugin.getResourceString("contribution.zoom.text"));
54         this.zoomSupport = zoomSupport;
55         zoomSupport.addZoomListener(zoomListener);
56     }
57     
58     protected IZoomSupport zoomSupport = null;
59     public IZoomSupport getZoomSupport() {
60         return zoomSupport;
61     }
62     public void setZoomSupport(IZoomSupport zoomSupport) {
63         this.zoomSupport = zoomSupport;
64     }
65
66 // protected Text text = null;
67
// protected Control createControl(Composite parent)
68
// {
69
// text = new Text(parent, SWT.BORDER);
70
// setText(getZoomSupport().getZoom());
71
// text.addDisposeListener(disposeListener);
72
// return text;
73
// }
74

75     protected ColorCombo combo = null;
76   protected Control createControl(Composite parent)
77   {
78     combo = new ColorCombo(parent, SWT.BORDER);
79     
80     // to set right width
81
String JavaDoc initString = "1000%";
82     combo.add(null, initString, 0);
83     if (getToolItem() != null) {
84         getToolItem().setWidth(computeWidth(combo));
85     }
86     combo.remove(0);
87     
88     initComboEntries(combo);
89     combo.addSelectionListener(selectionListener);
90     combo.addDisposeListener(disposeListener);
91     setText(zoomSupport.getZoom());
92     return combo;
93   }
94       
95     protected void initComboEntries(ColorCombo c)
96     {
97         addEntry(c, null, " 25%", 0.25);
98         addEntry(c, null, " 50%", 0.5);
99         addEntry(c, null, " 100%", 1.0);
100         addEntry(c, null, " 200%", 2.0);
101         addEntry(c, null, " 300%", 3.0);
102         addEntry(c, null, " 400%", 4.0);
103         addEntry(c, null, " 500%", 5.0);
104     }
105   
106     protected Map JavaDoc<String JavaDoc, Double JavaDoc> entry2ZoomValue = new HashMap JavaDoc<String JavaDoc, Double JavaDoc>();
107     protected void addEntry(ColorCombo c, Image img, String JavaDoc name, double zoomValue) {
108         c.add(img, name);
109         entry2ZoomValue.put(name, new Double JavaDoc(zoomValue));
110     }
111     
112   protected DisposeListener disposeListener = new DisposeListener()
113   {
114         public void widgetDisposed(DisposeEvent e) {
115             zoomSupport.removeZoomListener(zoomListener);
116             combo.removeSelectionListener(selectionListener);
117         }
118     };
119   
120   protected IZoomListener zoomListener = new IZoomListener()
121   {
122         public void zoomChanged(double zoom) {
123             setText(zoom);
124         }
125     };
126     
127     protected void setText(double zoom)
128     {
129         int percentage = (int) Math.floor(zoom * 100);
130         if (combo != null)
131             combo.setText(""+percentage+" %");
132     }
133     
134 // protected String initID() {
135
// return ID;
136
// }
137

138 // protected String initName() {
139
// return ViewerPlugin.getResourceString("contribution.zoom.text");
140
// }
141

142     protected SelectionListener selectionListener = new SelectionListener()
143     {
144         public void widgetDefaultSelected(SelectionEvent e) {
145             widgetSelected(e);
146         }
147         public void widgetSelected(SelectionEvent e)
148         {
149             ColorCombo c = (ColorCombo) e.getSource();
150             String JavaDoc text = c.getText();
151             if (text.contains("%")) {
152                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(text);
153                 int index = sb.lastIndexOf("%");
154                 text = sb.substring(0, index);
155             }
156             try {
157                 double newZoom = Double.parseDouble(text);
158                 double realZoom = newZoom / 100;
159                 getZoomSupport().setZoom(realZoom);
160             } catch (NumberFormatException JavaDoc nfe) {
161                 
162             }
163         }
164     };
165 }
166
Popular Tags