KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > ReorgCopyAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.refactoring.reorg;
12
13 import java.util.List JavaDoc;
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     public void selectionChanged(IStructuredSelection selection) {
50         if (!selection.isEmpty()) {
51             if (ReorgUtils.containsOnlyProjects(selection.toList())) {
52                 setEnabled(createWorkbenchAction(selection).isEnabled());
53                 return;
54             }
55             try {
56                 List JavaDoc elements= selection.toList();
57                 IResource[] resources= ReorgUtils.getResources(elements);
58                 IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
59                 if (elements.size() != resources.length + javaElements.length)
60                     setEnabled(false);
61                 else
62                     setEnabled(RefactoringAvailabilityTester.isCopyAvailable(resources, javaElements));
63             } catch (JavaModelException e) {
64                 // no ui here - this happens on selection changes
65
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
66
if (JavaModelUtil.isExceptionToBeLogged(e))
67                     JavaPlugin.log(e);
68                 setEnabled(false);
69             }
70         } else
71             setEnabled(false);
72     }
73
74     private CopyProjectAction createWorkbenchAction(IStructuredSelection selection) {
75         CopyProjectAction action= new CopyProjectAction(getShell());
76         action.selectionChanged(selection);
77         return action;
78     }
79     
80     public void run(IStructuredSelection selection) {
81         if (ReorgUtils.containsOnlyProjects(selection.toList())){
82             createWorkbenchAction(selection).run();
83             return;
84         }
85         try {
86             List JavaDoc elements= selection.toList();
87             IResource[] resources= ReorgUtils.getResources(elements);
88             IJavaElement[] javaElements= ReorgUtils.getJavaElements(elements);
89             if (RefactoringAvailabilityTester.isCopyAvailable(resources, javaElements))
90                 RefactoringExecutionStarter.startCopyRefactoring(resources, javaElements, getShell());
91         } catch (JavaModelException e) {
92             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
93         }
94     }
95 }
96
Popular Tags