Newer
Older
restamp / src / main / java / osm / surveyor / matchtime / gui / ImagePreview.java
@haya4 haya4 on 26 Jan 2020 3 KB Restamp
  1. /*
  2. * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Oracle or the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31.  
  32. package osm.surveyor.matchtime.gui;
  33.  
  34. import javax.swing.*;
  35.  
  36. import java.beans.*;
  37. import java.awt.*;
  38. import java.io.File;
  39.  
  40. /* ImagePreview.java by FileChooserDemo2.java. */
  41. @SuppressWarnings("serial")
  42. public class ImagePreview extends JComponent implements PropertyChangeListener {
  43. ImageIcon thumbnail = null;
  44. File file = null;
  45. static int IMAGE_SIZE_X = 320;
  46. static int IMAGE_SIZE_Y = 240;
  47.  
  48. @SuppressWarnings({"LeakingThisInConstructor", "OverridableMethodCallInConstructor"})
  49. public ImagePreview(JFileChooser fc) {
  50. setPreferredSize(new Dimension(IMAGE_SIZE_X + 10, IMAGE_SIZE_Y + 10));
  51. fc.addPropertyChangeListener(this);
  52. }
  53.  
  54. public void loadImage() {
  55. if (file == null) {
  56. thumbnail = null;
  57. return;
  58. }
  59.  
  60. //Don't use createImageIcon (which is a wrapper for getResource)
  61. //because the image we're trying to load is probably not one
  62. //of this program's own resources.
  63. ImageIcon tmpIcon = new ImageIcon(file.getPath());
  64. if (tmpIcon.getIconWidth() > IMAGE_SIZE_X) {
  65. thumbnail = new ImageIcon(tmpIcon.getImage().
  66. getScaledInstance(IMAGE_SIZE_X, -1, Image.SCALE_DEFAULT));
  67. } else { //no need to miniaturize
  68. thumbnail = tmpIcon;
  69. }
  70. }
  71.  
  72. @Override
  73. public void propertyChange(PropertyChangeEvent e) {
  74. boolean update = false;
  75. String prop = e.getPropertyName();
  76.  
  77. if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
  78. file = null;
  79. update = true;
  80. }
  81. else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
  82. file = (File) e.getNewValue();
  83. update = true;
  84. }
  85. if (update) {
  86. thumbnail = null;
  87. if (isShowing()) {
  88. loadImage();
  89. repaint();
  90. }
  91. }
  92. }
  93.  
  94. @Override
  95. protected void paintComponent(Graphics g) {
  96. if (thumbnail == null) {
  97. loadImage();
  98. }
  99. if (thumbnail != null) {
  100. int x = getWidth()/2 - thumbnail.getIconWidth()/2;
  101. int y = getHeight()/2 - thumbnail.getIconHeight()/2;
  102.  
  103. if (y < 0) {
  104. y = 0;
  105. }
  106.  
  107. if (x < 5) {
  108. x = 5;
  109. }
  110. thumbnail.paintIcon(this, g, x, y);
  111. }
  112. }
  113. }