Author: vyemialyanchyk
Date: 2009-05-07 11:53:50 -0400 (Thu, 07 May 2009)
New Revision: 15110
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenFileActionUtils.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-4285 - code review - add updateSelection function
and reduce "returns" - functionality remains the same
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenFileActionUtils.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenFileActionUtils.java 2009-05-07
15:00:34 UTC (rev 15109)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenFileActionUtils.java 2009-05-07
15:53:50 UTC (rev 15110)
@@ -1,3 +1,13 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
package org.hibernate.eclipse.console.actions;
import java.io.FileInputStream;
@@ -52,14 +62,20 @@
private static final String HIBERNATE_TAG_RESOURCE =
"resource"; //$NON-NLS-1$
private static final String HIBERNATE_TAG_CATALOG =
"catalog"; //$NON-NLS-1$
private static final String HIBERNATE_TAG_SCHEMA =
"schema"; //$NON-NLS-1$
+
+ private OpenFileActionUtils() {
+ }
public static IEditorPart openEditor(IWorkbenchPage page, IResource resource) throws
PartInitException {
return IDE.openEditor(page, (IFile) resource);
}
- public static boolean rootClassHasAnnotations(ConsoleConfiguration consoleConfiguration,
java.io.File configXMLFile, PersistentClass rootClass) {
- if (configXMLFile == null) return true;
+ public static boolean rootClassHasAnnotations(ConsoleConfiguration consoleConfiguration,
PersistentClass rootClass) {
+ java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
+ if (configXMLFile == null) {
+ return true;
+ }
Document doc = getDocument(consoleConfiguration, configXMLFile);
return getElements(doc, HIBERNATE_TAG_MAPPING, HIBERNATE_TAG_CLASS,
getPersistentClassName(rootClass)).hasNext();
}
@@ -82,15 +98,15 @@
private static boolean elementInResource(ConsoleConfiguration consoleConfiguration,
IResource resource, Object element) {
+ boolean res = false;
if (element instanceof RootClass) {
- return rootClassInResource(consoleConfiguration, resource, (RootClass)element);
+ res = rootClassInResource(consoleConfiguration, resource, (RootClass)element);
} else if (element instanceof Subclass) {
- return subclassInResource(consoleConfiguration, resource, (Subclass)element);
+ res = subclassInResource(consoleConfiguration, resource, (Subclass)element);
} else if (element instanceof Table) {
- return tableInResource(consoleConfiguration, resource, (Table)element);
- } else {
- return false;
+ res = tableInResource(consoleConfiguration, resource, (Table)element);
}
+ return res;
}
// TODO: this is *extremely* inefficient - no need to scan the whole tree again and
again.
@@ -143,8 +159,6 @@
) {
return true;
}
-
-
}
Attribute classNameAttr = element.attribute( HIBERNATE_TAG_NAME );
@@ -257,11 +271,12 @@
return doc;
}
- public static IResource getResource(ConsoleConfiguration consoleConfiguration,
IJavaProject proj, java.io.File configXMLFile, Object element) {
+ public static IResource getResource(ConsoleConfiguration consoleConfiguration,
IJavaProject proj, Object element) {
IResource resource = null;
if (consoleConfiguration == null) {
return resource;
}
+ java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
Document doc = getDocument(consoleConfiguration, configXMLFile);
if (proj != null && doc != null) {
Element sfNode = doc.getRootElement().element( HIBERNATE_TAG_SESSION_FACTORY );
@@ -270,9 +285,9 @@
Element subelement = (Element) elements.next();
Attribute file = subelement.attribute( HIBERNATE_TAG_RESOURCE );
if (file != null) {
- IPackageFragmentRoot[] packageFragmentRoots;
try {
- packageFragmentRoots = proj.getAllPackageFragmentRoots();
+ IPackageFragmentRoot[] packageFragmentRoots =
+ proj.getAllPackageFragmentRoots();
for (int i = 0; i < packageFragmentRoots.length; i++) {
//search in source folders.
if (packageFragmentRoots[i].getClass() == PackageFragmentRoot.class) {
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-07
15:00:34 UTC (rev 15109)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java 2009-05-07
15:53:50 UTC (rev 15110)
@@ -10,7 +10,6 @@
******************************************************************************/
package org.hibernate.eclipse.console.actions;
-import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
@@ -107,25 +106,22 @@
public static IEditorPart run(Object selection, ConsoleConfiguration
consoleConfiguration) throws PartInitException, JavaModelException, FileNotFoundException
{
IEditorPart editorPart = null;
IJavaProject proj = ProjectUtils.findJavaProject(consoleConfiguration);
- java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
IResource resource = null;
if (selection instanceof Property) {
Property p = (Property)selection;
if (p.getPersistentClass() != null) {
//use PersistentClass to open editor
- resource = OpenFileActionUtils.getResource(consoleConfiguration, proj, configXMLFile,
p.getPersistentClass());
+ resource = OpenFileActionUtils.getResource(consoleConfiguration, proj,
p.getPersistentClass());
//editorPart = openMapping(p.getPersistentClass(), consoleConfiguration);
}
}
else {
- resource = OpenFileActionUtils.getResource(consoleConfiguration, proj, configXMLFile,
selection);
+ resource = OpenFileActionUtils.getResource(consoleConfiguration, proj, selection);
//editorPart = openMapping(selection, consoleConfiguration);
}
if (resource != null) {
editorPart = openMapping(resource);
- if (editorPart != null){
- applySelectionToEditor(selection, editorPart);
- }
+ applySelectionToEditor(selection, editorPart);
}
if (editorPart == null) {
//try to find hibernate-annotations
@@ -140,7 +136,7 @@
}
}
if (rootClass != null){
- if (OpenFileActionUtils.rootClassHasAnnotations(consoleConfiguration, configXMLFile,
rootClass)) {
+ if (OpenFileActionUtils.rootClassHasAnnotations(consoleConfiguration, rootClass)) {
String fullyQualifiedName = rootClass.getClassName();
editorPart = OpenSourceAction.run(selection, proj, fullyQualifiedName);
}
@@ -154,6 +150,38 @@
}
/**
+ * @param selection
+ * @param editorPart
+ */
+ static public boolean applySelectionToEditor(Object selection, IEditorPart editorPart)
{
+ ITextEditor[] textEditors = getTextEditors(editorPart);
+ if (textEditors.length == 0) {
+ return false;
+ }
+ textEditors[0].selectAndReveal(0, 0);
+ FindReplaceDocumentAdapter findAdapter = null;
+ ITextEditor textEditor = null;
+ for (int i = 0; i < textEditors.length && findAdapter == null; i++) {
+ textEditor = textEditors[i];
+ findAdapter = getFindDocAdapter(textEditor);
+ }
+ if (findAdapter == null) {
+ return false;
+ }
+ IRegion selectRegion = null;
+ if (selection instanceof RootClass || selection instanceof Subclass) {
+ selectRegion = findSelection((PersistentClass)selection, findAdapter);
+ } else if (selection instanceof Property){
+ selectRegion = findSelection((Property)selection, findAdapter);
+ }
+ if (selectRegion != null){
+ textEditor.selectAndReveal(selectRegion.getOffset(), selectRegion.getLength());
+ return true;
+ }
+ return false;
+ }
+
+ /**
* @param compositeProperty
* @param parentProperty
* @param consoleConfiguration
@@ -163,79 +191,37 @@
* @throws BadLocationException
*/
public static IEditorPart run(Property compositeProperty, Property parentProperty,
ConsoleConfiguration consoleConfiguration) throws PartInitException, JavaModelException,
FileNotFoundException{
- if (parentProperty.getPersistentClass() == null) return null;
+ PersistentClass rootClass = parentProperty.getPersistentClass();
IJavaProject proj = ProjectUtils.findJavaProject(consoleConfiguration);
- java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
- if (configXMLFile == null) return null;
- IResource resource = OpenFileActionUtils.getResource(consoleConfiguration, proj,
configXMLFile, parentProperty.getPersistentClass());
-
+ IResource resource = OpenFileActionUtils.getResource(consoleConfiguration, proj,
rootClass);
IEditorPart editorPart = null;
if (resource != null){
editorPart = openMapping(resource);
- if (editorPart != null){
- ITextEditor[] textEditors = getTextEditors(editorPart);
- if (textEditors.length == 0) return editorPart;
- textEditors[0].selectAndReveal(0, 0);
- FindReplaceDocumentAdapter findAdapter = null;
- ITextEditor textEditor = null;
- for (int i = 0; i < textEditors.length && findAdapter == null; i++) {
- textEditor = textEditors[i];
- findAdapter = getFindDocAdapter(textEditor);
- }
- if (findAdapter == null) return null;
-
- IRegion parentRegion = findSelection(parentProperty, findAdapter);
- if (parentRegion == null) return editorPart;
- IRegion propRegion = null;
- try {
- propRegion = findAdapter.find(parentRegion.getOffset()+parentRegion.getLength(),
generatePattern(compositeProperty), true, true, false, true);
- if (propRegion == null && parentProperty.isComposite()
- && parentProperty.getPersistentClass().getIdentifierProperty() ==
parentProperty){
- // try to use key-property
- String pattern =
generatePattern(compositeProperty).replaceFirst("<property",
"<key-property"); //$NON-NLS-1$ //$NON-NLS-2$
- propRegion = findAdapter.find(parentRegion.getOffset()+parentRegion.getLength(),
pattern, true, true, false, true);
-
- if (propRegion == null){
- // try to use key-many-to-one
- pattern =
generatePattern(compositeProperty).replaceFirst("<many-to-one",
"<key-many-to-one"); //$NON-NLS-1$ //$NON-NLS-2$
- propRegion = findAdapter.find(parentRegion.getOffset()+parentRegion.getLength(),
pattern, true, true, false, true);
- }
- }
- } catch (BadLocationException e) {
- HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenMappingAction_selection_not_found,
e);
- }
-
- if (propRegion != null){
- int length = compositeProperty.getNodeName().length();
- int offset = propRegion.getOffset() + propRegion.getLength() - length - 1;
- propRegion = new Region(offset, length);
- textEditor.selectAndReveal(propRegion.getOffset(), propRegion.getLength());
- return editorPart;
- }
- }
- return editorPart;
+ updateEditorSelection(compositeProperty, parentProperty, editorPart);
}
-
- if (parentProperty.getPersistentClass() != null &&
parentProperty.isComposite()){
- PersistentClass rootClass = parentProperty.getPersistentClass();
- if (OpenFileActionUtils.rootClassHasAnnotations(consoleConfiguration, configXMLFile,
rootClass)) {
+ if (editorPart == null && parentProperty.isComposite()) {
+ if (OpenFileActionUtils.rootClassHasAnnotations(consoleConfiguration, rootClass)) {
String fullyQualifiedName =((Component)((Property)
parentProperty).getValue()).getComponentClassName();
- IEditorPart editor = OpenSourceAction.run(compositeProperty, proj,
fullyQualifiedName);
- return editor;
+ editorPart = OpenSourceAction.run(compositeProperty, proj, fullyQualifiedName);
}
}
- // here editorPart := null
- String out =
NLS.bind(HibernateConsoleMessages.OpenMappingAction_mapping_file_for_property_not_found,
compositeProperty.getNodeName());
- throw new FileNotFoundException(out);
+ if (editorPart == null) {
+ String out =
NLS.bind(HibernateConsoleMessages.OpenMappingAction_mapping_file_for_property_not_found,
compositeProperty.getNodeName());
+ throw new FileNotFoundException(out);
+ }
+ return editorPart;
}
/**
- * @param selection
+ * @param compositeProperty
+ * @param parentProperty
* @param editorPart
*/
- static public boolean applySelectionToEditor(Object selection, IEditorPart editorPart)
{
+ static public boolean updateEditorSelection(Property compositeProperty, Property
parentProperty, IEditorPart editorPart) {
ITextEditor[] textEditors = getTextEditors(editorPart);
- if (textEditors.length == 0) return false;
+ if (textEditors.length == 0) {
+ return false;
+ }
textEditors[0].selectAndReveal(0, 0);
FindReplaceDocumentAdapter findAdapter = null;
ITextEditor textEditor = null;
@@ -243,21 +229,39 @@
textEditor = textEditors[i];
findAdapter = getFindDocAdapter(textEditor);
}
- if (findAdapter == null) return false;
- IRegion selectRegion = null;
-
- if (selection instanceof RootClass
- || selection instanceof Subclass){
- selectRegion = findSelection((PersistentClass)selection, findAdapter);
- } else if (selection instanceof Property){
- selectRegion = findSelection((Property)selection, findAdapter);
+ if (findAdapter == null) {
+ return false;
}
-
- if (selectRegion != null){
- textEditor.selectAndReveal(selectRegion.getOffset(), selectRegion.getLength());
- return true;
+ IRegion parentRegion = findSelection(parentProperty, findAdapter);
+ if (parentRegion == null) {
+ return false;
}
- return false;
+ IRegion propRegion = null;
+ try {
+ propRegion = findAdapter.find(parentRegion.getOffset()+parentRegion.getLength(),
generatePattern(compositeProperty), true, true, false, true);
+ PersistentClass rootClass = parentProperty.getPersistentClass();
+ if (propRegion == null && parentProperty.isComposite()
+ && rootClass.getIdentifierProperty() == parentProperty) {
+ // try to use key-property
+ String pattern =
generatePattern(compositeProperty).replaceFirst("<property",
"<key-property"); //$NON-NLS-1$ //$NON-NLS-2$
+ propRegion = findAdapter.find(parentRegion.getOffset()+parentRegion.getLength(),
pattern, true, true, false, true);
+ if (propRegion == null) {
+ // try to use key-many-to-one
+ pattern =
generatePattern(compositeProperty).replaceFirst("<many-to-one",
"<key-many-to-one"); //$NON-NLS-1$ //$NON-NLS-2$
+ propRegion = findAdapter.find(parentRegion.getOffset()+parentRegion.getLength(),
pattern, true, true, false, true);
+ }
+ }
+ } catch (BadLocationException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.OpenMappingAction_selection_not_found,
e);
+ }
+ if (propRegion == null) {
+ return false;
+ }
+ int length = compositeProperty.getNodeName().length();
+ int offset = propRegion.getOffset() + propRegion.getLength() - length - 1;
+ propRegion = new Region(offset, length);
+ textEditor.selectAndReveal(propRegion.getOffset(), propRegion.getLength());
+ return true;
}
/**
@@ -270,78 +274,70 @@
if (textEditor.getDocumentProvider() != null){
document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
}
- if (document == null) return null;
+ if (document == null) {
+ return null;
+ }
FindReplaceDocumentAdapter findAdapter = new FindReplaceDocumentAdapter(document);
return findAdapter;
}
static public IEditorPart openMapping(IResource resource) {
+ IEditorPart editorPart = null;
if (resource != null && resource instanceof IFile){
try {
- return
OpenFileActionUtils.openEditor(HibernateConsolePlugin.getDefault().getActiveWorkbenchWindow().getActivePage(),
(IFile) resource);
+ editorPart =
OpenFileActionUtils.openEditor(HibernateConsolePlugin.getDefault().getActiveWorkbenchWindow().getActivePage(),
(IFile) resource);
} catch (PartInitException e) {
-
+ // ignore
}
} else {
HibernateConsolePlugin.getDefault().log(HibernateConsoleMessages.OpenMappingAction_cannot_open_mapping_file
+ resource);
}
- return null;
+ return editorPart;
}
- /*static public IEditorPart openMapping(Object selElement,
- ConsoleConfiguration consoleConfiguration) {
- IJavaProject proj = ProjectUtils.findJavaProject(consoleConfiguration);
- java.io.File configXMLFile = consoleConfiguration.getPreferences().getConfigXMLFile();
- IResource resource = OpenFileActionUtils.getResource(consoleConfiguration, proj,
configXMLFile, selElement);
-
- if (resource != null && resource instanceof IFile){
- try {
- return
OpenFileActionUtils.openEditor(HibernateConsolePlugin.getDefault().getActiveWorkbenchWindow().getActivePage(),
(IFile) resource);
- } catch (PartInitException e) {
- HibernateConsolePlugin.getDefault().logErrorMessage("Can't open
mapping or source file.", e);
- }
- } else {
- HibernateConsolePlugin.getDefault().log("Can't open mapping file for
" + selElement);
- }
- return null;
- }*/
-
public static IRegion findSelection(Property property, FindReplaceDocumentAdapter
findAdapter) {
Assert.isNotNull(property.getPersistentClass());
+ IRegion classRegion = findSelection(property.getPersistentClass(), findAdapter);
+ if (classRegion == null) {
+ return null;
+ }
+ IRegion finalRegion = null;
+ IRegion propRegion = null;
try {
- IRegion classRegion = findSelection(property.getPersistentClass(), findAdapter);
- if (classRegion == null) return null;
- IRegion finalRegion =
findAdapter.find(classRegion.getOffset()+classRegion.getLength(), "</class",
true, true, false, false); //$NON-NLS-1$
- IRegion propRegion =
findAdapter.find(classRegion.getOffset()+classRegion.getLength(),
generatePattern(property), true, true, false, true);
- if (propRegion == null) return null;
- if (finalRegion != null
- && propRegion.getOffset() > finalRegion.getOffset()){
- return null;
- } else {
- int length = property.getName().length();
- int offset = propRegion.getOffset() + propRegion.getLength() - length - 1;
- return new Region(offset, length);
- }
+ finalRegion = findAdapter.find(classRegion.getOffset()+classRegion.getLength(),
"</class", true, true, false, false); //$NON-NLS-1$
+ propRegion = findAdapter.find(classRegion.getOffset()+classRegion.getLength(),
generatePattern(property), true, true, false, true);
} catch (BadLocationException e) {
- return null;
+ //ignore
}
-
+ IRegion res = null;
+ if (propRegion != null) {
+ int length = property.getName().length();
+ int offset = propRegion.getOffset() + propRegion.getLength() - length - 1;
+ res = new Region(offset, length);
+ if (finalRegion != null && propRegion.getOffset() >
finalRegion.getOffset()) {
+ res = null;
+ }
+ }
+ return res;
}
public static IRegion findSelection(PersistentClass persClass,
FindReplaceDocumentAdapter findAdapter) {
+ IRegion res = null;
try {
String[] classPatterns = generatePatterns(persClass);
IRegion classRegion = null;
for (int i = 0; (classRegion == null) && (i < classPatterns.length); i++){
classRegion = findAdapter.find(0, classPatterns[i], true, true, false, true);
}
- if (classRegion == null) return null;
- int length = persClass.getNodeName().length();
- int offset = classRegion.getOffset() + classRegion.getLength() - length - 1;
- return new Region(offset, length);
+ if (classRegion != null) {
+ int length = persClass.getNodeName().length();
+ int offset = classRegion.getOffset() + classRegion.getLength() - length - 1;
+ res = new Region(offset, length);
+ }
} catch (BadLocationException e) {
- return null;
+ //ignore
}
+ return res;
}
private static String[] generatePatterns(PersistentClass persClass){
@@ -429,6 +425,7 @@
/*
* if EditorPart is MultiPageEditorPart then get ITextEditor from it.
*/
+ ITextEditor[] res = new ITextEditor[0];
if (editorPart instanceof MultiPageEditorPart) {
List testEditors = new ArrayList();
IEditorPart[] editors = ((MultiPageEditorPart)
editorPart).findEditors(editorPart.getEditorInput());
@@ -437,10 +434,10 @@
testEditors.add(editors[i]);
}
}
- return (ITextEditor[])testEditors.toArray(new ITextEditor[0]);
+ res = (ITextEditor[])testEditors.toArray(res);
} else if (editorPart instanceof ITextEditor){
- return new ITextEditor[]{(ITextEditor) editorPart};
+ res = new ITextEditor[]{(ITextEditor) editorPart};
}
- return new ITextEditor[0];
+ return res;
}
}