Author: dgeraskov
Date: 2007-12-11 03:54:38 -0500 (Tue, 11 Dec 2007)
New Revision: 5236
Added:
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/OpenSourceAction.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml
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/views/ConfigurationsViewActionGroup.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/KnownConfigurationsView.java
Log:
http://jira.jboss.com/jira/browse/JBIDE-1412
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml 2007-12-11
08:54:31 UTC (rev 5235)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml 2007-12-11
08:54:38 UTC (rev 5236)
@@ -135,7 +135,7 @@
</extension>
<extension point="org.eclipse.ui.popupMenus">
- <objectContribution id="mappingEditorAction"
objectClass="org.hibernate.mapping.RootClass">
+ <!--objectContribution id="mappingEditorAction"
objectClass="org.hibernate.mapping.RootClass">
<menu id="org.hibernate.eclipse.console.actions.openMappingMenu"
label="Open Mapping File" path="mappingEditorAction"/>
<action
id="org.hibernate.eclipse.console.actions.openMappingAction"
@@ -143,6 +143,14 @@
class="org.hibernate.eclipse.console.actions.OpenMappingActionDelegate">
</action>
</objectContribution>
+ <objectContribution id="mappingEditorAction2"
objectClass="org.hibernate.mapping.Property">
+ <menu id="org.hibernate.eclipse.console.actions.openMappingMenu"
label="Open Mapping File" path="mappingEditorAction"/>
+ <action
+ id="org.hibernate.eclipse.console.actions.openMappingAction"
+ label="Open Mapping File"
+
class="org.hibernate.eclipse.console.actions.OpenMappingActionDelegate">
+ </action>
+ </objectContribution>
<objectContribution id="sourceEditorAction"
objectClass="org.hibernate.mapping.RootClass">
<menu id="org.hibernate.eclipse.console.actions.openSourceMenu"
label="Open Source File" path="sourceEditorAction"/>
<action
@@ -151,6 +159,14 @@
class="org.hibernate.eclipse.console.actions.OpenSourceActionDelegate">
</action>
</objectContribution>
+ <objectContribution id="sourceEditorAction2"
objectClass="org.hibernate.mapping.Property">
+ <menu id="org.hibernate.eclipse.console.actions.openSourceMenu"
label="Open Source File" path="sourceEditorAction"/>
+ <action
+ id="org.hibernate.eclipse.console.actions.openSourceAction"
+ label="Open Source File"
+
class="org.hibernate.eclipse.console.actions.OpenSourceActionDelegate">
+ </action>
+ </objectContribution-->
</extension>
<extension
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 2007-12-11
08:54:31 UTC (rev 5235)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenFileActionUtils.java 2007-12-11
08:54:38 UTC (rev 5236)
@@ -18,6 +18,7 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
@@ -47,8 +48,8 @@
private static final String HIBERNATE_TAG_CATALOG = "catalog";
private static final String HIBERNATE_TAG_SCHEMA = "schema";
- public static void openEditor(IWorkbenchPage page, IResource resource) throws
PartInitException {
- IDE.openEditor(page, (IFile) resource);
+ public static IEditorPart openEditor(IWorkbenchPage page, IResource resource) throws
PartInitException {
+ return IDE.openEditor(page, (IFile) resource);
}
Added:
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
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java 2007-12-11
08:54:38 UTC (rev 5236)
@@ -0,0 +1,164 @@
+package org.hibernate.eclipse.console.actions;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.FindReplaceDocumentAdapter;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TreePath;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.actions.SelectionListenerAction;
+import org.eclipse.ui.part.MultiPageEditorPart;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.console.utils.ProjectUtils;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.RootClass;
+import org.hibernate.mapping.Subclass;
+import org.hibernate.tool.hbm2x.Cfg2HbmTool;
+
+/**
+ * @author Dmitry Geraskov
+ */
+
+public class OpenMappingAction extends SelectionListenerAction {
+
+ public OpenMappingAction() {
+ super("Open Mapping File");
+ setToolTipText("Open Mapping File");
+ setEnabled( true );
+ }
+
+ public void run() {
+ IStructuredSelection sel = getStructuredSelection();
+ if (sel instanceof TreeSelection){
+ TreePath path = ((TreeSelection)sel).getPaths()[0];
+ ConsoleConfiguration consoleConfiguration =
(ConsoleConfiguration)(path.getSegment(0));
+ run(path.getLastSegment(), consoleConfiguration);
+ }
+ }
+
+ public void run(Object selection, ConsoleConfiguration consoleConfiguration) {
+ IEditorPart editorPart = null;
+ if (selection instanceof Property){
+ Property p = (Property)selection;
+ if (p.getPersistentClass() == null) return;
+ //use PersistentClass to open editor
+ editorPart = openMapping(p.getPersistentClass(), consoleConfiguration);
+ } else {
+ editorPart = openMapping(selection, consoleConfiguration);
+ }
+
+ Assert.isNotNull(editorPart);
+ ITextEditor textEditor = getTextEditor(editorPart);
+ if (textEditor == null) return;
+ IDocument document =
textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
+ FindReplaceDocumentAdapter findAdapter = new FindReplaceDocumentAdapter(document);
+ 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());
+ }
+ }
+
+ 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());
+ try {
+ IRegion classRegion = findAdapter.find(0,
generatePattern(property.getPersistentClass()), true, true, false, true);
+ if (classRegion == null) return null;
+ IRegion finalRegion =
findAdapter.find(classRegion.getOffset()+classRegion.getLength(), "</class",
true, true, false, false);
+ IRegion propRegion =
findAdapter.find(classRegion.getOffset()+classRegion.getLength(),
generatePattern(property), true, true, false, true);
+ if (propRegion != null && 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);
+ }
+ } catch (BadLocationException e) {
+ return null;
+ }
+
+ }
+ public static IRegion findSelection(PersistentClass persClass,
+ FindReplaceDocumentAdapter findAdapter) {
+ try {
+ IRegion classRegion = findAdapter.find(0, generatePattern(persClass), 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);
+ } catch (BadLocationException e) {
+ return null;
+ }
+ }
+
+ private static String generatePattern(Object sel){
+ Cfg2HbmTool tool = new Cfg2HbmTool();
+ String name = null;
+ StringBuilder pattern = new StringBuilder("<");
+ if (sel instanceof PersistentClass) {
+ pattern.append(tool.getTag((PersistentClass)sel));
+ name = ((PersistentClass)sel).getNodeName();
+ } else if (sel instanceof Property){
+ pattern.append(tool.getTag((Property)sel));
+ name = ((Property)sel).getNodeName();
+ }
+ pattern.append("[\\s]+[.[^>]]*name[\\s]*=[\\s]*\"");
+ pattern.append(name).append("\"");
+ return pattern.toString();
+ }
+
+ private ITextEditor getTextEditor(IEditorPart editorPart) {
+ /*
+ * if EditorPart is MultiPageEditorPart then get ITextEditor from it.
+ */
+ if (editorPart instanceof MultiPageEditorPart) {
+ ITextEditor editor = null;
+ IEditorPart[] editors = ((MultiPageEditorPart)
editorPart).findEditors(editorPart.getEditorInput());
+ for (int i = 0; i < editors.length; i++) {
+ if (editors[i] instanceof ITextEditor){
+ editor = (ITextEditor) editors[i];
+ break;
+ }
+ }
+ return editor;
+ }
+ return null;
+ }
+}
Property changes on:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenMappingAction.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Author Id Revision Date
Name: svn:eol-style
+ native
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenSourceAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenSourceAction.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenSourceAction.java 2007-12-11
08:54:38 UTC (rev 5236)
@@ -0,0 +1,98 @@
+package org.hibernate.eclipse.console.actions;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TreePath;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.actions.SelectionListenerAction;
+import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.console.utils.ProjectUtils;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.RootClass;
+import org.hibernate.mapping.Subclass;
+
+/**
+ * @author Dmitry Geraskov
+ */
+
+public class OpenSourceAction extends SelectionListenerAction {
+
+ public OpenSourceAction() {
+ super("Open Source File");
+ setToolTipText("Open Source File");
+ setEnabled( true );
+ }
+
+ public void run() {
+ IStructuredSelection sel = getStructuredSelection();
+ if (sel instanceof TreeSelection){
+ TreePath path = ((TreeSelection)sel).getPaths()[0];
+ PersistentClass persClass = getPersistentClass(path.getLastSegment());
+ Assert.isNotNull(persClass);
+ ConsoleConfiguration consoleConfiguration =
(ConsoleConfiguration)(path.getSegment(0));
+ IJavaProject proj = ProjectUtils.findJavaProject(consoleConfiguration);
+
+ IResource resource = null;
+ String fullyQualifiedName = OpenFileActionUtils.getPersistentClassName(persClass);
+ IType type = null;
+ try {
+ type = proj.findType(fullyQualifiedName);
+ if (type != null) resource = type.getResource();
+
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("Can't find source
file.", e);
+ }
+
+ if (resource instanceof IFile){
+ try {
+ IEditorPart editorPart =
OpenFileActionUtils.openEditor(HibernateConsolePlugin.getDefault().getActiveWorkbenchWindow().getActivePage(),
(IFile) resource);
+ if (editorPart instanceof JavaEditor) {
+ IJavaElement jElement = null;
+ if (path.getLastSegment() instanceof Property){
+ jElement = type.getField(((Property)path.getLastSegment()).getName());
+ } else {
+ jElement = type;
+ }
+ JavaEditor jEditor = (JavaEditor) editorPart;
+ selectionToEditor(jElement, jEditor);
+ }
+ } catch (PartInitException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("Can't open source
file.", e);
+ }
+ }
+
+ if (resource == null) {
+ MessageDialog.openInformation(HibernateConsolePlugin.getDefault().getShell(),
"Open Source File", "Source file for class '" + fullyQualifiedName
+ "' not found.");
+ }
+ }
+ }
+
+ private PersistentClass getPersistentClass(Object selection){
+ if (selection instanceof Property){
+ return ((Property)selection).getPersistentClass();
+ } else if (selection instanceof Subclass){
+ return (PersistentClass)selection;
+ } else {
+ return (RootClass)selection;
+ }
+ }
+
+ private void selectionToEditor(IJavaElement jElement, JavaEditor jEditor) {
+ if (jEditor != null) {
+ jEditor.setSelection(jElement);
+ }
+ }
+
+}
Property changes on:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/actions/OpenSourceAction.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Author Id Revision Date
Name: svn:eol-style
+ native
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/ConfigurationsViewActionGroup.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/ConfigurationsViewActionGroup.java 2007-12-11
08:54:31 UTC (rev 5235)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/ConfigurationsViewActionGroup.java 2007-12-11
08:54:38 UTC (rev 5236)
@@ -24,18 +24,25 @@
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
+import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.actions.SelectionListenerAction;
+import org.hibernate.console.ConsoleConfiguration;
import org.hibernate.eclipse.console.actions.AddConfigurationAction;
import org.hibernate.eclipse.console.actions.CriteriaEditorAction;
import org.hibernate.eclipse.console.actions.DeleteConfigurationAction;
import org.hibernate.eclipse.console.actions.EditConsoleConfiguration;
import org.hibernate.eclipse.console.actions.HQLScratchpadAction;
+import org.hibernate.eclipse.console.actions.OpenMappingAction;
+import org.hibernate.eclipse.console.actions.OpenSourceAction;
import org.hibernate.eclipse.console.actions.RefreshAction;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.RootClass;
+import org.hibernate.mapping.Subclass;
/**
* @author max
@@ -53,6 +60,8 @@
private final StructuredViewer selectionProvider;
private SelectionListenerAction hqlEditorAction;
private CriteriaEditorAction criteriaEditorAction;
+ private SelectionListenerAction openMappingAction;
+ private SelectionListenerAction openSourceAction;
public ConfigurationsViewActionGroup(IViewPart part, StructuredViewer selectionProvider)
{
@@ -88,6 +97,13 @@
criteriaEditorAction = new CriteriaEditorAction();
selectionProvider.addSelectionChangedListener(criteriaEditorAction);
+
+ openMappingAction = new OpenMappingAction();
+ selectionProvider.addSelectionChangedListener(openMappingAction);
+
+ openSourceAction = new OpenSourceAction();
+ selectionProvider.addSelectionChangedListener(openSourceAction);
+
}
public void dispose() {
@@ -102,18 +118,36 @@
}
public void fillContextMenu(IMenuManager menu) {
-
+ if (getContext() == null) return;
+ IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
+ if (selection == null) return;
+ Object first = selection.getFirstElement();
menu.add(hqlEditorAction);
menu.add(criteriaEditorAction);
menu.add(new Separator() );
menu.add(addConfigurationAction);
- menu.add(reloadConfigurationAction);
- menu.add(editConfigurationAction);
- menu.add(deleteConfigurationAction);
+ if (first instanceof ConsoleConfiguration){
+ menu.add(reloadConfigurationAction);
+ menu.add(editConfigurationAction);
+ menu.add(deleteConfigurationAction);
+ }
menu.add(new Separator() );
menu.add(refreshAction);
- menu.add(schemaExportAction);
-
+ if (first instanceof ConsoleConfiguration){
+ menu.add(schemaExportAction);
+ }
+ menu.add(new Separator() );
+ if (first instanceof RootClass
+ //|| first instanceof Property
+ || first instanceof Subclass){
+ menu.add(openMappingAction);
+ menu.add(openSourceAction);
+ }
+ if (first instanceof Property
+ && ((Property)first).getPersistentClass() != null){
+ menu.add(openMappingAction);
+ menu.add(openSourceAction);
+ }
}
public void fillActionBars(IActionBars actionBars) {
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/KnownConfigurationsView.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/KnownConfigurationsView.java 2007-12-11
08:54:31 UTC (rev 5235)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/KnownConfigurationsView.java 2007-12-11
08:54:38 UTC (rev 5236)
@@ -40,7 +40,6 @@
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionGroup;
-import org.eclipse.ui.internal.ObjectPluginAction;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.PropertySheetPage;
@@ -48,10 +47,11 @@
import org.hibernate.console.KnownConfigurations;
import org.hibernate.console.node.BaseNode;
import org.hibernate.eclipse.console.actions.EditConsoleConfiguration;
-import org.hibernate.eclipse.console.actions.OpenMappingActionDelegate;
+import org.hibernate.eclipse.console.actions.OpenMappingAction;
import org.hibernate.eclipse.console.workbench.xpl.AnyAdaptableLabelProvider;
-import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
+import org.hibernate.mapping.Subclass;
import org.hibernate.util.StringHelper;
@@ -157,9 +157,13 @@
consoleConfiguration.executeHQLQuery( hql );
}
}
- } else if (firstElement instanceof RootClass) {
+ } else if (firstElement instanceof RootClass
+ || firstElement instanceof Subclass
+ || (firstElement instanceof Property
+ && ((Property)firstElement).getPersistentClass() != null)) {
ConsoleConfiguration consoleConfiguration =
(ConsoleConfiguration)((TreeSelection)selection).getPaths()[0].getSegment(0);
- OpenMappingActionDelegate.openMapping((RootClass) firstElement,
consoleConfiguration);
+ //OpenMappingActionDelegate.openMapping((RootClass) firstElement,
consoleConfiguration);
+ new OpenMappingAction().run(firstElement, consoleConfiguration);
}
}
};