KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > experimental > ui > CopyClassAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.experimental.ui;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import org.netbeans.jmi.javamodel.Element;
24 import org.netbeans.jmi.javamodel.JavaClass;
25 import org.netbeans.jmi.javamodel.Resource;
26 import org.netbeans.modules.java.JavaDataObject;
27 import org.netbeans.modules.javacore.JMManager;
28 import org.netbeans.modules.javacore.api.JavaModel;
29 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
30 import org.netbeans.modules.refactoring.spi.ui.AbstractRefactoringAction;
31 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
32 import org.openide.loaders.DataObject;
33 import org.openide.nodes.Node;
34 import org.openide.util.NbBundle;
35
36 /** Action that invokes Copy Class refactoring.
37  *
38  * @author Jan Becicka
39  */

40 public class CopyClassAction extends AbstractRefactoringAction {
41     
42     /** Creates a new instance of PullUpAction
43      */

44     public CopyClassAction() {
45         super(NbBundle.getMessage(CopyClassAction.class, "LBL_CopyClass_Action"), null); // NOI18N
46
putValue("noIconInMenu", Boolean.TRUE); // NOI18N
47
}
48     
49     /** Method responsible for creating RefactoringUI object.
50      * @param nodes Active nodes to perform the refactoring on.
51      * @param selectedElement Element to perform the refactoring on or null if the action
52      * was not invoked from the editor - in that case the active nodes take
53      * the precedence.
54      * @return RefactoringUI object for Copy Class refactoring.
55      */

56     protected RefactoringUI createRefactoringUI(Node[] nodes, Element selectedElement) {
57         if (selectedElement == null) {
58             // selected element is null -> action was invoked on nodes
59
JavaDataObject ob = (JavaDataObject) nodes[0].getCookie(JavaDataObject.class);
60             
61             selectedElement = JavaModel.getResource(ob.getPrimaryFile());
62         }
63         return new CopyClassRefactoringUI(selectedElement.getResource());
64     }
65     
66     /** Method that determines whether this action is enabled for the active nodes.
67      * @param activatedNodes Active nodes.
68      * @return Boolean indicating whether the action is enabled.
69      */

70     protected boolean enabled(Node[] activatedNodes) {
71         // if no nodes are active, the action should be disabled
72
if (activatedNodes.length != 1) return false;
73         
74         // the action should be enabled only if all selected nodes are associated
75
// with the same JavaDataObject (i.e. they are all declared in the same Java file)
76
// so, let's get dataobject from the first activated node
77
DataObject dobj = (DataObject) activatedNodes[0].getCookie(DataObject.class);
78         // check if the dataobject is instance of JavaDataObejct and that it represents a file
79
// that is on the IDE classpath (belongs to one of open projects)
80
if ((dobj instanceof JavaDataObject) && ((JMManager) JavaMetamodel.getManager()).mergedCPContains(dobj.getPrimaryFile())) {
81             return true;
82         } else {
83             return false;
84         }
85     }
86     
87     protected String JavaDoc iconResource () {
88         return "org/netbeans/modules/refactoring/resources/refactoring.gif"; // NOI18N
89
}
90 }
91
Popular Tags