/plugins/ImageViewer/tags/1.0/src/imageviewer/ImageViewer.java

# · Java · 193 lines · 121 code · 15 blank · 57 comment · 9 complexity · a5ff86696043dfcc2af7022c78833bb8 MD5 · raw file

  1. /*
  2. Copyright (c) 2009, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package imageviewer;
  26. import java.awt.*;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.awt.image.BufferedImage;
  30. import javax.swing.*;
  31. import org.gjt.sp.jedit.jEdit;
  32. import org.gjt.sp.jedit.GUIUtilities;
  33. /**
  34. * A simple image viewer. Provides scrolling and zooming, and that's it. The
  35. * zoom in is unrestricted, which means is is possible to run out of memory.
  36. * Zoom out stops at 2 x 2 pixels.
  37. * TODO: is a zoom percent spinner necessary? Right now, all images are loaded
  38. * at 100%. With a percent spinner, the initial size could be based on the
  39. * current spinner value, which might be handy when scanning a directory of
  40. * small icons or large images.
  41. */
  42. public class ImageViewer extends JPanel {
  43. private JLabel imageLabel;
  44. private JLabel filenameLabel;
  45. private JButton zoomIn;
  46. private JButton zoomOut;
  47. private JButton clear;
  48. private float originalWidth = 0.0f;
  49. private float originalHeight = 0.0f;
  50. private Image image = null;
  51. public ImageViewer() {
  52. installComponents();
  53. installListeners();
  54. }
  55. // create and layout the components
  56. private void installComponents() {
  57. setBorder( BorderFactory.createEmptyBorder( 6, 6, 6, 6 ) );
  58. setLayout( new BorderLayout() );
  59. // use a JLabel to actually display the image
  60. imageLabel = new JLabel();
  61. imageLabel.setVerticalTextPosition( JLabel.BOTTOM );
  62. imageLabel.setHorizontalTextPosition( JLabel.CENTER );
  63. imageLabel.setHorizontalAlignment( JLabel.CENTER );
  64. add( new JScrollPane( imageLabel ), BorderLayout.CENTER );
  65. // use another JLabel to show the name of the file being shown
  66. filenameLabel = new JLabel();
  67. // set up the zoom buttons
  68. clear = new JButton( GUIUtilities.loadIcon( "22x22/actions/edit-clear.png" ) );
  69. clear.setToolTipText( jEdit.getProperty( "imageviewer.clear", "Clear" ) );
  70. zoomIn = new JButton( GUIUtilities.loadIcon( "22x22/actions/zoom-in.png" ) );
  71. zoomIn.setToolTipText( jEdit.getProperty( "imageviewer.zoomin", "Zoom In" ) );
  72. zoomOut = new JButton( GUIUtilities.loadIcon( "22x22/actions/zoom-out.png" ) );
  73. zoomOut.setToolTipText( jEdit.getProperty( "imageviewer.zoomout", "Zoom Out" ) );
  74. JPanel btnPanel = new JPanel( new FlowLayout( FlowLayout.RIGHT, 0, 3 ) );
  75. btnPanel.add( clear );
  76. btnPanel.add( zoomIn );
  77. btnPanel.add( zoomOut );
  78. // create a panel for the toolbar
  79. JPanel toolbar = new JPanel( new BorderLayout() );
  80. toolbar.add( filenameLabel, BorderLayout.WEST );
  81. toolbar.add( btnPanel, BorderLayout.EAST );
  82. // add the toolbar panel
  83. add( toolbar, BorderLayout.NORTH );
  84. }
  85. // add any listeners necessary for the installed components
  86. private void installListeners() {
  87. clear.addActionListener(
  88. new ActionListener() {
  89. public void actionPerformed( ActionEvent ae ) {
  90. imageLabel.setIcon( null );
  91. ImageViewer.this.invalidate();
  92. ImageViewer.this.validate();
  93. }
  94. }
  95. );
  96. zoomIn.addActionListener(
  97. new ActionListener() {
  98. public void actionPerformed( ActionEvent ae ) {
  99. float width = originalWidth * 1.1f;
  100. float height = originalHeight * 1.1f;
  101. originalWidth = width;
  102. originalHeight = height;
  103. if ( width > 0 && height > 0 ) {
  104. Image zoomImage = getScaledImage( image, ( int ) width, ( int ) height );
  105. ImageIcon icon = new ImageIcon( zoomImage );
  106. imageLabel.setIcon( icon );
  107. imageLabel.setSize( ( int ) width, ( int ) height );
  108. ImageViewer.this.invalidate();
  109. ImageViewer.this.validate();
  110. }
  111. }
  112. }
  113. );
  114. zoomOut.addActionListener(
  115. new ActionListener() {
  116. public void actionPerformed( ActionEvent ae ) {
  117. float width = Math.max( 2, ( int ) ( ( float ) originalWidth * 0.9f ) );
  118. float height = Math.max( 2, ( int ) ( ( float ) originalHeight * 0.9f ) );
  119. originalWidth = width;
  120. originalHeight = height;
  121. if ( width > 0 && height > 0 ) {
  122. Image zoomImage = getScaledImage( image, ( int ) width, ( int ) height );
  123. ImageIcon icon = new ImageIcon( zoomImage );
  124. imageLabel.setIcon( icon );
  125. imageLabel.setSize( ( int ) width, ( int ) height );
  126. ImageViewer.this.invalidate();
  127. ImageViewer.this.validate();
  128. }
  129. }
  130. }
  131. );
  132. }
  133. /**
  134. * Display the image in this ImageViewer.
  135. * @param filename the name of the image file to display.
  136. */
  137. public void showImage( String filename ) {
  138. if ( isValidFilename( filename ) ) {
  139. filenameLabel.setText( filename );
  140. ImageIcon icon = new ImageIcon( filename );
  141. image = icon.getImage();
  142. originalWidth = ( float ) icon.getIconWidth();
  143. originalHeight = ( float ) icon.getIconHeight();
  144. imageLabel.setIcon( icon );
  145. imageLabel.setSize( ( int ) originalWidth, ( int ) originalHeight );
  146. invalidate();
  147. validate();
  148. }
  149. }
  150. /**
  151. * @return true if the filename, regardless of case, ends with .jpg, .gif, or .png.
  152. */
  153. public static boolean isValidFilename( String filename ) {
  154. if ( filename == null ) {
  155. return false;
  156. }
  157. String name = filename.toLowerCase();
  158. return name.endsWith( ".jpg" ) || name.endsWith( ".gif" ) || name.endsWith( ".png" );
  159. }
  160. /**
  161. * Resizes an image to the given width and height.
  162. * @param image source image to scale
  163. * @param w desired width
  164. * @param h desired height
  165. * @return the resized image
  166. */
  167. private Image getScaledImage( Image image, int w, int h ) {
  168. BufferedImage resizedImage = new BufferedImage( w, h, BufferedImage.TYPE_INT_RGB );
  169. Graphics2D g2 = resizedImage.createGraphics();
  170. g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
  171. g2.drawImage( image, 0, 0, w, h, null );
  172. g2.dispose();
  173. return resizedImage;
  174. }
  175. }