Source code: plugins/Encode.java
1 package plugins;
2
3 import java.util.Vector;
4 import java.util.List;
5 import java.util.Stack;
6 import java.util.ArrayList;
7
8 import javax.swing.JComponent;
9 import javax.swing.JPanel;
10 import javax.swing.JLabel;
11 import javax.swing.JScrollPane;
12 import javax.swing.JList;
13 import javax.swing.JFileChooser;
14
15 import java.awt.GridLayout;
16 import java.awt.event.*;
17 import java.awt.*;
18
19 import javax.swing.JButton;
20 import javax.swing.JSlider;
21 import javax.swing.JOptionPane;
22 import javax.swing.event.ChangeEvent;
23 import javax.swing.border.*;
24 import javax.swing.SwingUtilities;
25 import javax.swing.JFrame;
26
27 import javax.swing.event.*;
28
29 import java.awt.BorderLayout;
30 import java.awt.event.WindowAdapter;
31 import java.awt.event.WindowEvent;
32
33 import java.net.URL;
34 import java.io.File;
35
36 import musli.aorta.Creator;
37 import musli.aorta.Worker;
38 import musli.aorta.Tasklet;
39
40
41
42 import musli.util.Logger;
43 import musli.util.Form;
44 /**
45 * Creator part of the Encode Tasklet. With GUI and all.
46 *
47 * @author Jan.Vesely
48 */
49
50
51 public class Encode extends Creator
52 {
53 static public class Preferences implements java.io.Serializable
54 {
55 URL encodeExecutable;
56
57 public URL getEncodeExecutable()
58 {
59 return encodeExecutable;
60 }
61 public void setEncodeExecutable(URL url)
62 {
63 encodeExecutable=url;
64 }
65
66 }
67
68 // this is the parameter used by an Tasklet instance.
69 static public class Command implements java.io.Serializable
70 {
71 String command="";
72 String []cmdArray;
73 int secondsElapsed; // How long did it take to encode
74 String hostIP; // What was the IP of the aortaWorker.
75 String returnedStatus; // Possible info from encoder proggie.
76
77 public Command(String aCommand,String []aCmdArray)
78 {
79 command=aCommand;
80 cmdArray=aCmdArray;
81 }
82 }
83
84
85 URL media[]=null;
86 // URL encodeExec=null;
87
88 String mediaName[]=null;
89
90 JList mediaInfo;
91 JButton browseMedia;
92 JButton browseEncodeExec;
93
94 JButton start;
95 JScrollPane resultList;
96 JButton picture;
97
98 /**
99 * Initialize our GUI.
100 *
101 */
102 public void initGUI()
103 {
104
105 setPrefs((Preferences)musli.util.Reflect.loadObject(new Preferences()));
106
107 mediaInfo=new JList();
108 start=new JButton("Start");
109 browseMedia=new JButton("Select Media");
110 browseEncodeExec=new JButton("Preferences");
111
112 resultList=new JScrollPane();
113
114 JPanel commands=new JPanel(new GridLayout(5,1));
115 picture=new JButton();
116
117 commands.add(start);
118 commands.add(browseMedia);
119 commands.add(browseEncodeExec);
120
121
122 add
123 (commands);
124
125 add
126 (resultList);
127
128
129 resultList.getViewport().setView(mediaInfo);
130
131 // Anonoymous class to respond to "START" button and FIRE an tasklet.
132 start.addActionListener(new ActionListener()
133 {
134 public void actionPerformed(ActionEvent e)
135 {
136
137
138 if(null==media )
139 return ;
140
141
142 final EncodeTasklet task=new EncodeTasklet(Worker.getInstance().createTaskID());
143 Stack worklist=new Stack();
144
145 // Construct a command list with commands like "lame.exe -h in_x.wav out_x.mp3"
146 for(int i=0;i<media.length;i++)
147 {
148 List cmdList=new ArrayList();
149 cmdList.add(getPrefs().getEncodeExecutable().getFile());
150 cmdList.add("-h");
151 cmdList.add(nameInFile(i));
152 Logger.log(nameInFile(i));
153 cmdList.add(nameOutFile(i));
154 Logger.log(nameOutFile(i));
155 worklist.push(new Command(getPrefs().getEncodeExecutable().getFile(),(String[])cmdList.toArray(new String [0])));
156 }
157
158 task.setContent(worklist);
159
160 Thread t=new Thread (new Runnable()
161 {
162 public void run()
163 {
164 task.setProcessor(musli.aorta.TailRecursiveProcessor.class);
165 fireCompute(task);
166 }
167 }
168 );
169 t.start();
170 }
171 }
172 );
173
174 // Anonymous class to respond on "BROWSE" button and load media.
175 browseMedia.addActionListener(new ActionListener()
176 {
177 public void actionPerformed(ActionEvent e)
178 {
179 try
180 {
181
182 // Logger.log("Setting mediafile ="+ media.getFile());
183
184 Vector files=new Vector();
185 Vector medias=new Vector();
186 // Strip away the path. TODO put this in musli package !!!!
187 File file[]=getFiles();
188
189 for(int a=0;a<file.length;a++)
190 {
191 String str=file[a].toURL().getFile();
192 String mName="";
193 /*
194 if(str.lastIndexOf('/')>0) // File.pathSeparatorChar did not work. sigh..
195 str=str.substring(str.lastIndexOf('/')+1); // TODO , not safe !!! may be the last character !!! check the length!!!
196 */
197 mName=str;
198 if(str.toLowerCase().lastIndexOf(".wav")>0) // File.pathSeparatorChar did not work. sigh..
199 mName=mName.substring(0,mName.toLowerCase().lastIndexOf(".wav")); // TODO , not safe !!! may be the last character !!! check the length!!!
200
201 files.add(mName);
202
203 medias.add(file[a].toURL());
204 }
205
206 mediaName=(String[])files.toArray(new String[0]);
207 media=(URL[])medias.toArray(new URL[0]);
208
209
210 mediaInfo.setListData(files);
211 resultList.getViewport().setView(mediaInfo);
212
213 }
214 catch(Exception mediaErr)
215 {
216 Logger.log("Error during loading media",mediaErr);
217 media=null;
218 }
219
220
221 }
222 }
223 );
224
225 // Anonymous class to respond on "BROWSE" button and set EncodeExec
226 browseEncodeExec.addActionListener(new ActionListener()
227 {
228 public void actionPerformed(ActionEvent e)
229 {
230 //prefs=musli.util.Reflect.loadObject(new Preferences());
231 // encodeExec=getPrefs().getEncodeExecutable(); //getFile();
232 showPrefsWindow();
233 }
234 }
235 );
236 }
237
238
239
240 private void cleanupFiles(Stack files)
241 {
242 return ; // TODO delete all files in stack
243 }
244
245 private String nameInFile(int i)
246 {
247 return mediaName[i]+".wav";
248 }
249
250 private String nameOutFile(int i)
251 {
252 return mediaName[i]+".mp3";
253 }
254
255
256 /**
257 * Internal helper method to load a file.
258 * @return succes True
259 */
260 private URL getFile()
261 {
262 JFileChooser chooser = new JFileChooser();
263
264 chooser.setMultiSelectionEnabled(false);
265
266 int returnVal = chooser.showOpenDialog(null);
267
268 if(returnVal == JFileChooser.APPROVE_OPTION)
269 {
270
271 try
272 {
273 //getSelectedFiles()
274
275 return chooser.getSelectedFile().toURL();
276 }
277 catch(Exception e )
278 {
279 Logger.log("Error choosing file ",e);
280 }
281 }
282 return null;
283 }
284
285 /**
286 * Internal helper method to load multiple files.
287 * @return succes True
288 */
289 private File[] getFiles()
290 {
291 JFileChooser chooser = new JFileChooser();
292
293 chooser.setMultiSelectionEnabled(true);
294
295 int returnVal = chooser.showOpenDialog(null);
296
297 if(returnVal == JFileChooser.APPROVE_OPTION)
298 {
299
300 try
301 {
302 //getSelectedFiles()
303
304 return chooser.getSelectedFiles();
305 }
306 catch(Exception e )
307 {
308 Logger.log("Error choosing file ",e);
309 }
310 }
311 return null;
312 }
313
314 /**
315 * Get Preferences object.
316 */
317 public static Preferences getPrefs()
318 {
319 return (Preferences)getPrefsObject();
320 }
321 /**
322 * Set Preferences object.
323 */
324 public static void setPrefs(Preferences prefs)
325 {
326 setPrefsObject(prefs);
327 }
328
329 /**
330 * Present the result , the size of media , time ellapsed etc.
331 *@param task An Tasklet that has been computed.
332 */
333 public void viewResult(Object obj)
334 {
335 Tasklet task=(Tasklet)obj;
336
337 Vector theHistory=new Vector();
338 Stack s=(Stack)task.getResult();
339
340 while(!s.isEmpty())
341 {
342 Command c=(Command)s.pop();
343 theHistory.add("host="+c.hostIP+" exec="+c.command);
344 }
345 resultList.getViewport().setView( new JList(theHistory ));
346 }
347
348
349 /**
350 * This one ties us to our algoritm part.
351 * @return the algoritm class.
352 */
353
354
355 public Class taskClass()
356 {
357 Class theClass=null;
358 try
359 {
360 theClass=Class.forName("plugins.EncodeTasklet");
361 }
362 catch(Exception e)
363 {
364 Logger.log("Problems with Encode plugin:"+e);
365 }
366
367 return theClass;
368 }
369
370
371
372
373
374 }
375
376
377