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.LinkedList;
14: import java.util.List;
15:
16: import org.eclipse.jface.action.ContributionItem;
17: import org.eclipse.swt.SWT;
18: import org.eclipse.swt.events.SelectionAdapter;
19: import org.eclipse.swt.events.SelectionEvent;
20: import org.eclipse.swt.widgets.Menu;
21: import org.eclipse.swt.widgets.MenuItem;
22: import org.eclipse.ui.presentations.IPresentablePart;
23:
24: /**
25: * @since 3.0
26: */
27: public class CloseOthersContributionItem extends ContributionItem {
28: private WrappedTabsPartPresentation presentation;
29:
30: private SelectionAdapter selectionListener = new SelectionAdapter() {
31: public void widgetSelected(SelectionEvent e) {
32: IPresentablePart current = presentation.getCurrent();
33: List parts = Arrays.asList(presentation.getParts());
34: List otherParts = new LinkedList();
35: otherParts.addAll(parts);
36: otherParts.remove(current);
37: presentation.close((IPresentablePart[]) otherParts
38: .toArray(new IPresentablePart[otherParts.size()]));
39: }
40: };
41:
42: public CloseOthersContributionItem(
43: WrappedTabsPartPresentation presentation) {
44: this .presentation = presentation;
45: }
46:
47: public void dispose() {
48: super .dispose();
49: presentation = null;
50: selectionListener = null;
51: }
52:
53: public void fill(Menu menu, int index) {
54: if (presentation.getParts().length > 1) {
55: MenuItem item = new MenuItem(menu, SWT.NONE, index);
56: item.setText("Close others");
57: item.addSelectionListener(selectionListener);
58:
59: IPresentablePart current = presentation.getCurrent();
60: item.setEnabled(current != null);
61: }
62: }
63:
64: public boolean isDynamic() {
65: return true;
66: }
67: }
|