1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.jface.action.ContributionItem; 14 import org.eclipse.jface.preference.IPreferenceStore; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.SelectionAdapter; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.graphics.GC; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.widgets.ToolBar; 23 import org.eclipse.swt.widgets.ToolItem; 24 import org.eclipse.ui.IPerspectiveDescriptor; 25 import org.eclipse.ui.IWorkbenchPage; 26 import org.eclipse.ui.IWorkbenchPreferenceConstants; 27 import org.eclipse.ui.internal.util.PrefUtil; 28 29 33 public class PerspectiveBarContributionItem extends ContributionItem { 34 35 private IPerspectiveDescriptor perspective; 36 37 private IPreferenceStore apiPreferenceStore = PrefUtil 38 .getAPIPreferenceStore(); 39 40 private ToolItem toolItem = null; 41 42 private Image image; 43 44 private IWorkbenchPage workbenchPage; 45 46 52 public PerspectiveBarContributionItem(IPerspectiveDescriptor perspective, 53 IWorkbenchPage workbenchPage) { 54 super(perspective.getId()); 55 this.perspective = perspective; 56 this.workbenchPage = workbenchPage; 57 } 58 59 62 public void dispose() { 63 super.dispose(); 64 if (image != null && !image.isDisposed()) { 65 image.dispose(); 66 image = null; 67 } 68 apiPreferenceStore = null; 69 workbenchPage = null; 70 perspective = null; 71 72 } 73 74 public void fill(ToolBar parent, int index) { 75 if (toolItem == null && parent != null && !parent.isDisposed()) { 76 77 if (index >= 0) { 78 toolItem = new ToolItem(parent, SWT.CHECK, index); 79 } else { 80 toolItem = new ToolItem(parent, SWT.CHECK); 81 } 82 83 if (image == null || image.isDisposed()) { 84 createImage(); 85 } 86 toolItem.setImage(image); 87 88 toolItem.setToolTipText(NLS.bind(WorkbenchMessages.PerspectiveBarContributionItem_toolTip, perspective.getLabel())); 89 toolItem.addSelectionListener(new SelectionAdapter() { 90 91 public void widgetSelected(SelectionEvent event) { 92 select(); 93 } 94 }); 95 toolItem.setData(this); update(); 97 } 98 } 99 100 private void createImage() { 101 ImageDescriptor imageDescriptor = perspective.getImageDescriptor(); 102 if (imageDescriptor != null) { 103 image = imageDescriptor.createImage(); 104 } else { 105 image = WorkbenchImages.getImageDescriptor( 106 IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE) 107 .createImage(); 108 } 109 } 110 111 Image getImage() { 112 if (image == null) { 113 createImage(); 114 } 115 return image; 116 } 117 118 121 public void select() { 122 if (workbenchPage.getPerspective() != perspective) { 123 workbenchPage.setPerspective(perspective); 124 } else { 125 toolItem.setSelection(true); 126 } 127 } 128 129 public void update() { 130 if (toolItem != null && !toolItem.isDisposed()) { 131 toolItem 132 .setSelection(workbenchPage.getPerspective() == perspective); 133 if (apiPreferenceStore 134 .getBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR)) { 135 if (apiPreferenceStore.getString( 136 IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR) 137 .equals(IWorkbenchPreferenceConstants.TOP_LEFT)) { 138 toolItem.setText(perspective.getLabel()); 139 } else { 140 toolItem.setText(shortenText(perspective.getLabel(), 141 toolItem)); 142 } 143 } else { 144 toolItem.setText(""); } 146 } 147 } 148 149 153 public void update(IPerspectiveDescriptor newDesc) { 154 perspective = newDesc; 155 if (toolItem != null && !toolItem.isDisposed()) { 156 ImageDescriptor imageDescriptor = perspective.getImageDescriptor(); 157 if (imageDescriptor != null) { 158 toolItem.setImage(imageDescriptor.createImage()); 159 } else { 160 toolItem.setImage(WorkbenchImages.getImageDescriptor( 161 IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE) 162 .createImage()); 163 } 164 toolItem.setToolTipText(NLS.bind(WorkbenchMessages.PerspectiveBarContributionItem_toolTip, perspective.getLabel() )); 165 } 166 update(); 167 } 168 169 IWorkbenchPage getPage() { 170 return workbenchPage; 171 } 172 173 IPerspectiveDescriptor getPerspective() { 174 return perspective; 175 } 176 177 ToolItem getToolItem() { 178 return toolItem; 179 } 180 181 189 public boolean handles(IPerspectiveDescriptor perspective, 190 IWorkbenchPage workbenchPage) { 191 return this.perspective == perspective 192 && this.workbenchPage == workbenchPage; 193 } 194 195 199 public void setPerspective(IPerspectiveDescriptor newPerspective) { 200 this.perspective = newPerspective; 201 } 202 203 void setSelection(boolean b) { 205 if (toolItem != null && !toolItem.isDisposed()) { 206 toolItem.setSelection(b); 207 } 208 } 209 210 static int getMaxWidth(Image image) { 211 return image.getBounds().width * 5; 212 } 213 214 private static final String ellipsis = "..."; 216 protected String shortenText(String textValue, ToolItem item) { 217 if (textValue == null || toolItem == null || toolItem.isDisposed()) { 218 return null; 219 } 220 String returnText = textValue; 221 GC gc = new GC(item.getParent()); 222 int maxWidth = getMaxWidth(item.getImage()); 223 if (gc.textExtent(textValue).x >= maxWidth) { 224 for (int i = textValue.length(); i > 0; i--) { 225 String test = textValue.substring(0, i); 226 test = test + ellipsis; 227 if (gc.textExtent(test).x < maxWidth) { 228 returnText = test; 229 break; 230 } 231 } 232 } 233 gc.dispose(); 234 return returnText; 235 } 236 } 237 | Popular Tags |