001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.io.IOException;
044: import java.io.UnsupportedEncodingException;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.net.URLEncoder;
048: import java.text.MessageFormat;
049: import java.util.Collection;
050: import java.util.Collections;
051: import javax.swing.event.ChangeListener;
052: import org.netbeans.api.java.classpath.ClassPath;
053: import org.netbeans.api.java.source.TreePathHandle;
054: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
055: import org.netbeans.modules.refactoring.api.MoveRefactoring;
056: import org.netbeans.modules.refactoring.api.Problem;
057: import org.netbeans.modules.refactoring.java.RetoucheUtils;
058: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
059: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
060: import org.netbeans.modules.refactoring.spi.ui.RefactoringUIBypass;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.URLMapper;
063: import org.openide.loaders.DataObject;
064: import org.openide.util.Exceptions;
065: import org.openide.util.HelpCtx;
066: import org.openide.util.NbBundle;
067: import org.openide.util.datatransfer.PasteType;
068: import org.openide.util.lookup.Lookups;
069:
070: public class MoveClassUI implements RefactoringUI, RefactoringUIBypass {
071:
072: private DataObject javaObject;
073: private MoveClassPanel panel;
074: private MoveRefactoring refactoring;
075: private String targetPkgName = "";
076: private boolean disable;
077: private FileObject targetFolder;
078: private PasteType pasteType;
079:
080: static final String getString(String key) {
081: return NbBundle.getMessage(MoveClassUI.class, key);
082: }
083:
084: public MoveClassUI(DataObject javaObject) {
085: this (javaObject, null, null, Collections
086: .<TreePathHandle> emptyList());
087: }
088:
089: public MoveClassUI(DataObject javaObject, FileObject targetFolder,
090: PasteType pasteType, Collection<TreePathHandle> handles) {
091: this .disable = targetFolder != null;
092: this .targetFolder = targetFolder;
093: this .javaObject = javaObject;
094: this .pasteType = pasteType;
095: this .refactoring = new MoveRefactoring(Lookups.fixed(javaObject
096: .getPrimaryFile(), handles.toArray(new Object[handles
097: .size()])));
098: this .refactoring.getContext().add(
099: RetoucheUtils.getClasspathInfoFor(javaObject
100: .getPrimaryFile()));
101: }
102:
103: public String getName() {
104: return getString("LBL_MoveClass");
105: }
106:
107: public String getDescription() {
108: return new MessageFormat(getString("DSC_MoveClass"))
109: .format(new Object[] { javaObject.getName(),
110: packageName() });
111: }
112:
113: public boolean isQuery() {
114: return false;
115: }
116:
117: public CustomRefactoringPanel getPanel(ChangeListener parent) {
118: if (panel == null) {
119: String pkgName = targetFolder != null ? getPackageName(targetFolder)
120: : getPackageName(javaObject.getPrimaryFile()
121: .getParent());
122: panel = new MoveClassPanel(parent, pkgName,
123: new MessageFormat(getString("LBL_MoveClassNamed"))
124: .format(new Object[] { javaObject
125: .getPrimaryFile().getName() }),
126: targetFolder != null ? targetFolder
127: : (javaObject != null ? javaObject
128: .getPrimaryFile() : null));
129: panel.setCombosEnabled(!disable);
130: }
131: return panel;
132: }
133:
134: private static String getPackageName(FileObject file) {
135: ClassPath cp = ClassPath.getClassPath(file, ClassPath.SOURCE);
136: return cp.getResourceName(file, '.', false);
137: }
138:
139: private String packageName() {
140: return targetPkgName.trim().length() == 0 ? getString("LBL_DefaultPackage")
141: : targetPkgName.trim();
142: }
143:
144: private Problem setParameters(boolean checkOnly) {
145: if (panel == null)
146: return null;
147: targetPkgName = panel.getPackageName();
148:
149: URL url = URLMapper.findURL(panel.getRootFolder(),
150: URLMapper.EXTERNAL);
151: try {
152: refactoring.setTarget(Lookups.singleton(new URL(url
153: .toExternalForm()
154: + URLEncoder.encode(panel.getPackageName().replace(
155: '.', '/'), "utf-8")))); // NOI18N
156: } catch (UnsupportedEncodingException ex) {
157: Exceptions.printStackTrace(ex);
158: } catch (MalformedURLException ex) {
159: Exceptions.printStackTrace(ex);
160: }
161: if (checkOnly) {
162: return refactoring.fastCheckParameters();
163: } else {
164: return refactoring.checkParameters();
165: }
166: }
167:
168: public Problem checkParameters() {
169: return setParameters(true);
170: }
171:
172: public Problem setParameters() {
173: return setParameters(false);
174: }
175:
176: public AbstractRefactoring getRefactoring() {
177: return refactoring;
178: }
179:
180: public boolean hasParameters() {
181: return true;
182: }
183:
184: public HelpCtx getHelpCtx() {
185: return new HelpCtx(MoveClassUI.class);
186: }
187:
188: public boolean isRefactoringBypassRequired() {
189: return !panel.isUpdateReferences();
190: }
191:
192: public void doRefactoringBypass() throws IOException {
193: pasteType.paste();
194: }
195: }
|