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