blob: 68325f521508ba72b60d275a13dd9a5790236035 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.refactoring.php;
import java.io.IOException;
import java.lang.ref.WeakReference;
import javax.swing.text.StyledDocument;
import org.netbeans.modules.csl.spi.support.ModificationResult;
import org.netbeans.modules.csl.spi.support.ModificationResult.Difference;
import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
import org.openide.filesystems.FileObject;
import org.openide.text.PositionBounds;
import org.openide.text.PositionRef;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
/**
*
* @todo Copied from javascript module. Should be a part of either CSL or better
* the refactoring API.
*
* @author Jan Becicka
*/
public class DiffElement extends SimpleRefactoringElementImplementation {
private PositionBounds bounds;
private String displayText;
private FileObject parentFile;
private Difference diff;
private ModificationResult modification;
private WeakReference<String> newFileContent;
public DiffElement(Difference diff, PositionBounds bounds, FileObject parentFile, ModificationResult modification) {
this.bounds = bounds;
this.displayText = diff.getDescription();
this.parentFile = parentFile;
this.diff = diff;
this.modification = modification;
}
@Override
public String getDisplayText() {
return displayText;
}
@Override
public Lookup getLookup() {
return Lookups.fixed(diff);
}
@Override
public FileObject getParentFile() {
return getFile();
}
@Override
public void setEnabled(boolean enabled) {
diff.exclude(!enabled);
newFileContent = null;
super.setEnabled(enabled);
}
@Override
public PositionBounds getPosition() {
return bounds;
}
@Override
public String getText() {
return displayText;
}
@Override
public void performChange() {
}
public FileObject getFile() {
return parentFile;
}
@Override
protected String getNewFileContent() {
String result;
if (newFileContent != null) {
result = newFileContent.get();
if (result != null) {
return result;
}
}
try {
result = modification.getResultingSource(parentFile);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
return null;
}
newFileContent = new WeakReference<>(result);
return result;
}
public static DiffElement create(Difference diff, FileObject fileObject, ModificationResult modification) {
PositionRef start = diff.getStartPosition();
PositionRef end = diff.getEndPosition();
StyledDocument doc = null;
PositionBounds bounds = new PositionBounds(start, end);
return new DiffElement(diff, bounds, fileObject, modification);
}
}