use of org.eclipse.compare.ITypedElement in project egit by eclipse.
the class GitCompareFileRevisionEditorInput method addDirectoryFiles.
private DiffNode addDirectoryFiles(ITypedElement elem, int diffType) {
ITypedElement l = null;
ITypedElement r = null;
if (diffType == Differencer.DELETION) {
r = elem;
} else {
l = elem;
}
if (elem.getType().equals(ITypedElement.FOLDER_TYPE)) {
DiffNode diffNode = null;
diffNode = new DiffNode(null, Differencer.CHANGE, null, l, r);
ITypedElement[] children = (ITypedElement[]) ((IStructureComparator) elem).getChildren();
for (ITypedElement child : children) {
diffNode.add(addDirectoryFiles(child, diffType));
}
return diffNode;
} else {
return new DiffNode(diffType, null, l, r);
}
}
use of org.eclipse.compare.ITypedElement in project eclipse.jdt.ui by eclipse-jdt.
the class JavaAddElementFromHistoryImpl method run.
@Override
public void run(ISelection selection) {
String errorTitle = CompareMessages.AddFromHistory_title;
String errorMessage = CompareMessages.AddFromHistory_internalErrorMessage;
Shell shell = getShell();
ICompilationUnit cu = null;
IParent parent = null;
IMember input = null;
// analyze selection
if (selection.isEmpty()) {
// no selection: we try to use the editor's input
JavaEditor editor = getEditor();
if (editor != null) {
IEditorInput editorInput = editor.getEditorInput();
IWorkingCopyManager manager = JavaPlugin.getDefault().getWorkingCopyManager();
if (manager != null) {
cu = manager.getWorkingCopy(editorInput);
parent = cu;
}
}
} else {
input = getEditionElement(selection);
if (input != null) {
cu = input.getCompilationUnit();
parent = input;
input = null;
} else {
if (selection instanceof IStructuredSelection) {
Object o = ((IStructuredSelection) selection).getFirstElement();
if (o instanceof ICompilationUnit) {
cu = (ICompilationUnit) o;
parent = cu;
}
}
}
}
if (parent == null || cu == null) {
String invalidSelectionMessage = CompareMessages.AddFromHistory_invalidSelectionMessage;
MessageDialog.openInformation(shell, errorTitle, invalidSelectionMessage);
return;
}
IFile file = getFile(parent);
if (file == null) {
MessageDialog.openError(shell, errorTitle, errorMessage);
return;
}
boolean inEditor = beingEdited(file);
IStatus status = Resources.makeCommittable(file, shell);
if (!status.isOK()) {
return;
}
// get the document where to insert the text
IPath path = file.getFullPath();
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer textFileBuffer = null;
try {
bufferManager.connect(path, LocationKind.IFILE, null);
textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
IDocument document = textFileBuffer.getDocument();
// configure EditionSelectionDialog and let user select an edition
ITypedElement target = new JavaTextBufferNode(file, document, inEditor);
ITypedElement[] editions = buildEditions(target, file);
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME);
EditionSelectionDialog d = new EditionSelectionDialog(shell, bundle);
d.setAddMode(true);
d.setHelpContextId(IJavaHelpContextIds.ADD_ELEMENT_FROM_HISTORY_DIALOG);
ITypedElement selected = d.selectEdition(target, editions, parent);
if (selected == null)
// user cancel
return;
ICompilationUnit cu2 = cu;
if (parent instanceof IMember)
cu2 = ((IMember) parent).getCompilationUnit();
CompilationUnit root = parsePartialCompilationUnit(cu2);
ASTRewrite rewriter = ASTRewrite.create(root.getAST());
for (ITypedElement result : d.getSelection()) {
// create an AST node
ASTNode newNode = createASTNode(rewriter, result, TextUtilities.getDefaultLineDelimiter(document), cu.getJavaProject());
if (newNode == null) {
MessageDialog.openError(shell, errorTitle, errorMessage);
return;
}
// now determine where to put the new node
if (newNode instanceof PackageDeclaration) {
rewriter.set(root, CompilationUnit.PACKAGE_PROPERTY, newNode, null);
} else if (newNode instanceof ImportDeclaration) {
ListRewrite lw = rewriter.getListRewrite(root, CompilationUnit.IMPORTS_PROPERTY);
lw.insertFirst(newNode, null);
} else {
if (parent instanceof ICompilationUnit) {
// top level
ListRewrite lw = rewriter.getListRewrite(root, CompilationUnit.TYPES_PROPERTY);
int index = BodyDeclarationRewrite.getInsertionIndex((BodyDeclaration) newNode, root.types());
lw.insertAt(newNode, index, null);
} else if (parent instanceof IType) {
ASTNode declaration = getBodyContainer(root, (IType) parent);
if (declaration instanceof TypeDeclaration || declaration instanceof AnnotationTypeDeclaration) {
List<BodyDeclaration> container = ASTNodes.getBodyDeclarations(declaration);
int index = BodyDeclarationRewrite.getInsertionIndex((BodyDeclaration) newNode, container);
ListRewrite lw = rewriter.getListRewrite(declaration, ASTNodes.getBodyDeclarationsProperty(declaration));
lw.insertAt(newNode, index, null);
} else if (declaration instanceof EnumDeclaration) {
List<EnumConstantDeclaration> container = ((EnumDeclaration) declaration).enumConstants();
int index = BodyDeclarationRewrite.getInsertionIndex((FieldDeclaration) newNode, container);
ListRewrite lw = rewriter.getListRewrite(declaration, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
lw.insertAt(newNode, index, null);
}
} else {
// $NON-NLS-1$
JavaPlugin.logErrorMessage("JavaAddElementFromHistoryImpl: unknown container " + parent);
}
}
}
Map<String, String> options = null;
IJavaProject javaProject = cu2.getJavaProject();
if (javaProject != null)
options = javaProject.getOptions(true);
applyChanges(rewriter, document, textFileBuffer, shell, inEditor, options);
} catch (InvocationTargetException ex) {
ExceptionHandler.handle(ex, shell, errorTitle, errorMessage);
} catch (InterruptedException ex) {
// shouldn't be called because is not cancelable
Assert.isTrue(false);
} catch (CoreException ex) {
ExceptionHandler.handle(ex, shell, errorTitle, errorMessage);
} finally {
try {
if (textFileBuffer != null)
bufferManager.disconnect(path, LocationKind.IFILE, null);
} catch (CoreException e) {
JavaPlugin.log(e);
}
}
}
use of org.eclipse.compare.ITypedElement in project eclipse.jdt.ui by eclipse-jdt.
the class JavaHistoryActionImpl method buildEditions.
final ITypedElement[] buildEditions(ITypedElement target, IFile file) {
// setup array of editions
IFileState[] states = null;
// add available editions
try {
states = file.getHistory(null);
} catch (CoreException ex) {
JavaPlugin.log(ex);
}
int count = 1;
if (states != null)
count += states.length;
ITypedElement[] editions = new ITypedElement[count];
editions[0] = new ResourceNode(file);
if (states != null)
for (int i = 0; i < states.length; i++) editions[i + 1] = new HistoryItem(target, states[i]);
return editions;
}
use of org.eclipse.compare.ITypedElement in project eclipse.jdt.ui by eclipse-jdt.
the class JavaStructureDiffViewer method initialSelection.
/**
* Overridden to find and expand the first class.
*/
@Override
protected void initialSelection() {
Object firstClass = null;
Object o = getRoot();
if (o != null) {
Object[] children = getSortedChildren(o);
if (children != null) {
for (Object child : children) {
o = child;
Object[] sortedChildren = getSortedChildren(o);
if (sortedChildren != null) {
for (Object sortedChild : sortedChildren) {
o = sortedChild;
if (o instanceof DiffNode) {
DiffNode dn = (DiffNode) o;
ITypedElement e = dn.getId();
if (e instanceof JavaNode) {
JavaNode jn = (JavaNode) e;
int tc = jn.getTypeCode();
if (tc == JavaNode.CLASS || tc == JavaNode.INTERFACE) {
firstClass = dn;
}
}
}
}
}
}
}
}
if (firstClass != null)
expandToLevel(firstClass, 1);
else
expandToLevel(2);
}
use of org.eclipse.compare.ITypedElement in project subclipse by subclipse.
the class SVNLocalCompareInput method prepareCompareInput.
/**
* Runs the compare operation and returns the compare result.
*
* @throws InterruptedException
*/
protected ICompareInput prepareCompareInput(IProgressMonitor monitor) throws InterruptedException {
initLabels();
if (resource instanceof LocalFolder) {
try {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
// $NON-NLS-1$
monitor.beginTask(Policy.bind("SVNCompareEditorInput.comparing"), 30);
IProgressMonitor sub = new SubProgressMonitor(monitor, 30);
// $NON-NLS-1$
sub.beginTask(Policy.bind("SVNCompareEditorInput.comparing"), 100);
Object[] result = new Object[] { null };
ArrayList resourceSummaryNodeList = new ArrayList();
ArrayList summaryEditionNodeList = new ArrayList();
ISVNClientAdapter client = null;
if (resources == null) {
resources = new ISVNLocalResource[] { resource };
}
if (remoteFolders == null) {
remoteFolders = new ISVNRemoteFolder[] { (ISVNRemoteFolder) remoteResource };
}
try {
for (int i = 0; i < resources.length; i++) {
ISVNLocalResource resource = resources[i];
ISVNRemoteFolder remoteFolder = remoteFolders[i];
SVNDiffSummary[] diffSummary = null;
client = SVNProviderPlugin.getPlugin().getSVNClientManager().getSVNClient();
File file = new File(resource.getResource().getLocation().toString());
getUnadded(client, resource, file);
IResource[] unaddedResources = new IResource[unaddedList.size()];
unaddedList.toArray(unaddedResources);
SVNWorkspaceRoot workspaceRoot = new SVNWorkspaceRoot(resource.getResource().getProject());
AddResourcesCommand command = new AddResourcesCommand(workspaceRoot, unaddedResources, IResource.DEPTH_INFINITE);
command.run(monitor);
diffSummary = client.diffSummarize(file, remoteFolder.getUrl(), remoteFolder.getRevision(), true);
for (IResource unaddedResource : unaddedResources) {
try {
SVNWorkspaceRoot.getSVNResourceFor(unaddedResource).revert();
} catch (Exception e) {
}
}
SVNProviderPlugin.getPlugin().getSVNClientManager().returnSVNClient(client);
client = null;
if (diffSummary != null && diffSummary.length > 0) {
diffSummary = getDiffSummaryWithSubfolders(diffSummary);
ITypedElement left = new SVNLocalResourceSummaryNode(resource, diffSummary, resource.getResource().getLocation().toString());
SummaryEditionNode right = new SummaryEditionNode(remoteFolder);
right.setName(resource.getFile().getName());
right.setRootFolder((RemoteFolder) remoteFolder);
right.setNodeType(SummaryEditionNode.RIGHT);
right.setRoot(true);
right.setDiffSummary(diffSummary);
String localCharset = Utilities.getCharset(resource.getIResource());
try {
right.setCharset(localCharset);
} catch (CoreException e) {
SVNUIPlugin.log(IStatus.ERROR, e.getMessage(), e);
}
resourceSummaryNodeList.add(left);
summaryEditionNodeList.add(right);
}
}
if (resourceSummaryNodeList.size() == 0) {
result[0] = null;
} else {
Object[] resourceSummaryNodes = new Object[resourceSummaryNodeList.size()];
resourceSummaryNodeList.toArray(resourceSummaryNodes);
Object[] summaryEditionNodes = new Object[summaryEditionNodeList.size()];
summaryEditionNodeList.toArray(summaryEditionNodes);
MultipleSelectionNode left = new MultipleSelectionNode(resourceSummaryNodes);
MultipleSelectionNode right = new MultipleSelectionNode(summaryEditionNodes);
result[0] = new SummaryDifferencer().findDifferences(false, monitor, null, null, left, right);
fRoot = result[0];
}
} finally {
sub.done();
if (client != null) {
SVNProviderPlugin.getPlugin().getSVNClientManager().returnSVNClient(client);
}
}
if (result[0] instanceof DiffNode) {
DiffNode diffNode = (DiffNode) result[0];
if (!diffNode.hasChildren()) {
return null;
}
}
return (ICompareInput) result[0];
} catch (OperationCanceledException e) {
throw new InterruptedException(e.getMessage());
} catch (Exception e) {
SVNUIPlugin.log(IStatus.ERROR, e.getMessage(), e);
return null;
} finally {
monitor.done();
}
} else {
ITypedElement left = new SVNLocalResourceNode(resource);
ResourceEditionNode right = new ResourceEditionNode(remoteResource, pegRevision);
if (left.getType() == ITypedElement.FOLDER_TYPE) {
right.setLocalResource((SVNLocalResourceNode) left);
}
if (right.getType() == ITypedElement.FOLDER_TYPE) {
((SVNLocalResourceNode) left).setRemoteResource((ResourceEditionNode) right);
}
String localCharset = Utilities.getCharset(resource.getIResource());
try {
right.setCharset(localCharset);
} catch (CoreException e) {
SVNUIPlugin.log(IStatus.ERROR, e.getMessage(), e);
}
ICompareInput compareInput;
if (SVNRevision.BASE.equals(remoteRevision)) {
compareInput = (ICompareInput) new StatusAwareDifferencer().findDifferences(false, monitor, null, null, left, right);
} else {
compareInput = (ICompareInput) new RevisionAwareDifferencer((SVNLocalResourceNode) left, right, diffFile, pegRevision).findDifferences(false, monitor, null, null, left, right);
}
return compareInput;
}
}
Aggregations