Search in sources :

Example 26 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project jbosstools-hibernate by jbosstools.

the class NewHibernateMappingPreviewPage method updateOneChange.

/**
 * Try to create one change according with input file (fileSrc).
 * In case of success change be added into cc and returns true.
 * @param cc
 * @param proj
 * @param fileSrc
 * @return
 */
protected boolean updateOneChange(final CompositeChange cc, final IJavaProject proj, File fileSrc) {
    boolean res = false;
    if (!fileSrc.exists()) {
        return res;
    }
    if (fileSrc.isDirectory()) {
        return res;
    }
    final IPath place2Gen = getRootPlace2Gen().append(proj.getElementName());
    final IPath filePathFrom = new Path(fileSrc.getPath());
    final IPath filePathTo_Proj = filePathFrom.makeRelativeTo(place2Gen);
    final IPath filePathTo_Show = proj.getPath().append(filePathTo_Proj);
    final IResource res2Update = proj.getProject().findMember(filePathTo_Proj);
    if (res2Update != null) {
        final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
        ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(filePathTo_Show, LocationKind.IFILE);
        if (textFileBuffer == null) {
            try {
                bufferManager.connect(filePathTo_Show, LocationKind.IFILE, null);
                paths2Disconnect.add(filePathTo_Show);
            } catch (CoreException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
            }
            textFileBuffer = bufferManager.getTextFileBuffer(filePathTo_Show, LocationKind.IFILE);
        }
        if (textFileBuffer != null) {
            IDocument documentChange = textFileBuffer.getDocument();
            // 
            String str = readInto(fileSrc);
            TextEdit textEdit = new ReplaceEdit(0, documentChange.getLength(), str.toString());
            // 
            TextFileChange change = new TextFileChange(filePathTo_Show.toString(), (IFile) res2Update);
            change.setSaveMode(TextFileChange.LEAVE_DIRTY);
            change.setEdit(textEdit);
            cc.add(change);
            // 
            res = true;
        }
    } else {
        String str = readInto(fileSrc);
        // $NON-NLS-1$
        CreateTextFileChange change = new CreateTextFileChange(filePathTo_Show, str.toString(), null, "hbm.xml");
        cc.add(change);
        // 
        res = true;
    }
    return res;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) CreateTextFileChange(org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CreateTextFileChange(org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Example 27 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project jbosstools-hibernate by jbosstools.

the class NewHibernateMappingPreviewPage method performCommit.

protected void performCommit() {
    final CompositeChange cc = (CompositeChange) getChange();
    if (cc == null) {
        return;
    }
    final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    Change[] changes = cc.getChildren();
    for (int i = 0; i < changes.length; i++) {
        Change change = changes[i];
        if (!(change instanceof TextFileChange)) {
            continue;
        }
        TextFileChange tfc = (TextFileChange) change;
        if (tfc.isEnabled() && tfc.getEdit() != null) {
            IPath path = new Path(tfc.getName());
            ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
            IDocument document = textFileBuffer.getDocument();
            try {
                tfc.getEdit().apply(document);
            } catch (MalformedTreeException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("MalformedTreeException: ", e);
            } catch (BadLocationException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("BadLocationException: ", e);
            }
            try {
                // commit changes to underlying file
                textFileBuffer.commit(null, true);
            } catch (CoreException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
            }
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CreateTextFileChange(org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CreateTextFileChange(org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 28 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project jbosstools-hibernate by jbosstools.

the class AllEntitiesProcessor method performDisconnect.

public void performDisconnect() {
    final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    for (int i = 0; i < changes.size(); i++) {
        ChangeStructure cs = changes.get(i);
        try {
            bufferManager.disconnect(cs.path, LocationKind.IFILE, null);
        } catch (CoreException e) {
            // $NON-NLS-1$
            HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
        }
    }
    changes.clear();
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager)

Example 29 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project jbosstools-hibernate by jbosstools.

the class AllEntitiesProcessor method performCommit.

protected void performCommit(final Map<String, EntityInfo> entities) {
    final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    for (int i = 0; i < changes.size(); i++) {
        ChangeStructure cs = changes.get(i);
        if (cs.textEdit != null && ((cs.change != null && cs.change.isEnabled()) || (cs.change == null))) {
            ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(cs.path, LocationKind.IFILE);
            IDocument document = textFileBuffer.getDocument();
            try {
                cs.textEdit.apply(document);
            } catch (MalformedTreeException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("MalformedTreeException: ", e);
            } catch (BadLocationException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("BadLocationException: ", e);
            }
            try {
                // commit changes to underlying file
                textFileBuffer.commit(null, true);
            } catch (CoreException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
            }
        }
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 30 with ITextFileBufferManager

use of org.eclipse.core.filebuffers.ITextFileBufferManager in project jbosstools-hibernate by jbosstools.

the class AllEntitiesProcessor method reCollectModification.

public void reCollectModification(Map<String, EntityInfo> entities) {
    changes.clear();
    final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
    HashMap<IPath, EntityInfosCollection> modifications = new HashMap<IPath, EntityInfosCollection>();
    Iterator<Map.Entry<String, EntityInfo>> it = entities.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, EntityInfo> entry = it.next();
        if (entry.getValue().isInterfaceFlag()) {
            continue;
        }
        final String javaProjectName = entry.getValue().getJavaProjectName();
        final String fullyQualifiedName = entry.getValue().getFullyQualifiedName();
        IJavaProject javaProject = Utils.findJavaProject(javaProjectName);
        ICompilationUnit icu = Utils.findCompilationUnit(javaProject, fullyQualifiedName);
        if (icu == null) {
            continue;
        }
        org.eclipse.jdt.core.dom.CompilationUnit cu = Utils.getCompilationUnit(icu, true);
        final IPath path = cu.getJavaElement().getPath();
        EntityInfosCollection eiCollection = null;
        if (modifications.containsKey(path)) {
            eiCollection = modifications.get(path);
        } else {
            eiCollection = new EntityInfosCollection();
            eiCollection.setPath(path);
            eiCollection.setICompilationUnit(icu);
            eiCollection.setCompilationUnit(cu);
            modifications.put(path, eiCollection);
            try {
                bufferManager.connect(path, LocationKind.IFILE, null);
            } catch (CoreException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
            }
        }
        final EntityInfo entityInfo = entry.getValue();
        // 
        entityInfo.updateColumnAnnotationImport(defaultStrLength != columnLength);
        entityInfo.updateVersionImport(enableOptLock && entityInfo.isAddVersionFlag());
        // 
        eiCollection.addEntityInfo(entityInfo);
    }
    Iterator<EntityInfosCollection> itEIC = modifications.values().iterator();
    while (itEIC.hasNext()) {
        EntityInfosCollection eic = itEIC.next();
        eic.updateExistingImportSet();
        eic.updateRequiredImportSet();
        collectModification(bufferManager, eic, entities);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) HashMap(java.util.HashMap) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) EntityInfosCollection(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfosCollection) EntityInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)86 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)65 IDocument (org.eclipse.jface.text.IDocument)53 IPath (org.eclipse.core.runtime.IPath)51 CoreException (org.eclipse.core.runtime.CoreException)36 BadLocationException (org.eclipse.jface.text.BadLocationException)26 IFile (org.eclipse.core.resources.IFile)22 Test (org.junit.Test)17 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 IOException (java.io.IOException)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IResource (org.eclipse.core.resources.IResource)8 Path (org.eclipse.core.runtime.Path)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 TextEdit (org.eclipse.text.edits.TextEdit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5