KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > MoveClassUI


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.java.ui;
20
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
28 import org.netbeans.modules.refactoring.api.MoveRefactoring;
29 import org.netbeans.modules.refactoring.api.Problem;
30 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
31 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
32 import org.netbeans.modules.refactoring.spi.ui.RefactoringUIBypass;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.URLMapper;
35 import org.openide.loaders.DataObject;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.datatransfer.PasteType;
39 import org.openide.util.lookup.Lookups;
40
41 public class MoveClassUI implements RefactoringUI, RefactoringUIBypass {
42     
43     private DataObject javaObject;
44     private MoveClassPanel panel;
45     private MoveRefactoring refactoring;
46     private String JavaDoc targetPkgName = "";
47     private boolean disable;
48     private FileObject targetFolder;
49     private PasteType pasteType;
50     
51     static final String JavaDoc getString(String JavaDoc key) {
52         return NbBundle.getMessage(MoveClassUI.class, key);
53     }
54     
55     public MoveClassUI (DataObject javaObject) {
56         this(javaObject, null, null);
57     }
58     
59     public MoveClassUI (DataObject javaObject, FileObject targetFolder, PasteType pasteType) {
60         this.disable = targetFolder != null ;
61         this.targetFolder = targetFolder;
62         this.javaObject = javaObject;
63         this.pasteType = pasteType;
64     }
65     
66     public String JavaDoc getName() {
67         return getString ("LBL_MoveClass");
68     }
69      
70     public String JavaDoc getDescription() {
71         return new MessageFormat JavaDoc(getString("DSC_MoveClass")).format(
72                 new Object JavaDoc[] {javaObject.getName(), packageName()}
73         );
74     }
75     
76     public boolean isQuery() {
77         return false;
78     }
79         
80     public CustomRefactoringPanel getPanel(ChangeListener JavaDoc parent) {
81         if (panel == null) {
82             String JavaDoc pkgName = targetFolder!=null?getPackageName(targetFolder):getPackageName(javaObject.getPrimaryFile().getParent());
83             panel = new MoveClassPanel (parent, pkgName,
84                     new MessageFormat JavaDoc(getString("LBL_MoveClassNamed")).format (
85                     new Object JavaDoc[] {javaObject.getPrimaryFile().getName()}
86                 ),
87                 targetFolder != null ? targetFolder : (javaObject != null ? javaObject.getPrimaryFile(): null)
88             );
89             panel.setCombosEnabled(!disable);
90         }
91         return panel;
92     }
93     
94     private static String JavaDoc getPackageName(FileObject file) {
95         ClassPath cp = ClassPath.getClassPath(file, ClassPath.SOURCE);
96         return cp.getResourceName(file, '.', false);
97     }
98
99     private String JavaDoc packageName () {
100         return targetPkgName.trim().length() == 0 ? getString ("LBL_DefaultPackage") : targetPkgName.trim ();
101     }
102     
103     private Problem setParameters(boolean checkOnly) {
104         if (panel==null)
105             return null;
106         targetPkgName = panel.getPackageName ();
107
108         URL JavaDoc url = URLMapper.findURL(panel.getRootFolder(), URLMapper.EXTERNAL);
109         try {
110             refactoring.setTarget(Lookups.singleton(new URL JavaDoc(url.toExternalForm() + "/" + panel.getPackageName().replace('.','/'))));
111         } catch (MalformedURLException JavaDoc ex) {
112             ex.printStackTrace();
113         }
114         if (checkOnly) {
115             return refactoring.fastCheckParameters();
116         } else {
117             return refactoring.checkParameters();
118         }
119     }
120     
121     public Problem checkParameters() {
122         return setParameters(true);
123     }
124     
125     public Problem setParameters() {
126         return setParameters(false);
127     }
128     
129     public AbstractRefactoring getRefactoring() {
130         if (refactoring == null) {
131             refactoring = new MoveRefactoring(Lookups.singleton(javaObject.getPrimaryFile()));
132         }
133         return refactoring;
134     }
135     
136     public boolean hasParameters() {
137         return true;
138     }
139     
140     public HelpCtx getHelpCtx() {
141         return new HelpCtx(MoveClassUI.class);
142     }
143
144     public boolean isRefactoringBypassRequired() {
145         return !panel.isUpdateReferences();
146     }
147     public void doRefactoringBypass() throws IOException JavaDoc {
148         pasteType.paste();
149     }
150 }
151
Popular Tags