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.awt.event.*; 10 import java.io.*; 11 import java.util.logging.Logger ; 12 import java.util.logging.Level ; 13 14 import sun.audio.*; 15 16 22 public class audioeditor extends basicbinaryeditor 23 { 24 protected CBButton btnPlay, btnStop; 25 protected JLabel label; 26 protected File file; 27 protected AudioStream audioStream; 28 protected audioaccessory audioAccess; 29 30 private static Logger log = Logger.getLogger(audioeditor.class.getName()); 31 32 36 public audioeditor(Frame owner) 37 { 38 this(owner, false); 39 } 40 41 47 public audioeditor(Frame owner, boolean viewable) 48 { 49 super(owner); 50 setModal(true); 51 setTitle(CBIntText.get("Audio")); 52 53 display = new CBPanel(); 54 55 label = new JLabel(new ImageIcon("images" + File.separator + "audio.gif")); 56 label.setOpaque(true); 57 label.setBackground(Color.white); 58 59 btnPlay = new CBButton(CBIntText.get("Play"), CBIntText.get("Click here to play the audio clip.")); 60 btnPlay.addActionListener(new ActionListener() { 61 public void actionPerformed(ActionEvent e) { 62 audioStop(); 63 audioPlay(); 64 }}); 65 66 btnStop = new CBButton(CBIntText.get("Stop"), CBIntText.get("Click here to stop playing the audio clip.")); 67 btnStop.addActionListener(new ActionListener() { 68 public void actionPerformed(ActionEvent e) { 69 audioStop(); 70 }}); 71 72 btnLoad = new CBButton(CBIntText.get("Load"), CBIntText.get("Click here to load an external audio file.")); 73 btnLoad.addActionListener(new ActionListener() { 74 public void actionPerformed(ActionEvent e) { 75 audioStop(); 76 load(); 77 }}); 78 79 btnSave = new CBButton(CBIntText.get("Save"), CBIntText.get("Click here to save the audio clip to an external file.")); 80 btnSave.addActionListener(new ActionListener() { 81 public void actionPerformed(ActionEvent e) { 82 audioStop(); 83 save(); 84 }}); 85 86 btnOK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click here to make the changes (remember to click Submit in the table editor).")); 87 btnOK.addActionListener(new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 audioStop(); 90 setValue(); 91 }}); 92 93 btnCancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit.")); 94 btnCancel.addActionListener(new ActionListener() { 95 public void actionPerformed(ActionEvent e) { 96 audioStop(); 97 quit(); 98 }}); 99 100 btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help.")); CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_AUDIO); 102 103 display.makeHeavy(); 104 display.addln(label); 105 display.makeLight(); 106 107 JPanel buttonPanelTop = new JPanel(); 108 JPanel buttonPanelBottom = new JPanel(); 109 110 buttonPanelTop.add(btnPlay); 111 buttonPanelTop.add(btnStop); 112 buttonPanelTop.add(btnHelp); 113 114 buttonPanelBottom.add(btnLoad); 115 buttonPanelBottom.add(btnSave); 116 buttonPanelBottom.add(btnOK); 117 buttonPanelBottom.add(btnCancel); 118 119 display.addln(buttonPanelTop); 120 display.addln(buttonPanelBottom); 121 122 getContentPane().add(display); 123 setSize(300,320); 124 CBUtility.center(this, owner); 125 } 126 127 130 public void audioPlay() 131 { 132 try 133 { 134 audioStream = new AudioStream(new ByteArrayInputStream(bytes)); 135 } 136 catch (IOException e) 137 { 138 CBUtility.error(CBIntText.get("Error with audio file: ") + e); 139 } 140 catch (NullPointerException ee) 141 { 142 log.log(Level.WARNING, CBIntText.get("No data available: "), ee); 143 } 144 145 AudioPlayer.player.start(audioStream); 146 } 147 148 151 public void audioStop() 152 { 153 AudioPlayer.player.stop(audioStream); 154 } 155 156 160 public void setValue(editablebinary editMe) 161 { 162 this.editMe = editMe; 163 164 bytes = editMe.getValue(); 165 166 oldBytes = bytes; 168 169 if(bytes == null) 170 setButtons(false); 171 } 172 173 176 protected void load() 177 { 178 if (currentDN != null) 180 CBCache.cleanCache(currentDN.toString()); 181 182 JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir")); 183 184 audioAccess = new audioaccessory(); 186 chooser.setAccessory(audioAccess); 187 chooser.addPropertyChangeListener(audioAccess); 188 189 if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) 190 { 191 audioAccess.stopPlay(); 193 return; 194 } 195 196 audioAccess.stopPlay(); 198 199 file = chooser.getSelectedFile(); 200 201 JXplorer.setProperty("binary.homeDir", chooser.getSelectedFile().getParent()); 202 203 try 204 { 205 FileInputStream input = new FileInputStream(file); 206 int length = (int)file.length(); 207 if (length > 0) 208 { 209 bytes = new byte[length]; 210 int read = input.read(bytes); 211 212 setButtons(true); 214 } 215 input.close(); 216 } 217 catch(IOException e) 218 { 219 CBUtility.error(CBIntText.get("Error writing to the file: ") + e); 220 } 221 } 222 223 226 protected void save() 227 { 228 JFileChooser chooser = new JFileChooser(); 229 if (chooser.showSaveDialog(frame) != JFileChooser.APPROVE_OPTION) 230 return; 231 232 File file = chooser.getSelectedFile(); 233 try 234 { 235 FileOutputStream output = new FileOutputStream(file); 236 output.write(bytes); 237 output.close(); 238 } 239 catch(IOException e) 240 { 241 CBUtility.error(CBIntText.get("Error writing to the file: ") + e); 242 } 243 } 244 245 249 public void setButtons(boolean enabled) 250 { 251 btnStop.setEnabled(enabled); 252 btnPlay.setEnabled(enabled); 253 btnSave.setEnabled(enabled); 254 btnOK.setEnabled(enabled); 255 } 256 257 261 public byte[] getNewValue() 262 { 263 if (bytes!=null && bytes.length!=0) 264 return bytes; 265 else 266 return null; 267 } 268 269 272 public void setValue() 273 { 274 if (isChanged()) 275 editMe.setValue(getNewValue()); 276 quit(); 277 } 278 } | Popular Tags |