1 package com.ca.directory.jxplorer.editor; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.directory.jxplorer.JXplorer; 5 import com.ca.directory.jxplorer.HelpIDs; 6 7 import javax.swing.*; 8 import java.awt.*; 9 import java.io.*; 10 import javax.swing.ImageIcon ; 11 import java.beans.PropertyChangeEvent ; 12 import java.beans.PropertyChangeListener ; 13 import java.util.logging.Logger ; 14 import java.util.logging.Level ; 15 16 21 public class jpegphotoeditor extends basicbinaryeditor 22 { 23 protected ImageIcon iconBytes; 24 protected JLabel label; 25 protected int imageHeight, imageWidth, screenHeight, screenWidth; protected JScrollPane scrollPaneLabel; 27 28 private final static Logger log = Logger.getLogger(jpegphotoeditor.class.getName()); 29 30 34 public jpegphotoeditor(Frame owner) 35 { 36 this(owner, false); 37 } 38 39 44 public jpegphotoeditor(Frame owner, boolean viewable) 45 { 46 super(owner); 47 setTitle(CBIntText.get("jpegPhoto")); 48 btnEdit.setVisible(false); 49 } 50 51 54 public void addMainViewComponent() 55 { 56 label = new JLabel(); 57 scrollPaneLabel = new JScrollPane(label); 58 display.makeHeavy(); 59 display.addln(scrollPaneLabel); 60 display.makeLight(); 61 } 62 63 67 public CBButton addHelp() 68 { 69 btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help.")); CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_JPEGPHOTO); 71 72 return btnHelp; 73 } 74 75 80 public void preferredSize(int newWidth, int newHeight) 81 { 82 83 newHeight = newHeight + 80; if (newWidth < 345) 85 { 86 newWidth = 345; 87 } 89 Toolkit toolKit = Toolkit.getDefaultToolkit(); 90 91 screenWidth = toolKit.getScreenSize().width; 92 screenHeight = toolKit.getScreenSize().height; 93 94 if (newWidth >= screenWidth) 95 { 96 newWidth = screenWidth - 50; 97 } if (newHeight >= screenHeight) 99 { 100 newHeight = screenHeight - 50; 101 } 103 setBounds((screenWidth - newWidth) / 2, (screenHeight - newHeight) / 2, newWidth, newHeight); } 105 106 109 public void setValue(editablebinary editMe) 110 { 111 this.editMe = editMe; 112 113 bytes = editMe.getValue(); 114 115 oldBytes = bytes; 116 117 if (bytes != null) 118 { 119 setButtons(true); iconBytes = new ImageIcon (bytes); 121 imageHeight = iconBytes.getIconHeight(); 122 imageWidth = iconBytes.getIconWidth(); 123 preferredSize(imageWidth, imageHeight); 124 label.setIcon(iconBytes); 125 } 126 else 127 { 128 preferredSize(400, 300); 129 setButtons(false); } 131 } 132 133 136 protected void load() 137 { 138 CBCache.cleanCache(currentDN.toString()); 140 JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir")); 141 chooser.addChoosableFileFilter(new CBFileFilter(new String []{"jpeg", "jpg"}, "JPEG Files (*.jpeg, *.jpg)")); 142 143 ImageAccessory ip = new ImageAccessory(); chooser.setAccessory(ip); 145 chooser.addPropertyChangeListener(ip); 146 147 148 if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) 149 return; 150 File file = chooser.getSelectedFile(); 151 JXplorer.setProperty("binary.homeDir", chooser.getSelectedFile().getParent()); 152 153 try 154 { 155 FileInputStream input = new FileInputStream(file); 156 157 int length = (int) file.length(); 158 if (length > 0) 159 { 160 bytes = new byte[length]; 161 int read = input.read(bytes); 162 if (read > 0) 163 { 164 iconBytes = new ImageIcon (bytes); imageHeight = iconBytes.getIconHeight(); 166 imageWidth = iconBytes.getIconWidth(); 167 label.setIcon(iconBytes); 168 preferredSize(imageWidth, imageHeight); 169 setButtons(true); } 171 } 172 input.close(); 173 } 174 catch (IOException e) 175 { 176 log.log(Level.WARNING,"Error opening the file!",e); 177 } 178 setVisible(false); setVisible(true); } 181 182 185 protected void save() 186 { 187 188 JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir")); 189 190 if (chooser.showSaveDialog(frame) != JFileChooser.APPROVE_OPTION) 191 return; 192 193 File file = chooser.getSelectedFile(); 194 try 195 { 196 FileOutputStream output = new FileOutputStream(file); 197 output.write(bytes); 198 output.close(); 199 } 200 catch (IOException e) 201 { 202 log.log(Level.WARNING,"Error writing to the file! ", e); 203 } 204 } 205 206 209 public void setButtons(boolean enabled) 210 { 211 btnOK.setEnabled(enabled); 212 btnSave.setEnabled(enabled); 213 } 214 215 219 public byte[] getNewValue() 220 { 221 if (bytes != null && bytes.length != 0) 222 return bytes; 223 else 224 return null; 225 } 226 227 230 public void setValue() 231 { 232 if (isChanged()) 233 editMe.setValue(getNewValue()); 234 quit(); 235 } 236 237 241 public class ImageAccessory extends JComponent 242 implements PropertyChangeListener 243 { 244 ImageIcon image = null; 245 File file = null; 246 247 250 public ImageAccessory() 251 { 252 setPreferredSize(new Dimension(100, 50)); 253 } 254 255 258 public void loadImage() 259 { 260 if (file == null) 261 { 262 image = null; 263 return; 264 } 265 266 ImageIcon tempImage = new ImageIcon (file.getPath()); 267 if (tempImage != null) 268 { 269 if (tempImage.getIconWidth() > 90) 270 image = new ImageIcon (tempImage.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT)); 271 else 272 image = tempImage; 273 } 274 } 275 276 280 public void propertyChange(PropertyChangeEvent e) 281 { 282 boolean update = false; 283 String prop = e.getPropertyName(); 284 285 if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) 286 { 287 file = null; 288 update = true; 289 } 290 else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) 291 { 292 file = (File) e.getNewValue(); 293 update = true; 294 } 295 296 if (update) 297 { 298 image = null; 299 if (isShowing()) 300 { 301 loadImage(); 302 repaint(); 303 } 304 } 305 } 306 307 311 public void paintComponent(Graphics g) 312 { 313 if (image == null) 314 { 315 loadImage(); 316 } 317 if (image != null) 318 { 319 int x = getWidth() / 2 - image.getIconWidth() / 2; 320 int y = getHeight() / 2 - image.getIconHeight() / 2; 321 322 if (y < 0) 323 { 324 y = 0; 325 } 326 327 if (x < 5) 328 { 329 x = 5; 330 } 331 image.paintIcon(this, g, x, y); 332 } 333 } 334 } 335 } | Popular Tags |