Author: vyemialyanchyk
Date: 2009-09-24 13:13:17 -0400 (Thu, 24 Sep 2009)
New Revision: 17718
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/DiagramViewer.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/ExportImageAction.java
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/META-INF/MANIFEST.MF
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/test/ExportImageActionTest.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-4873 - fix for clause (7) save inside the
workspace
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/DiagramViewer.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/DiagramViewer.java 2009-09-24
14:56:02 UTC (rev 17717)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/DiagramViewer.java 2009-09-24
17:13:17 UTC (rev 17718)
@@ -292,29 +292,49 @@
ObjectEditorInput objectEditorInput = (ObjectEditorInput)input;
ConsoleConfiguration configuration = objectEditorInput.getConfiguration();
Object obj = objectEditorInput.getObject();
+ setPartName(DiagramViewerMessages.DiagramViewer_diagram_for + " " +
getDiagramName(obj)); //$NON-NLS-1$
if (obj instanceof RootClass) {
RootClass rootClass = (RootClass)obj;
- setPartName(DiagramViewerMessages.DiagramViewer_diagram_for + " " +
getItemName(rootClass)); //$NON-NLS-1$
ormDiagram = new OrmDiagram(configuration, rootClass);
} else if (obj instanceof RootClass[]) {
RootClass[] rootClasses = (RootClass[])obj;
+ ormDiagram = new OrmDiagram(configuration, rootClasses);
+ }
+ super.setInput(input);
+ loadProperties();
+ }
+
+ protected String getDiagramName(Object obj) {
+ String name = ""; //$NON-NLS-1$
+ if (obj instanceof RootClass) {
+ RootClass rootClass = (RootClass)obj;
+ name = getItemName(rootClass);
+ } else if (obj instanceof RootClass[]) {
+ RootClass[] rootClasses = (RootClass[])obj;
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < rootClasses.length; i++) {
names.add(getItemName(rootClasses[i]));
}
// sort to get same name for same combinations of entities
Collections.sort(names);
- String name = names.size() > 0 ? names.get(0) : ""; //$NON-NLS-1$
+ name = names.size() > 0 ? names.get(0) : ""; //$NON-NLS-1$
for (int i = 1; i < rootClasses.length; i++) {
name += " & " + names.get(i); //$NON-NLS-1$
}
- setPartName(DiagramViewerMessages.DiagramViewer_diagram_for + " " + name);
//$NON-NLS-1$
- ormDiagram = new OrmDiagram(configuration, rootClasses);
}
- super.setInput(input);
- loadProperties();
+ return name;
}
+ public String getDiagramName() {
+ IEditorInput input = getEditorInput();
+ Object obj = null;
+ if (input instanceof ObjectEditorInput) {
+ ObjectEditorInput objectEditorInput = (ObjectEditorInput)input;
+ obj = objectEditorInput.getObject();
+ }
+ return getDiagramName(obj);
+ }
+
/**
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
*/
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/ExportImageAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/ExportImageAction.java 2009-09-24
14:56:02 UTC (rev 17717)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/ExportImageAction.java 2009-09-24
17:13:17 UTC (rev 17718)
@@ -13,7 +13,12 @@
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.SWTGraphics;
@@ -21,6 +26,7 @@
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device;
@@ -28,7 +34,8 @@
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
-import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
+import org.eclipse.ui.dialogs.SaveAsDialog;
import org.hibernate.eclipse.console.HibernateConsolePlugin;
import org.jboss.tools.hibernate.ui.diagram.DiagramViewerMessages;
import org.jboss.tools.hibernate.ui.diagram.editors.DiagramViewer;
@@ -40,7 +47,8 @@
public static final String[] dialogFilterNames = new String[] {
DiagramViewerMessages.ExportImageAction_png_format,
DiagramViewerMessages.ExportImageAction_jpg_format,
DiagramViewerMessages.ExportImageAction_bmp_format };
- private FileDialog saveDialog = null;
+ //private FileDialog saveDialog = null;
+ private SaveAsDialog saveDialog = null;
private boolean showErrDialog = true;
public static final ImageDescriptor img =
ImageDescriptor.createFromFile(DiagramViewer.class, "icons/export.png");
//$NON-NLS-1$
@@ -58,7 +66,7 @@
*
* @param saveDialog
*/
- public void setSaveDialog(FileDialog saveDialog) {
+ public void setSaveDialog(SaveAsDialog saveDialog) {
this.saveDialog = saveDialog;
}
@@ -68,49 +76,76 @@
public void run() {
+ boolean createdSaveDialog = false;
if (saveDialog == null) {
- saveDialog = new FileDialog(
- getDiagramViewer().getSite().getShell(), SWT.SAVE);
+ saveDialog = new
SaveAsDialog(getDiagramViewer().getSite().getWorkbenchWindow().getShell());
+ createdSaveDialog = true;
}
- saveDialog.setFilterExtensions(dialogFilterExtensions);
- saveDialog.setFilterNames(dialogFilterNames);
-
- String filePath = saveDialog.open();
- saveDialog = null;
- if (filePath == null || filePath.trim().length() == 0) {
+ saveDialog.setOriginalName(getDiagramViewer().getDiagramName());
+ saveDialog.open();
+ final IPath pathSave = saveDialog.getResult();
+ if (pathSave == null) {
return;
}
- IFigure fig = ((ScalableFreeformRootEditPart) getDiagramViewer()
+ final IFigure fig = ((ScalableFreeformRootEditPart) getDiagramViewer()
.getEditPartViewer().getRootEditPart())
.getLayer(LayerConstants.PRINTABLE_LAYERS);
- int imageType = SWT.IMAGE_BMP;
- if (filePath.toLowerCase().endsWith(".jpg")) { //$NON-NLS-1$
- imageType = SWT.IMAGE_JPEG;
- } else if (filePath.toLowerCase().endsWith(".png")) { //$NON-NLS-1$
- imageType = SWT.IMAGE_PNG;
- }
- FileOutputStream outStream = null;
- try {
- byte[] imageData = createImage(fig, imageType);
- outStream = new FileOutputStream(filePath);
- outStream.write(imageData);
- outStream.flush();
- } catch (IOException e) {
- HibernateConsolePlugin.getDefault().logErrorMessage("ExportImageAction", e);
//$NON-NLS-1$
- if (showErrDialog) {
- MessageDialog.openInformation(getDiagramViewer().getSite().getShell(),
- DiagramViewerMessages.ExportImageAction_error,
DiagramViewerMessages.ExportImageAction_failed_to_export_image + e.getMessage());
+ int imageTypeTmp = SWT.IMAGE_BMP;
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ String ext = pathSave.getFileExtension();
+ if (ext != null) {
+ ext = ext.toLowerCase();
+ if (ext.endsWith("jpg")) { //$NON-NLS-1$
+ imageTypeTmp = SWT.IMAGE_JPEG;
+ } else if (ext.endsWith("png")) { //$NON-NLS-1$
+ imageTypeTmp = SWT.IMAGE_PNG;
+ } else if (ext.endsWith("gif")) { //$NON-NLS-1$
+ imageTypeTmp = SWT.IMAGE_GIF;
+ } else if (ext.endsWith("bmp")) { //$NON-NLS-1$
+ imageTypeTmp = SWT.IMAGE_BMP;
}
}
- finally {
- if (outStream != null) {
+ IPath pathTmp = workspace.getRoot().getFullPath().append(pathSave);
+ pathTmp = workspace.getRoot().getLocation().append(pathTmp);
+ if (ext == null) {
+ pathTmp = pathTmp.addFileExtension("bmp"); //$NON-NLS-1$
+ }
+ final IPath path = pathTmp;
+ final int imageType = imageTypeTmp;
+
+ WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
+ public void execute(final IProgressMonitor monitor) {
+ FileOutputStream outStream = null;
try {
- outStream.close();
+ byte[] imageData = createImage(fig, imageType);
+ outStream = new FileOutputStream(path.toString());
+ outStream.write(imageData);
+ outStream.flush();
} catch (IOException e) {
- // ignore
+ HibernateConsolePlugin.getDefault().logErrorMessage("ExportImageAction",
e); //$NON-NLS-1$
+ if (showErrDialog) {
+ MessageDialog.openInformation(getDiagramViewer().getSite().getShell(),
+ DiagramViewerMessages.ExportImageAction_error,
DiagramViewerMessages.ExportImageAction_failed_to_export_image + e.getMessage());
+ }
}
+ finally {
+ if (outStream != null) {
+ try {
+ outStream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
}
+ };
+ try {
+ new ProgressMonitorDialog(
+ createdSaveDialog ? getDiagramViewer().getSite().getWorkbenchWindow().getShell() :
null)
+ .run(false, true, op);
+ } catch (InvocationTargetException e) {
+ } catch (InterruptedException e) {
}
}
Modified:
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/META-INF/MANIFEST.MF
===================================================================
---
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/META-INF/MANIFEST.MF 2009-09-24
14:56:02 UTC (rev 17717)
+++
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/META-INF/MANIFEST.MF 2009-09-24
17:13:17 UTC (rev 17718)
@@ -7,6 +7,7 @@
org.eclipse.wst.common.project.facet.ui;bundle-version="1.3.0",
org.eclipse.jdt.core,
org.eclipse.gef,
+ org.eclipse.ui.ide,
org.hibernate.eclipse.console.test,
org.jboss.tools.hibernate.ui,
org.hibernate.eclipse
Modified:
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/test/ExportImageActionTest.java
===================================================================
---
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/test/ExportImageActionTest.java 2009-09-24
14:56:02 UTC (rev 17717)
+++
trunk/hibernatetools/tests/org.jboss.tools.hibernate.ui.test/src/org/jboss/tools/hibernate/ui/diagram/editors/actions/test/ExportImageActionTest.java 2009-09-24
17:13:17 UTC (rev 17718)
@@ -12,6 +12,10 @@
import java.io.File;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.geometry.Rectangle;
@@ -20,7 +24,7 @@
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.ui.dialogs.SaveAsDialog;
import org.jboss.tools.hibernate.ui.diagram.editors.DiagramViewer;
import org.jboss.tools.hibernate.ui.diagram.editors.actions.ExportImageAction;
import org.jmock.Expectations;
@@ -44,24 +48,30 @@
public void testAction() {
- final DiagramViewer editor = context.mock(DiagramViewer.class);;
- final FileDialog saveDialog = context.mock(FileDialog.class);;
- final GraphicalViewer graphicalViewer = context.mock(GraphicalViewer.class);;
- final ScalableFreeformRootEditPart scalableFreeformRootEditPart =
context.mock(ScalableFreeformRootEditPart.class);;
- final IFigure figure = context.mock(IFigure.class);;
- final Control control = context.mock(Control.class);;
- final Display display = context.mock(Display.class);;
+ final DiagramViewer editor = context.mock(DiagramViewer.class);
+ final SaveAsDialog saveDialog = context.mock(SaveAsDialog.class);
+ final GraphicalViewer graphicalViewer = context.mock(GraphicalViewer.class);
+ final ScalableFreeformRootEditPart scalableFreeformRootEditPart =
context.mock(ScalableFreeformRootEditPart.class);
+ final IFigure figure = context.mock(IFigure.class);
+ final Control control = context.mock(Control.class);
+ final Display display = context.mock(Display.class);
final Rectangle rectangle = new Rectangle(0, 0, 20, 10);
final String filePath = "test.jpg"; //$NON-NLS-1$
+ final IPath resPath = new Path(filePath);
context.checking(new Expectations() {
{
- allowing(saveDialog).setFilterExtensions(ExportImageAction.dialogFilterExtensions);
- allowing(saveDialog).setFilterNames(ExportImageAction.dialogFilterNames);
+ allowing(editor).getDiagramName();
+ will(returnValue(filePath));
+ allowing(saveDialog).setOriginalName(filePath);
+
oneOf(saveDialog).open();
- will(returnValue(filePath));
+ will(returnValue(0));
+ oneOf(saveDialog).getResult();
+ will(returnValue(resPath));
+
allowing(editor).getSite();
will(returnValue(null));
@@ -99,7 +109,10 @@
exportImageAction.setShowErrDialog(false);
exportImageAction.run();
// test is the file created
- File file = new File(filePath);
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IPath path = workspace.getRoot().getFullPath().append(resPath);
+ path = workspace.getRoot().getLocation().append(path);
+ File file = path.toFile();
assertTrue(file.exists() && file.isFile());
//
boolean res = file.delete();