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.spi;
042:
043: import java.io.IOException;
044: import java.lang.ref.WeakReference;
045: import javax.lang.model.element.ElementKind;
046: import org.netbeans.api.java.source.ModificationResult;
047: import org.netbeans.api.java.source.ModificationResult.Difference;
048: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
049: import org.netbeans.modules.refactoring.java.ui.tree.ElementGripFactory;
050: import org.openide.filesystems.FileObject;
051: import org.openide.text.PositionBounds;
052: import static org.netbeans.modules.refactoring.java.RetoucheUtils.*;
053: import org.openide.text.PositionRef;
054: import org.openide.util.Exceptions;
055: import org.openide.util.Lookup;
056: import org.openide.util.lookup.Lookups;
057:
058: /**
059: * Implementatation of RefactoringElementImplementation specific to refactoring
060: * in java files.
061: *
062: * @author Jan Becicka
063: */
064: public final class DiffElement extends
065: SimpleRefactoringElementImplementation {
066: private PositionBounds bounds;
067: private String displayText;
068: private FileObject parentFile;
069: private Difference diff;
070: private ModificationResult modification;
071: private WeakReference<String> newFileContent;
072:
073: private DiffElement(Difference diff, PositionBounds bounds,
074: FileObject parentFile, ModificationResult modification) {
075: this .bounds = bounds;
076: this .displayText = diff.getDescription();
077: this .parentFile = parentFile;
078: this .diff = diff;
079: this .modification = modification;
080: }
081:
082: public String getDisplayText() {
083: return displayText;
084: }
085:
086: public Lookup getLookup() {
087: Object composite = null;
088: if (bounds != null) {
089: composite = ElementGripFactory.getDefault().get(parentFile,
090: bounds.getBegin().getOffset());
091: }
092: if (composite == null)
093: composite = parentFile;
094: return Lookups.fixed(composite, diff);
095: }
096:
097: @Override
098: public void setEnabled(boolean enabled) {
099: diff.exclude(!enabled);
100: newFileContent = null;
101: super .setEnabled(enabled);
102: }
103:
104: public PositionBounds getPosition() {
105: return bounds;
106: }
107:
108: public String getText() {
109: return displayText;
110: }
111:
112: public void performChange() {
113: }
114:
115: public FileObject getParentFile() {
116: if (diff.getKind() == Difference.Kind.CREATE) {
117: return parentFile.getParent();
118: }
119: return parentFile;
120: }
121:
122: @Override
123: protected String getNewFileContent() {
124: String result;
125: if (newFileContent != null) {
126: result = newFileContent.get();
127: if (result != null)
128: return result;
129: }
130: try {
131: if (diff.getKind() == Difference.Kind.CREATE) {
132: result = diff.getNewText();
133: } else {
134: result = modification.getResultingSource(parentFile);
135: }
136: } catch (IOException ex) {
137: Exceptions.printStackTrace(ex);
138: return null;
139: }
140: newFileContent = new WeakReference<String>(result);
141: return result;
142: }
143:
144: /**
145: * Factory method for DiffElement
146: * @param diff diff instance corresponding to thid Element
147: * @param fileObject fileObject corresponding to this Element
148: * @param modification
149: * @return ModificationResult corresponding to this change
150: */
151: public static DiffElement create(Difference diff,
152: FileObject fileObject, ModificationResult modification) {
153: PositionRef start = diff.getStartPosition();
154: PositionRef end = diff.getEndPosition();
155: PositionBounds bounds = null;
156: if (diff.getKind() != Difference.Kind.CREATE)
157: bounds = new PositionBounds(start, end);
158: return new DiffElement(diff, bounds, fileObject, modification);
159: }
160: }
|