01: /*******************************************************************************
02: * Copyright (c) 2004 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Common Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/cpl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.presentation.wrappedtabs;
11:
12: import java.util.Arrays;
13: import java.util.Comparator;
14:
15: import org.eclipse.jface.action.ContributionItem;
16: import org.eclipse.swt.SWT;
17: import org.eclipse.swt.events.SelectionAdapter;
18: import org.eclipse.swt.events.SelectionEvent;
19: import org.eclipse.swt.widgets.Menu;
20: import org.eclipse.swt.widgets.MenuItem;
21: import org.eclipse.ui.presentations.IPresentablePart;
22: import org.eclipse.ui.presentations.IStackPresentationSite;
23:
24: /**
25: * @since 3.0
26: */
27: public class PartListContributionItem extends ContributionItem {
28: private WrappedTabsPartPresentation presentation;
29: private IStackPresentationSite site;
30:
31: private static final String DATA_ITEM = "org.eclipse.ui.examples.presentation.wrappedtabs.PartListContributionItem.DATA_ITEM";
32:
33: private SelectionAdapter selectionListener = new SelectionAdapter() {
34: public void widgetSelected(SelectionEvent e) {
35: MenuItem item = (MenuItem) e.widget;
36:
37: IPresentablePart part = (IPresentablePart) item
38: .getData(DATA_ITEM);
39:
40: if (part != null) {
41: site.selectPart(part);
42: }
43: }
44: };
45:
46: public PartListContributionItem(
47: WrappedTabsPartPresentation presentation,
48: IStackPresentationSite site) {
49: this .presentation = presentation;
50: this .site = site;
51: }
52:
53: public void dispose() {
54: super .dispose();
55: presentation = null;
56: site = null;
57: selectionListener = null;
58: }
59:
60: public void fill(Menu menu, int index) {
61: IPresentablePart[] parts = presentation.getParts();
62:
63: // Don't include a part list if there's only one part
64: if (parts.length <= 1) {
65: return;
66: }
67:
68: new MenuItem(menu, SWT.SEPARATOR, index++);
69:
70: Arrays.sort(parts, new Comparator() {
71: public int compare(Object arg0, Object arg1) {
72: IPresentablePart part0 = (IPresentablePart) arg0;
73: IPresentablePart part1 = (IPresentablePart) arg1;
74:
75: return part0.getName().compareToIgnoreCase(
76: part1.getName());
77: }
78: });
79:
80: for (int i = 0; i < parts.length; i++) {
81: IPresentablePart part = parts[i];
82:
83: MenuItem item = new MenuItem(menu, SWT.NONE, index++);
84: item.setText(part.getName());
85: item.setImage(part.getTitleImage());
86: item.addSelectionListener(selectionListener);
87: item.setData(DATA_ITEM, part);
88: }
89: }
90:
91: public boolean isDynamic() {
92: return true;
93: }
94: }
|