[jbosstools-commits] JBoss Tools SVN: r9348 - in trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse: criteriaeditor and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Jul 28 07:47:02 EDT 2008


Author: vyemialyanchyk
Date: 2008-07-28 07:47:01 -0400 (Mon, 28 Jul 2008)
New Revision: 9348

Modified:
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditorInput.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/criteriaeditor/CriteriaEditor.java
   trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-2561

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java	2008-07-28 10:40:53 UTC (rev 9347)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/AbstractQueryEditor.java	2008-07-28 11:47:01 UTC (rev 9348)
@@ -1,13 +1,27 @@
 package org.hibernate.eclipse.console;
 
+import java.io.File;
+
+import org.eclipse.core.filesystem.EFS;
+import org.eclipse.core.filesystem.IFileStore;
+import org.eclipse.core.filesystem.URIUtil;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceRoot;
+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.IStatus;
 import org.eclipse.jface.action.ActionContributionItem;
 import org.eclipse.jface.action.ControlContribution;
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.action.ToolBarManager;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.IMessageProvider;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.window.Window;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.ModifyEvent;
@@ -19,12 +33,22 @@
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IShowEditorInput;
+import org.eclipse.ui.IURIEditorInput;
 import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.dialogs.SaveAsDialog;
 import org.eclipse.ui.editors.text.TextEditor;
+import org.eclipse.ui.ide.FileStoreEditorInput;
+import org.eclipse.ui.internal.WorkbenchPlugin;
+import org.eclipse.ui.internal.editors.text.EditorsPlugin;
+import org.eclipse.ui.internal.editors.text.NLSUtility;
+import org.eclipse.ui.part.FileEditorInput;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 import org.hibernate.console.ConsoleConfiguration;
 import org.hibernate.console.KnownConfigurations;
