Search in sources :

Example 11 with ITypedElement

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);
    }
}
Also used : DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ITypedElement(org.eclipse.compare.ITypedElement)

Example 12 with ITypedElement

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);
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IMember(org.eclipse.jdt.core.IMember) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) IType(org.eclipse.jdt.core.IType) Shell(org.eclipse.swt.widgets.Shell) EditionSelectionDialog(org.eclipse.compare.EditionSelectionDialog) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPath(org.eclipse.core.runtime.IPath) IParent(org.eclipse.jdt.core.IParent) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITypedElement(org.eclipse.compare.ITypedElement) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaEditor(org.eclipse.jdt.internal.ui.javaeditor.JavaEditor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ResourceBundle(java.util.ResourceBundle) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) IWorkingCopyManager(org.eclipse.jdt.ui.IWorkingCopyManager) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 13 with ITypedElement

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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) HistoryItem(org.eclipse.compare.HistoryItem) IFileState(org.eclipse.core.resources.IFileState) ITypedElement(org.eclipse.compare.ITypedElement) ResourceNode(org.eclipse.compare.ResourceNode)

Example 14 with ITypedElement

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);
}
Also used : DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ITypedElement(org.eclipse.compare.ITypedElement)

Example 15 with ITypedElement

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;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SVNWorkspaceRoot(org.tigris.subversion.subclipse.core.resources.SVNWorkspaceRoot) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ISVNLocalResource(org.tigris.subversion.subclipse.core.ISVNLocalResource) ISVNRemoteFolder(org.tigris.subversion.subclipse.core.ISVNRemoteFolder) AddResourcesCommand(org.tigris.subversion.subclipse.core.commands.AddResourcesCommand) ISVNClientAdapter(org.tigris.subversion.svnclientadapter.ISVNClientAdapter) ITypedElement(org.eclipse.compare.ITypedElement) ICompareInput(org.eclipse.compare.structuremergeviewer.ICompareInput) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) SVNException(org.tigris.subversion.subclipse.core.SVNException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SVNClientException(org.tigris.subversion.svnclientadapter.SVNClientException) IOException(java.io.IOException) LocalFolder(org.tigris.subversion.subclipse.core.resources.LocalFolder) SVNDiffSummary(org.tigris.subversion.svnclientadapter.SVNDiffSummary) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Aggregations

ITypedElement (org.eclipse.compare.ITypedElement)49 IFile (org.eclipse.core.resources.IFile)18 DiffNode (org.eclipse.compare.structuremergeviewer.DiffNode)15 CoreException (org.eclipse.core.runtime.CoreException)12 IOException (java.io.IOException)11 GitCompareFileRevisionEditorInput (org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput)10 File (java.io.File)6 CompareEditorInput (org.eclipse.compare.CompareEditorInput)6 ICompareInput (org.eclipse.compare.structuremergeviewer.ICompareInput)6 IDiffElement (org.eclipse.compare.structuremergeviewer.IDiffElement)6 IResource (org.eclipse.core.resources.IResource)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IFileRevision (org.eclipse.team.core.history.IFileRevision)6 IPath (org.eclipse.core.runtime.IPath)5 FileRevisionTypedElement (org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)5 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)5 ArrayList (java.util.ArrayList)4 CompareConfiguration (org.eclipse.compare.CompareConfiguration)4 IResourceProvider (org.eclipse.compare.IResourceProvider)4