001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.action.ContributionItem;
013: import org.eclipse.jface.action.IContributionItem;
014: import org.eclipse.jface.action.IMenuListener;
015: import org.eclipse.jface.action.IMenuManager;
016: import org.eclipse.jface.action.MenuManager;
017: import org.eclipse.jface.action.ToolBarManager;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionAdapter;
020: import org.eclipse.swt.events.SelectionEvent;
021: import org.eclipse.swt.events.SelectionListener;
022: import org.eclipse.swt.graphics.Image;
023: import org.eclipse.swt.widgets.Menu;
024: import org.eclipse.swt.widgets.MenuItem;
025: import org.eclipse.swt.widgets.ToolBar;
026: import org.eclipse.swt.widgets.ToolItem;
027: import org.eclipse.ui.IViewReference;
028: import org.eclipse.ui.internal.layout.TrimToolBarBase;
029:
030: /**
031: * @since 3.3
032: *
033: */
034: public class ViewStackTrimToolBar extends TrimToolBarBase {
035: private boolean restoreOnUnzoom = false;
036:
037: // The orientation of the fast view pane when showing a view
038: private int paneOrientation;
039:
040: // The id of the part that was showing when we minimized
041: private String selectedTabId;
042:
043: public ViewStackTrimToolBar(String id, int curSide,
044: int paneOrientation, WorkbenchWindow wbw) {
045: super (id, curSide, wbw);
046:
047: this .paneOrientation = paneOrientation;
048: dock(curSide);
049: }
050:
051: /**
052: * Put the stack back into the presentation
053: */
054: protected void restoreToPresentation() {
055: Perspective persp = wbw.getActiveWorkbenchPage()
056: .getActivePerspective();
057: //FastViewManager fvMgr = persp.getFastViewManager();
058:
059: LayoutPart part = persp.getPresentation().findPart(getId(),
060: null);
061: if (part instanceof ContainerPlaceholder) {
062: ViewStack stack = (ViewStack) ((ContainerPlaceholder) part)
063: .getRealContainer();
064: stack.setMinimized(false);
065: }
066: //fvMgr.restoreToPresentation(getId());
067: }
068:
069: public void initToolBarManager(final ToolBarManager mgr) {
070: // Set up the ToolBar with a restore button
071: IContributionItem restoreContrib = new ContributionItem() {
072: public void fill(ToolBar parent, int index) {
073: ToolItem restoreItem = new ToolItem(mgr.getControl(),
074: SWT.PUSH, index);
075: Image tbImage = WorkbenchImages
076: .getImage(IWorkbenchGraphicConstants.IMG_ETOOL_RESTORE_TRIMPART);
077: restoreItem.setImage(tbImage);
078: String menuTip = WorkbenchMessages.StandardSystemToolbar_Restore;
079: restoreItem.setToolTipText(menuTip);
080: restoreItem
081: .addSelectionListener(new SelectionListener() {
082: public void widgetDefaultSelected(
083: SelectionEvent e) {
084: restoreToPresentation();
085: }
086:
087: public void widgetSelected(SelectionEvent e) {
088: restoreToPresentation();
089: }
090: });
091: }
092: };
093: mgr.add(restoreContrib);
094:
095: ShowFastViewContribution sfvc = new ShowFastViewContribution(
096: wbw, getId());
097: mgr.add(sfvc);
098:
099: // Add context menu items
100: mgr.setContextMenuManager(new MenuManager());
101: MenuManager menuMgr = mgr.getContextMenuManager();
102:
103: final IContributionItem closeContrib = new ContributionItem() {
104: public void fill(Menu parent, int index) {
105: MenuItem closeItem = new MenuItem(parent, SWT.NONE,
106: index++);
107: closeItem
108: .setText(WorkbenchMessages.WorkbenchWindow_close);
109: closeItem.addSelectionListener(new SelectionAdapter() {
110: public void widgetSelected(SelectionEvent e) {
111: IViewReference selectedView = null;
112: if (contextToolItem != null) {
113: selectedView = (IViewReference) contextToolItem
114: .getData(ShowFastViewContribution.FAST_VIEW);
115: }
116:
117: if (selectedView != null) {
118: WorkbenchPage page = wbw
119: .getActiveWorkbenchPage();
120: if (page != null) {
121: page.hideView(selectedView);
122: }
123: }
124: }
125: });
126: }
127: };
128:
129: // We have to manage the visiblity this way...?
130: menuMgr.addMenuListener(new IMenuListener() {
131: public void menuAboutToShow(IMenuManager manager) {
132: IViewReference selectedView = null;
133: if (contextToolItem != null) {
134: selectedView = (IViewReference) contextToolItem
135: .getData(ShowFastViewContribution.FAST_VIEW);
136: }
137:
138: // Only show the 'close' item if we've clicked on a view
139: Perspective persp = wbw.getActiveWorkbenchPage()
140: .getActivePerspective();
141: closeContrib.setVisible(selectedView != null
142: && persp.isCloseable(selectedView));
143: manager.update(true);
144: }
145: });
146:
147: menuMgr.add(closeContrib);
148: }
149:
150: /* (non-Javadoc)
151: * @see org.eclipse.ui.internal.layout.TrimToolBarBase#hookControl(org.eclipse.swt.widgets.ToolBar)
152: */
153: public void hookControl(ToolBarManager mgr) {
154: // Hook a drop Listener to the control
155: // NOTE: the drop target is self-managing...it
156: // both hooks the new target and removes it on dispose
157: new FastViewDnDHandler(id, mgr, wbw);
158: }
159:
160: /**
161: * Sets whether or not the stack gets restored on an unzoom
162: * operation.
163: *
164: * @param restoreOnUnzoom
165: */
166: public void setRestoreOnUnzoom(boolean restoreOnUnzoom) {
167: this .restoreOnUnzoom = restoreOnUnzoom;
168: }
169:
170: public boolean restoreOnUnzoom() {
171: return restoreOnUnzoom;
172: }
173:
174: /**
175: * @param ref
176: * @param selected
177: */
178: public void setIconSelection(IViewReference ref, boolean selected) {
179: ToolItem item = ShowFastViewContribution.getItem(tbMgr
180: .getControl(), ref);
181: if (item != null) {
182: item.setSelection(selected);
183:
184: if (selected) {
185: selectedTabId = ref.getId();
186:
187: // Create a 'compound' id if this is a multi-instance part
188: if (ref.getSecondaryId() != null)
189: selectedTabId = selectedTabId + ':'
190: + ref.getSecondaryId();
191: }
192: }
193: }
194:
195: /**
196: * @return Returns the paneOrientation.
197: */
198: public int getPaneOrientation() {
199: return paneOrientation;
200: }
201:
202: /**
203: * Cache the tba that was on top when we were minimized
204: * @param selectedTab The id of the PartPane for the tab
205: */
206: public void setSelectedTabId(String id) {
207: selectedTabId = id;
208: }
209:
210: /**
211: * @return The id of the layout part representing the 'top' tab
212: */
213: public String getSelectedTabId() {
214: return selectedTabId;
215: }
216:
217: /**
218: * @param newOrientation The new orientation for the fact view display
219: * @param wbw The currently active WorkbenchWindow
220: */
221: public void setOrientation(int newOrientation, WorkbenchWindow wbw) {
222: if (newOrientation == paneOrientation)
223: return;
224:
225: paneOrientation = newOrientation;
226:
227: // If there's a fast view showing, toggle it to pick up the change
228: if (wbw.getActivePage() instanceof WorkbenchPage) {
229: WorkbenchPage wbp = (WorkbenchPage) wbw.getActivePage();
230: Perspective persp = wbp.getActivePerspective();
231: if (persp != null) {
232: IViewReference curRef = persp.getActiveFastView();
233: if (curRef != null) {
234: persp.setActiveFastView(null);
235: persp.setActiveFastView(curRef);
236: }
237: }
238:
239: }
240:
241: }
242: }
|