1 31 32 package org.opencms.applet.upload; 33 34 import java.awt.Color ; 35 import java.awt.Dimension ; 36 import java.awt.Font ; 37 import java.awt.Graphics ; 38 import java.awt.Image ; 39 import java.beans.PropertyChangeEvent ; 40 import java.beans.PropertyChangeListener ; 41 import java.io.BufferedReader ; 42 import java.io.File ; 43 import java.io.FileReader ; 44 import java.io.IOException ; 45 46 import javax.swing.ImageIcon ; 47 import javax.swing.JComponent ; 48 import javax.swing.JFileChooser ; 49 50 61 public class ImagePreview extends JComponent implements PropertyChangeListener { 62 63 private static final int C_MODE_EMPTY = 0; 64 65 private static final int C_MODE_IMAGE = 1; 66 private static final int C_MODE_TEXT = 2; 67 68 private static final long serialVersionUID = 4851280416316056303L; 69 70 private File m_file; 71 private Font m_font; 72 private String m_messageNoPreview; 73 private int m_mode; 74 private String [] m_text; 75 private ImageIcon m_thumbnail; 76 77 83 public ImagePreview(JFileChooser fc, String messageNoPreview) { 84 85 setPreferredSize(new Dimension (200, 100)); 86 fc.addPropertyChangeListener(this); 87 m_font = new java.awt.Font (null, Font.PLAIN, 9); 88 m_messageNoPreview = messageNoPreview; 89 m_mode = C_MODE_EMPTY; 90 } 91 92 95 public void loadImage() { 96 97 if (m_file == null) { 98 m_thumbnail = null; 99 return; 100 } 101 ImageIcon tmpIcon = new ImageIcon (m_file.getPath()); 103 if (tmpIcon.getIconWidth() > 190) { 104 m_thumbnail = new ImageIcon (tmpIcon.getImage().getScaledInstance(190, -1, Image.SCALE_DEFAULT)); 106 } else { m_thumbnail = tmpIcon; 108 } 109 } 110 111 114 public void paintComponent(Graphics g) { 115 116 if (m_mode == C_MODE_IMAGE) { 117 if (m_thumbnail == null) { 119 loadImage(); 120 } 121 if (m_thumbnail != null) { 122 int x = getWidth() / 2 - m_thumbnail.getIconWidth() / 2; 123 int y = getHeight() / 2 - m_thumbnail.getIconHeight() / 2; 124 125 if (y < 0) { 126 y = 0; 127 } 128 129 if (x < 5) { 130 x = 5; 131 } 132 m_thumbnail.paintIcon(this, g, x, y); 133 } 134 } else if (m_mode == C_MODE_TEXT) { 135 g.setColor(Color.white); 137 g.fillRect(10, 0, getWidth() - 1, getHeight() - 1); 138 g.setColor(Color.black); 139 g.drawRect(10, 0, getWidth() - 11, getHeight() - 1); 140 g.setFont(m_font); 141 for (int i = 0; i < 35; i++) { 142 if (m_text[i] != null) { 143 g.drawString(m_text[i], 13, (i + 1) * 11); 144 } 145 } 146 } else { 147 g.setColor(Color.black); 148 g.setFont(m_font); 149 g.drawString(m_messageNoPreview, 30, getHeight() / 2); 150 } 151 } 152 153 158 public void propertyChange(PropertyChangeEvent e) { 159 160 boolean update = false; 161 String prop = e.getPropertyName(); 162 163 if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) { 164 m_file = null; 166 m_text = null; 167 update = true; 168 m_mode = C_MODE_EMPTY; 169 170 } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) { 171 m_file = (File )e.getNewValue(); 173 m_mode = getMode(); 174 update = true; 175 } 176 177 if (update) { 179 m_thumbnail = null; 180 if (isShowing()) { 181 if (m_mode == (C_MODE_IMAGE)) { 182 loadImage(); 183 repaint(); 184 } else if (m_mode == (C_MODE_TEXT)) { 185 loadText(); 186 repaint(); 187 } else { 188 repaint(); 189 } 190 } 191 } 192 } 193 194 199 private int getMode() { 200 201 int mode = C_MODE_EMPTY; 202 String extension = FileUploadUtils.getExtension(m_file); 203 if (FileUploadUtils.isImageExtension(extension)) { 204 mode = C_MODE_IMAGE; 205 } else if (FileUploadUtils.isTextExtension(extension)) { 206 mode = C_MODE_TEXT; 207 } 208 return mode; 209 } 210 211 214 private void loadText() { 215 216 m_text = new String [35]; 217 BufferedReader fileStream = null; 218 try { 219 fileStream = new BufferedReader (new FileReader (m_file)); 220 for (int i = 0; i < 35; i++) { 221 m_text[i] = fileStream.readLine(); 222 } 223 } catch (Exception e) { 224 } finally { 226 try { 227 if (fileStream != null) { 228 fileStream.close(); 229 } 230 } catch (IOException e) { 231 } 233 } 234 } 235 } | Popular Tags |