1 27 28 package org.nightlabs.editor2d.custom; 29 30 import org.eclipse.draw2d.FigureUtilities; 31 import org.eclipse.gef.editparts.ZoomListener; 32 import org.eclipse.gef.editparts.ZoomManager; 33 import org.eclipse.gef.ui.actions.GEFActionConstants; 34 import org.eclipse.jface.action.ContributionItem; 35 import org.eclipse.jface.util.Assert; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.SWTException; 38 import org.eclipse.swt.events.FocusEvent; 39 import org.eclipse.swt.events.FocusListener; 40 import org.eclipse.swt.events.SelectionEvent; 41 import org.eclipse.swt.events.SelectionListener; 42 import org.eclipse.swt.widgets.Combo; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Menu; 46 import org.eclipse.swt.widgets.ToolBar; 47 import org.eclipse.swt.widgets.ToolItem; 48 import org.eclipse.ui.IPartListener; 49 import org.eclipse.ui.IPartService; 50 import org.eclipse.ui.IWorkbenchPart; 51 52 53 public class EditorZoomComboContributionItem 54 extends ContributionItem 56 implements ZoomListener 57 { 58 protected Combo combo; 59 protected String [] initStrings; 60 protected ToolItem toolitem; 61 protected ZoomManager zoomManager; 62 protected IPartService service; 63 protected IPartListener partListener; 64 65 public double[] zoomLevels = {.01, .05, .1, .25, .5, .75, 1.0, 1.5, 2.0, 2.5, 3, 4, 5}; 66 67 71 public EditorZoomComboContributionItem(IPartService partService) 72 { 73 this(partService, "8888%"); } 75 76 81 public EditorZoomComboContributionItem(IPartService partService, String initString) 82 { 83 this(partService, new String [] {initString}); 84 } 85 86 91 public EditorZoomComboContributionItem(IPartService partService, String [] initStrings) 92 { 93 super(GEFActionConstants.ZOOM_TOOLBAR_WIDGET); 94 this.initStrings = initStrings; 96 service = partService; 97 Assert.isNotNull(partService); 98 partService.addPartListener(partListener = new IPartListener() { 99 public void partActivated(IWorkbenchPart part) 100 { 101 Object adapter = part.getAdapter(ZoomManager.class); 102 if (adapter != null && adapter instanceof ZoomManager) 103 { 104 setZoomManager((ZoomManager)adapter); 105 getZoomManager().setZoomLevels(zoomLevels); 106 refresh(true); 108 } 109 } 110 public void partBroughtToTop(IWorkbenchPart p) { } 111 public void partClosed(IWorkbenchPart p) { } 112 public void partDeactivated(IWorkbenchPart p) { } 113 public void partOpened(IWorkbenchPart p) { } 114 }); 115 } 116 117 protected void refresh(boolean repopulateCombo) 118 { 119 if (combo == null || combo.isDisposed()) 120 return; 121 try { 123 if (zoomManager == null) { 124 combo.setEnabled(false); 125 combo.setText(""); } else { 127 if (repopulateCombo) { 128 combo.setItems(getZoomManager().getZoomLevelsAsText()); 129 } 130 String zoom = getZoomManager().getZoomAsText(); 131 int index = combo.indexOf(zoom); 132 if (index != -1) 133 combo.select(index); 134 else 135 combo.setText(zoom); 136 combo.setEnabled(true); 137 } 138 } catch (SWTException exception) { 139 if (!SWT.getPlatform().equals("gtk")) throw exception; 141 } 142 } 143 144 148 public ZoomManager getZoomManager() 149 { 150 return zoomManager; 151 } 152 153 157 public void setZoomManager(ZoomManager zm) 158 { 159 if (zoomManager == zm) 160 return; 161 if (zoomManager != null) 162 zoomManager.removeZoomListener(this); 163 164 zoomManager = zm; 165 refresh(true); 166 167 if (zoomManager != null) 168 zoomManager.addZoomListener(this); 169 } 170 171 174 private void handleWidgetDefaultSelected(SelectionEvent event) { 175 handleWidgetSelected(event); 176 } 177 178 181 private void handleWidgetSelected(SelectionEvent event) { 182 if (zoomManager != null) 184 if (combo.getSelectionIndex() >= 0) 185 zoomManager.setZoomAsText(combo.getItem(combo.getSelectionIndex())); 186 else 187 { 188 zoomManager.setZoomAsText(combo.getText()); 189 } 190 199 refresh(false); 200 } 201 202 205 public void zoomChanged(double zoom) { 206 refresh(false); 207 } 208 209 214 protected int computeWidth(Control control) { 215 int width = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x; 216 if (SWT.getPlatform().equals("win32")) width += FigureUtilities.getTextWidth("8", control.getFont()); return width; 221 } 222 223 230 protected Control createControl(Composite parent) { 231 combo = new Combo(parent, SWT.DROP_DOWN); 232 combo.addSelectionListener(new SelectionListener() { 233 public void widgetSelected(SelectionEvent e) { 234 handleWidgetSelected(e); 235 } 236 public void widgetDefaultSelected(SelectionEvent e) { 237 handleWidgetDefaultSelected(e); 238 } 239 }); 240 combo.addFocusListener(new FocusListener() { 241 public void focusGained(FocusEvent e) { 242 } 244 public void focusLost(FocusEvent e) { 245 refresh(false); 246 } 247 }); 248 249 combo.setItems(initStrings); 251 toolitem.setWidth(computeWidth(combo)); 252 refresh(true); 253 return combo; 254 } 255 256 259 public void dispose() { 260 if (partListener == null) 261 return; 262 service.removePartListener(partListener); 263 if (zoomManager != null) { 264 zoomManager.removeZoomListener(this); 265 zoomManager = null; 266 } 267 combo = null; 268 partListener = null; 269 } 270 271 282 public void fill(ToolBar parent, int index) { 283 toolitem = new ToolItem(parent, SWT.SEPARATOR, index); 284 Control control = createControl(parent); 285 toolitem.setControl(control); 286 } 287 288 296 public final void fill(Composite parent) { 297 createControl(parent); 298 } 299 300 307 public final void fill(Menu parent, int index) { 308 Assert.isTrue(false, "Can't add a control to a menu"); } 310 } 311 | Popular Tags |