BasicGraphEditor editor = getEditor(e);
if (editor != null)
{
mxGraphComponent graphComponent = editor.getGraphComponent();
mxGraph graph = graphComponent.getGraph();
FileFilter selectedFilter = null;
DefaultFileFilter xmlPngFilter = new DefaultFileFilter(".png",
"PNG+XML " + mxResources.get("file") + " (.png)");
FileFilter vmlFileFilter = new DefaultFileFilter(".html",
"VML " + mxResources.get("file") + " (.html)");
String filename = null;
boolean dialogShown = false;
if (showDialog || editor.getCurrentFile() == null)
{
String wd;
if (lastDir != null)
{
wd = lastDir;
}
else if (editor.getCurrentFile() != null)
{
wd = editor.getCurrentFile().getParent();
}
else
{
wd = System.getProperty("user.dir");
}
JFileChooser fc = new JFileChooser(wd);
// Adds the default file format
FileFilter defaultFilter = xmlPngFilter;
fc.addChoosableFileFilter(defaultFilter);
// Adds special vector graphics formats and HTML
fc.addChoosableFileFilter(new DefaultFileFilter(".mxe",
"mxGraph Editor " + mxResources.get("file")
+ " (.mxe)"));
fc.addChoosableFileFilter(new DefaultFileFilter(".txt",
"Graph Drawing " + mxResources.get("file")
+ " (.txt)"));
fc.addChoosableFileFilter(new DefaultFileFilter(".svg",
"SVG " + mxResources.get("file") + " (.svg)"));
fc.addChoosableFileFilter(vmlFileFilter);
fc.addChoosableFileFilter(new DefaultFileFilter(".html",
"HTML " + mxResources.get("file") + " (.html)"));
// Adds a filter for each supported image format
Object[] imageFormats = ImageIO.getReaderFormatNames();
// Finds all distinct extensions
HashSet<String> formats = new HashSet<String>();
for (int i = 0; i < imageFormats.length; i++)
{
String ext = imageFormats[i].toString().toLowerCase();
formats.add(ext);
}
imageFormats = formats.toArray();
for (int i = 0; i < imageFormats.length; i++)
{
String ext = imageFormats[i].toString();
fc.addChoosableFileFilter(new DefaultFileFilter("."
+ ext, ext.toUpperCase() + " "
+ mxResources.get("file") + " (." + ext + ")"));
}
// Adds filter that accepts all supported image formats
fc.addChoosableFileFilter(new DefaultFileFilter.ImageFileFilter(
mxResources.get("allImages")));
fc.setFileFilter(defaultFilter);
int rc = fc.showDialog(null, mxResources.get("save"));
dialogShown = true;
if (rc != JFileChooser.APPROVE_OPTION)
{
return;
}
else
{
lastDir = fc.getSelectedFile().getParent();
}
filename = fc.getSelectedFile().getAbsolutePath();
selectedFilter = fc.getFileFilter();
if (selectedFilter instanceof DefaultFileFilter)
{
String ext = ((DefaultFileFilter) selectedFilter)
.getExtension();
if (!filename.toLowerCase().endsWith(ext))
{
filename += ext;
}
}
if (new File(filename).exists()
&& JOptionPane.showConfirmDialog(graphComponent,
mxResources.get("overwriteExistingFile")) != JOptionPane.YES_OPTION)
{
return;
}
}
else
{
filename = editor.getCurrentFile().getAbsolutePath();
}
try
{
String ext = filename
.substring(filename.lastIndexOf('.') + 1);
if (ext.equalsIgnoreCase("svg"))
{
mxSvgCanvas canvas = (mxSvgCanvas) mxCellRenderer
.drawCells(graph, null, 1, null,
new CanvasFactory()
{
public mxICanvas createCanvas(
int width, int height)
{
mxSvgCanvas canvas = new mxSvgCanvas(
mxUtils.createSvgDocument(
width, height));
canvas.setEmbedded(true);
return canvas;
}
});
mxUtils.writeFile(mxUtils.getXml(canvas.getDocument()),
filename);
}
else if (selectedFilter == vmlFileFilter)
{
mxUtils.writeFile(mxUtils.getXml(mxCellRenderer
.createVmlDocument(graph, null, 1, null, null)
.getDocumentElement()), filename);
}
else if (ext.equalsIgnoreCase("html"))
{
mxUtils.writeFile(mxUtils.getXml(mxCellRenderer
.createHtmlDocument(graph, null, 1, null, null)
.getDocumentElement()), filename);
}
else if (ext.equalsIgnoreCase("mxe")
|| ext.equalsIgnoreCase("xml"))
{
mxCodec codec = new mxCodec();
String xml = mxUtils.getXml(codec.encode(graph
.getModel()));
mxUtils.writeFile(xml, filename);
editor.setModified(false);