@@ -110,11 +134,32 @@
 
 	final public void doSave(IProgressMonitor progressMonitor) {
 		// super.doSave(progressMonitor);
-		if(getEditorInput() instanceof QueryEditorInput) {
-			QueryEditorInput hei = (QueryEditorInput) getEditorInput();
+		IDocumentProvider p= getDocumentProvider();
+		if (p == null)
+			return;
+
+		QueryEditorInput hei = null;
+		if (getEditorInput() instanceof QueryEditorInput) {
+			hei = (QueryEditorInput) getEditorInput();
 			hei.setQuery( getQueryString() );
 		}
-		performSave( false, progressMonitor );
+
+		if (p.isDeleted(getEditorInput()) || (hei != null && hei.getStorage() instanceof QueryEditorStorage)) {
+			if (isSaveAsAllowed()) {
+				// 1GEUSSR: ITPUI:ALL - User should never loose changes made in the editors.
+				// Changed Behavior to make sure that if called inside a regular save (because
+				// of deletion of input element) there is a way to report back to the caller.
+				performSaveAs(progressMonitor);
+
+			} else {
+				Shell shell = getSite().getShell();
+				String title = HibernateConsoleMessages.AbstractQueryEditor_cannot_save;
+				String msg = HibernateConsoleMessages.AbstractQueryEditor_the_file_has_been_deleted_or_is_not_accessible;
+				MessageDialog.openError(shell, title, msg);
+			}
+		} else {
+			performSave( false, progressMonitor );
+		}
 	}
 
 	protected void doSetInput(IEditorInput input) throws CoreException {
@@ -262,4 +307,150 @@
 	}
 
 	protected abstract String getConnectedImageFilePath();
+
+	/**
+	 * returns file extension for "Save As" dialog
+	 */
+	protected abstract String getSaveAsFileExtension();
+
+	/**
+	 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#performSaveAs(IProgressMonitor)
+	 * the same method except line: "dialog.setOriginalName(getSaveAsFileExtension());"
+	 */
+	protected void performSaveAs(IProgressMonitor progressMonitor) {
+		Shell shell= getSite().getShell();
+		final IEditorInput input= getEditorInput();
+
+		IDocumentProvider provider= getDocumentProvider();
+		final IEditorInput newInput;
+
+		if (input instanceof IURIEditorInput && !(input instanceof IFileEditorInput)) {
+			FileDialog dialog= new FileDialog(shell, SWT.SAVE);
+			IPath oldPath= URIUtil.toPath(((IURIEditorInput)input).getURI());
+			if (oldPath != null) {
+				dialog.setFileName(oldPath.lastSegment());
+				dialog.setFilterPath(oldPath.toOSString());
+			}
+
+			String path= dialog.open();
+			if (path == null) {
+				if (progressMonitor != null)
+					progressMonitor.setCanceled(true);
+				return;
+			}
+
+			// Check whether file exists and if so, confirm overwrite
+			final File localFile= new File(path);
+			if (localFile.exists()) {
+		        MessageDialog overwriteDialog= new MessageDialog(
+		        		shell,
+		        		HibernateConsoleMessages.AbstractQueryEditor_save_as,
+		        		null,
+		        		NLSUtility.format(HibernateConsoleMessages.AbstractQueryEditor_already_exists_do_you_want_to_replace_it, path),
+		        		MessageDialog.WARNING,
+		        		new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL },
+		        		1); // 'No' is the default
+				if (overwriteDialog.open() != Window.OK) {
+					if (progressMonitor != null) {
+						progressMonitor.setCanceled(true);
+						return;
+					}
+				}
+			}
+
+			IFileStore fileStore;
+			try {
+				fileStore= EFS.getStore(localFile.toURI());
+			} catch (CoreException ex) {
+				EditorsPlugin.log(ex.getStatus());
+				String title= HibernateConsoleMessages.AbstractQueryEditor_problems_during_save_as;
+				String msg= NLSUtility.format(HibernateConsoleMessages.AbstractQueryEditor_save_could_not_be_completed, ex.getMessage());
+				MessageDialog.openError(shell, title, msg);
+				return;
+			}
+
+			IFile file= getWorkspaceFile(fileStore);
+			if (file != null)
+				newInput= new FileEditorInput(file);
+			else
+				newInput= new FileStoreEditorInput(fileStore);
+
+		} else {
+			SaveAsDialog dialog= new SaveAsDialog(shell);
+
+			IFile original= (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
+			if (original != null) {
+				dialog.setOriginalFile(original);
+			}
+			else {
+				dialog.setOriginalName(getSaveAsFileExtension());
+			}
+
+			dialog.create();
+
+			if (provider.isDeleted(input) && original != null) {
+				String message= NLSUtility.format(HibernateConsoleMessages.AbstractQueryEditor_the_original_file_has_been_deleted_or_is_not_accessible, original.getName());
+				dialog.setErrorMessage(null);
+				dialog.setMessage(message, IMessageProvider.WARNING);
+			}
+
+			if (dialog.open() == Window.CANCEL) {
+				if (progressMonitor != null)
+					progressMonitor.setCanceled(true);
+				return;
+			}
+
+			IPath filePath= dialog.getResult();
+			if (filePath == null) {
+				if (progressMonitor != null)
+					progressMonitor.setCanceled(true);
+				return;
+			}
+
+			IWorkspace workspace= ResourcesPlugin.getWorkspace();
+			IFile file= workspace.getRoot().getFile(filePath);
+			newInput= new FileEditorInput(file);
+
+		}
+
+		if (provider == null) {
+			// editor has programmatically been  closed while the dialog was open
+			return;
+		}
+
+		boolean success= false;
+		try {
+
+			provider.aboutToChange(newInput);
+			provider.saveDocument(progressMonitor, newInput, provider.getDocument(input), true);
+			success= true;
+
+		} catch (CoreException x) {
+			final IStatus status= x.getStatus();
+			if (status == null || status.getSeverity() != IStatus.CANCEL) {
+				String title= HibernateConsoleMessages.AbstractQueryEditor_problems_during_save_as;
+				String msg= NLSUtility.format(HibernateConsoleMessages.AbstractQueryEditor_save_could_not_be_completed, x.getMessage());
+				MessageDialog.openError(shell, title, msg);
+			}
+		} finally {
+			provider.changed(newInput);
+			if (success)
+				setInput(newInput);
+		}
+
+		if (progressMonitor != null)
+			progressMonitor.setCanceled(!success);
+	}
+
+	/**
+	 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#getWorkspaceFile(IFileStore)
+	 * the same PROTECTED method instead of PRIVATE
+	 */
+	protected IFile getWorkspaceFile(IFileStore fileStore) {
+		IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
+		IFile[] files= workspaceRoot.findFilesForLocation(URIUtil.toPath(fileStore.toURI()));
+		if (files != null && files.length == 1)
+			return files[0];
+		return null;
+	}
 }

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java	2008-07-28 10:40:53 UTC (rev 9347)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java	2008-07-28 11:47:01 UTC (rev 9348)
@@ -18,6 +18,13 @@
 	public static String AbstractQueryEditor_do_you_want_open_session_factory;
 	public static String AbstractQueryEditor_max_results;
 	public static String AbstractQueryEditor_open_session_factory;
+	public static String AbstractQueryEditor_cannot_save;
+	public static String AbstractQueryEditor_the_file_has_been_deleted_or_is_not_accessible;
+	public static String AbstractQueryEditor_save_as;
+	public static String AbstractQueryEditor_already_exists_do_you_want_to_replace_it;
+	public static String AbstractQueryEditor_problems_during_save_as;
+	public static String AbstractQueryEditor_save_could_not_be_completed;
+	public static String AbstractQueryEditor_the_original_file_has_been_deleted_or_is_not_accessible;
 	public static String EclipseConsoleConfigurationPreferences_could_not_compute_def_classpath;
 	public static String EclipseConsoleConfigurationPreferences_could_not_resolve_to_file;
 	public static String EclipseConsoleConfigurationPreferences_could_not_resolve_classpaths;

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties	2008-07-28 10:40:53 UTC (rev 9347)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties	2008-07-28 11:47:01 UTC (rev 9348)
@@ -9,6 +9,13 @@
 AbstractQueryEditor_do_you_want_open_session_factory=Do you want to open the session factory for {0} ?
 AbstractQueryEditor_max_results=Max results:
 AbstractQueryEditor_open_session_factory=Open Session factory
