| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.component; |
| 16 | |
| 17 | import java.awt.image.RenderedImage; |
| 18 | import java.io.File; |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | import javax.imageio.ImageTypeSpecifier; |
| 22 | import javax.imageio.ImageWriteParam; |
| 23 | import javax.imageio.ImageWriter; |
| 24 | import javax.swing.JButton; |
| 25 | import javax.swing.JComponent; |
| 26 | import javax.swing.JFileChooser; |
| 27 | |
| 28 | import org.jdesktop.application.ApplicationContext; |
| 29 | import org.jdesktop.application.ResourceMap; |
| 30 | |
| 31 | import net.sourceforge.hivegui.dialog.DialogPanel; |
| 32 | import net.sourceforge.hivegui.dialog.GenericDialog; |
| 33 | import net.sourceforge.hivegui.imaging.ImageIOHelper; |
| 34 | |
| 35 | /** |
| 36 | * Extended File Chooser for saving images with a selectable format. |
| 37 | * Available formats are those provided as plugins through the ImageIO API. |
| 38 | * <p> |
| 39 | * Additional formats are available in JAI ImageI/O Tools available at |
| 40 | * java.sun.com. |
| 41 | * |
| 42 | * @author Jean-Francois Poilpret |
| 43 | */ |
| 44 | public class ImageSaveChooser extends JFileChooser implements DialogPanel |
| 45 | { |
| 46 | public ImageSaveChooser() |
| 47 | { |
| 48 | this(null); |
| 49 | } |
| 50 | |
| 51 | public ImageSaveChooser(OverwriteFileConfirmer overwriteFileConfirm) |
| 52 | { |
| 53 | super(); |
| 54 | setName("image-save-chooser"); |
| 55 | _overwriteFileConfirm = overwriteFileConfirm; |
| 56 | } |
| 57 | |
| 58 | public void setQualityDetailUnits(boolean detail) |
| 59 | { |
| 60 | _detail = detail; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Filters the list of possible save formats according to the passed image |
| 65 | * type. This method must be called at least once before first display, and |
| 66 | * normally every time the type of the image to be saved will change (the |
| 67 | * type is based only on the ColorModel of the image). |
| 68 | * <p> |
| 69 | * This method is automatically called by <code>setImage</code>. |
| 70 | */ |
| 71 | public void setImageType(ImageTypeSpecifier imageType) |
| 72 | { |
| 73 | _settings = new ImageWriterSettingsPanel(imageType, _detail); |
| 74 | setAccessory(_settings); |
| 75 | // Set list of filters based on acceptable extensions |
| 76 | String[] extensions = ImageIOHelper.getAcceptedExtensions(imageType); |
| 77 | resetChoosableFileFilters(); |
| 78 | addChoosableFileFilter(new ExtensionsFileFilter(extensions)); |
| 79 | } |
| 80 | |
| 81 | public void setOverwriteFileConfirmer(OverwriteFileConfirmer overwriteFileConfirm) |
| 82 | { |
| 83 | _overwriteFileConfirm = overwriteFileConfirm; |
| 84 | } |
| 85 | |
| 86 | public boolean canClose() |
| 87 | { |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | public JButton getDefaultButton() |
| 92 | { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | public JComponent getPanel() |
| 97 | { |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | public void reset() |
| 102 | { |
| 103 | // Do nothing (because everything is set before this method is called) |
| 104 | } |
| 105 | |
| 106 | public void setContext(ApplicationContext context) |
| 107 | { |
| 108 | ResourceMap map = context.getResourceMap(getClass()); |
| 109 | map.injectComponents(this); |
| 110 | map.injectFields(this); |
| 111 | } |
| 112 | |
| 113 | public void setParent(GenericDialog dialog) |
| 114 | { |
| 115 | _dialog = dialog; |
| 116 | } |
| 117 | |
| 118 | // Overridden to add a correct extension to the file name |
| 119 | @Override public File getSelectedFile() |
| 120 | { |
| 121 | File file = super.getSelectedFile(); |
| 122 | if (_settings == null || file == null) |
| 123 | { |
| 124 | return file; |
| 125 | } |
| 126 | // Add correct extension to file |
| 127 | String[] extensions = ImageIOHelper.getWriterExtensions(getImageWriter()); |
| 128 | if (extensions != null) |
| 129 | { |
| 130 | int index = file.getName().lastIndexOf("."); |
| 131 | String name = null; |
| 132 | if (index >= 0) |
| 133 | { |
| 134 | String ext = file.getName().substring(index + 1).toLowerCase(); |
| 135 | if (Arrays.binarySearch(extensions, ext) < 0) |
| 136 | { |
| 137 | name = file.getName().substring(0, index); |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | name = file.getName(); |
| 143 | } |
| 144 | if (name != null) |
| 145 | { |
| 146 | file = new File(file.getParent(), name + "." + extensions[0]); |
| 147 | } |
| 148 | } |
| 149 | return file; |
| 150 | } |
| 151 | |
| 152 | // Overridden to ask file overwriting confirmation when needed |
| 153 | @Override public void approveSelection() |
| 154 | { |
| 155 | File file = getSelectedFile(); |
| 156 | if ( (!file.exists()) |
| 157 | || ( _overwriteFileConfirm != null |
| 158 | && _overwriteFileConfirm.confirm(file))) |
| 159 | { |
| 160 | super.approveSelection(); |
| 161 | _dialog.close(false); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override public void cancelSelection() |
| 166 | { |
| 167 | super.cancelSelection(); |
| 168 | _dialog.close(true); |
| 169 | } |
| 170 | |
| 171 | public void setImage(RenderedImage image) |
| 172 | { |
| 173 | setImageType(new ImageTypeSpecifier(image)); |
| 174 | _settings.setImage(image); |
| 175 | } |
| 176 | |
| 177 | public ImageWriter getImageWriter() |
| 178 | { |
| 179 | return _settings.getImageWriter(); |
| 180 | } |
| 181 | |
| 182 | public ImageWriteParam getImageWriteParam() |
| 183 | { |
| 184 | return _settings.getParameters(); |
| 185 | } |
| 186 | |
| 187 | // Fix for Java bug #6317789 (sporadic hang in JFileChooser on Windows) |
| 188 | @Override public void updateUI() |
| 189 | { |
| 190 | putClientProperty("FileChooser.useShellFolder", Boolean.FALSE); |
| 191 | super.updateUI(); |
| 192 | } |
| 193 | |
| 194 | private boolean _detail; |
| 195 | private OverwriteFileConfirmer _overwriteFileConfirm; |
| 196 | private ImageWriterSettingsPanel _settings; |
| 197 | private GenericDialog _dialog; |
| 198 | } |