Initial revision
[old-projects.git] / ekit / com / hexidec / ekit / component / ImageFileChooserPreview.java
diff --git a/ekit/com/hexidec/ekit/component/ImageFileChooserPreview.java b/ekit/com/hexidec/ekit/component/ImageFileChooserPreview.java
new file mode 100644 (file)
index 0000000..307aa7e
--- /dev/null
@@ -0,0 +1,126 @@
+/*\r
+GNU Lesser General Public License\r
+\r
+ImageFileChooserPreview\r
+Copyright (C) 2000-2002  Frits Jalvingh & Howard Kistler\r
+\r
+This library is free software; you can redistribute it and/or\r
+modify it under the terms of the GNU Lesser General Public\r
+License as published by the Free Software Foundation; either\r
+version 2.1 of the License, or (at your option) any later version.\r
+\r
+This library is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+Lesser General Public License for more details.\r
+\r
+You should have received a copy of the GNU Lesser General Public\r
+License along with this library; if not, write to the Free Software\r
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+*/\r
+\r
+package com.hexidec.ekit.component;\r
+\r
+import java.awt.Dimension;\r
+import java.awt.Graphics;\r
+import java.awt.Image;\r
+import java.io.File;\r
+import javax.swing.ImageIcon;\r
+import javax.swing.JComponent;\r
+import javax.swing.JFileChooser;\r
+import java.beans.PropertyChangeEvent;\r
+import java.beans.PropertyChangeListener;\r
+\r
+/** Class provides a preview window for the selected image file\r
+  */\r
+class ImageFileChooserPreview extends JComponent implements PropertyChangeListener\r
+{\r
+       private static final int previewWidth  = 100;\r
+       private static final int previewHeight = 100;\r
+\r
+       private ImageIcon imageThumb = null;\r
+       private File imageFile = null;\r
+\r
+       /** This class requires a file chooser to register with so this class will\r
+         * be notified when a new file is selected in the browser.\r
+         * @param JFileChooser that this preview window is used in.\r
+         */\r
+       public ImageFileChooserPreview(JFileChooser parent)\r
+       {\r
+               setPreferredSize(new Dimension(previewWidth , previewHeight));\r
+               parent.addPropertyChangeListener(this);\r
+       }\r
+\r
+       /** Loads a new image into the preview window, and scales it if necessary.\r
+         */\r
+       public void loadImage()\r
+       {\r
+               if(imageFile == null)\r
+               {\r
+                       imageThumb = null;\r
+                       return;\r
+               }\r
+               imageThumb = new ImageIcon(imageFile.getPath());\r
+\r
+               // Check if thumb requires scaling\r
+               if(imageThumb.getIconHeight() < previewHeight && imageThumb.getIconWidth() < previewWidth)\r
+               {\r
+                       return;\r
+               }\r
+               int     w = previewWidth;\r
+               int     h = previewHeight;\r
+               if(imageThumb.getIconHeight() > imageThumb.getIconWidth())\r
+               {\r
+                       w = -1;\r
+               }\r
+               else\r
+               {\r
+                       h = -1;\r
+               }\r
+               imageThumb = new ImageIcon(imageThumb.getImage().getScaledInstance(w, h, Image.SCALE_DEFAULT));\r
+       }\r
+\r
+       /** Callback (event handler) to indicate that a property of the\r
+         * JFileChooser has changed. If the selected file has changed cause a new\r
+         * thumbnail to load.\r
+         */\r
+       public void propertyChange(PropertyChangeEvent e)\r
+       {\r
+               if(e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))\r
+               {\r
+                       imageFile = (File)e.getNewValue();\r
+                       if(isShowing())\r
+                       {\r
+                               loadImage();\r
+                               repaint();\r
+                       }\r
+               }\r
+       }\r
+\r
+       /** Paints the icon of the current image, if one's present..\r
+         * @param Graphics object to use when painting the component.\r
+         */\r
+       public void paintComponent(Graphics g)\r
+       {\r
+               if(imageThumb == null)\r
+               {\r
+                       loadImage();\r
+               }\r
+               if(imageThumb == null)\r
+               {\r
+                       return;\r
+               }\r
+               int     x = (getWidth() - imageThumb.getIconWidth()) / 2;\r
+               int     y = (getHeight() - imageThumb.getIconHeight()) / 2;\r
+               if(y < 0)\r
+               {\r
+                       y = 0;\r
+               }\r
+               if(x < 5)\r
+               {\r
+                       x = 5;\r
+               }\r
+               imageThumb.paintIcon(this, g, x, y);\r
+       }\r
+}\r
+\r