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.cnd.refactoring.ui;
042:
043: import java.io.IOException;
044: import java.text.MessageFormat;
045: import javax.swing.event.ChangeListener;
046: import org.netbeans.modules.cnd.api.model.CsmObject;
047: import org.netbeans.modules.cnd.refactoring.support.CsmRefactoringUtils;
048: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
049: import org.netbeans.modules.refactoring.api.RenameRefactoring;
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.RefactoringUIBypass;
053: import org.openide.filesystems.FileObject;
054: import org.openide.loaders.DataFolder;
055: import org.openide.loaders.DataObject;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.NbBundle;
058: import org.openide.util.lookup.Lookups;
059:
060: /**
061: *
062: * @author Vladimir Voskresensky
063: */
064: public class RenameRefactoringUI implements RefactoringUI,
065: RefactoringUIBypass {
066: private final AbstractRefactoring refactoring;
067: private String oldName = null;
068: private String dispOldName;
069: private String newName;
070: private RenamePanel panel;
071: private boolean fromListener = false;
072: // private TreePathHandle handle;
073: private final CsmObject origObject;
074: private FileObject byPassFolder;
075: private boolean byPassPakageRename;
076:
077: public RenameRefactoringUI(CsmObject csmObject) {
078: this .origObject = csmObject;
079: this .dispOldName = this .oldName = CsmRefactoringUtils
080: .getSimpleText(this .origObject);
081: this .refactoring = new RenameRefactoring(Lookups
082: .singleton(csmObject));
083: }
084:
085: // public RenameRefactoringUI(TreePathHandle handle, CompilationInfo info) {
086: // this.handle = handle;
087: // this.refactoring = new RenameRefactoring(Lookups.singleton(handle));
088: // Element element = handle.resolveElement(info);
089: // oldName = element.getSimpleName().toString();
090: // if (element.getModifiers().contains(Modifier.PRIVATE)) {
091: // refactoring.getContext().add(RetoucheUtils.getClasspathInfoFor(false, handle.getFileObject()));
092: // } else {
093: // refactoring.getContext().add(RetoucheUtils.getClasspathInfoFor(handle));
094: // }
095: // dispOldName = oldName;
096: // }
097:
098: // public RenameRefactoringUI(FileObject file, TreePathHandle handle, CompilationInfo info) {
099: // if (handle!=null) {
100: // this.handle = handle;
101: // this.refactoring = new RenameRefactoring(Lookups.fixed(file, handle));
102: // oldName = handle.resolveElement(info).getSimpleName().toString();
103: // } else {
104: // this.refactoring = new RenameRefactoring(Lookups.fixed(file));
105: // oldName = file.getName();
106: // }
107: // dispOldName = oldName;
108: // ClasspathInfo cpInfo = handle==null?RetoucheUtils.getClasspathInfoFor(file):RetoucheUtils.getClasspathInfoFor(handle);
109: // refactoring.getContext().add(cpInfo);
110: // }
111:
112: // public RenameRefactoringUI(NonRecursiveFolder file) {
113: // this.refactoring = new RenameRefactoring(Lookups.singleton(file));
114: // oldName = RetoucheUtils.getPackageName(file.getFolder());
115: // refactoring.getContext().add(RetoucheUtils.getClasspathInfoFor(file.getFolder()));
116: // dispOldName = oldName;
117: // pkgRename = true;
118: // }
119:
120: // RenameRefactoringUI(FileObject jmiObject, String newName, TreePathHandle handle, CompilationInfo info) {
121: // if (handle!=null) {
122: // this.refactoring = new RenameRefactoring(Lookups.fixed(jmiObject, handle));
123: // } else {
124: // this.refactoring = new RenameRefactoring(Lookups.fixed(jmiObject));
125: // }
126: //
127: // oldName = newName;
128: // //[FIXME] this should be oldName of refactored object
129: // this.dispOldName = newName;
130: // ClasspathInfo cpInfo = handle==null?RetoucheUtils.getClasspathInfoFor(jmiObject):RetoucheUtils.getClasspathInfoFor(handle);
131: // refactoring.getContext().add(cpInfo);
132: // fromListener = true;
133: // }
134:
135: // RenameRefactoringUI(NonRecursiveFolder jmiObject, String newName) {
136: // this.refactoring = new RenameRefactoring(Lookups.singleton(jmiObject));
137: // refactoring.getContext().add(RetoucheUtils.getClasspathInfoFor(jmiObject.getFolder()));
138: // oldName = newName;
139: // //[FIXME] this should be oldName of refactored object
140: // this.dispOldName = newName;
141: // fromListener = true;
142: // pkgRename = true;
143: // }
144:
145: public boolean isQuery() {
146: return false;
147: }
148:
149: public CustomRefactoringPanel getPanel(ChangeListener parent) {
150: if (panel == null) {
151: String name = oldName;
152: String title = NbBundle.getMessage(RenamePanel.class,
153: "LBL_RenamePanelTitle", "", oldName); // NOI18N
154: panel = new RenamePanel(this .origObject, name, parent,
155: title, !fromListener, fromListener
156: && !byPassPakageRename);
157: }
158: return panel;
159: }
160:
161: private static String getString(String key) {
162: return NbBundle.getMessage(RenameRefactoringUI.class, key);
163: }
164:
165: public org.netbeans.modules.refactoring.api.Problem setParameters() {
166: newName = panel.getNameValue();
167: if (refactoring instanceof RenameRefactoring) {
168: ((RenameRefactoring) refactoring).setNewName(newName);
169: ((RenameRefactoring) refactoring).setSearchInComments(panel
170: .searchInComments());
171: }
172: return refactoring.checkParameters();
173: }
174:
175: public org.netbeans.modules.refactoring.api.Problem checkParameters() {
176: if (!panel.isUpdateReferences())
177: return null;
178: newName = panel.getNameValue();
179: if (refactoring instanceof RenameRefactoring) {
180: ((RenameRefactoring) refactoring).setNewName(newName);
181: }
182: return refactoring.fastCheckParameters();
183: }
184:
185: public org.netbeans.modules.refactoring.api.AbstractRefactoring getRefactoring() {
186: return refactoring;
187: }
188:
189: public String getDescription() {
190: return new MessageFormat(NbBundle.getMessage(RenamePanel.class,
191: "DSC_Rename")).format(new Object[] { dispOldName,
192: newName });
193: }
194:
195: public String getName() {
196: return NbBundle.getMessage(RenamePanel.class, "LBL_Rename");
197: }
198:
199: public boolean hasParameters() {
200: return true;
201: }
202:
203: public HelpCtx getHelpCtx() {
204: String postfix = "";
205: // if (handle==null) {
206: // postfix = ".JavaPackage";//NOI18N
207: // } else {
208: // ElementKind k = RetoucheUtils.getElementKind(handle);
209: //
210: // if (k.isClass() || k.isInterface())
211: // postfix = ".JavaClass";//NOI18N
212: // else if (k == ElementKind.METHOD)
213: // postfix = ".Method";//NOI18N
214: // else if (k.isField())
215: // postfix = ".Field";//NOI18N
216: // else
217: // postfix = "";
218: // }
219:
220: return new HelpCtx(RenameRefactoringUI.class.getName()
221: + postfix);
222: }
223:
224: public boolean isRefactoringBypassRequired() {
225: return !panel.isUpdateReferences();
226: }
227:
228: public void doRefactoringBypass() throws IOException {
229: DataObject dob = null;
230: if (byPassFolder != null) {
231: dob = DataFolder.findFolder(byPassFolder);
232: } else {
233: dob = DataObject.find(refactoring.getRefactoringSource()
234: .lookup(FileObject.class));
235: }
236: dob.rename(panel.getNameValue());
237: }
238: }
|