001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.jdt.internal.ui.actions;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.SelectionChangedEvent;
018: import org.eclipse.jface.wizard.WizardDialog;
019:
020: import org.eclipse.ui.IImportWizard;
021: import org.eclipse.ui.IObjectActionDelegate;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.PlatformUI;
025:
026: import org.eclipse.jdt.core.IClasspathEntry;
027: import org.eclipse.jdt.core.IPackageFragmentRoot;
028: import org.eclipse.jdt.core.JavaModelException;
029:
030: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
031: import org.eclipse.jdt.internal.ui.JavaPlugin;
032: import org.eclipse.jdt.internal.ui.jarimport.JarImportWizard;
033:
034: /**
035: * Combined action and action delegate for the jar import action.
036: *
037: * @since 3.2
038: */
039: public class JarImportWizardAction extends Action implements
040: IObjectActionDelegate, ISelectionChangedListener {
041:
042: /** The wizard height */
043: public static final int SIZING_WIZARD_HEIGHT = 520;
044:
045: /** The wizard width */
046: public static final int SIZING_WIZARD_WIDTH = 470;
047:
048: /** The structured selection, or <code>null</code> */
049: private IStructuredSelection fSelection = null;
050:
051: /** The active workbench part, or <code>null</code> */
052: private IWorkbenchPart fWorkbenchPart = null;
053:
054: /**
055: * {@inheritDoc}
056: */
057: public void run() {
058: run(this );
059: }
060:
061: /**
062: * {@inheritDoc}
063: */
064: public void run(final IAction action) {
065: if (fWorkbenchPart == null || fSelection == null)
066: return;
067: final IImportWizard wizard = new JarImportWizard(false);
068: final IWorkbenchWindow window = fWorkbenchPart.getSite()
069: .getWorkbenchWindow();
070: wizard.init(window.getWorkbench(), fSelection);
071: final WizardDialog dialog = new WizardDialog(window.getShell(),
072: wizard);
073: dialog.create();
074: dialog.getShell().setSize(
075: Math.max(SIZING_WIZARD_WIDTH, dialog.getShell()
076: .getSize().x), SIZING_WIZARD_HEIGHT);
077: PlatformUI.getWorkbench().getHelpSystem().setHelp(
078: dialog.getShell(),
079: IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
080: dialog.open();
081: }
082:
083: /**
084: * {@inheritDoc}
085: */
086: public void selectionChanged(final IAction action,
087: final ISelection selection) {
088: fSelection = null;
089: if (selection instanceof IStructuredSelection) {
090: final IStructuredSelection structured = (IStructuredSelection) selection;
091: if (structured.size() == 1) {
092: final Object element = structured.getFirstElement();
093: if (element instanceof IPackageFragmentRoot) {
094: final IPackageFragmentRoot root = (IPackageFragmentRoot) element;
095: try {
096: final IClasspathEntry entry = root
097: .getRawClasspathEntry();
098: if (entry != null) {
099: if (JarImportWizard
100: .isValidClassPathEntry(entry)
101: && JarImportWizard
102: .isValidJavaProject(root
103: .getJavaProject()))
104: fSelection = structured;
105: }
106: } catch (JavaModelException exception) {
107: JavaPlugin.log(exception);
108: }
109: }
110: }
111: }
112: action.setEnabled(fSelection != null);
113: }
114:
115: /**
116: * {@inheritDoc}
117: */
118: public void selectionChanged(final SelectionChangedEvent event) {
119: selectionChanged(this , event.getSelection());
120: }
121:
122: /**
123: * {@inheritDoc}
124: */
125: public void setActivePart(final IAction action,
126: final IWorkbenchPart part) {
127: fWorkbenchPart = part;
128: }
129: }
|