Author: vyemialyanchyk
Date: 2009-05-26 12:10:23 -0400 (Tue, 26 May 2009)
New Revision: 15515
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/OpenMappingUtils.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/ProjectUtils.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-4364 - "Open Mapping File" should work
when there is no project associated with a console configuration
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java 2009-05-26
14:29:18 UTC (rev 15514)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java 2009-05-26
16:10:23 UTC (rev 15515)
@@ -62,9 +62,9 @@
TreePath[] paths = ((TreeSelection)sel).getPaths();
for (int i = 0; i < paths.length; i++) {
TreePath path = paths[i];
- ConsoleConfiguration consoleConfiguration =
(ConsoleConfiguration)(path.getSegment(0));
+ ConsoleConfiguration consoleConfig = (ConsoleConfiguration)(path.getSegment(0));
try {
- run(path, consoleConfiguration);
+ run(path, consoleConfig);
} catch (JavaModelException e) {
HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenMappingAction_cannot_find_mapping_file,
e);
} catch (PartInitException e) {
@@ -77,13 +77,13 @@
/**
* @param path
- * @param consoleConfiguration
+ * @param consoleConfig
* @return
* @throws PartInitException
* @throws JavaModelException
* @throws FileNotFoundException
*/
- public static IEditorPart run(TreePath path, ConsoleConfiguration consoleConfiguration)
throws PartInitException, JavaModelException, FileNotFoundException {
+ public static IEditorPart run(TreePath path, ConsoleConfiguration consoleConfig) throws
PartInitException, JavaModelException, FileNotFoundException {
boolean isPropertySel = (path.getLastSegment().getClass() == Property.class);
if (isPropertySel) {
Property propertySel = (Property)path.getLastSegment();
@@ -92,39 +92,38 @@
|| (RootClass.class.isAssignableFrom(persClass.getClass())
&& persClass.getClass() != RootClass.class)) {
Property parentProp = (Property)path.getParentPath().getLastSegment();
- return run(propertySel, parentProp, consoleConfiguration);
+ return run(propertySel, parentProp, consoleConfig);
}
}
- return run(path.getLastSegment(), consoleConfiguration);
+ return run(path.getLastSegment(), consoleConfig);
}
/**
* @param selection
- * @param consoleConfiguration
+ * @param consoleConfig
* @throws JavaModelException
* @throws PartInitException
* @throws PresistanceClassNotFoundException
* @throws FileNotFoundException
*/
- public static IEditorPart run(Object selection, ConsoleConfiguration
consoleConfiguration) throws PartInitException, JavaModelException, FileNotFoundException
{
+ public static IEditorPart run(Object selection, ConsoleConfiguration consoleConfig)
throws PartInitException, JavaModelException, FileNotFoundException {
IEditorPart editorPart = null;
- IJavaProject proj = ProjectUtils.findJavaProject(consoleConfiguration);
IFile file = null;
if (selection instanceof Property) {
Property p = (Property)selection;
if (p.getPersistentClass() != null) {
//use PersistentClass to open editor
- file = OpenMappingUtils.searchFileToOpen(consoleConfiguration, proj,
p.getPersistentClass());
- //editorPart = openMapping(p.getPersistentClass(), consoleConfiguration);
+ file = OpenMappingUtils.searchFileToOpen(consoleConfig, p.getPersistentClass());
+ //editorPart = openMapping(p.getPersistentClass(), consoleConfig);
}
}
else {
- file = OpenMappingUtils.searchFileToOpen(consoleConfiguration, proj, selection);
- //editorPart = openMapping(selection, consoleConfiguration);
+ file = OpenMappingUtils.searchFileToOpen(consoleConfig, selection);
+ //editorPart = openMapping(selection, consoleConfig);
}
if (file != null) {
editorPart = OpenMappingUtils.openFileInEditor(file);
- updateEditorSelection(proj, editorPart, selection);
+ updateEditorSelection(editorPart, selection);
}
if (editorPart == null) {
//try to find hibernate-annotations
@@ -139,8 +138,10 @@
}
}
if (rootClass != null){
- if (OpenMappingUtils.hasConfigXMLMappingClassAnnotation(consoleConfiguration,
rootClass)) {
+ if (OpenMappingUtils.hasConfigXMLMappingClassAnnotation(consoleConfig, rootClass)) {
String fullyQualifiedName = rootClass.getClassName();
+ // TODO: get rid of this - JBIDE-4363
+ IJavaProject proj = ProjectUtils.findJavaProject(consoleConfig);
editorPart = OpenSourceAction.run(selection, proj, fullyQualifiedName);
}
}
@@ -155,24 +156,25 @@
/**
* @param compositeProperty
* @param parentProperty
- * @param consoleConfiguration
+ * @param consoleConfig
* @throws JavaModelException
* @throws PartInitException
* @throws FileNotFoundException
* @throws BadLocationException
*/
- public static IEditorPart run(Property compositeProperty, Property parentProperty,
ConsoleConfiguration consoleConfiguration) throws PartInitException, JavaModelException,
FileNotFoundException{
+ public static IEditorPart run(Property compositeProperty, Property parentProperty,
ConsoleConfiguration consoleConfig) throws PartInitException, JavaModelException,
FileNotFoundException{
PersistentClass rootClass = parentProperty.getPersistentClass();
- IJavaProject proj = ProjectUtils.findJavaProject(consoleConfiguration);
- IFile file = OpenMappingUtils.searchFileToOpen(consoleConfiguration, proj, rootClass);
+ IFile file = OpenMappingUtils.searchFileToOpen(consoleConfig, rootClass);
IEditorPart editorPart = null;
if (file != null){
editorPart = OpenMappingUtils.openFileInEditor(file);
- updateEditorSelection(proj, editorPart, compositeProperty, parentProperty);
+ updateEditorSelection(editorPart, compositeProperty, parentProperty);
}
if (editorPart == null && parentProperty.isComposite()) {
- if (OpenMappingUtils.hasConfigXMLMappingClassAnnotation(consoleConfiguration,
rootClass)) {
+ if (OpenMappingUtils.hasConfigXMLMappingClassAnnotation(consoleConfig, rootClass)) {
String fullyQualifiedName
=((Component)parentProperty.getValue()).getComponentClassName();
+ // TODO: get rid of this - JBIDE-4363
+ IJavaProject proj = ProjectUtils.findJavaProject(consoleConfig);
editorPart = OpenSourceAction.run(compositeProperty, proj, fullyQualifiedName);
}
}
@@ -187,7 +189,7 @@
* @param editorPart
* @param selection
*/
- public static boolean updateEditorSelection(IJavaProject proj, IEditorPart editorPart,
Object selection) {
+ public static boolean updateEditorSelection(IEditorPart editorPart, Object selection) {
ITextEditor[] textEditors = OpenMappingUtils.getTextEditors(editorPart);
if (textEditors.length == 0) {
return false;
@@ -202,6 +204,7 @@
if (findAdapter == null) {
return false;
}
+ IJavaProject proj = ProjectUtils.findJavaProject(editorPart);
IRegion selectRegion = OpenMappingUtils.findSelectRegion(proj, findAdapter,
selection);
if (selectRegion != null) {
textEditor.selectAndReveal(selectRegion.getOffset(), selectRegion.getLength());
@@ -215,7 +218,7 @@
* @param compositeProperty
* @param parentProperty
*/
- public static boolean updateEditorSelection(IJavaProject proj, IEditorPart editorPart,
Property compositeProperty, Property parentProperty) {
+ public static boolean updateEditorSelection(IEditorPart editorPart, Property
compositeProperty, Property parentProperty) {
ITextEditor[] textEditors = OpenMappingUtils.getTextEditors(editorPart);
if (textEditors.length == 0) {
return false;
@@ -230,6 +233,7 @@
if (findAdapter == null) {
return false;
}
+ IJavaProject proj = ProjectUtils.findJavaProject(editorPart);
IRegion parentRegion = OpenMappingUtils.findSelectRegion(proj, findAdapter,
parentProperty);
if (parentRegion == null) {
return false;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/OpenMappingUtils.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/OpenMappingUtils.java 2009-05-26
14:29:18 UTC (rev 15514)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/OpenMappingUtils.java 2009-05-26
16:10:23 UTC (rev 15515)
@@ -29,19 +29,10 @@
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
-import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
-import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.core.dom.AST;
-import org.eclipse.jdt.core.dom.ASTParser;
-import org.eclipse.jdt.core.dom.ITypeBinding;
-import org.eclipse.jdt.core.dom.Type;
-import org.eclipse.jdt.core.dom.TypeDeclaration;
-import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
-import org.eclipse.jdt.internal.core.SourceType;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
@@ -83,6 +74,7 @@
* @author Dmitry Geraskov
* @author Vitali Yemialyanchyk
*/
+@SuppressWarnings("restriction")
public class OpenMappingUtils {
public static final String HIBERNATE_TAG_CLASS = "class";
//$NON-NLS-1$
@@ -137,36 +129,36 @@
}
/**
- * Check has consoleConfiguration config.xml file a mapping class for provided
rootClass.
- * @param consoleConfiguration
+ * Check has consoleConfig config.xml file a mapping class for provided rootClass.
+ * @param consoleConfig
* @param rootClass
* @return
*/
- public static boolean hasConfigXMLMappingClassAnnotation(ConsoleConfiguration
consoleConfiguration, PersistentClass rootClass) {
- java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
+ public static boolean hasConfigXMLMappingClassAnnotation(ConsoleConfiguration
consoleConfig, PersistentClass rootClass) {
+ java.io.File configXMLFile = consoleConfig.getPreferences().getConfigXMLFile();
if (configXMLFile == null) {
return true;
}
- EntityResolver entityResolver =
consoleConfiguration.getConfiguration().getEntityResolver();
+ EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
Document doc = getDocument(configXMLFile, entityResolver);
return getElements(doc, HIBERNATE_TAG_MAPPING, HIBERNATE_TAG_CLASS,
getPersistentClassName(rootClass)).hasNext();
}
/**
* Check has this particular element correspondence in the file.
- * @param consoleConfiguration
+ * @param consoleConfig
* @param file
* @param element
* @return
*/
- public static boolean elementInFile(ConsoleConfiguration consoleConfiguration, IFile
file, Object element) {
+ public static boolean elementInFile(ConsoleConfiguration consoleConfig, IFile file,
Object element) {
boolean res = false;
if (element instanceof RootClass) {
- res = rootClassInFile(consoleConfiguration, file, (RootClass)element);
+ res = rootClassInFile(consoleConfig, file, (RootClass)element);
} else if (element instanceof Subclass) {
- res = subclassInFile(consoleConfiguration, file, (Subclass)element);
+ res = subclassInFile(consoleConfig, file, (Subclass)element);
} else if (element instanceof Table) {
- res = tableInFile(consoleConfiguration, file, (Table)element);
+ res = tableInFile(consoleConfig, file, (Table)element);
}
return res;
}
@@ -180,13 +172,13 @@
/**
* Check has this particular rootClass correspondence in the file.
- * @param consoleConfiguration
+ * @param consoleConfig
* @param file
* @param rootClass
* @return
*/
- public static boolean rootClassInFile(ConsoleConfiguration consoleConfiguration, IFile
file, RootClass rootClass) {
- EntityResolver entityResolver =
consoleConfiguration.getConfiguration().getEntityResolver();
+ public static boolean rootClassInFile(ConsoleConfiguration consoleConfig, IFile file,
RootClass rootClass) {
+ EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
Document doc = getDocument(file.getLocation().toFile(), entityResolver);
final String clName = getPersistentClassName(rootClass);
final String clNameUnq = StringHelper.unqualify(clName);
@@ -214,13 +206,13 @@
/**
* Check has this particular subclass correspondence in the file.
- * @param consoleConfiguration
+ * @param consoleConfig
* @param file
* @param subclass
* @return
*/
- public static boolean subclassInFile(ConsoleConfiguration consoleConfiguration, IFile
file, Subclass subclass) {
- EntityResolver entityResolver =
consoleConfiguration.getConfiguration().getEntityResolver();
+ public static boolean subclassInFile(ConsoleConfiguration consoleConfig, IFile file,
Subclass subclass) {
+ EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
Document doc = getDocument(file.getLocation().toFile(), entityResolver);
final String clName = getPersistentClassName(subclass);
final String clNameUnq = StringHelper.unqualify(clName);
@@ -237,13 +229,13 @@
/**
* Check has this particular table correspondence in the file.
- * @param consoleConfiguration
+ * @param consoleConfig
* @param file
* @param table
* @return
*/
- public static boolean tableInFile(ConsoleConfiguration consoleConfiguration, IFile file,
Table table) {
- EntityResolver entityResolver =
consoleConfiguration.getConfiguration().getEntityResolver();
+ public static boolean tableInFile(ConsoleConfiguration consoleConfig, IFile file, Table
table) {
+ EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
Document doc = getDocument(file.getLocation().toFile(), entityResolver);
Iterator<Element> classes = getElements(doc, HIBERNATE_TAG_CLASS);
boolean res = false;
@@ -272,7 +264,7 @@
classNameAttr = element.attribute(HIBERNATE_TAG_ENTITY_NAME);
}
if (classNameAttr != null) {
- String physicalTableName =
consoleConfiguration.getConfiguration().getNamingStrategy().classToTableName(classNameAttr.getValue());
+ String physicalTableName =
consoleConfig.getConfiguration().getNamingStrategy().classToTableName(classNameAttr.getValue());
if (table.getName().equals(physicalTableName)) {
res = true;
break;
@@ -386,57 +378,74 @@
}
return doc;
}
+
+ public static PackageFragmentRoot[] getCCPackageFragmentRoots(ConsoleConfiguration
consoleConfiguration) {
+ IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfiguration);
+ ArrayList<PackageFragmentRoot> res = new ArrayList<PackageFragmentRoot>();
+ try {
+ for (int i = 0; i < projs.length; i++) {
+ IPackageFragmentRoot[] pfrs = projs[i].getAllPackageFragmentRoots();
+ for (int j = 0; j < pfrs.length; j++) {
+ if (pfrs[j].getClass() != PackageFragmentRoot.class) {
+ continue;
+ }
+ res.add((PackageFragmentRoot)pfrs[j]);
+ }
+ }
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenFileActionUtils_problems_while_get_project_package_fragment_roots,
e);
+ }
+ return res.toArray(new PackageFragmentRoot[0]);
+ }
/**
* Trying to find hibernate console config mapping file,
* which is corresponding to provided element.
*
- * @param consoleConfiguration
- * @param proj
+ * @param consoleConfig
* @param element
* @return
*/
@SuppressWarnings("unchecked")
- public static IFile searchInMappingFiles(ConsoleConfiguration consoleConfiguration,
IJavaProject proj, Object element) {
+ public static IFile searchInMappingFiles(ConsoleConfiguration consoleConfig, Object
element) {
IFile file = null;
- if (consoleConfiguration == null) {
+ if (consoleConfig == null) {
return file;
}
- java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
- EntityResolver entityResolver =
consoleConfiguration.getConfiguration().getEntityResolver();
+ java.io.File configXMLFile = consoleConfig.getPreferences().getConfigXMLFile();
+ EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
Document doc = getDocument(configXMLFile, entityResolver);
if (doc == null) {
return file;
}
- IPackageFragmentRoot[] packageFragmentRoots = new IPackageFragmentRoot[0];
- try {
- if (proj != null) {
- packageFragmentRoots = proj.getAllPackageFragmentRoots();
- }
- } catch (JavaModelException e) {
- HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenFileActionUtils_problems_while_get_project_package_fragment_roots,
e);
+ //
+ PackageFragmentRoot[] packageFragments = getCCPackageFragmentRoots(consoleConfig);
+ //
+ ArrayList<IPath> paths = new ArrayList<IPath>();
+ for (int i = 0; i < packageFragments.length; i++) {
+ paths.add(packageFragments[i].getPath());
}
- Element sfNode = doc.getRootElement().element(HIBERNATE_TAG_SESSION_FACTORY);
- Iterator<Element> elements = sfNode.elements(HIBERNATE_TAG_MAPPING).iterator();
- while (elements.hasNext() && file == null) {
- Element subelement = elements.next();
- Attribute resourceAttr = subelement.attribute(HIBERNATE_TAG_RESOURCE);
- if (resourceAttr == null) {
- continue;
- }
- for (int i = 0; i < packageFragmentRoots.length; i++) {
- //search in source folders.
- if (packageFragmentRoots[i].getClass() != PackageFragmentRoot.class) {
+ // last chance to find file is the same place as configXMLFile
+ paths.add(Path.fromOSString(configXMLFile.getParent()));
+ //
+ for (int i = 0; i < paths.size() && file == null; i++) {
+ Element sfNode = doc.getRootElement().element(HIBERNATE_TAG_SESSION_FACTORY);
+ Iterator<Element> elements = sfNode.elements(HIBERNATE_TAG_MAPPING).iterator();
+ while (elements.hasNext() && file == null) {
+ Element subelement = elements.next();
+ Attribute resourceAttr = subelement.attribute(HIBERNATE_TAG_RESOURCE);
+ if (resourceAttr == null) {
continue;
}
- IPackageFragmentRoot packageFragmentRoot = packageFragmentRoots[i];
- IPath path = packageFragmentRoot.getPath().append(resourceAttr.getValue());
+ IPath path = paths.get(i).append(resourceAttr.getValue());
file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
- if (file == null) {
- continue;
+ if (file == null || !file.exists()) {
+ file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
}
- if (file.exists() && elementInFile(consoleConfiguration, file, element)) {
- break;
+ if (file != null && file.exists()) {
+ if (elementInFile(consoleConfig, file, element)) {
+ break;
+ }
}
file = null;
}
@@ -448,16 +457,16 @@
* Trying to find console configuration additional mapping file,
* which is corresponding to provided element.
*
- * @param consoleConfiguration
+ * @param consoleConfig
* @param element
* @return
*/
- public static IFile searchInAdditionalMappingFiles(ConsoleConfiguration
consoleConfiguration, Object element) {
+ public static IFile searchInAdditionalMappingFiles(ConsoleConfiguration consoleConfig,
Object element) {
IFile file = null;
- if (consoleConfiguration == null) {
+ if (consoleConfig == null) {
return file;
}
- java.io.File[] files = consoleConfiguration.getPreferences().getMappingFiles();
+ java.io.File[] files = consoleConfig.getPreferences().getMappingFiles();
for (int i = 0; i < files.length; i++) {
java.io.File fileTmp = files[i];
if (fileTmp == null) {
@@ -467,7 +476,7 @@
if (file == null) {
continue;
}
- if (file.exists() && elementInFile(consoleConfiguration, file, element)) {
+ if (file.exists() && elementInFile(consoleConfig, file, element)) {
break;
}
file = null;
@@ -479,58 +488,68 @@
* Trying to find hibernate console config ejb3 mapping file,
* which is corresponding to provided element.
*
- * @param consoleConfiguration
- * @param proj
+ * @param consoleConfig
* @param element
* @return
*/
- public static IFile searchInEjb3MappingFiles(ConsoleConfiguration consoleConfiguration,
IJavaProject proj, Object element) {
+ @SuppressWarnings("unchecked")
+ public static IFile searchInEjb3MappingFiles(ConsoleConfiguration consoleConfig, Object
element) {
IFile file = null;
- if (consoleConfiguration == null || proj == null) {
+ if (consoleConfig == null) {
return file;
}
- final ConsoleConfiguration cc2 = consoleConfiguration;
- List<String> documentPaths =
(List<String>)consoleConfiguration.getExecutionContext().execute(new
ExecutionContext.Command() {
+ final ConsoleConfiguration cc2 = consoleConfig;
+ List<String> documentPaths =
(List<String>)consoleConfig.getExecutionContext().execute(new
ExecutionContext.Command() {
public Object execute() {
return OpenMappingUtilsEjb3.enumDocuments(cc2);
}
});
- IPath projPath = proj.getPath();
- IPath projFullPath = proj.getResource().getLocation();
- IPath outPath = Path.EMPTY;
- IPackageFragmentRoot[] packageFragmentRoots = new IPackageFragmentRoot[0];
+ IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfig);
+ ArrayList<IPath> pathsSrc = new ArrayList<IPath>();
+ ArrayList<IPath> pathsOut = new ArrayList<IPath>();
+ ArrayList<IPath> pathsFull = new ArrayList<IPath>();
try {
- outPath = proj.getOutputLocation();
- packageFragmentRoots = proj.getPackageFragmentRoots();
+ for (int i = 0; i < projs.length; i++) {
+ IJavaProject proj = projs[i];
+ IPath projPath = proj.getPath();
+ IPath projPathFull = proj.getResource().getLocation();
+ IPath projPathOut = proj.getOutputLocation();
+ projPathOut = projPathOut.makeRelativeTo(projPath);
+ IPackageFragmentRoot[] pfrs = proj.getAllPackageFragmentRoots();
+ for (int j = 0; j < pfrs.length; j++) {
+ // TODO: think about possibility to open resources from jar files
+ if (pfrs[j].getClass() != PackageFragmentRoot.class) {
+ continue;
+ }
+ pathsSrc.add(((PackageFragmentRoot)pfrs[j]).getPath());
+ pathsOut.add(projPathOut);
+ pathsFull.add(projPathFull);
+ }
+ }
} catch (JavaModelException e) {
HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenFileActionUtils_problems_while_get_project_package_fragment_roots,
e);
}
- outPath = outPath.makeRelativeTo(projPath);
- for (int i = 0; i < packageFragmentRoots.length && file == null; i++) {
- if (!(packageFragmentRoots[i] instanceof PackageFragmentRoot)) {
- continue;
- }
- if (packageFragmentRoots[i] instanceof JarPackageFragmentRoot) {
- // TODO: add possibility open resorces from jar files
- continue;
- }
- PackageFragmentRoot packageFragmentRoot =
(PackageFragmentRoot)packageFragmentRoots[i];
- IPath packageFragmentRootPath = packageFragmentRoot.getResource().getFullPath();
+ int scanSize = Math.min(pathsSrc.size(), pathsOut.size());
+ scanSize = Math.min(pathsFull.size(), scanSize);
+ for (int i = 0; i < scanSize && file == null; i++) {
+ IPath pathSrc = pathsSrc.get(i);
+ IPath pathOut = pathsOut.get(i);
+ IPath pathFull = pathsFull.get(i);
Iterator<String> it = documentPaths.iterator();
- while (it.hasNext()) {
+ while (it.hasNext() && file == null) {
String docPath = it.next();
IPath path2DocFull = Path.fromOSString(docPath);
- IPath path2Doc = path2DocFull.makeRelativeTo(projFullPath);
- IPath resPath = projPath.append(path2Doc);
- resPath = resPath.makeRelativeTo(projPath);
- resPath = resPath.makeRelativeTo(outPath);
- resPath = packageFragmentRootPath.append(resPath);
+ IPath resPath = path2DocFull.makeRelativeTo(pathFull);
+ resPath = resPath.makeRelativeTo(pathOut);
+ resPath = pathSrc.append(resPath);
file = ResourcesPlugin.getWorkspace().getRoot().getFile(resPath);
- if (file == null) {
- continue;
+ if (file == null || !file.exists()) {
+ file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(resPath);
}
- if (file.exists() && elementInFile(consoleConfiguration, file, element)) {
- break;
+ if (file != null && file.exists()) {
+ if (elementInFile(consoleConfig, file, element)) {
+ break;
+ }
}
file = null;
}
@@ -542,18 +561,17 @@
* This function is trying to find hibernate console config file,
* which is corresponding to provided element.
*
- * @param consoleConfiguration
- * @param proj
+ * @param consoleConfig
* @param element
* @return
*/
- public static IFile searchFileToOpen(ConsoleConfiguration consoleConfiguration,
IJavaProject proj, Object element) {
- IFile file = searchInMappingFiles(consoleConfiguration, proj, element);
+ public static IFile searchFileToOpen(ConsoleConfiguration consoleConfig, Object element)
{
+ IFile file = searchInMappingFiles(consoleConfig, element);
if (file == null) {
- file = searchInAdditionalMappingFiles(consoleConfiguration, element);
+ file = searchInAdditionalMappingFiles(consoleConfig, element);
}
if (file == null) {
- file = searchInEjb3MappingFiles(consoleConfiguration, proj, element);
+ file = searchInEjb3MappingFiles(consoleConfig, element);
}
return file;
}
@@ -589,6 +607,7 @@
/**
* Finds a document region, which corresponds of given selection object.
+ * @param proj
* @param findAdapter
* @param selection
* @return a proper document region
@@ -605,6 +624,7 @@
/**
* Finds a document region, which corresponds of given property.
+ * @param proj
* @param findAdapter
* @param property
* @return a proper document region
@@ -670,6 +690,7 @@
/**
* Finds a document region, which corresponds of given persistent class.
+ * @param proj
* @param findAdapter
* @param persistentClass
* @return a proper document region
@@ -695,6 +716,7 @@
/**
* Finds a document region, which corresponds of given persistent class.
+ * @param proj
* @param findAdapter
* @param className
* @return a proper document region
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/ProjectUtils.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/ProjectUtils.java 2009-05-26
14:29:18 UTC (rev 15514)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/utils/ProjectUtils.java 2009-05-26
16:10:23 UTC (rev 15515)
@@ -21,18 +21,27 @@
*/
package org.hibernate.eclipse.console.utils;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import org.eclipse.core.internal.resources.File;
+import org.eclipse.core.internal.resources.ICoreConstants;
+import org.eclipse.core.internal.resources.ResourceInfo;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
@@ -53,6 +62,7 @@
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.console.preferences.ConsoleConfigurationPreferences;
import org.hibernate.eclipse.console.HibernateConsoleMessages;
import org.hibernate.eclipse.console.HibernateConsolePlugin;
import org.hibernate.eclipse.launch.ICodeGenerationLaunchConstants;
@@ -61,6 +71,7 @@
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;
+@SuppressWarnings("restriction")
public class ProjectUtils {
private ProjectUtils() {
@@ -169,41 +180,148 @@
return null;
}
- static public IJavaProject findJavaProject(String name) {
- if(StringHelper.isEmpty( name )) {
+
+ /**
+ * Returns a handle to the project resource with the given name
+ * which is a child of workspace.
+ * @param name - java project name
+ * @return a handle to the project resource
+ */
+ public static IProject findProject(String name) {
+ if (StringHelper.isEmpty(name)) {
return null;
}
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
- IProject project = null;
- project = root.getProject(name);
+ IProject project = root.getProject(name);
+ return project;
+ }
+
+ /**
+ * Returns the created Java project corresponding to the given name.
+ * @param name - java project name
+ * @return a Java project
+ */
+ public static IJavaProject findJavaProject(String name) {
+ IProject project = findProject(name);
if (project != null) {
return JavaCore.create(project);
- } else {
- return null;
}
+ return null;
}
- public static IJavaProject findJavaProject(ConsoleConfiguration consoleConfiguration) {
- IJavaProject proj = null;
+ /**
+ * Returns a handle to the project resource corresponding to
+ * the given console configuration name.
+ * @param consoleConfiguration
+ * @return a handle to the project resource
+ */
+ public static IProject findProject(ConsoleConfiguration consoleConfiguration) {
+ IProject res = null;
if (consoleConfiguration != null) {
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType launchConfigurationType =
launchManager.getLaunchConfigurationType(
ICodeGenerationLaunchConstants.CONSOLE_CONFIGURATION_LAUNCH_TYPE_ID );
- ILaunchConfiguration[] launchConfigurations;
try {
- launchConfigurations = launchManager.getLaunchConfigurations( launchConfigurationType
);
- for (int i = 0; i < launchConfigurations.length; i++) { // can't believe there
is no look up by name API
+ ILaunchConfiguration[] launchConfigurations =
+ launchManager.getLaunchConfigurations( launchConfigurationType );
+ // can't believe there is no look up by name API
+ for (int i = 0; i < launchConfigurations.length && res == null; i++) {
ILaunchConfiguration launchConfiguration = launchConfigurations[i];
- if(launchConfiguration.getName().equals(consoleConfiguration.getName())) {
- proj =
ProjectUtils.findJavaProject(launchConfiguration.getAttribute(IConsoleConfigurationLaunchConstants.PROJECT_NAME,
"")); //$NON-NLS-1$
+ if (launchConfiguration.getName().equals(consoleConfiguration.getName())) {
+ String projName = launchConfiguration.getAttribute(
+ IConsoleConfigurationLaunchConstants.PROJECT_NAME, ""); //$NON-NLS-1$
+ res = findProject(projName);
}
}
} catch (CoreException e1) {
HibernateConsolePlugin.getDefault().log(e1);
}
}
- return proj;
+ return res;
}
+
+ /**
+ * Returns the created Java project corresponding to
+ * the given console configuration name.
+ * @param consoleConfiguration
+ * @return a Java project
+ */
+ public static IJavaProject findJavaProject(ConsoleConfiguration consoleConfiguration) {
+ IProject project = findProject(consoleConfiguration);
+ if (project != null) {
+ return JavaCore.create(project);
+ }
+ return null;
+ }
+
+ /**
+ * Checks is file, folder or project exist.
+ * @param file
+ * @return true if a resource exist
+ */
+ public static boolean exists(IFile f) {
+ if (!(f instanceof File)) {
+ return false;
+ }
+ File file = (File)f;
+ ResourceInfo info = file.getResourceInfo(false, false);
+ int flags = file.getFlags(info);
+ if (flags != ICoreConstants.NULL_FLAG) {
+ int type = ResourceInfo.getType(flags);
+ if (type == IResource.FILE || type == IResource.FOLDER || type == IResource.PROJECT)
{
+ return true;
+ }
+ }
+ return false;
+ }
+ private static boolean updateCollection(ArrayList<IProject> projects, IProject
project) {
+ if (project == null) {
+ return false;
+ }
+ for (Iterator<IProject> it = projects.iterator(); it.hasNext();) {
+ if (project.equals(it.next())) {
+ return false;
+ }
+ }
+ projects.add(project);
+ return true;
+ }
+
+ /**
+ * Returns the created Java projects corresponding to
+ * the given console configuration (classpath & console configuration name).
+ * Projects are listed in priority order, sequence is important.
+ *
+ * @param consoleConfiguration
+ * @return a list of Java projects in order
+ */
+ public static IJavaProject[] findJavaProjects(ConsoleConfiguration consoleConfiguration)
{
+ ConsoleConfigurationPreferences ccp = consoleConfiguration.getPreferences();
+ URL[] classPathURLs = new URL[0];
+ if (ccp != null) {
+ classPathURLs = ccp.getCustomClassPathURLS();
+ }
+ ArrayList<IProject> projects = new ArrayList<IProject>();
+ IFile file = null;
+ for (int i = 0; i < classPathURLs.length; i++) {
+ IPath path = Path.fromOSString(classPathURLs[i].getFile());
+ file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+ if (file == null || !exists(file)) {
+ file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
+ }
+ if (file != null && exists(file)) {
+ updateCollection(projects, file.getProject());
+ }
+ }
+ // insert this in last place
+ updateCollection(projects, findProject(consoleConfiguration));
+ ArrayList<IJavaProject> res = new ArrayList<IJavaProject>();
+ for (Iterator<IProject> it = projects.iterator(); it.hasNext();) {
+ res.add(JavaCore.create(it.next()));
+ }
+ return res.toArray(new IJavaProject[0]);
+ }
+
static public org.eclipse.jdt.core.dom.CompilationUnit getCompilationUnit(
ICompilationUnit source, boolean bindings) {
ASTParser parser = ASTParser.newParser(AST.JLS3);