1 11 package org.eclipse.ui.internal.presentations.util; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.MouseAdapter; 19 import org.eclipse.swt.events.MouseEvent; 20 import org.eclipse.swt.events.MouseListener; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.swt.widgets.Listener; 27 import org.eclipse.ui.presentations.IStackPresentationSite; 28 import org.eclipse.ui.presentations.PresentationUtil; 29 30 33 public abstract class AbstractTabFolder { 34 35 private List listeners = new ArrayList (1); 36 37 private Control toolbar; 38 private int state; 39 40 public abstract Point computeSize(int widthHint, int heightHint); 41 42 public abstract AbstractTabItem add(int index, int flags); 43 44 public abstract Composite getContentParent(); 45 public abstract void setContent(Control newContent); 46 47 public abstract AbstractTabItem[] getItems(); 48 49 public abstract AbstractTabItem getSelection(); 50 public abstract void setSelection(AbstractTabItem toSelect); 51 public abstract void setSelectedInfo(PartInfo info); 52 public abstract void enablePaneMenu(boolean enabled); 53 private int activeState = IStackPresentationSite.STATE_RESTORED; 54 55 private Listener menuListener = new Listener() { 56 59 public void handleEvent(Event event) { 60 Point globalPos = new Point(event.x, event.y); 61 handleContextMenu(globalPos, event); 62 } 63 }; 64 65 private Listener dragListener = new Listener() { 66 public void handleEvent(Event e) { 67 Point globalPos = ((Control)e.widget).toDisplay(e.x, e.y); 68 handleDragStarted(globalPos, e); 69 } 70 }; 71 72 private MouseListener mouseListener = new MouseAdapter() { 73 74 public void mouseDown(MouseEvent e) { 77 Point p = ((Control)e.widget).toDisplay(e.x, e.y); 78 79 handleMouseDown(p, e); 80 } 81 82 83 public void mouseDoubleClick(MouseEvent e) { 85 Point p = ((Control)e.widget).toDisplay(e.x, e.y); 86 87 handleDoubleClick(p, e); 88 } 89 }; 90 91 public void setActive(int activeState) { 92 this.activeState = activeState; 93 } 94 95 public int getActive() { 96 return activeState; 97 } 98 99 106 public Point getPaneMenuLocation() { 107 return getControl().toDisplay(new Point(0,0)); 108 } 109 110 117 public Point getPartListLocation() { 118 return getSystemMenuLocation(); 119 } 120 121 128 public Point getSystemMenuLocation() { 129 return getControl().toDisplay(new Point(0,0)); 130 } 131 132 140 public abstract Composite getToolbarParent(); 141 142 148 public abstract Control getControl(); 149 150 public AbstractTabItem getItem(int idx) { 151 return getItems()[idx]; 152 } 153 154 public AbstractTabItem getItem(Point toFind) { 155 AbstractTabItem[] items = getItems(); 156 157 for (int i = 0; i < items.length; i++) { 158 AbstractTabItem item = items[i]; 159 160 if (item.getBounds().contains(toFind)) { 161 return item; 162 } 163 } 164 165 return null; 166 } 167 168 public AbstractTabItem findItem(Object dataToFind) { 169 AbstractTabItem[] items = getItems(); 170 171 for (int i = 0; i < items.length; i++) { 172 AbstractTabItem item = items[i]; 173 174 if (item.getData() == dataToFind) { 175 return item; 176 } 177 } 178 179 return null; 180 } 181 182 190 public int indexOf(AbstractTabItem item) { 191 AbstractTabItem[] items = getItems(); 192 193 for (int idx = 0; idx < items.length; idx++) { 194 AbstractTabItem next = items[idx]; 195 196 if (next == item) { 197 return idx; 198 } 199 } 200 201 return -1; 202 } 203 204 public int getItemCount() { 205 return getItems().length; 206 } 207 208 public void setToolbar(Control toolbarControl) { 209 this.toolbar = toolbarControl; 210 } 211 212 public final Control getToolbar() { 213 return toolbar; 214 } 215 216 221 public void setState(int state) { 222 this.state = state; 223 } 224 225 230 public abstract Rectangle getTabArea(); 231 232 238 public void shellActive(boolean isActive) { 239 } 240 241 246 public final void addListener(TabFolderListener newListener) { 247 listeners.add(newListener); 248 } 249 250 255 public final void removeListener(TabFolderListener toRemove) { 256 listeners.remove(toRemove); 257 } 258 259 public void flushToolbarSize() { 260 261 } 262 263 protected final void fireEvent(TabFolderEvent e) { 264 for (Iterator iter = listeners.iterator(); iter.hasNext();) { 265 TabFolderListener next = (TabFolderListener)iter.next(); 266 267 next.handleEvent(e); 268 } 269 } 270 271 protected final void fireEvent(int id) { 272 fireEvent(new TabFolderEvent(id)); 273 } 274 275 protected final void fireEvent(int id, AbstractTabItem w) { 276 fireEvent(new TabFolderEvent(id, w, 0, 0)); 277 } 278 279 protected final void fireEvent(int id, AbstractTabItem w, Point pos) { 280 fireEvent(new TabFolderEvent(id, w, pos)); 281 } 282 283 public void layout(boolean flushCache) { 284 } 285 286 public void setTabPosition(int tabPosition) { 287 } 288 289 public int getTabPosition() { 290 return SWT.TOP; 291 } 292 293 public int getState() { 294 return state; 295 } 296 297 protected void attachListeners(Control theControl, boolean recursive) { 298 theControl.addListener(SWT.MenuDetect, menuListener); 299 theControl.addMouseListener(mouseListener); 300 PresentationUtil.addDragListener(theControl, dragListener); 301 302 if (recursive && theControl instanceof Composite) { 303 Composite composite = (Composite) theControl; 304 Control[] children = composite.getChildren(); 305 306 for (int i = 0; i < children.length; i++) { 307 Control control = children[i]; 308 309 attachListeners(control, recursive); 310 } 311 } 312 } 313 314 protected void detachListeners(Control theControl, boolean recursive) { 315 theControl.removeListener(SWT.MenuDetect, menuListener); 316 theControl.removeMouseListener(mouseListener); 317 PresentationUtil.removeDragListener(theControl, dragListener); 318 319 if (recursive && theControl instanceof Composite) { 320 Composite composite = (Composite) theControl; 321 Control[] children = composite.getChildren(); 322 323 for (int i = 0; i < children.length; i++) { 324 Control control = children[i]; 325 326 detachListeners(control, recursive); 327 } 328 } 329 } 330 331 protected void handleContextMenu(Point displayPos, Event e) { 332 if (isOnBorder(displayPos)) { 333 return; 334 } 335 336 AbstractTabItem tab = getItem(displayPos); 337 338 fireEvent(TabFolderEvent.EVENT_SYSTEM_MENU, tab, displayPos); 339 } 340 341 protected void handleMouseDown(Point displayPos, MouseEvent e) { 342 fireEvent(TabFolderEvent.EVENT_GIVE_FOCUS_TO_PART); 343 } 344 345 protected void handleDoubleClick(Point displayPos, MouseEvent e) { 346 if (isOnBorder(displayPos)) { 347 return; 348 } 349 350 if (getState() == IStackPresentationSite.STATE_MAXIMIZED) { 351 fireEvent(TabFolderEvent.EVENT_RESTORE); 352 } else { 353 fireEvent(TabFolderEvent.EVENT_MAXIMIZE); 354 } 355 } 356 357 protected void handleDragStarted(Point displayPos, Event e) { 358 359 if (isOnBorder(displayPos)) { 360 return; 361 } 362 363 AbstractTabItem tab = getItem(displayPos); 364 fireEvent(TabFolderEvent.EVENT_DRAG_START, tab, displayPos); 365 } 366 367 376 public boolean isOnBorder(Point toTest) { 377 return false; 378 } 379 380 388 public void setVisible(boolean visible) { 389 getControl().setVisible(visible); 390 } 391 392 400 public void showMinMax(boolean show) { 401 } 402 } 403 | Popular Tags |