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-2007 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:
042: package org.netbeans.modules.form;
043:
044: import java.io.File;
045: import java.net.URISyntaxException;
046: import java.net.URL;
047: import java.util.HashMap;
048: import java.util.Map;
049: import org.netbeans.api.java.classpath.ClassPath;
050: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
051: import org.netbeans.modules.refactoring.api.MoveRefactoring;
052: import org.netbeans.modules.refactoring.api.RenameRefactoring;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.util.Exceptions;
056:
057: /**
058: * Holds information about one refactoring. Knows the type of refactoring change,
059: * the originating source file and its corresponding class name. For each
060: * affected source file (form) keeps the transaction that loads and updates
061: * the form. When refactoring starts, an instance of RefactoringInfo is attached
062: * to the refactoring's context - so it can be accessed from different places
063: * (refactoring plugin, guarded block handler).
064: *
065: * @author Tomas Pavek
066: */
067: public class RefactoringInfo {
068:
069: public enum ChangeType {
070: VARIABLE_RENAME, // field or local variable in initComponents
071: CLASS_RENAME, CLASS_MOVE, // can be a form, or a component class, or both
072: CLASS_COPY, // a form class
073: CLASS_DELETE, // a form class (safe delete)
074: PACKAGE_RENAME, FOLDER_RENAME, // non-recursive folder and folder with subfolders
075: EVENT_HANDLER_RENAME, // method in a form class
076: OTHER_FORM_CHANGE
077: }
078:
079: private AbstractRefactoring refactoring;
080: private ChangeType changeType;
081: private FileObject primaryFile; // the source file where the refactoring change originated
082: private String oldName;
083: private Map<FileObject, FormRefactoringUpdate> fileToUpdateMap = new HashMap<FileObject, FormRefactoringUpdate>();
084:
085: RefactoringInfo(AbstractRefactoring refactoring,
086: ChangeType changeType, FileObject primaryFile,
087: String oldName) {
088: this .refactoring = refactoring;
089: this .changeType = changeType;
090: this .primaryFile = primaryFile;
091: this .oldName = oldName;
092: }
093:
094: AbstractRefactoring getRefactoring() {
095: return refactoring;
096: }
097:
098: public ChangeType getChangeType() {
099: return changeType;
100: }
101:
102: public FileObject getPrimaryFile() {
103: return primaryFile;
104: }
105:
106: public boolean isForm() {
107: return primaryFile != null && isJavaFileOfForm(primaryFile);
108: }
109:
110: void setOldName(String oldName) {
111: this .oldName = oldName;
112: }
113:
114: String getOldName() {
115: return oldName;
116: }
117:
118: String getNewName() {
119: if (refactoring instanceof RenameRefactoring) {
120: // return the new name of the file/element
121: return ((RenameRefactoring) refactoring).getNewName();
122: } else if (refactoring instanceof MoveRefactoring) {
123: // return full class name of the java file on its new location
124: FileObject targetFolder = getTargetFolder((MoveRefactoring) refactoring);
125: if (targetFolder != null) {
126: String pkg = ClassPath.getClassPath(targetFolder,
127: ClassPath.SOURCE).getResourceName(targetFolder,
128: '.', false);
129: return (pkg != null && pkg.length() > 0) ? pkg + "."
130: + primaryFile.getName() // NOI18N
131: : primaryFile.getName();
132: }
133: }
134: return null;
135: }
136:
137: FileObject getTargetFolder(MoveRefactoring refactoring) {
138: URL targetURL = refactoring.getTarget().lookup(URL.class);
139: FileObject targetFolder = null;
140: try {
141: File f = FileUtil
142: .normalizeFile(new File(targetURL.toURI()));
143: targetFolder = FileUtil.toFileObject(f);
144: } catch (URISyntaxException ex) {
145: Exceptions.printStackTrace(ex);
146: }
147: return targetFolder != null && targetFolder.isFolder() ? targetFolder
148: : null;
149: }
150:
151: public FormRefactoringUpdate getUpdateForFile(FileObject fo) {
152: FormRefactoringUpdate update = fileToUpdateMap.get(fo);
153: if (update == null) {
154: assert isJavaFileOfForm(fo);
155: update = new FormRefactoringUpdate(this , fo);
156: fileToUpdateMap.put(fo, update);
157: }
158: return update;
159: }
160:
161: // -----
162:
163: static boolean isJavaFile(FileObject fo) {
164: return "text/x-java".equals(fo.getMIMEType()); // NOI18N
165: }
166:
167: static boolean isJavaFileOfForm(FileObject fo) {
168: return isJavaFile(fo) && fo.existsExt("form"); // NOI18N
169: }
170: }
|