[jbosstools-commits] JBoss Tools SVN: r6911 - in trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui: src/org/hibernate/eclipse/jdt/ui/internal and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Thu Mar 13 11:28:10 EDT 2008


Author: dgeraskov
Date: 2008-03-13 11:28:10 -0400 (Thu, 13 Mar 2008)
New Revision: 6911

Added:
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java
Modified:
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/CriteriaQuickAssistProcessor.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/HQLQuickAssistProcessor.java
Log:
http://jira.jboss.com/jira/browse/JBIDE-1668

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF	2008-03-13 15:27:50 UTC (rev 6910)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF	2008-03-13 15:28:10 UTC (rev 6911)
@@ -16,7 +16,8 @@
  org.eclipse.ui.workbench.texteditor,
  org.eclipse.gef,
  org.hibernate.eclipse,
- org.hibernate.eclipse.console
+ org.hibernate.eclipse.console,
+ org.eclipse.ui.editors
 Eclipse-LazyStart: true
 Export-Package: org.hibernate.eclipse.jdt.ui,
  org.hibernate.eclipse.jdt.ui.internal

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/CriteriaQuickAssistProcessor.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/CriteriaQuickAssistProcessor.java	2008-03-13 15:27:50 UTC (rev 6910)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/CriteriaQuickAssistProcessor.java	2008-03-13 15:28:10 UTC (rev 6911)
@@ -33,10 +33,15 @@
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.ui.texteditor.ITextEditor;
 import org.hibernate.console.ImageConstants;
 import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.console.actions.OpenMappingAction;
 import org.hibernate.eclipse.console.utils.EclipseImages;
