| 1 8 package com.nightlabs.editor2d.custom; 9 10 import org.eclipse.draw2d.FigureUtilities; 11 import org.eclipse.gef.editparts.ZoomListener; 12 import org.eclipse.gef.editparts.ZoomManager; 13 import org.eclipse.gef.ui.actions.GEFActionConstants; 14 import org.eclipse.jface.action.ContributionItem; 15 import org.eclipse.jface.util.Assert; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.SWTException; 18 import org.eclipse.swt.events.FocusEvent; 19 import org.eclipse.swt.events.FocusListener; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.events.SelectionListener; 22 import org.eclipse.swt.widgets.Combo; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Menu; 26 import org.eclipse.swt.widgets.ToolBar; 27 import org.eclipse.swt.widgets.ToolItem; 28 import org.eclipse.ui.IPartListener; 29 import org.eclipse.ui.IPartService; 30 import org.eclipse.ui.IWorkbenchPart; 31 32 33 public class EditorZoomComboContributionItem 34 extends ContributionItem 36 implements ZoomListener 37 { 38 protected Combo combo; 39 protected String [] initStrings; 40 protected ToolItem toolitem; 41 protected ZoomManager zoomManager; 42 protected IPartService service; 43 protected IPartListener partListener; 44 45 public double[] zoomLevels = {.01, .05, .1, .25, .5, .75, 1.0, 1.5, 2.0, 2.5, 3, 4, 5}; 46 47 51 public EditorZoomComboContributionItem(IPartService partService) 52 { 53 this(partService, "8888%"); } 55 56 61 public EditorZoomComboContributionItem(IPartService partService, String initString) 62 { 63 this(partService, new String [] {initString}); 64 } 65 66 71 public EditorZoomComboContributionItem(IPartService partService, String [] initStrings) 72 { 73 super(GEFActionConstants.ZOOM_TOOLBAR_WIDGET); 74 this.initStrings = initStrings; 76 service = partService; 77 Assert.isNotNull(partService); 78 partService.addPartListener(partListener = new IPartListener() { 79 public void partActivated(IWorkbenchPart part) 80 { 81 Object adapter = part.getAdapter(ZoomManager.class); 82 if (adapter != null && adapter instanceof ZoomManager) 83 { 84 setZoomManager((ZoomManager)adapter); 85 getZoomManager().setZoomLevels(zoomLevels); 86 refresh(true); 88 } 89 } 90 public void partBroughtToTop(IWorkbenchPart p) { } 91 public void partClosed(IWorkbenchPart p) { } 92 public void partDeactivated(IWorkbenchPart p) { } 93 public void partOpened(IWorkbenchPart p) { } 94 }); 95 } 96 97 protected void refresh(boolean repopulateCombo) 98 { 99 if (combo == null || combo.isDisposed()) 100 return; 101 try { 103 if (zoomManager == null) { 104 combo.setEnabled(false); 105 combo.setText(""); } else { 107 if (repopulateCombo) { 108 combo.setItems(getZoomManager().getZoomLevelsAsText()); 109 } 110 String zoom = getZoomManager().getZoomAsText(); 111 int index = combo.indexOf(zoom); 112 if (index != -1) 113 combo.select(index); 114 else 115 combo.setText(zoom); 116 combo.setEnabled(true); 117 } 118 } catch (SWTException exception) { 119 if (!SWT.getPlatform().equals("gtk")) throw exception; 121 } 122 } 123 124 128 public ZoomManager getZoomManager() 129 { 130 return zoomManager; 131 } 132 133 137 public void setZoomManager(ZoomManager zm) 138 { 139 if (zoomManager == zm) 140 return; 141 if (zoomManager != null) 142 zoomManager.removeZoomListener(this); 143 144 zoomManager = zm; 145 refresh(true); 146 147 if (zoomManager != null) 148 zoomManager.addZoomListener(this); 149 } 150 151 154 private void handleWidgetDefaultSelected(SelectionEvent event) { 155 handleWidgetSelected(event); 156 } 157 158 161 private void handleWidgetSelected(SelectionEvent event) { 162 if (zoomManager != null) 164 if (combo.getSelectionIndex() >= 0) 165 zoomManager.setZoomAsText(combo.getItem(combo.getSelectionIndex())); 166 else 167 { 168 zoomManager.setZoomAsText(combo.getText()); 169 } 170 179 refresh(false); 180 } 181 182 185 public void zoomChanged(double zoom) { 186 refresh(false); 187 } 188 189 194 protected int computeWidth(Control control) { 195 int width = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x; 196 if (SWT.getPlatform().equals("win32")) width += FigureUtilities.getTextWidth("8", control.getFont()); return width; 201 } 202 203 210 protected Control createControl(Composite parent) { 211 combo = new Combo(parent, SWT.DROP_DOWN); 212 combo.addSelectionListener(new SelectionListener() { 213 public void widgetSelected(SelectionEvent e) { 214 handleWidgetSelected(e); 215 } 216 public void widgetDefaultSelected(SelectionEvent e) { 217 handleWidgetDefaultSelected(e); 218 } 219 }); 220 combo.addFocusListener(new FocusListener() { 221 public void focusGained(FocusEvent e) { 222 } 224 public void focusLost(FocusEvent e) { 225 refresh(false); 226 } 227 }); 228 229 combo.setItems(initStrings); 231 toolitem.setWidth(computeWidth(combo)); 232 refresh(true); 233 return combo; 234 } 235 236 239 public void dispose() { 240 if (partListener == null) 241 return; 242 service.removePartListener(partListener); 243 if (zoomManager != null) { 244 zoomManager.removeZoomListener(this); 245 zoomManager = null; 246 } 247 combo = null; 248 partListener = null; 249 } 250 251 262 public void fill(ToolBar parent, int index) { 263 toolitem = new ToolItem(parent, SWT.SEPARATOR, index); 264 Control control = createControl(parent); 265 toolitem.setControl(control); 266 } 267 268 276 public final void fill(Composite parent) { 277 createControl(parent); 278 } 279 280 287 public final void fill(Menu parent, int index) { 288 Assert.isTrue(false, "Can't add a control to a menu"); } 290 } 291 | Popular Tags |