KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > JarImportWizardAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.jdt.internal.ui.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.wizard.WizardDialog;
20
21 import org.eclipse.ui.IImportWizard;
22 import org.eclipse.ui.IObjectActionDelegate;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26
27 import org.eclipse.jdt.core.IClasspathEntry;
28 import org.eclipse.jdt.core.IPackageFragmentRoot;
29 import org.eclipse.jdt.core.JavaModelException;
30
31 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.jarimport.JarImportWizard;
34
35 /**
36  * Combined action and action delegate for the jar import action.
37  *
38  * @since 3.2
39  */

40 public class JarImportWizardAction extends Action implements IObjectActionDelegate, ISelectionChangedListener {
41
42     /** The wizard height */
43     public static final int SIZING_WIZARD_HEIGHT= 520;
44
45     /** The wizard width */
46     public static final int SIZING_WIZARD_WIDTH= 470;
47
48     /** The structured selection, or <code>null</code> */
49     private IStructuredSelection fSelection= null;
50
51     /** The active workbench part, or <code>null</code> */
52     private IWorkbenchPart fWorkbenchPart= null;
53
54     /**
55      * {@inheritDoc}
56      */

57     public void run() {
58         run(this);
59     }
60
61     /**
62      * {@inheritDoc}
63      */

64     public void run(final IAction action) {
65         if (fWorkbenchPart == null || fSelection == null)
66             return;
67         final IImportWizard wizard= new JarImportWizard(false);
68         final IWorkbenchWindow window= fWorkbenchPart.getSite().getWorkbenchWindow();
69         wizard.init(window.getWorkbench(), fSelection);
70         final WizardDialog dialog= new WizardDialog(window.getShell(), wizard);
71         dialog.create();
72         dialog.getShell().setSize(Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x), SIZING_WIZARD_HEIGHT);
73         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
74         dialog.open();
75     }
76
77     /**
78      * {@inheritDoc}
79      */

80     public void selectionChanged(final IAction action, final ISelection selection) {
81         fSelection= null;
82         if (selection instanceof IStructuredSelection) {
83             final IStructuredSelection structured= (IStructuredSelection) selection;
84             if (structured.size() == 1) {
85                 final Object JavaDoc element= structured.getFirstElement();
86                 if (element instanceof IPackageFragmentRoot) {
87                     final IPackageFragmentRoot root= (IPackageFragmentRoot) element;
88                     try {
89                         final IClasspathEntry entry= root.getRawClasspathEntry();
90                         if (entry != null) {
91                             if (JarImportWizard.isValidClassPathEntry(entry) && JarImportWizard.isValidJavaProject(root.getJavaProject()))
92                                 fSelection= structured;
93                         }
94                     } catch (JavaModelException exception) {
95                         JavaPlugin.log(exception);
96                     }
97                 }
98             }
99         }
100         action.setEnabled(fSelection != null);
101     }
102
103     /**
104      * {@inheritDoc}
105      */

106     public void selectionChanged(final SelectionChangedEvent event) {
107         selectionChanged(this, event.getSelection());
108     }
109
110     /**
111      * {@inheritDoc}
112      */

113     public void setActivePart(final IAction action, final IWorkbenchPart part) {
114         fWorkbenchPart= part;
115     }
116 }
117
Popular Tags