KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ShowFastViewContribution


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.action.ContributionItem;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.events.DisposeListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.widgets.ToolBar;
24 import org.eclipse.swt.widgets.ToolItem;
25 import org.eclipse.ui.IPropertyListener;
26 import org.eclipse.ui.IViewReference;
27 import org.eclipse.ui.IWorkbenchPartConstants;
28 import org.eclipse.ui.IWorkbenchPartReference;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.internal.util.Util;
31
32 /**
33  * A dynamic contribution item which supports to switch to other Contexts.
34  */

35 public class ShowFastViewContribution extends ContributionItem {
36     public static final String JavaDoc FAST_VIEW = "FastView"; //$NON-NLS-1$
37

38     private IWorkbenchWindow window;
39     private String JavaDoc fvbId;
40
41     /**
42      * Create a new ToolBar item.
43      */

44     public ShowFastViewContribution(IWorkbenchWindow window, String JavaDoc id) {
45         super("showFastViewContr"); //$NON-NLS-1$
46
this.window = window;
47         this.fvbId = id;
48     }
49
50     /**
51      * Lagacy constructor...automatically points to the legacy FastViewBar
52      * @param window
53      */

54     public ShowFastViewContribution(IWorkbenchWindow window) {
55         this(window, FastViewBar.FASTVIEWBAR_ID);
56     }
57
58     private void updateItem(ToolItem item, IViewReference ref) {
59         if (item.getImage() != ref.getTitleImage()) {
60             item.setImage(ref.getTitleImage());
61         }
62
63         if (!Util.equals(item.getToolTipText(), ref.getTitle())) {
64             item.setToolTipText(ref.getTitle());
65         }
66     }
67     
68     public static ToolItem getItem(ToolBar toSearch, IWorkbenchPartReference ref) {
69         ToolItem[] items = toSearch.getItems();
70         
71         for (int i = 0; i < items.length; i++) {
72             ToolItem item = items[i];
73             
74             if (item.getData(FAST_VIEW) == ref) {
75                 return item;
76             }
77         }
78         
79         return null;
80     }
81     
82     /**
83      * The default implementation of this <code>IContributionItem</code>
84      * method does nothing. Subclasses may override.
85      */

86     public void fill(ToolBar parent, int index) {
87         // Get page.
88
WorkbenchPage page = (WorkbenchPage) window.getActivePage();
89         if (page == null) {
90             return;
91         }
92
93         // Get views...
94
List JavaDoc fvs = new ArrayList JavaDoc();
95         Perspective perspective = page.getActivePerspective();
96         if (perspective != null)
97             fvs = perspective.getFastViewManager().getFastViews(fvbId);
98
99         // Create tool item for each view.
100
for (Iterator JavaDoc 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 JavaDoc 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 }
160
Popular Tags