001: /*******************************************************************************
002: * Copyright (c) 2000, 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 java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.action.ContributionItem;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.DisposeEvent;
019: import org.eclipse.swt.events.DisposeListener;
020: import org.eclipse.swt.events.SelectionAdapter;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.widgets.ToolBar;
023: import org.eclipse.swt.widgets.ToolItem;
024: import org.eclipse.ui.IPropertyListener;
025: import org.eclipse.ui.IViewReference;
026: import org.eclipse.ui.IWorkbenchPartConstants;
027: import org.eclipse.ui.IWorkbenchPartReference;
028: import org.eclipse.ui.IWorkbenchWindow;
029: import org.eclipse.ui.internal.util.Util;
030:
031: /**
032: * A dynamic contribution item which supports to switch to other Contexts.
033: */
034: public class ShowFastViewContribution extends ContributionItem {
035: public static final String FAST_VIEW = "FastView"; //$NON-NLS-1$
036:
037: private IWorkbenchWindow window;
038: private String fvbId;
039:
040: /**
041: * Create a new ToolBar item.
042: */
043: public ShowFastViewContribution(IWorkbenchWindow window, String id) {
044: super ("showFastViewContr"); //$NON-NLS-1$
045: this .window = window;
046: this .fvbId = id;
047: }
048:
049: /**
050: * Lagacy constructor...automatically points to the legacy FastViewBar
051: * @param window
052: */
053: public ShowFastViewContribution(IWorkbenchWindow window) {
054: this (window, FastViewBar.FASTVIEWBAR_ID);
055: }
056:
057: private void updateItem(ToolItem item, IViewReference ref) {
058: if (item.getImage() != ref.getTitleImage()) {
059: item.setImage(ref.getTitleImage());
060: }
061:
062: if (!Util.equals(item.getToolTipText(), ref.getTitle())) {
063: item.setToolTipText(ref.getTitle());
064: }
065: }
066:
067: public static ToolItem getItem(ToolBar toSearch,
068: IWorkbenchPartReference ref) {
069: ToolItem[] items = toSearch.getItems();
070:
071: for (int i = 0; i < items.length; i++) {
072: ToolItem item = items[i];
073:
074: if (item.getData(FAST_VIEW) == ref) {
075: return item;
076: }
077: }
078:
079: return null;
080: }
081:
082: /**
083: * The default implementation of this <code>IContributionItem</code>
084: * method does nothing. Subclasses may override.
085: */
086: public void fill(ToolBar parent, int index) {
087: // Get page.
088: WorkbenchPage page = (WorkbenchPage) window.getActivePage();
089: if (page == null) {
090: return;
091: }
092:
093: // Get views...
094: List fvs = new ArrayList();
095: Perspective perspective = page.getActivePerspective();
096: if (perspective != null)
097: fvs = perspective.getFastViewManager().getFastViews(fvbId);
098:
099: // Create tool item for each view.
100: for (Iterator fvIter = fvs.iterator(); fvIter.hasNext();) {
101: final IViewReference ref = (IViewReference) fvIter.next();
102: final ToolItem item = new ToolItem(parent, SWT.CHECK, index);
103: updateItem(item, ref);
104: item.setData(FAST_VIEW, ref);
105:
106: final IPropertyListener propertyListener = new IPropertyListener() {
107:
108: public void propertyChanged(Object source, int propId) {
109: if (propId == IWorkbenchPartConstants.PROP_TITLE) {
110: if (!item.isDisposed()) {
111: updateItem(item, ref);
112: }
113: }
114: }
115:
116: };
117:
118: ref.addPropertyListener(propertyListener);
119:
120: item.addDisposeListener(new DisposeListener() {
121: /* (non-Javadoc)
122: * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
123: */
124: public void widgetDisposed(DisposeEvent e) {
125: ref.removePropertyListener(propertyListener);
126: }
127: });
128:
129: // Select the active fast view's icon.
130: if (ref == page.getActiveFastView()) {
131: item.setSelection(true);
132: } else {
133: item.setSelection(false);
134: }
135:
136: item.addSelectionListener(new SelectionAdapter() {
137: public void widgetSelected(SelectionEvent e) {
138: showView(ref);
139: }
140: });
141: index++;
142: }
143: }
144:
145: /**
146: * Returns whether the contribution is dynamic.
147: */
148: public boolean isDynamic() {
149: return true;
150: }
151:
152: /**
153: * Open a view.
154: */
155: private void showView(IViewReference ref) {
156: WorkbenchPage page = (WorkbenchPage) ref.getPage();
157: page.toggleFastView(ref);
158: }
159: }
|