01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.jarpackager;
11:
12: import java.util.Iterator;
13:
14: import org.eclipse.core.resources.IFile;
15:
16: import org.eclipse.swt.widgets.Shell;
17:
18: import org.eclipse.jface.action.IAction;
19: import org.eclipse.jface.viewers.ISelection;
20: import org.eclipse.jface.viewers.IStructuredSelection;
21: import org.eclipse.jface.viewers.StructuredSelection;
22:
23: import org.eclipse.ui.IObjectActionDelegate;
24: import org.eclipse.ui.IWorkbench;
25: import org.eclipse.ui.IWorkbenchPart;
26: import org.eclipse.ui.IWorkbenchPartSite;
27: import org.eclipse.ui.PlatformUI;
28:
29: import org.eclipse.jdt.internal.ui.JavaPlugin;
30:
31: /**
32: * This abstract action delegate offers base functionality used by
33: * other JAR Package based action delegates.
34: */
35: abstract class JarPackageActionDelegate implements
36: IObjectActionDelegate {
37:
38: private IStructuredSelection fSelection;
39: private Shell fShell;
40:
41: /**
42: * Returns the active shell.
43: */
44: protected Shell getShell() {
45: if (fShell != null)
46: return fShell;
47: return JavaPlugin.getActiveWorkbenchShell();
48: }
49:
50: /* (non-Javadoc)
51: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
52: */
53: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
54: IWorkbenchPartSite site = targetPart.getSite();
55: fShell = site != null ? site.getShell() : null;
56: }
57:
58: /*
59: * @see IActionDelegate
60: */
61: public void selectionChanged(IAction action, ISelection selection) {
62: if (selection instanceof IStructuredSelection)
63: fSelection = (IStructuredSelection) selection;
64: else
65: fSelection = StructuredSelection.EMPTY;
66: }
67:
68: /**
69: * Returns the description file for the first description file in
70: * the selection. Use this method if this action is only active if
71: * one single file is selected.
72: */
73: protected IFile getDescriptionFile(IStructuredSelection selection) {
74: return (IFile) selection.getFirstElement();
75: }
76:
77: /**
78: * Returns a description file for each description file in
79: * the selection. Use this method if this action allows multiple
80: * selection.
81: */
82: protected IFile[] getDescriptionFiles(IStructuredSelection selection) {
83: IFile[] files = new IFile[selection.size()];
84: Iterator iter = selection.iterator();
85: int i = 0;
86: while (iter.hasNext())
87: files[i++] = (IFile) iter.next();
88: return files;
89: }
90:
91: protected IWorkbench getWorkbench() {
92: return PlatformUI.getWorkbench();
93: }
94:
95: protected IStructuredSelection getSelection() {
96: return fSelection;
97: }
98: }
|