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.awt.datatransfer.Transferable;
044: import java.io.IOException;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import javax.swing.event.ChangeListener;
048: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
049: import org.netbeans.modules.refactoring.api.SingleCopyRefactoring;
050: import org.netbeans.modules.refactoring.api.Problem;
051: import org.netbeans.modules.refactoring.java.RetoucheUtils;
052: import org.netbeans.modules.refactoring.java.ui.CopyClassPanel;
053: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
054: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
055: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
056: import org.netbeans.modules.refactoring.spi.ui.RefactoringUIBypass;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.URLMapper;
059: import org.openide.loaders.DataFolder;
060: import org.openide.loaders.DataObject;
061: import org.openide.util.HelpCtx;
062: import org.openide.util.NbBundle;
063: import org.openide.util.datatransfer.PasteType;
064: import org.openide.util.lookup.Lookups;
065:
066: /** Refactoring UI object for Copy Class refactoring.
067: *
068: * @author Jan Becicka
069: */
070: public class CopyClassRefactoringUI implements RefactoringUI,
071: RefactoringUIBypass {
072: // reference to pull up refactoring this UI object corresponds to
073: private final SingleCopyRefactoring refactoring;
074: // UI panel for collecting parameters
075: private CopyClassPanel panel;
076: private FileObject resource;
077: private FileObject targetFolder;
078: private PasteType paste;
079:
080: public CopyClassRefactoringUI(FileObject resource,
081: FileObject target, PasteType paste) {
082: refactoring = new SingleCopyRefactoring(Lookups
083: .singleton(resource));
084: this .resource = resource;
085: this .targetFolder = target;
086: this .paste = paste;
087: }
088:
089: // --- IMPLEMENTATION OF RefactoringUI INTERFACE ---------------------------
090:
091: public boolean isQuery() {
092: return false;
093: }
094:
095: public CustomRefactoringPanel getPanel(ChangeListener parent) {
096: if (panel == null) {
097: FileObject target = targetFolder != null ? targetFolder
098: : resource.getParent();
099: panel = new CopyClassPanel(parent,
100: getName() + " - " + resource.getName(), // NOI18N
101: RetoucheUtils.getPackageName(target), target,
102: resource.getName());
103: panel.setCombosEnabled(!(targetFolder != null));
104: }
105: return panel;
106: }
107:
108: public Problem setParameters() {
109: setupRefactoring();
110: return refactoring.checkParameters();
111: }
112:
113: public Problem checkParameters() {
114: if (panel == null)
115: return null;
116: setupRefactoring();
117: return refactoring.fastCheckParameters();
118: }
119:
120: private void setupRefactoring() {
121: refactoring.setNewName(panel.getNewName());
122: URL url = URLMapper.findURL(panel.getRootFolder(),
123: URLMapper.EXTERNAL);
124: try {
125: refactoring.setTarget(Lookups.singleton(new URL(url
126: .toExternalForm()
127: + "/" + panel.getPackageName().replace('.', '/')))); // NOI18N
128: } catch (MalformedURLException ex) {
129: ex.printStackTrace();
130: }
131: }
132:
133: public AbstractRefactoring getRefactoring() {
134: return refactoring;
135: }
136:
137: public String getDescription() {
138: return NbBundle.getMessage(CopyClassRefactoringUI.class,
139: "DSC_CopyClass", refactoring.getNewName()); // NOI18N
140: }
141:
142: public String getName() {
143: return NbBundle.getMessage(CopyClassRefactoringUI.class,
144: "LBL_CopyClass"); // NOI18N
145: }
146:
147: public boolean hasParameters() {
148: return true;
149: }
150:
151: public HelpCtx getHelpCtx() {
152: return new HelpCtx(CopyClassRefactoringUI.class.getName());
153: }
154:
155: public boolean isRefactoringBypassRequired() {
156: if (panel == null)
157: return false;
158: return !panel.isUpdateReferences();
159: }
160:
161: public void doRefactoringBypass() throws IOException {
162: //Transferable t = paste.paste();
163: FileObject source = refactoring.getRefactoringSource().lookup(
164: FileObject.class);
165: FileObject target = RetoucheUtils.getOrCreateFolder(refactoring
166: .getTarget().lookup(URL.class));
167: if (source != null) {
168: DataObject sourceDo = DataObject.find(source);
169: DataFolder targetFolder = DataFolder.findFolder(target);
170: sourceDo.copy(targetFolder).rename(panel.getNewName());
171: }
172: }
173: }
|