+AbstractQueryEditor_cannot_save=Cannot Save
+AbstractQueryEditor_the_file_has_been_deleted_or_is_not_accessible=The file has been deleted or is not accessible.
+AbstractQueryEditor_save_as=Save As
+AbstractQueryEditor_already_exists_do_you_want_to_replace_it={0} already exists.\nDo you want to replace it?
+AbstractQueryEditor_problems_during_save_as=Problems During Save As...
+AbstractQueryEditor_save_could_not_be_completed=Save could not be completed. {0}
+AbstractQueryEditor_the_original_file_has_been_deleted_or_is_not_accessible=The original file ''{0}'' has been deleted or is not accessible.
 EclipseConsoleConfigurationPreferences_could_not_compute_def_classpath=Could not compute default classpath from project
 EclipseConsoleConfigurationPreferences_could_not_resolve_to_file=Could not resolve {0} to a file
 EclipseConsoleConfigurationPreferences_could_not_resolve_classpaths=Could not resolve classpaths

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditorInput.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditorInput.java	2008-07-28 10:40:53 UTC (rev 9347)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/QueryEditorInput.java	2008-07-28 11:47:01 UTC (rev 9348)
@@ -15,7 +15,7 @@
     /**
      * Constructs an instance of this class with the given <code>IStorage</code>
      * object as the editor input source.
-     * 
+     *
      * @param storage the storage object for this editor input
      */
     public QueryEditorInput( IStorage storage ) {
@@ -24,17 +24,17 @@
         }
         setStorage( storage );
         setName( storage.getName() );
-    }    
+    }
 
     /**
      * Gets whether the editor input exists.
-     * 
+     *
      * @see org.eclipse.ui.IEditorInput#exists()
      */
     public boolean exists() {
         if (storage != null)
             return true;
-        
+
         return false;
     }
 
@@ -54,11 +54,11 @@
         return storage;
     }
 
-    public String getToolTipText() {        
-        return getName();        
+    public String getToolTipText() {
+        return getName();
     }
 
-  
+
     public void setName( String name ) {
         this.name = name;
     }
@@ -71,22 +71,24 @@
 		return this;
 	}
 
-	
+
 	public boolean equals(Object obj) { // used to identify if HQL editor is the same
 		/*if(obj instanceof HQLEditorInput) {
-			
+
 			return ((HQLEditorInput)obj).getConsoleConfigurationName().equals(getConsoleConfigurationName());
 		}*/
 		return super.equals(obj);
 	}
-	
+
 	public int hashCode() {
 		return name==null?0:name.hashCode();
 	}
 
 
 	public void setQuery(String query) {
-		((QueryEditorStorage)getStorage()).setContents(query);		
+		if (getStorage() instanceof QueryEditorStorage) {
+			((QueryEditorStorage)getStorage()).setContents(query);
+		}
 	}
 
 	public String getConsoleConfigurationName() {
@@ -94,7 +96,7 @@
 	}
 
 	public void setConsoleConfigurationName(String name2) {
-		((QueryEditorStorage)getStorage()).setConfigurationName(name2);		
+		((QueryEditorStorage)getStorage()).setConfigurationName(name2);
 	}
 
 	abstract public void resetName();

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/criteriaeditor/CriteriaEditor.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/criteriaeditor/CriteriaEditor.java	2008-07-28 10:40:53 UTC (rev 9347)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/criteriaeditor/CriteriaEditor.java	2008-07-28 11:47:01 UTC (rev 9348)
@@ -211,4 +211,9 @@
 	protected String getConnectedImageFilePath() {
 		return 	"icons/images/criteria_editor_connect.gif";		//$NON-NLS-1$
 	}
+
+	@Override
+	protected String getSaveAsFileExtension() {
+		return "*.crit";	//$NON-NLS-1$
+	}
 }

Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java	2008-07-28 10:40:53 UTC (rev 9347)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/hqleditor/HQLEditor.java	2008-07-28 11:47:01 UTC (rev 9348)
@@ -411,4 +411,9 @@
 		return 	"icons/images/hql_editor_connect.gif";		//$NON-NLS-1$
 	}
 
+
+	@Override
+	protected String getSaveAsFileExtension() {
+		return "*.hql";	//$NON-NLS-1$
+	}
 }




More information about the jbosstools-commits mailing list