KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > editor > audioeditor


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 JavaDoc;
12 import java.util.logging.Level JavaDoc;
13
14 import sun.audio.*;
15
16 /**
17  * Simple audio editor that trys to play the audio clip.
18  * Allows the user to save and load audio clips from a file and
19  * set the audio clip in the table editor.
20  * @author Trudi.
21  */

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 JavaDoc log = Logger.getLogger(audioeditor.class.getName());
31
32    /**
33     * Constructor.
34     * @param owner handle to the application frame, used to display dialog boxes.
35     */

36     public audioeditor(Frame owner)
37     {
38         this(owner, false);
39     }
40
41    /**
42     * Sets up the frame with one panel, one label with an image and seven buttons.
43     * @param owner handle to the application frame, used to display dialog boxes.
44     * @param viewable specifies if there is a viewer for the binary data,
45     * if true, the "view" button is added to the panel.
46     */

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.")); //TE: creates a new help button with a listener that will open JX help at appropriate location.
101
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    /**
128     * Creates an audio stream and plays it.
129     */

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 JavaDoc ee)
141         {
142             log.log(Level.WARNING, CBIntText.get("No data available: "), ee);
143         }
144             
145          AudioPlayer.player.start(audioStream);
146     }
147
148    /**
149     * Stops the audio playing.
150     */

151     public void audioStop()
152     {
153          AudioPlayer.player.stop(audioStream);
154     }
155
156    /**
157     * Sets the value to display in the editor. If no data the stop, play, ok &
158     * save buttons are disabled.
159     */

160     public void setValue(editablebinary editMe)
161     {
162         this.editMe = editMe;
163
164         bytes = editMe.getValue();
165
166         // Backup copy of data...
167
oldBytes = bytes;
168         
169         if(bytes == null)
170             setButtons(false);
171     }
172
173    /**
174     * Load audio clip from a file. Opens a file chooser that can play the clip.
175     */

176     protected void load()
177     {
178         // Delete any temporary files associates with this entry...
179
if (currentDN != null)
180             CBCache.cleanCache(currentDN.toString());
181             
182         JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir"));
183
184         // Sets up the 'play' and 'stop' feature in the JFileChooser...
185
audioAccess = new audioaccessory();
186         chooser.setAccessory(audioAccess);
187         chooser.addPropertyChangeListener(audioAccess);
188         
189         if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
190         {
191             // Stops the current auto clip if playing...
192
audioAccess.stopPlay();
193             return;
194         }
195
196         // Stops the current auto clips if playing...
197
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                 // Enables the buttons because there is data available...
213
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    /**
224     * Save binary data to the file.
225     */

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    /**
246     * Enables/disables the components in the editor.
247     * @param enabled flag to determine if the buttons should be enabled or not.
248     */

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    /**
258     * Returns a new value.
259     * @return new value.
260     */

261     public byte[] getNewValue()
262     {
263         if (bytes!=null && bytes.length!=0)
264             return bytes;
265         else
266             return null;
267     }
268
269    /**
270     * Sets the value in the table editor.
271     */

272     public void setValue()
273     {
274         if (isChanged())
275             editMe.setValue(getNewValue());
276         quit();
277     }
278 }
Popular Tags