/*
* Copyright Jan 15, 2010 John T. Langton
* email: jlangton at visitrend dot com
* www.visitrend.com
*
* License: GPLv2 or (at your option) any later GPL version
*
* This file is part of NDVis.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* There should be a copy of the GNU General Public License applied to
* NDVis in the file "NDVis-license" in the folder "license". If not, see
* <http://www.gnu.org/licenses/> or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.visitrend.ndvis.persistence;
import com.visitrend.ndvis.app.NDVis;
import com.visitrend.ndvis.gui.spi.DataVisualization;
import com.visitrend.ndvis.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.openide.cookies.SaveCookie;
import org.openide.util.lookup.ServiceProvider;
@ServiceProvider(service=SaveCookie.class)
public class SavePngImageAction implements SaveCookie {
private NDVis owner;
public SavePngImageAction() {
this.owner = NDVis.getDefault();
}
@Override
public void save() throws IOException {
Preferences preferences = Preferences.userNodeForPackage(getClass());
String path = preferences.get("img-path", "");
JFileChooser chooser = new JFileChooser(path);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(DimStackImageFileFilter.getInstance());
DataVisualization dataVis = owner.getActiveDataVisualization();
int returnVal = chooser.showSaveDialog(dataVis.getTopComponent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
File img = chooser.getSelectedFile();
// save path info for future file operations
if (img.isDirectory()) {
preferences.put("img-path", img.getAbsolutePath());
} else {
preferences.put("img-path", img.getParent());
}
// handle if file already exists
if (img.exists()) {
int answer = JOptionPane.showConfirmDialog(null,
"Overwrite existing file?");
if (answer != JOptionPane.OK_OPTION) {
return;
}
} else {
// If this is a new file, make sure
// it has the necessary file ending
if (FileUtils.getExtension(img) == null) {
javax.swing.filechooser.FileFilter filter = chooser.getFileFilter();
String str = img.getAbsolutePath()
+ filter.getDescription();
img = new File(str);
}
}
writeFile(img);
dataVis.fireSaveStateChange(false);
}
}
private void writeFile(File img) {
try {
// Save as PNG
ImageIO.write(owner.getActiveDataVisualization().getOffScreenImage(),
"png", img);
} catch (IOException e) {
e.printStackTrace();
}
}
}