]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.refactoring.reorg; | |
12 | ||
13 | import java.util.List; | |
14 | ||
15 | import org.eclipse.core.resources.IResource; | |
16 | ||
17 | import org.eclipse.jface.viewers.IStructuredSelection; | |
18 | ||
19 | import org.eclipse.ui.IWorkbenchSite; | |
20 | import org.eclipse.ui.PlatformUI; | |
21 | import org.eclipse.ui.actions.CopyProjectAction; | |
22 | ||
23 | import org.eclipse.jdt.core.IJavaElement; | |
24 | import org.eclipse.jdt.core.JavaModelException; | |
25 | ||
26 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; | |
27 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; | |
28 | import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils; | |
29 | import org.eclipse.jdt.internal.corext.util.JavaModelUtil; | |
30 | ||
31 | import org.eclipse.jdt.ui.actions.SelectionDispatchAction; | |
32 | ||
33 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
34 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
35 | import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; | |
36 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
37 | ||
38 | ||
39 | public class ReorgCopyAction extends SelectionDispatchAction { | |
40 | ||
41 | public ReorgCopyAction(IWorkbenchSite site) { | |
42 | super(site); | |
43 | setText(ReorgMessages.ReorgCopyAction_3); | |
44 | setDescription(ReorgMessages.ReorgCopyAction_4); | |
45 | ||
46 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.COPY_ACTION); | |
47 | } | |
48 | ||
49 | @Override | |
50 | public void selectionChanged(IStructuredSelection selection) { | |
51 | if (!selection.isEmpty()) { | |
52 | if (ReorgUtils.containsOnlyProjects(selection.toList())) { | |
53 | setEnabled(createWorkbenchAction(selection).isEnabled()); | |
54 | return; | |
55 | } | |
56 | try { | |
57 | List<?> elements= selection.toList(); | |
58 | IResource[] resources= ReorgUtils.getResources(elements); | |
59 | IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements); | |
60 | if (elements.size() != resources.length + javaElements.length) | |
61 | setEnabled(false); | |
62 | else | |
63 | setEnabled(RefactoringAvailabilityTester.isCopyAvailable(resources, javaElements)); | |
64 | } catch (JavaModelException e) { | |
65 | // no ui here - this happens on selection changes | |
66 | // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253 | |
67 | if (JavaModelUtil.isExceptionToBeLogged(e)) | |
68 | JavaPlugin.log(e); | |
69 | setEnabled(false); | |
70 | } | |
71 | } else | |
72 | setEnabled(false); | |
73 | } | |
74 | ||
75 | private CopyProjectAction createWorkbenchAction(IStructuredSelection selection) { | |
76 | CopyProjectAction action= new CopyProjectAction(getShell()); | |
77 | action.selectionChanged(selection); | |
78 | return action; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void run(IStructuredSelection selection) { | |
83 | if (ReorgUtils.containsOnlyProjects(selection.toList())){ | |
84 | createWorkbenchAction(selection).run(); | |
85 | return; | |
86 | } | |
87 | try { | |
88 | List<?> elements= selection.toList(); | |
89 | IResource[] resources= ReorgUtils.getResources(elements); | |
90 | IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements); | |
91 | if (RefactoringAvailabilityTester.isCopyAvailable(resources, javaElements)) | |
92 | RefactoringExecutionStarter.startCopyRefactoring(resources, javaElements, getShell()); | |
93 | } catch (JavaModelException e) { | |
94 | ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); | |
95 | } | |
96 | } | |
97 | } |