001: package com.vividsolutions.jump.workbench.ui.plugin;
002:
003: import java.awt.image.BufferedImage;
004: import java.awt.image.RenderedImage;
005: import java.io.File;
006: import java.io.IOException;
007: import java.util.*;
008: import java.util.Arrays;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012:
013: import javax.imageio.ImageIO;
014: import javax.swing.JComponent;
015: import javax.swing.JFileChooser;
016: import javax.swing.filechooser.FileFilter;
017:
018: import com.vividsolutions.jts.util.Assert;
019: import com.vividsolutions.jump.I18N;
020: import com.vividsolutions.jump.workbench.WorkbenchContext;
021: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
022: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
023: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
024: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
025: import com.vividsolutions.jump.workbench.ui.GUIUtil;
026:
027: public class SaveImageAsPlugIn extends ExportImagePlugIn {
028: //ImageIO doesn't know about the "gif" format. I guess it's a copyright
029: // issue [Jon Aquino 11/6/2003]
030: //Don't use TYPE_INT_ARGB for jpegs -- they will turn pink [Jon Aquino
031: // 11/6/2003]
032:
033: private List myFileFilters = Arrays.asList(new Object[] {
034: createFileFilter("PNG - Portable Network Graphics", "png",
035: BufferedImage.TYPE_INT_ARGB),
036: createFileFilter("JPEG - Joint Photographic Experts Group",
037: "jpg", BufferedImage.TYPE_INT_RGB) });
038:
039: private JFileChooser fileChooser = null;
040: private WorkbenchContext workbenchContext;
041:
042: private JFileChooser getFileChooser() {
043: if (fileChooser == null) {
044: fileChooser = new GUIUtil.FileChooserWithOverwritePrompting() {
045: protected File selectedFile() {
046: return new File(addExtension(super .selectedFile()
047: .getPath(),
048: ((MyFileFilter) getFileFilter())
049: .getFormat()));
050: }
051: };
052: fileChooser.setDialogTitle(I18N
053: .get("ui.plugin.SaveImageAsPlugIn.save-image"));
054: //Remove *.* [Jon Aquino 11/6/2003]
055: GUIUtil.removeChoosableFileFilters(fileChooser);
056: Map formatToFileFilterMap = new HashMap();
057: for (Iterator i = myFileFilters.iterator(); i.hasNext();) {
058: MyFileFilter fileFilter = (MyFileFilter) i.next();
059: fileChooser.addChoosableFileFilter(fileFilter);
060: formatToFileFilterMap.put(fileFilter.getFormat(),
061: fileFilter);
062: }
063: String lastFilename = (String) PersistentBlackboardPlugIn
064: .get(workbenchContext).get(LAST_FILENAME_KEY);
065: if (lastFilename != null) {
066: fileChooser.setSelectedFile(new File(lastFilename));
067: }
068: fileChooser
069: .setFileFilter((FileFilter) formatToFileFilterMap
070: .get(PersistentBlackboardPlugIn.get(
071: workbenchContext).get(FORMAT_KEY,
072: "png")));
073: }
074: return fileChooser;
075: }
076:
077: private MyFileFilter createFileFilter(String description,
078: String format, int bufferedmageType) {
079: return new MyFileFilter(description, format);
080: }
081:
082: private static class MyFileFilter extends FileFilter {
083: private FileFilter fileFilter;
084: private String format;
085:
086: public MyFileFilter(String description, String format) {
087: fileFilter = GUIUtil.createFileFilter(description,
088: new String[] { format });
089: this .format = format;
090: }
091:
092: public boolean accept(File f) {
093: return fileFilter.accept(f);
094: }
095:
096: public String getDescription() {
097: return fileFilter.getDescription();
098: }
099:
100: public String getFormat() {
101: return format;
102: }
103: }
104:
105: private static final String FORMAT_KEY = "FORMAT";
106: private static final String LAST_FILENAME_KEY = "LAST FILENAME";
107:
108: public boolean execute(PlugInContext context) throws Exception {
109: this .workbenchContext = context.getWorkbenchContext();
110: if (JFileChooser.APPROVE_OPTION != getFileChooser()
111: .showSaveDialog(context.getWorkbenchFrame())) {
112: return false;
113: }
114: MyFileFilter fileFilter = (MyFileFilter) getFileChooser()
115: .getFileFilter();
116: BufferedImage image = image(context.getLayerViewPanel());
117: String filename = addExtension(getFileChooser()
118: .getSelectedFile().getPath(), fileFilter.getFormat());
119: save(image, fileFilter.getFormat(), new File(filename));
120: PersistentBlackboardPlugIn.get(workbenchContext).put(
121: FORMAT_KEY, fileFilter.getFormat());
122: PersistentBlackboardPlugIn.get(workbenchContext).put(
123: LAST_FILENAME_KEY, filename);
124: return true;
125: }
126:
127: private void save(RenderedImage image, String format, File file)
128: throws IOException {
129: boolean writerFound = ImageIO.write(image, format, file);
130: Assert
131: .isTrue(
132: writerFound,
133: I18N
134: .get("ui.plugin.SaveImageAsPlugIn.cannot-find-writer-for-image-format")
135: + " '" + format + "'");
136: }
137:
138: public static MultiEnableCheck createEnableCheck(
139: WorkbenchContext workbenchContext) {
140: EnableCheckFactory checkFactory = new EnableCheckFactory(
141: workbenchContext);
142: return new MultiEnableCheck()
143: .add(
144: checkFactory
145: .createWindowWithLayerViewPanelMustBeActiveCheck())
146: .add(new EnableCheck() {
147: public String check(JComponent component) {
148: //Need Java 1.4's ImageIO class [Jon Aquino 11/6/2003]
149: return !java14OrNewer() ? "This feature requires Java 1.4 or newer"
150: : null;
151: }
152: });
153: }
154:
155: private String addExtension(String path, String extension) {
156: if (path.toUpperCase().endsWith(extension.toUpperCase())) {
157: return path;
158: }
159: if (path.endsWith(".")) {
160: return path + extension;
161: }
162: return path + "." + extension;
163: }
164: }
|