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.visualweb.insync.faces.refactoring;
043:
044: import java.io.IOException;
045: import java.lang.ref.WeakReference;
046:
047: import org.netbeans.api.java.source.ModificationResult;
048: import org.netbeans.api.java.source.ModificationResult.Difference;
049: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
050: import org.openide.filesystems.FileObject;
051: import org.openide.text.PositionBounds;
052: import org.openide.text.PositionRef;
053: import org.openide.util.Exceptions;
054: import org.openide.util.Lookup;
055: import org.openide.util.lookup.Lookups;
056:
057: // NOTE:Copied from org.netbeans.modules.refactoring.java.DiffElement.
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 = ElementGripFactory.getDefault().get(
088: parentFile, bounds.getBegin().getOffset());
089: if (composite == null)
090: composite = parentFile;
091: return Lookups.fixed(composite, diff);
092: }
093:
094: public void setEnabled(boolean enabled) {
095: diff.exclude(!enabled);
096: newFileContent = null;
097: super .setEnabled(enabled);
098: }
099:
100: public PositionBounds getPosition() {
101: return bounds;
102: }
103:
104: public String getText() {
105: return displayText;
106: }
107:
108: public void performChange() {
109: }
110:
111: public FileObject getParentFile() {
112: return parentFile;
113: }
114:
115: protected String getNewFileContent() {
116: String result;
117: if (newFileContent != null) {
118: result = newFileContent.get();
119: if (result != null)
120: return result;
121: }
122: try {
123: result = modification.getResultingSource(parentFile);
124: } catch (IOException ex) {
125: Exceptions.printStackTrace(ex);
126: return null;
127: }
128: newFileContent = new WeakReference(result);
129: return result;
130: }
131:
132: /**
133: * Factory method for DiffElement
134: * @param diff diff instance corresponding to thid Element
135: * @param fileObject fileObject corresponding to this Element
136: * @param modification
137: * @return ModificationResult corresponding to this change
138: */
139: public static DiffElement create(Difference diff,
140: FileObject fileObject, ModificationResult modification) {
141: PositionRef start = diff.getStartPosition();
142: PositionRef end = diff.getEndPosition();
143: PositionBounds bounds = new PositionBounds(start, end);
144: return new DiffElement(diff, bounds, fileObject, modification);
145: }
146: }
|