1 package rero.dck.items; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 import javax.swing.event.*; 8 9 import java.io.File ; 10 import java.beans.*; 11 12 import rero.dck.*; 13 import rero.config.*; 14 15 import java.util.*; 16 import javax.swing.filechooser.*; 17 18 public class ImageInput extends SuperInput implements ActionListener 19 { 20 protected JLabel label; 21 protected JTextField text; 22 protected SmallButton button; 23 protected String value; 24 protected JFileChooser chooser; 25 26 protected boolean directory; 27 28 public ImageInput(String _variable, String _value, String _label, char mnemonic) 29 { 30 label = new JLabel(_label); 31 text = new JTextField(); 32 button = new SmallButton(text.getBorder(), "Click to open an image chooser dialog"); 33 34 button.addActionListener(this); 35 36 setLayout(new BorderLayout(2, 2)); 37 38 add(label, BorderLayout.WEST); 39 add(text, BorderLayout.CENTER); 40 add(button, BorderLayout.EAST); 41 42 label.setLabelFor(button); 43 label.setDisplayedMnemonic(mnemonic); 44 45 variable = _variable; 46 value = _value; 47 } 48 49 public void actionPerformed(ActionEvent ev) 50 { 51 if (chooser == null) 52 { 53 chooser = new JFileChooser(); 54 chooser.setApproveButtonText("Select Image"); 55 56 chooser.addChoosableFileFilter(new ImageFilter()); 57 chooser.setAccessory(new ImagePreview(chooser)); 58 } 59 60 if (chooser.showDialog(this, null) == JFileChooser.APPROVE_OPTION) 61 { 62 text.setText(chooser.getSelectedFile().getAbsolutePath()); 63 text.requestFocus(); 64 } 65 66 notifyParent(); 67 } 68 69 public void save() 70 { 71 ClientState.getClientState().setString(getVariable(), text.getText()); 72 } 73 74 public int getEstimatedWidth() 75 { 76 return (int)label.getPreferredSize().getWidth(); 77 } 78 79 public void setAlignWidth(int width) 80 { 81 label.setPreferredSize(new Dimension(width, 0)); 82 revalidate(); 83 } 84 85 public JComponent getComponent() 86 { 87 return this; 88 } 89 90 public void refresh() 91 { 92 text.setText(ClientState.getClientState().getString(getVariable(), value)); 93 } 94 95 protected class ImagePreview extends JPanel implements PropertyChangeListener 96 { 97 protected ImageIcon thumbnail = null; 98 protected File file = null; 99 protected JLabel label; 100 101 public ImagePreview(JFileChooser fc) 102 { 103 setBorder(text.getBorder()); 104 setPreferredSize(new Dimension(150, 75)); 105 fc.addPropertyChangeListener(this); 106 } 107 108 public void loadImage() 109 { 110 if (file == null) 111 { 112 return; 113 } 114 115 ImageIcon tmpIcon = new ImageIcon(file.getPath()); 116 117 if (tmpIcon.getIconWidth() > (getWidth() - 5)) 118 { 119 thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(getWidth() - 10, -1, Image.SCALE_DEFAULT)); 120 121 if (thumbnail.getIconHeight() > (getHeight() - 5)) 122 { 123 thumbnail = new ImageIcon(thumbnail.getImage().getScaledInstance(-1, getHeight() - 10, Image.SCALE_DEFAULT)); 124 } 125 } 126 else 127 { 128 thumbnail = tmpIcon; 129 } 130 } 131 132 public void paintComponent(Graphics g) 133 { 134 super.paintComponent(g); 135 136 if (thumbnail == null) 137 { 138 loadImage(); 139 } 140 141 if (thumbnail != null) 142 { 143 int x = getWidth()/2 - thumbnail.getIconWidth()/2; 144 int y = getHeight()/2 - thumbnail.getIconHeight()/2; 145 146 if (y < 0) 147 { 148 y = 0; 149 } 150 151 if (x < 5) { 152 x = 5; 153 } 154 155 thumbnail.paintIcon(this, g, x, y); 156 } 157 158 paintBorder(g); 159 } 160 161 162 public void propertyChange(PropertyChangeEvent e) 163 { 164 String prop = e.getPropertyName(); 165 if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) 166 { 167 file = (File ) e.getNewValue(); 168 if (isShowing()) 169 { 170 loadImage(); 171 repaint(); 172 } 173 } 174 } 175 } 176 177 protected static class ImageFilter extends FileFilter 178 { 179 protected static HashSet extensions; 180 181 static 182 { 183 extensions = new HashSet(); 184 extensions.add(".png"); 185 extensions.add(".tif"); 186 extensions.add(".tiff"); 187 extensions.add(".gif"); 188 extensions.add(".jpg"); 189 extensions.add(".jpeg"); 190 } 191 192 193 public boolean accept(File f) 194 { 195 if (f.isDirectory()) 196 { 197 return true; 198 } 199 200 String file = f.getName(); 201 202 if (file.lastIndexOf('.') > -1) 203 { 204 String ext = file.substring(file.lastIndexOf('.'), file.length()).toLowerCase(); 205 206 return extensions.contains(ext); 207 } 208 209 return false; 210 } 211 212 public String getDescription() 213 { 214 return "Image Files"; 215 } 216 } 217 } 218 219 220 | Popular Tags |