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;
}
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);
}
}
}
}
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();
}
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);
}
}
}
}
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);
}
}
Aggregations