001: package net.sourceforge.squirrel_sql.plugins.graph.graphtofiles;
002:
003: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
004: import net.sourceforge.squirrel_sql.fw.util.StringManager;
005: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
006: import net.sourceforge.squirrel_sql.fw.util.ExtensionFilter;
007:
008: import javax.imageio.ImageIO;
009: import javax.swing.*;
010: import java.awt.image.BufferedImage;
011: import java.awt.event.*;
012: import java.io.File;
013: import java.io.IOException;
014: import java.util.prefs.Preferences;
015:
016: public class GraphToFilesCtrlr {
017: GraphToFilesDlg _dlg;
018:
019: private static final String PREF_KEY_LAST_IMAGE_DIR = "SquirrelSQL.graph.lastImageDir";
020:
021: private static final StringManager s_stringMgr = StringManagerFactory
022: .getStringManager(GraphToFilesCtrlr.class);
023: private BufferedImage[] _images;
024: private MainFrame _mainFrame;
025:
026: public GraphToFilesCtrlr(BufferedImage[] images, MainFrame mainFrame) {
027: _images = images;
028: _mainFrame = mainFrame;
029: _dlg = new GraphToFilesDlg(mainFrame, images);
030:
031: _dlg.btnClose.addActionListener(new ActionListener() {
032: public void actionPerformed(ActionEvent e) {
033: onClosing();
034: _dlg.setVisible(false);
035: _dlg.dispose();
036: }
037: });
038:
039: _dlg.btnSaveToFile.addActionListener(new ActionListener() {
040: public void actionPerformed(ActionEvent e) {
041: onSaveToFile();
042: }
043: });
044:
045: _dlg.addWindowListener(new WindowAdapter() {
046:
047: public void windowClosing(WindowEvent e) {
048: onClosing();
049: }
050: });
051: }
052:
053: private void onClosing() {
054: for (int i = 0; i < _images.length; i++) {
055: _images[i].flush();
056: }
057: }
058:
059: private void onSaveToFile() {
060: try {
061: String lastDir = Preferences.userRoot().get(
062: PREF_KEY_LAST_IMAGE_DIR,
063: System.getProperty("user.home"));
064: JFileChooser fc = new JFileChooser(lastDir);
065: // i18n[graphToFile.fileChooserTitle=Save image file(s)]
066: fc.setDialogTitle(s_stringMgr
067: .getString("graphToFile.fileChooserTitle"));
068:
069: if (1 < _images.length) {
070: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
071: } else {
072: fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
073:
074: ExtensionFilter ef = new ExtensionFilter();
075: // i18n[graphToFile.ImageFileSpec=JPG image format]
076: ef.addExtension(s_stringMgr
077: .getString("graphToFile.ImageFileSpec"), "jpg");
078: fc.setFileFilter(ef);
079: }
080:
081: if (fc.showSaveDialog(_mainFrame) == JFileChooser.APPROVE_OPTION) {
082: File selectedFile = fc.getSelectedFile();
083: if (null != selectedFile) {
084: if (1 == _images.length) {
085: if (false == selectedFile.getPath()
086: .toUpperCase().endsWith(".JPG")) {
087: selectedFile = new File(selectedFile
088: .getPath()
089: + ".jpg");
090: }
091: ImageIO.write(_images[0], "jpg", selectedFile);
092:
093: Preferences.userRoot().put(
094: PREF_KEY_LAST_IMAGE_DIR,
095: selectedFile.getParent());
096:
097: } else {
098: selectedFile.mkdirs();
099:
100: for (int i = 0; i < _images.length; i++) {
101: File f = new File(selectedFile, "Page_"
102: + (i + 1) + ".jpg");
103: ImageIO.write(_images[i], "jpg", f);
104: }
105: Preferences.userRoot().put(
106: PREF_KEY_LAST_IMAGE_DIR,
107: selectedFile.getPath());
108: }
109: }
110:
111: }
112: } catch (IOException e) {
113: throw new RuntimeException(e);
114: }
115: }
116: }
|