1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.jface.action.IContributionItem; 14 import org.eclipse.jface.action.ToolBarManager; 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.graphics.Font; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.graphics.Rectangle; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.CoolBar; 25 import org.eclipse.swt.widgets.CoolItem; 26 import org.eclipse.swt.widgets.Menu; 27 import org.eclipse.swt.widgets.MenuItem; 28 import org.eclipse.swt.widgets.ToolBar; 29 import org.eclipse.swt.widgets.ToolItem; 30 import org.eclipse.ui.internal.layout.LayoutUtil; 31 32 public class PerspectiveBarManager extends ToolBarManager { 33 34 37 public static final String SMALL_FONT = "org.eclipse.ui.smallFont"; 39 public PerspectiveBarManager(int style) { 40 super(style); 41 } 42 43 public ToolBar createControl(Composite parent) { 44 ToolBar control = super.createControl(parent); 45 46 if (control != null && !control.isDisposed()) { 47 control.setFont(getFont()); 48 } 49 50 return control; 51 } 52 53 private CoolBar coolBar; 56 private Menu chevronMenu = null; 57 58 public void handleChevron(SelectionEvent event) { 59 CoolItem item = (CoolItem) event.widget; 60 Control control = getControl(); 62 if (!(control instanceof ToolBar)) { 63 return; } 65 66 Rectangle itemBounds = item.getBounds(); 67 68 Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y)); 69 itemBounds.x = pt.x; 70 itemBounds.y = pt.y; 71 72 ToolBar toolBar = (ToolBar) control; 73 ToolItem[] tools = toolBar.getItems(); 74 int toolCount = tools.length; 75 int i = 0; 76 while (i < toolCount) { 77 81 Rectangle toolBounds = tools[i].getBounds(); 82 83 pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y)); 84 toolBounds.x = pt.x; 85 toolBounds.y = pt.y; 86 90 Rectangle intersection = itemBounds.intersection(toolBounds); 91 96 if (!intersection.equals(toolBounds)) { 97 break; 98 } 99 i++; 100 } 101 102 103 if (chevronMenu != null && !chevronMenu.isDisposed()) 105 chevronMenu.dispose(); 106 107 chevronMenu = new Menu(coolBar); 109 110 for (int j = i; j < toolCount; j++) { 111 ToolItem tool = tools[j]; 112 MenuItem menuItem = new MenuItem(chevronMenu, SWT.NONE); 113 if (tool.getSelection()) { 114 menuItem.setEnabled(false); 115 } 116 117 if (tool.getData() instanceof PerspectiveBarContributionItem) { 119 menuItem.setText(((PerspectiveBarContributionItem) tool 120 .getData()).getPerspective().getLabel()); 121 } else { 122 menuItem.setText(tool.getText()); 123 } 124 menuItem.setImage(tool.getImage()); 125 126 menuItem.setData("IContributionItem", tool.getData()); 128 menuItem.addSelectionListener(new SelectionAdapter() { 129 public void widgetSelected(SelectionEvent e) { 130 MenuItem menuItem = (MenuItem) e.widget; 133 Object item = menuItem.getData("IContributionItem"); if (item instanceof PerspectiveBarContributionItem) { 135 PerspectiveBarContributionItem contribItem = (PerspectiveBarContributionItem) item; 136 update(false); 137 contribItem.select(); 138 } 139 } 140 }); 141 } 142 148 pt = coolBar.toDisplay(new Point(event.x, event.y)); 149 chevronMenu.setLocation(pt.x, pt.y); 150 chevronMenu.setVisible(true); 151 } 152 153 156 protected void relayout(ToolBar toolBar, int oldCount, int newCount) { 157 super.relayout(toolBar, oldCount, newCount); 158 159 if (getControl() != null) { 160 LayoutUtil.resize(getControl()); 161 } 162 } 163 164 void setParent(CoolBar cool) { 165 this.coolBar = cool; 166 } 167 168 170 private Font getFont() { 171 return JFaceResources.getFont(SMALL_FONT); 172 } 173 174 179 void select(PerspectiveBarContributionItem contribItem) { 180 if (contribItem.getToolItem() == null) { 181 return; 182 } 183 if (getControl().isVisible() 185 && !isItemVisible(contribItem.getToolItem())) { 186 ensureVisible(contribItem); 187 } 188 } 189 190 195 public void addItem(PerspectiveBarContributionItem item) { 196 insert(1, item); 197 update(false); 198 } 199 200 205 public void removeItem(PerspectiveBarContributionItem item) { 206 remove(item); 207 } 208 209 215 private void ensureVisible(PerspectiveBarContributionItem contribItem) { 216 relocate(contribItem, 1); 217 } 218 219 void relocate(PerspectiveBarContributionItem contribItem, int index) { 220 PerspectiveBarContributionItem newItem = new PerspectiveBarContributionItem( 221 contribItem.getPerspective(), contribItem.getPage()); 222 223 removeItem(contribItem); 224 contribItem.dispose(); 225 contribItem = null; 226 227 insert(index, newItem); 228 update(false); 229 } 230 231 234 private boolean isItemVisible(ToolItem toolItem) { 235 Rectangle barBounds = getControl().getBounds(); 236 Rectangle itemBounds = toolItem.getBounds(); 237 return (barBounds.intersection(itemBounds).equals(itemBounds)); 238 } 239 240 243 public void arrangeToolbar() { 244 if (!getControl().isVisible()) { 246 return; 247 } 248 249 if (getControl().getItemCount() < 3) { 252 return; 253 } 254 255 IContributionItem[] items = getItems(); 257 for (int i = 2; i < items.length; i++) { 258 PerspectiveBarContributionItem contribItem = (PerspectiveBarContributionItem) items[i]; 259 if (contribItem.getToolItem().getSelection()) { 260 if (!isItemVisible(contribItem.getToolItem())) { 261 ensureVisible(contribItem); 262 break; 263 } 264 } 265 } 266 } 267 } 268 | Popular Tags |