+import org.hibernate.eclipse.jdt.ui.Activator;
 
 
 public class CriteriaQuickAssistProcessor extends BasicQuickAssistProcessor  {
@@ -50,10 +55,16 @@
 		IDocument document = getDocument( context.getCompilationUnit() );
 		try {
 			String contents = document.get( context.getSelectionOffset(), context.getSelectionLength() );
+			// position of selection
+			final Point position = new Point( context.getSelectionOffset(), context.getSelectionLength() );
 			result = new IJavaCompletionProposal[1];			
 			result[0] = new ExternalActionQuickAssistProposal(contents, EclipseImages.getImage(ImageConstants.CRITERIA_EDITOR), "Copy to Criteria Editor", context) {
 				public void apply(IDocument target) {
-					HibernateConsolePlugin.getDefault().openCriteriaEditor(getName(), getContents());
+					//IEditorPart editorPart = HibernateConsolePlugin.getDefault().openCriteriaEditor(getName(), getContents());
+					IEditorPart editorPart = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+					ITextEditor[] textEditors = OpenMappingAction.getTextEditors(editorPart);
+					if (textEditors.length == 0) return;
+					new SaveQueryEditorListener(textEditors[0], getName(), getContents(), position, SaveQueryEditorListener.CriteriaEditor);
 				}
 			};
 		}

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/HQLQuickAssistProcessor.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/HQLQuickAssistProcessor.java	2008-03-13 15:27:50 UTC (rev 6910)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/HQLQuickAssistProcessor.java	2008-03-13 15:28:10 UTC (rev 6911)
@@ -28,13 +28,17 @@
 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
 import org.eclipse.jdt.ui.text.java.IProblemLocation;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.texteditor.ITextEditor;
 import org.hibernate.console.ImageConstants;
-import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.console.actions.OpenMappingAction;
 import org.hibernate.eclipse.console.utils.EclipseImages;
+import org.hibernate.eclipse.jdt.ui.Activator;
 
 public class HQLQuickAssistProcessor extends BasicQuickAssistProcessor {
 
-	public IJavaCompletionProposal[] getAssists(IInvocationContext context,
+	public IJavaCompletionProposal[] getAssists(final IInvocationContext context,
 			IProblemLocation[] locations) throws CoreException {
 		
 		IJavaCompletionProposal[] result = new IJavaCompletionProposal[0];
@@ -45,16 +49,19 @@
 			return result;
 		}
 		
-		StringLiteral stringLiteral= (StringLiteral) coveringNode;
+		final StringLiteral stringLiteral= (StringLiteral) coveringNode;
 		String contents= stringLiteral.getLiteralValue();
 		result = new IJavaCompletionProposal[1];			
 		result[0] = new ExternalActionQuickAssistProposal(contents, EclipseImages.getImage(ImageConstants.HQL_EDITOR), "Copy to HQL Editor", context) {
 			public void apply(IDocument document) {
-				HibernateConsolePlugin.getDefault().openScratchHQLEditor( getName(), getContents() );				
+				IEditorPart editorPart = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+				ITextEditor[] textEditors = OpenMappingAction.getTextEditors(editorPart);
+				if (textEditors.length == 0) return;
+				Point position = new Point(stringLiteral.getStartPosition() + 1, stringLiteral.getLength() - 2);
+				new SaveQueryEditorListener(textEditors[0], getName(), getContents(), position, SaveQueryEditorListener.HQLEditor);
 			}
 		};
 		
 		return result;
 	}
-		
 }

Added: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java	                        (rev 0)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java	2008-03-13 15:28:10 UTC (rev 6911)
@@ -0,0 +1,99 @@
+/*******************************************************************************
+  * Copyright (c) 2007-2008 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.jdt.ui.internal;
+
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IPropertyListener;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.hibernate.eclipse.console.AbstractQueryEditor;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class SaveQueryEditorListener implements IPropertyListener {
+	
+	public static final int HQLEditor = 0;
+	
+	public static final int CriteriaEditor = 1;
+	
+	private ITextEditor fromEditorPart;
+	private AbstractQueryEditor editor;	
+	private String query;
+	private Point position;
+	
+	public SaveQueryEditorListener(ITextEditor fromEditorPart, String consoleName, 
+			String query, Point position, int editorNum){
+		this.fromEditorPart = fromEditorPart;
+		this.query = query;
+		this.position = position;
+		switch (editorNum) {
+		case HQLEditor:
+			editor = (AbstractQueryEditor) HibernateConsolePlugin.getDefault()
+			.openScratchHQLEditor( consoleName, query );
+			break;
+
+		default:
+			editor = (AbstractQueryEditor) HibernateConsolePlugin.getDefault()
+			.openCriteriaEditor( consoleName, query );
+		}
+		editor.addPropertyListener(this);
+	}
+	
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
+	 */
+	public void propertyChanged(Object source, int propId) {
+		if (IEditorPart.PROP_DIRTY == propId && !editor.isDirty()){
+			String newQuery = editor.getQueryString();
+			
+			IDocumentProvider docProvider = fromEditorPart.getDocumentProvider();
+			if (docProvider == null){	// editor was disposed							
+				editor.removePropertyListener(this);
+				return;
+			}
+
+			IDocument doc = docProvider.getDocument( fromEditorPart.getEditorInput() );
+			boolean isDocChanged = true;
+			try {
+				if (query.equals(doc.get(position.x, position.y))){
+					isDocChanged = false;
+				}
+			} catch (BadLocationException e1) {
+				//document changed and we can get the exception
+			}
+			
+			if (isDocChanged){
+				String title = "Document was changed";
+				String question = "Document was changed. Can't find string to replace.";
+				MessageDialog.openConfirm( null, title, question);
+				return;
+			}
+			
+			try {
+				// replace old string with new one and change positions
+				doc.replace(position.x, position.y, newQuery);
+				position.y = newQuery.length();
+				query = newQuery;
+			} catch (BadLocationException e) {
+				HibernateConsolePlugin.getDefault().log(e);
+			}
+		}
+	}
+
+}


Property changes on: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/SaveQueryEditorListener.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Author Id Revision Date
Name: svn:eol-style
   + native




More information about the jbosstools-commits mailing list