|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.internal.presentations.defaultpresentation; |
| 12 |
|
| 13 |
import org.eclipse.jface.util.Geometry; |
| 14 |
import org.eclipse.swt.SWT; |
| 15 |
import org.eclipse.swt.custom.CTabFolder; |
| 16 |
import org.eclipse.swt.custom.CTabFolder2Adapter; |
| 17 |
import org.eclipse.swt.custom.CTabFolderEvent; |
| 18 |
import org.eclipse.swt.custom.CTabItem; |
| 19 |
import org.eclipse.swt.custom.ViewForm; |
| 20 |
import org.eclipse.swt.events.DisposeEvent; |
| 21 |
import org.eclipse.swt.events.DisposeListener; |
| 22 |
import org.eclipse.swt.events.MouseAdapter; |
| 23 |
import org.eclipse.swt.events.MouseEvent; |
| 24 |
import org.eclipse.swt.graphics.Cursor; |
| 25 |
import org.eclipse.swt.graphics.Image; |
| 26 |
import org.eclipse.swt.graphics.Point; |
| 27 |
import org.eclipse.swt.graphics.Rectangle; |
| 28 |
import org.eclipse.swt.widgets.Composite; |
| 29 |
import org.eclipse.swt.widgets.Control; |
| 30 |
import org.eclipse.swt.widgets.Event; |
| 31 |
import org.eclipse.swt.widgets.Listener; |
| 32 |
import org.eclipse.swt.widgets.ToolBar; |
| 33 |
import org.eclipse.swt.widgets.ToolItem; |
| 34 |
import org.eclipse.swt.widgets.Widget; |
| 35 |
import org.eclipse.ui.IPropertyListener; |
| 36 |
import org.eclipse.ui.internal.IWorkbenchGraphicConstants; |
| 37 |
import org.eclipse.ui.internal.WorkbenchImages; |
| 38 |
import org.eclipse.ui.internal.WorkbenchMessages; |
| 39 |
import org.eclipse.ui.internal.dnd.DragUtil; |
| 40 |
import org.eclipse.ui.internal.layout.CellLayout; |
| 41 |
import org.eclipse.ui.internal.layout.LayoutUtil; |
| 42 |
import org.eclipse.ui.internal.layout.Row; |
| 43 |
import org.eclipse.ui.internal.layout.SizeCache; |
| 44 |
import org.eclipse.ui.internal.presentations.util.AbstractTabFolder; |
| 45 |
import org.eclipse.ui.internal.presentations.util.AbstractTabItem; |
| 46 |
import org.eclipse.ui.internal.presentations.util.PartInfo; |
| 47 |
import org.eclipse.ui.internal.presentations.util.ProxyControl; |
| 48 |
import org.eclipse.ui.internal.presentations.util.TabFolderEvent; |
| 49 |
import org.eclipse.ui.internal.util.Util; |
| 50 |
|
| 51 |
/** |
| 52 |
* |
| 53 |
* |
| 54 |
* |
| 55 |
* @since 3.1 |
| 56 |
*/ |
| 57 |
public class DetachedViewTabFolder extends AbstractTabFolder { |
| 58 |
|
| 59 |
private Composite control; |
| 60 |
private CTabFolder folder; |
| 61 |
private Control viewToolBar; |
| 62 |
private ViewForm viewForm; |
| 63 |
private ProxyControl toolbarProxy; |
| 64 |
private Composite topLeftControl; |
| 65 |
private SizeCache toolbarCache; |
| 66 |
private Cursor dragCursor; |
| 67 |
|
| 68 |
private static final String FULL_TITLE = "part_title"; //$NON-NLS-1$ |
| 69 |
|
| 70 |
// CTabFolder listener |
| 71 |
private CTabFolder2Adapter expandListener = new CTabFolder2Adapter() { |
| 72 |
/* |
| 73 |
* (non-Javadoc) |
| 74 |
* |
| 75 |
* @see org.eclipse.swt.custom.CTabFolder2Adapter#close(org.eclipse.swt.custom.CTabFolderEvent) |
| 76 |
*/ |
| 77 |
public void close(CTabFolderEvent event) { |
| 78 |
event.doit = false; |
| 79 |
fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(event.item)); |
| 80 |
} |
| 81 |
|
| 82 |
public void showList(CTabFolderEvent event) { |
| 83 |
event.doit = false; |
| 84 |
fireEvent(TabFolderEvent.EVENT_SHOW_LIST); |
| 85 |
} |
| 86 |
|
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
private Listener selectionListener = new Listener() { |
| 91 |
public void handleEvent(Event e) { |
| 92 |
fireEvent(TabFolderEvent.EVENT_TAB_SELECTED, getTab(e.item)); |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
private IPropertyListener systemToolbarListener = new IPropertyListener() { |
| 97 |
|
| 98 |
public void propertyChanged(Object source, int propId) { |
| 99 |
Point location; |
| 100 |
|
| 101 |
if (propId == TabFolderEvent.EVENT_PANE_MENU) { |
| 102 |
location = getPaneMenuLocation(); |
| 103 |
} else { |
| 104 |
location = new Point(0,0); |
| 105 |
} |
| 106 |
|
| 107 |
fireEvent(propId, getSelection(), location); |
| 108 |
} |
| 109 |
|
| 110 |
}; |
| 111 |
|
| 112 |
private DisposeListener tabDisposeListener = new DisposeListener() { |
| 113 |
public void widgetDisposed(DisposeEvent e) |
| 114 |
{ |
| 115 |
if (e.widget == control) { |
| 116 |
disposed(); |
| 117 |
} |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
public DetachedViewTabFolder(Composite parent) { |
| 122 |
control = new Composite(parent, SWT.NONE); |
| 123 |
control.addDisposeListener(tabDisposeListener); |
| 124 |
CellLayout layout = new CellLayout(1) |
| 125 |
.setDefaultRow(Row.fixed()) |
| 126 |
.setDefaultColumn(Row.growing()) |
| 127 |
.setRow(0, Row.growing()) |
| 128 |
.setSpacing(0, 0) |
| 129 |
.setMargins(0, 0); |
| 130 |
control.setLayout(layout); |
| 131 |
dragCursor = parent.getDisplay().getSystemCursor(SWT.CURSOR_SIZEALL); |
| 132 |
|
| 133 |
viewForm = new ViewForm(control, SWT.FLAT); |
| 134 |
attachListeners(viewForm, false); |
| 135 |
|
| 136 |
topLeftControl = new Composite(viewForm, SWT.NONE); |
| 137 |
topLeftControl.setLayout(new CellLayout(0).setDefaultColumn(Row.fixed()).setSpacing(0,0).setMargins(0,0)); |
| 138 |
topLeftControl.setCursor(dragCursor); |
| 139 |
attachListeners(topLeftControl, false); |
| 140 |
toolbarProxy = new ProxyControl(topLeftControl); |
| 141 |
viewForm.setTopLeft(topLeftControl); |
| 142 |
|
| 143 |
toolbarCache = new SizeCache(); |
| 144 |
|
| 145 |
folder = new CTabFolder(control, SWT.BOTTOM); |
| 146 |
folder.setMinimizeVisible(false); |
| 147 |
folder.setMaximizeVisible(false); |
| 148 |
folder.setUnselectedCloseVisible(true); |
| 149 |
folder.setUnselectedImageVisible(true); |
| 150 |
folder.addListener(SWT.Selection, selectionListener); |
| 151 |
folder.addCTabFolder2Listener(expandListener); |
| 152 |
attachListeners(folder, false); |
| 153 |
|
| 154 |
// Initialize view menu dropdown |
| 155 |
{ |
| 156 |
ToolBar actualToolBar = new ToolBar(viewForm, SWT.FLAT | SWT.NO_BACKGROUND); |
| 157 |
viewToolBar = actualToolBar; |
| 158 |
|
| 159 |
ToolItem pullDownButton = new ToolItem(actualToolBar, SWT.PUSH); |
| 160 |
Image hoverImage = WorkbenchImages |
| 161 |
.getImage(IWorkbenchGraphicConstants.IMG_LCL_RENDERED_VIEW_MENU); |
| 162 |
pullDownButton.setDisabledImage(hoverImage); |
| 163 |
pullDownButton.setImage(hoverImage); |
| 164 |
pullDownButton.setToolTipText(WorkbenchMessages.Menu); |
| 165 |
actualToolBar.addMouseListener(new MouseAdapter() { |
| 166 |
public void mouseDown(MouseEvent e) { |
| 167 |
fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), getPaneMenuLocation()); |
| 168 |
} |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
viewForm.setTopRight(viewToolBar); |
| 173 |
} |
| 174 |
|
| 175 |
protected void disposed() { |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
/* (non-Javadoc) |
| 180 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#computeSize(int, int) |
| 181 |
*/ |
| 182 |
public Point computeSize(int widthHint, int heightHint) { |
| 183 |
return new Point(50, 50); |
| 184 |
} |
| 185 |
|
| 186 |
/* (non-Javadoc) |
| 187 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#add(int) |
| 188 |
*/ |
| 189 |
public AbstractTabItem add(int index, int flags) { |
| 190 |
DetachedViewTabItem item = new DetachedViewTabItem(this, index, flags); |
| 191 |
item.getWidget().setData(item); |
| 192 |
item.getWidget().addDisposeListener(tabDisposeListener); |
| 193 |
|
| 194 |
if (folder.getItemCount() == 2) { |
| 195 |
layout(true); |
| 196 |
} |
| 197 |
|
| 198 |
return item; |
| 199 |
} |
| 200 |
|
| 201 |
/* (non-Javadoc) |
| 202 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#layout(boolean) |
| 203 |
*/ |
| 204 |
public void layout(boolean flushCache) { |
| 205 |
|
| 206 |
Rectangle oldBounds = viewForm.getBounds(); |
| 207 |
|
| 208 |
CellLayout layout = (CellLayout)control.getLayout(); |
| 209 |
if (folder.getItemCount() < 2) { |
| 210 |
layout.setRow(1, Row.fixed(0)); |
| 211 |
} else { |
| 212 |
layout.setRow(1, Row.fixed(folder.getTabHeight() + 2)); |
| 213 |
} |
| 214 |
|
| 215 |
super.layout(flushCache); |
| 216 |
|
| 217 |
control.layout(flushCache); |
| 218 |
|
| 219 |
Rectangle newBounds = viewForm.getBounds(); |
| 220 |
|
| 221 |
if (Util.equals(oldBounds, newBounds)) { |
| 222 |
viewForm.layout(flushCache); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/* (non-Javadoc) |
| 227 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getClientArea() |
| 228 |
*/ |
| 229 |
public Rectangle getClientArea() { |
| 230 |
Control content = viewForm.getContent(); |
| 231 |
|
| 232 |
if (content == null) { |
| 233 |
return new Rectangle(0,0,0,0); |
| 234 |
} |
| 235 |
|
| 236 |
return Geometry.toControl(control, DragUtil.getDisplayBounds(content)); |
| 237 |
} |
| 238 |
|
| 239 |
/* (non-Javadoc) |
| 240 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItems() |
| 241 |
*/ |
| 242 |
public AbstractTabItem[] getItems() { |
| 243 |
CTabItem[] items = folder.getItems(); |
| 244 |
|
| 245 |
AbstractTabItem[] result = new AbstractTabItem[items.length]; |
| 246 |
|
| 247 |
for (int i = 0; i < result.length; i++) { |
| 248 |
result[i] = getTab(items[i]); |
| 249 |
} |
| 250 |
|
| 251 |
return result; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* @param item |
| 256 |
* @return |
| 257 |
* @since 3.1 |
| 258 |
*/ |
| 259 |
private AbstractTabItem getTab(Widget item) { |
| 260 |
return (AbstractTabItem)item.getData(); |
| 261 |
} |
| 262 |
|
| 263 |
/* (non-Javadoc) |
| 264 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelection(org.eclipse.ui.internal.presentations.util.Widget) |
| 265 |
*/ |
| 266 |
public void setSelection(AbstractTabItem toSelect) { |
| 267 |
if (toSelect == null) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
DetachedViewTabItem tab = (DetachedViewTabItem) toSelect; |
| 272 |
folder.setSelection((CTabItem)tab.getWidget()); |
| 273 |
} |
| 274 |
|
| 275 |
/* (non-Javadoc) |
| 276 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelectedInfo(org.eclipse.ui.internal.presentations.util.PartInfo) |
| 277 |
*/ |
| 278 |
public void setSelectedInfo(PartInfo info) { |
| 279 |
} |
| 280 |
/* (non-Javadoc) |
| 281 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getToolbarParent() |
| 282 |
*/ |
| 283 |
public Composite getToolbarParent() { |
| 284 |
return viewForm; |
| 285 |
} |
| 286 |
|
| 287 |
/* (non-Javadoc) |
| 288 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getTabArea() |
| 289 |
*/ |
| 290 |
public Rectangle getTabArea() { |
| 291 |
return Geometry.toDisplay(folder.getParent(), folder.getBounds()); |
| 292 |
} |
| 293 |
|
| 294 |
/* (non-Javadoc) |
| 295 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setToolbar(org.eclipse.swt.widgets.Control) |
| 296 |
*/ |
| 297 |
public void setToolbar(Control toolbarControl) { |
| 298 |
|
| 299 |
if (toolbarControl == getToolbar()) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
if (toolbarControl != null) { |
| 304 |
toolbarCache.setControl(toolbarControl); |
| 305 |
toolbarProxy.setTarget(toolbarCache); |
| 306 |
LayoutUtil.resize(toolbarProxy.getControl()); |
| 307 |
|
| 308 |
toolbarProxy.layout(); |
| 309 |
//toolbarControl.moveAbove(null); |
| 310 |
// if (!toolbarControl.getVisible()) { |
| 311 |
// System.out.println("not visible"); //$NON-NLS-1$ |
| 312 |
// } |
| 313 |
// |
| 314 |
// |
| 315 |
// Rectangle rect = DragUtil.getDisplayBounds(toolbarControl); |
| 316 |
// Rectangle proxyRect = DragUtil.getDisplayBounds(toolbarProxy.getControl()); |
| 317 |
// if (!proxyRect.equals(rect)) { |
| 318 |
// System.out.println("wrong bounds"); //$NON-NLS-1$ |
| 319 |
// } |
| 320 |
} else { |
| 321 |
toolbarCache.setControl(null); |
| 322 |
toolbarProxy.setTarget(null); |
| 323 |
} |
| 324 |
|
| 325 |
super.setToolbar(toolbarControl); |
| 326 |
} |
| 327 |
|
| 328 |
public Control getControl() { |
| 329 |
return control; |
| 330 |
} |
| 331 |
|
| 332 |
/* (non-Javadoc) |
| 333 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#isOnBorder(org.eclipse.swt.graphics.Point) |
| 334 |
*/ |
| 335 |
public boolean isOnBorder(Point globalPos) { |
| 336 |
Point localPos = getControl().toControl(globalPos); |
| 337 |
|
| 338 |
Rectangle clientArea = getClientArea(); |
| 339 |
return localPos.y > clientArea.y && localPos.y < clientArea.y + clientArea.height; |
| 340 |
} |
| 341 |
|
| 342 |
public AbstractTabItem getSelection() { |
| 343 |
CTabItem sel = folder.getSelection(); |
| 344 |
|
| 345 |
if (sel == null) { |
| 346 |
return null; |
| 347 |
} |
| 348 |
|
| 349 |
return getTab(sel); |
| 350 |
} |
| 351 |
|
| 352 |
/* (non-Javadoc) |
| 353 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getContentParent() |
| 354 |
*/ |
| 355 |
public Composite getContentParent() { |
| 356 |
return viewForm; |
| 357 |
} |
| 358 |
|
| 359 |
/* (non-Javadoc) |
| 360 |
* @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setContent(org.eclipse.swt.widgets.Control) |
| 361 |
*/ |
| 362 |
public void setContent(Control newContent) { |
| 363 |
viewForm.setContent(newContent); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* @return |
| 368 |
* @since 3.1 |
| 369 |
*/ |
| 370 |
public CTabFolder getTabFolder() { |
| 371 |
return folder; |
| 372 |
} |
| 373 |
|
| 374 |
protected void handleDragStarted(Point displayPos, Event e) { |
| 375 |
if (isOnBorder(displayPos)) { |
| 376 |
return; |
| 377 |
} |
| 378 |
|
| 379 |
AbstractTabItem tab = null; |
| 380 |
|
| 381 |
if (DragUtil.getDisplayBounds(viewForm).contains(displayPos)) { |
| 382 |
tab = getSelection(); |
| 383 |
} else { |
| 384 |
tab = getItem(displayPos); |
| 385 |
} |
| 386 |
|
| 387 |
fireEvent(TabFolderEvent.EVENT_DRAG_START, tab, displayPos); |
| 388 |
} |
| 389 |
|
| 390 |
public Point getPaneMenuLocation() { |
| 391 |
Point toolbarSize = viewToolBar.getSize(); |
| 392 |
|
| 393 |
return viewToolBar.toDisplay(0,toolbarSize.y); |
| 394 |
} |
| 395 |
|
| 396 |
public void enablePaneMenu(boolean enabled) { |
| 397 |
if (enabled) { |
| 398 |
viewToolBar.setVisible(true); |
| 399 |
} else { |
| 400 |
viewToolBar.setVisible(false); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
public void itemRemoved() { |
| 405 |
if (folder.getItemCount() == 1 && !control.isDisposed() && !viewForm.isDisposed()) { |
| 406 |
layout(true); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
} |