1 26 27 package org.nightlabs.editor2d.viewer.action; 28 29 import java.util.HashMap ; 30 import java.util.Map ; 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 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 75 protected ColorCombo combo = null; 76 protected Control createControl(Composite parent) 77 { 78 combo = new ColorCombo(parent, SWT.BORDER); 79 80 String 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 <String , Double > entry2ZoomValue = new HashMap <String , Double >(); 107 protected void addEntry(ColorCombo c, Image img, String name, double zoomValue) { 108 c.add(img, name); 109 entry2ZoomValue.put(name, new Double (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 138 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 text = c.getText(); 151 if (text.contains("%")) { 152 StringBuffer sb = new StringBuffer (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 nfe) { 161 162 } 163 } 164 }; 165 } 166 | Popular Tags |