X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=ekit%2Fcom%2Fhexidec%2Fekit%2Fcomponent%2FImageFileChooserPreview.java;h=2ff8d903b5d91e5bfcf445ca9d7031cde9359035;hb=6ce136dad31e39a1684a657ed5c9b33fba069623;hp=307aa7e1567ff6357ef9027c7dd2bb55e1fed447;hpb=6dd702802493556cff5e55eb4982d23e79a30832;p=old-projects.git diff --git a/ekit/com/hexidec/ekit/component/ImageFileChooserPreview.java b/ekit/com/hexidec/ekit/component/ImageFileChooserPreview.java index 307aa7e..2ff8d90 100644 --- a/ekit/com/hexidec/ekit/component/ImageFileChooserPreview.java +++ b/ekit/com/hexidec/ekit/component/ImageFileChooserPreview.java @@ -1,126 +1,126 @@ -/* -GNU Lesser General Public License - -ImageFileChooserPreview -Copyright (C) 2000-2002 Frits Jalvingh & Howard Kistler - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -This library 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 -Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -package com.hexidec.ekit.component; - -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Image; -import java.io.File; -import javax.swing.ImageIcon; -import javax.swing.JComponent; -import javax.swing.JFileChooser; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; - -/** Class provides a preview window for the selected image file - */ -class ImageFileChooserPreview extends JComponent implements PropertyChangeListener -{ - private static final int previewWidth = 100; - private static final int previewHeight = 100; - - private ImageIcon imageThumb = null; - private File imageFile = null; - - /** This class requires a file chooser to register with so this class will - * be notified when a new file is selected in the browser. - * @param JFileChooser that this preview window is used in. - */ - public ImageFileChooserPreview(JFileChooser parent) - { - setPreferredSize(new Dimension(previewWidth , previewHeight)); - parent.addPropertyChangeListener(this); - } - - /** Loads a new image into the preview window, and scales it if necessary. - */ - public void loadImage() - { - if(imageFile == null) - { - imageThumb = null; - return; - } - imageThumb = new ImageIcon(imageFile.getPath()); - - // Check if thumb requires scaling - if(imageThumb.getIconHeight() < previewHeight && imageThumb.getIconWidth() < previewWidth) - { - return; - } - int w = previewWidth; - int h = previewHeight; - if(imageThumb.getIconHeight() > imageThumb.getIconWidth()) - { - w = -1; - } - else - { - h = -1; - } - imageThumb = new ImageIcon(imageThumb.getImage().getScaledInstance(w, h, Image.SCALE_DEFAULT)); - } - - /** Callback (event handler) to indicate that a property of the - * JFileChooser has changed. If the selected file has changed cause a new - * thumbnail to load. - */ - public void propertyChange(PropertyChangeEvent e) - { - if(e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) - { - imageFile = (File)e.getNewValue(); - if(isShowing()) - { - loadImage(); - repaint(); - } - } - } - - /** Paints the icon of the current image, if one's present.. - * @param Graphics object to use when painting the component. - */ - public void paintComponent(Graphics g) - { - if(imageThumb == null) - { - loadImage(); - } - if(imageThumb == null) - { - return; - } - int x = (getWidth() - imageThumb.getIconWidth()) / 2; - int y = (getHeight() - imageThumb.getIconHeight()) / 2; - if(y < 0) - { - y = 0; - } - if(x < 5) - { - x = 5; - } - imageThumb.paintIcon(this, g, x, y); - } -} - +/* +GNU Lesser General Public License + +ImageFileChooserPreview +Copyright (C) 2000 Frits Jalvingh & Howard Kistler + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library 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 +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +package com.hexidec.ekit.component; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Image; +import java.io.File; +import javax.swing.ImageIcon; +import javax.swing.JComponent; +import javax.swing.JFileChooser; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +/** Class provides a preview window for the selected image file + */ +class ImageFileChooserPreview extends JComponent implements PropertyChangeListener +{ + private static final int previewWidth = 100; + private static final int previewHeight = 100; + + private ImageIcon imageThumb = null; + private File imageFile = null; + + /** This class requires a file chooser to register with so this class will + * be notified when a new file is selected in the browser. + * @param JFileChooser that this preview window is used in. + */ + public ImageFileChooserPreview(JFileChooser parent) + { + setPreferredSize(new Dimension(previewWidth , previewHeight)); + parent.addPropertyChangeListener(this); + } + + /** Loads a new image into the preview window, and scales it if necessary. + */ + public void loadImage() + { + if(imageFile == null) + { + imageThumb = null; + return; + } + imageThumb = new ImageIcon(imageFile.getPath()); + + // Check if thumb requires scaling + if(imageThumb.getIconHeight() < previewHeight && imageThumb.getIconWidth() < previewWidth) + { + return; + } + int w = previewWidth; + int h = previewHeight; + if(imageThumb.getIconHeight() > imageThumb.getIconWidth()) + { + w = -1; + } + else + { + h = -1; + } + imageThumb = new ImageIcon(imageThumb.getImage().getScaledInstance(w, h, Image.SCALE_DEFAULT)); + } + + /** Callback (event handler) to indicate that a property of the + * JFileChooser has changed. If the selected file has changed cause a new + * thumbnail to load. + */ + public void propertyChange(PropertyChangeEvent e) + { + if(e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) + { + imageFile = (File)e.getNewValue(); + if(isShowing()) + { + loadImage(); + repaint(); + } + } + } + + /** Paints the icon of the current image, if one's present.. + * @param Graphics object to use when painting the component. + */ + public void paintComponent(Graphics g) + { + if(imageThumb == null) + { + loadImage(); + } + if(imageThumb == null) + { + return; + } + int x = (getWidth() - imageThumb.getIconWidth()) / 2; + int y = (getHeight() - imageThumb.getIconHeight()) / 2; + if(y < 0) + { + y = 0; + } + if(x < 5) + { + x = 5; + } + imageThumb.paintIcon(this, g, x, y); + } +} +