PageRenderTime 46ms CodeModel.GetById 14ms app.highlight 28ms RepoModel.GetById 0ms app.codeStats 1ms

/GUI/Anonymous/src/GUI.java

http://github.com/anonymous-admin/anonymous
Java | 716 lines | 617 code | 64 blank | 35 comment | 49 complexity | 8eddbf9121e04819690e6e79bfbdc13c MD5 | raw file
  1import java.awt.*;
  2import java.awt.event.ActionEvent;
  3import java.awt.event.ActionListener;
  4import java.awt.event.KeyEvent;
  5import java.awt.event.WindowAdapter;
  6import java.awt.event.WindowEvent;
  7import java.io.IOException;
  8import java.util.ArrayList;
  9
 10import javax.swing.*;
 11
 12import com.ericsson.otp.erlang.OtpErlangAtom;
 13import com.ericsson.otp.erlang.OtpErlangDecodeException;
 14import com.ericsson.otp.erlang.OtpErlangLong;
 15
 16import components.TorrentFilter;
 17
 18public class GUI {
 19	
 20	static JLayeredPane pane;
 21	protected JLabel menuborder;
 22	protected ImageIcon border;
 23	public static MenuState currentMenu;
 24	protected Container conn;
 25	protected static ImageIcon fileContentImg;
 26    protected ImageIcon infoBarImg;
 27    protected ImageIcon buttonImg;
 28    protected static ImageIcon trashImg;
 29    protected static ImageIcon startImg;
 30    protected static ImageIcon stopImg;
 31    protected static ImageIcon pauseImg;
 32    protected ImageIcon addImg;
 33    protected ImageIcon downloadDirImg;
 34    protected JLabel buttonBoarder;
 35    protected JButton addButton;
 36    protected static JButton startButton;
 37    protected static JButton pauseButton;
 38    protected static JButton trashButton;
 39    protected static JButton stopButton;
 40    protected JButton downloadDirButton;
 41    protected static JButton fileContentButton;
 42    protected JMenuBar menuBar;
 43    protected JMenu fileMenu;
 44    protected JMenuItem menuItem;
 45    protected ActionListener openTorrentListener;
 46    protected ActionListener openUrlTorrentListener;
 47    protected ActionListener optionMenuItemListener;
 48    protected ActionListener aboutMenuItemListener;
 49    protected JMenu settingMenu;
 50    protected JMenu helpMenu;
 51    protected JMenuItem openUrlMenuItem;
 52    protected JMenuItem openMenuItem;
 53    protected JMenuItem optionMenuItem;
 54    protected JMenuItem aboutMenuItem;
 55    protected static JTextArea fileNameField;
 56    protected static JTextArea fileSizeField;
 57    protected static JTextArea trackerField;
 58    protected static JTextArea statusField;
 59    protected static JTextArea timeLeftField;
 60    protected static JTextArea downloadSpeedField;
 61    protected static JTextArea uploadSpeedField;
 62    protected static JTextArea seedersField;
 63    protected static JTextArea leechersField;
 64    protected static JTextArea downloadedField;
 65    protected static JTextArea uploadedField;
 66    protected static JTextArea defaultDirField;
 67    protected static JLabel openToStart;
 68    final static JFileChooser fc = new JFileChooser();
 69    final JFileChooser fc2 = new JFileChooser();
 70    protected static JProgressBar progressBar;
 71    protected static TalkToErlang tte;
 72    static JFrame frame;
 73    protected static long fileSize;
 74    protected static JInternalFrame internalFrame;
 75    protected static Container internalContainer;
 76    protected static JTextArea internalTextArea;
 77    protected static ArrayList<Torrent> torrents;
 78    protected static ArrayList<JButton> torrentButtons;
 79    
 80	
 81    public enum MenuState {
 82        MAIN, START, START2, START3
 83    }
 84    
 85    public GUI(TalkToErlang obj) {
 86    	frame = new JFrame("Anonymous");
 87        frame.setSize(1025, 573);
 88        frame.setPreferredSize(new Dimension(1025,573));
 89//        frame.setIconImage(new ImageIcon("resources/titleimg.png").getImage());
 90        fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 91        tte = obj;
 92        torrents = new ArrayList<Torrent>();
 93        torrentButtons = new ArrayList<JButton>();
 94        setUpGui();
 95        
 96    }
 97    
 98    public static void displayMenu(MenuState current, final MenuState previous) {
 99        	undisplayMenu(); // we test this position first, before the change of currentMenu, to see how that works out
100        	currentMenu = current;
101
102        switch (currentMenu) {
103        case MAIN: // do all the stuff needed to display the objects unique for main menu (intialize, set all bounds, add to layered pane etc etc)
104        	openToStart = new JLabel("Add a torrent to start downloading");
105        	openToStart.setFont(new Font("Aharoni", Font.BOLD, 30));
106        	openToStart.setVisible(true);
107        	openToStart.setBorder(null);
108        	openToStart.setBounds(250,300,550,50);
109            pane.add(openToStart,1);
110            break;
111        case START:
112        	setTorrentState();
113            break;
114
115        case START2:
116
117
118            break;
119            
120        case START3:
121        	break;
122        }
123    }
124    
125    /**
126     * Undisplays the components to be undisplayed. Sets them to not visible, then
127     * to null. At last it calls the garbage collector, making sure that anything not visble
128     * is not allocated in the memory.
129     */
130    public static void undisplayMenu() {
131
132        switch (currentMenu) {
133        case MAIN: // set all visual objects unique for main menu to null (this will work since this method will be called before the currentMenu is changed)
134            if(openToStart != null) {
135        	openToStart.setVisible(false);
136            openToStart = null; 
137            }
138        	java.lang.Runtime.getRuntime().gc(); //this comes last just before break
139            break;
140        case START:
141        	fileNameField.setVisible(false);
142            fileNameField = null;
143            fileSizeField.setVisible(false);
144            fileSizeField = null;
145            trackerField.setVisible(false);
146            trackerField = null;
147            statusField.setVisible(false);
148            statusField = null;
149            timeLeftField.setVisible(false);
150            timeLeftField = null;
151            trashButton.setVisible(false);
152            trashButton = null;
153            downloadSpeedField.setVisible(false);
154            downloadSpeedField = null;
155            uploadSpeedField.setVisible(false);
156            uploadSpeedField = null;
157            seedersField.setVisible(false);
158            seedersField = null;
159            leechersField.setVisible(false);
160            leechersField = null;
161            downloadedField.setVisible(false);
162            downloadedField = null;
163            uploadedField.setVisible(false);
164            uploadedField = null;
165            progressBar.setVisible(false);
166            progressBar = null;
167            startButton.setVisible(false);
168            startButton = null;
169            pauseButton.setVisible(false);
170            pauseButton = null;
171            stopButton.setVisible(false);
172            stopButton = null;
173            fileContentButton.setVisible(false);
174            fileContentButton = null;
175            java.lang.Runtime.getRuntime().gc();
176            break;
177//
178//        case START2:
179//            java.lang.Runtime.getRuntime().gc(); //this comes last just before break
180//            break;
181//        case START3:
182//          java.lang.Runtime.getRuntime().gc(); //this comes last just before break
183//          break;
184        }
185
186    }
187    
188    /**
189     * Sets up the GUI at startup.
190     */
191    public void setUpGui() {
192        currentMenu = MenuState.MAIN;
193        frame.setLayout(new BorderLayout());
194        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
195        frame.setResizable(false);
196//        files = new ArrayList<String>();
197
198        frame.addWindowListener(new WindowAdapter()
199        {
200              public void windowClosing(WindowEvent e)
201              {
202            	  try {
203  					tte.sendMessage1("exit");
204					System.exit(0);
205   				} catch (Exception e1) {
206					// TODO Auto-generated catch block
207					e1.printStackTrace();
208				}
209              }
210        });
211
212        conn = frame.getContentPane();
213
214        //layeredpane
215        pane = new JLayeredPane();
216        pane.setBounds(0, 25, 1024, 600);
217
218        // main jlabel
219        border = new ImageIcon("resources/mainbar.png");
220        menuborder = new JLabel(border);
221        menuborder.setVisible(true);
222        menuborder.setBounds(0, 100, border.getIconWidth(), border.getIconHeight());
223        border = null;
224        pane.add(menuborder, -1);
225
226        //jlabel for Buttons
227        buttonImg = new ImageIcon("resources/buttonbar.png");
228        buttonBoarder = new JLabel(buttonImg);
229        buttonBoarder.setVisible(true);
230        buttonBoarder.setBounds(0,0,buttonImg.getIconWidth(),buttonImg.getIconHeight());
231        buttonImg = null;
232        pane.add(buttonBoarder, -1);
233        
234        addImg = new ImageIcon("resources/add.png");
235        addButton = new JButton(addImg);
236        addButton.setToolTipText("Open a new torrent file");
237        addButton.setVisible(true);
238        addButton.setBorder(null);
239        addButton.setContentAreaFilled(false);
240        addButton.setBounds(25,20,addImg.getIconWidth(),addImg.getIconHeight());
241        addImg = null;
242        pane.add(addButton,1);
243        
244        addButton.addActionListener(new ActionListener() {
245            public void actionPerformed(ActionEvent e) {
246            	fc.addChoosableFileFilter(new TorrentFilter());
247            	int returnval = fc.showOpenDialog(addButton);
248            	if (returnval == JFileChooser.CANCEL_OPTION) {
249            		System.out.println("canceled by user"); 
250            	} else {
251                	String path;
252//                	undisplayMenu();
253                	displayMenu(MenuState.START, MenuState.MAIN);
254                	path = fc.getSelectedFile().getAbsolutePath();
255                	System.out.println(path);
256                	try {
257                		tte.sendMessage("open", path);
258                		statusField.setText("Status: " +"Active");
259            		} catch (Exception e1) {
260            			// TODO Auto-generated catch block
261            			e1.printStackTrace();
262            		}
263            	}
264            }
265        });
266
267        downloadDirImg = new ImageIcon("resources/downloaddir.png");
268        downloadDirButton = new JButton(downloadDirImg);
269        downloadDirButton.setVisible(true);
270        downloadDirButton.setBorder(null);
271        downloadDirButton.setToolTipText("Choose a download directory");
272        downloadDirButton.setContentAreaFilled(false);
273        downloadDirButton.setBounds(920,20,downloadDirImg.getIconWidth(),downloadDirImg.getIconHeight());
274        downloadDirImg = null;
275        pane.add(downloadDirButton,1);
276        
277        
278        downloadDirButton.addActionListener(new ActionListener() {
279            public void actionPerformed(ActionEvent e) {
280            	int returnval = fc2.showOpenDialog(downloadDirButton);
281            	if (returnval == JFileChooser.CANCEL_OPTION) {
282            		System.out.println("canceled by user"); 
283            	} else {
284                	String path;
285                	path = fc2.getSelectedFile().getAbsolutePath();
286                	System.out.println(path);
287                	try {
288                		tte.sendMessage("dir", path);
289                    	defaultDirField.setText(path);
290            		} catch (Exception e1) {
291            			// TODO Auto-generated catch block
292            			e1.printStackTrace();
293            		}
294            	}
295            }
296        });
297        
298        defaultDirField = new JTextArea();
299        defaultDirField.setFont(new Font("Aharoni", 0, 10));
300        defaultDirField.setVisible(true);
301        defaultDirField.setBorder(null);
302        defaultDirField.setOpaque(false);
303        defaultDirField.setEditable(false);
304        defaultDirField.setBounds(700,80,300,50);
305        pane.add(defaultDirField, 0);
306        
307        //Menubar
308        menuBar = new JMenuBar();
309        //MenuItems
310        helpMenu = new JMenu("Help");
311        helpMenu.setMnemonic(KeyEvent.VK_F);
312        menuBar.add(helpMenu);
313
314        //Help Menu Items
315        aboutMenuItem = new JMenuItem("About", KeyEvent.VK_N);
316        helpMenu.add(aboutMenuItem);
317        
318        aboutMenuItem.addActionListener(new ActionListener() {
319            public void actionPerformed(ActionEvent e) {
320            	//about case or new frame?
321            }
322        });
323        
324        displayMenu(MenuState.MAIN, MenuState.MAIN);
325        conn.add(pane);
326        frame.setJMenuBar(menuBar);
327        frame.pack();
328        frame.setVisible(true);
329    }
330    	public static void setTorrentState() {
331    		
332    		 startImg = new ImageIcon("resources/play.png");
333             startButton = new JButton(startImg);
334             startButton.setToolTipText("Starts a paused or stopped torrent");
335             startButton.setVisible(false);
336             startButton.setBorder(null);
337             startButton.setContentAreaFilled(false);
338             startButton.setBounds(150,20,startImg.getIconWidth(),startImg.getIconHeight());
339             startImg = null;
340             pane.add(startButton,1);
341             
342             startButton.addActionListener(new ActionListener() {
343                 public void actionPerformed(ActionEvent e) {
344     				try {
345     					tte.sendMessage(getActiveTorrent().getId(), "start");
346     					startButton.setVisible(false);
347     					pauseButton.setVisible(true);
348     					stopButton.setVisible(true);
349     					statusField.setText("Status: " +"Active");
350     				} catch (Exception e1) {
351     					// TODO Auto-generated catch block
352     					e1.printStackTrace();
353     				}
354                 }
355             });
356             
357             pauseImg = new ImageIcon("resources/pause.png");
358             pauseButton = new JButton(pauseImg);
359             pauseButton.setVisible(true);
360             pauseButton.setBorder(null);
361             pauseButton.setToolTipText("Pauses torrent");
362             pauseButton.setContentAreaFilled(false);
363             pauseButton.setBounds(275,20,pauseImg.getIconWidth(),pauseImg.getIconHeight());
364             pauseImg = null;
365             pane.add(pauseButton,1);
366             
367             pauseButton.addActionListener(new ActionListener() {
368                 public void actionPerformed(ActionEvent e) {
369                 	System.out.println("1");
370     							try {
371     								pauseButton.setVisible(false);
372     								stopButton.setVisible(false);
373     								startButton.setVisible(true);
374     								tte.sendMessage(getActiveTorrent().getId(), "pause");
375     								statusField.setText("Status: " +"Paused");
376     							} catch (Exception e1) {
377     								// TODO Auto-generated catch block
378     								e1.printStackTrace();
379     							}
380                 }
381             });
382             
383             stopImg = new ImageIcon("resources/stop.png");
384             stopButton = new JButton(stopImg);
385             stopButton.setVisible(true);
386             stopButton.setToolTipText("Stops a downloading torrent");
387             stopButton.setBorder(null);
388             stopButton.setContentAreaFilled(false);
389             stopButton.setBounds(400,20,stopImg.getIconWidth(),stopImg.getIconHeight());
390             pane.add(stopButton,1);
391             stopImg = null;
392             
393             stopButton.addActionListener(new ActionListener() {
394                 public void actionPerformed(ActionEvent e) {
395     							try {
396     								stopButton.setVisible(false);
397     								pauseButton.setVisible(false);
398     								startButton.setVisible(true);
399     								tte.sendMessage(getActiveTorrent().getId(), "stop");
400     								statusField.setText("Status: " +"Stopped");
401     							} catch (Exception e1) {
402     								// TODO Auto-generated catch block
403     								e1.printStackTrace();
404     							}
405                 
406                 }
407             });
408             
409             fileContentImg = new ImageIcon("resources/files.png");
410             fileContentButton = new JButton(fileContentImg);
411             fileContentButton.setVisible(true);
412             fileContentButton.setToolTipText("Display downloading content");
413             fileContentButton.setBorder(null);
414             fileContentButton.setContentAreaFilled(false);
415             fileContentButton.setBounds(20,320,fileContentImg.getIconWidth(),fileContentImg.getIconHeight());
416             pane.add(fileContentButton,1);
417             fileContentImg = null;
418             
419             fileContentButton.addActionListener(new ActionListener() {
420                 public void actionPerformed(ActionEvent e) {
421                	 internalFrame = new JInternalFrame("Files downloading");
422                	 internalFrame.setSize(300, 300);
423                	 internalFrame.setVisible(true);
424                	 internalFrame.setLocation(400, 200);
425                	 internalFrame.setClosable(true);
426                	 pane.add(internalFrame, 0);
427                	 internalTextArea = new JTextArea();
428                	 internalTextArea.setBounds(0, 0, 400, 200);
429                	 internalTextArea.setVisible(true);
430                	 internalTextArea.setFont(new Font("Aharoni", 0, 15));
431                	 internalTextArea.setBackground(Color.orange);
432                	 internalContainer = internalFrame.getContentPane();
433                	 String text = "";
434                	 for (int i = 0; i < getActiveTorrent().getFiles().size(); i++) {
435                		 text += getActiveTorrent().getFiles().get(i) + "\n";
436                	 }
437                	 internalTextArea.setText(text);
438                	 internalContainer.add(internalTextArea);
439                 }
440             });
441             
442             trashImg = new ImageIcon("resources/trash.png");
443             trashButton = new JButton();
444             trashButton.setIcon(trashImg);
445             trashButton.setVisible(true);
446             trashButton.setToolTipText("Remove torrent");
447             trashButton.setContentAreaFilled(false);
448             trashButton.setBorder(null);
449             trashButton.setBounds(525,20,trashImg.getIconWidth(),trashImg.getIconHeight());
450             trashImg = null;
451             pane.add(trashButton,1);
452             
453             trashButton.addActionListener(new ActionListener() {
454                 public void actionPerformed(ActionEvent e) {
455                    	int returnval = JOptionPane.showConfirmDialog(
456                    		    frame,
457                    		    "Would you really want to delete this torrent? all data will be lost",
458                    		    "Delete torrent?",
459                    		    JOptionPane.YES_NO_OPTION);
460                 	if (returnval == JOptionPane.NO_OPTION) {
461                 		System.out.println("canceled by user"); 
462                 	} else {
463     				try {
464     					tte.sendMessage(getActiveTorrent().getId(), "delete");
465     					Torrent torrent = getActiveTorrent();
466     					for (int i = 0; i < torrents.size(); i++) {
467     						if (torrent.getId().equals(torrents.get(i).getId())) {
468     							torrents.get(i).getTorrentButton().setVisible(false);
469     							torrents.set(i, null);
470     							torrents.remove(i);
471     						}
472     						else if(torrents.size() == i+1)
473     							torrents.get(i).getTorrentButton().setLocation((100*i+3), torrents.get(i).getTorrentButton().getY());
474     					}
475     				} catch (Exception e1) {
476     					// TODO Auto-generated catch block
477     					e1.printStackTrace();
478     				}
479                 	}
480                 }
481             });
482
483             //TextFields for information about torrent(filename, size, tracker)       
484             fileNameField = new JTextArea("File name:");
485             fileNameField.setFont(new Font("Aharoni", 0, 15));
486             fileNameField.setVisible(true);
487             fileNameField.setBorder(null);
488             fileNameField.setOpaque(false);
489             fileNameField.setEditable(false);
490             fileNameField.setBounds(20, 145, 500, 50);
491             pane.add(fileNameField, 0);
492             
493             fileSizeField = new JTextArea("File size:");
494             fileSizeField.setFont(new Font("Aharoni", 0, 15));
495             fileSizeField.setVisible(true);
496             fileSizeField.setBorder(null);
497             fileSizeField.setOpaque(false);
498             fileSizeField.setEditable(false);
499             fileSizeField.setBounds(20, 185, 400, 50);
500             pane.add(fileSizeField, 0);
501             
502             trackerField = new JTextArea("Tracker:");
503             trackerField.setFont(new Font("Aharoni", 0, 15));
504             trackerField.setVisible(true);
505             trackerField.setBorder(null);
506             trackerField.setOpaque(false);
507             trackerField.setEditable(false);
508             trackerField.setBounds(20, 225, 400, 50);
509             pane.add(trackerField, 0);
510             
511             statusField = new JTextArea("Status:");
512             statusField.setFont(new Font("Aharoni", 0, 15));
513             statusField.setVisible(true);
514             statusField.setBorder(null);
515             statusField.setOpaque(false);
516             statusField.setEditable(false);
517             statusField.setBounds(20, 265, 400, 50);
518             pane.add(statusField, 0);
519             
520             timeLeftField = new JTextArea("Time left:");
521             timeLeftField.setFont(new Font("Aharoni", 0, 15));
522             timeLeftField.setVisible(true);
523             timeLeftField.setBorder(null);
524             timeLeftField.setOpaque(false);
525             timeLeftField.setEditable(false);
526             timeLeftField.setBounds(750, 145, 200, 50);
527             pane.add(timeLeftField, 0);
528             
529             //seeders,leechers,download & upload speeds
530             downloadSpeedField = new JTextArea("Download speed:");
531             downloadSpeedField.setFont(new Font("Aharoni", 0, 15));
532             downloadSpeedField.setVisible(true);
533             downloadSpeedField.setBorder(null);
534             downloadSpeedField.setOpaque(false);
535             downloadSpeedField.setEditable(false);
536             downloadSpeedField.setBounds(20, 420, 200, 50);
537             pane.add(downloadSpeedField, 0);
538             
539             uploadSpeedField = new JTextArea("Upload speed:");
540             uploadSpeedField.setFont(new Font("Aharoni", 0, 15));
541             uploadSpeedField.setVisible(true);
542             uploadSpeedField.setBorder(null);
543             uploadSpeedField.setOpaque(false);
544             uploadSpeedField.setEditable(false);
545             uploadSpeedField.setBounds(260, 420, 200, 50);
546             pane.add(uploadSpeedField, 0);
547             
548             seedersField = new JTextArea("Seeders:");
549             seedersField.setFont(new Font("Aharoni", 0, 15));
550             seedersField.setVisible(true);
551             seedersField.setBorder(null);
552             seedersField.setOpaque(false);
553             seedersField.setEditable(false);
554             seedersField.setBounds(500, 420, 200, 50);
555             pane.add(seedersField, 0);
556             
557             leechersField = new JTextArea("Leechers:");
558             leechersField.setFont(new Font("Aharoni", 0, 15));
559             leechersField.setVisible(true);
560             leechersField.setBorder(null);
561             leechersField.setOpaque(false);
562             leechersField.setEditable(false);
563             leechersField.setBounds(740, 420, 200, 50);
564             pane.add(leechersField, 0);
565             
566             downloadedField = new JTextArea("Downloaded:");
567             downloadedField.setFont(new Font("Aharoni", 0, 15));
568             downloadedField.setVisible(true);
569             downloadedField.setBorder(null);
570             downloadedField.setOpaque(false);
571             downloadedField.setEditable(false);
572             downloadedField.setBounds(750, 185, 200, 50);
573             pane.add(downloadedField, 0);
574             
575             uploadedField = new JTextArea("Uploaded:");
576             uploadedField.setFont(new Font("Aharoni", 0, 15));
577             uploadedField.setVisible(true);
578             uploadedField.setBorder(null);
579             uploadedField.setOpaque(false);
580             uploadedField.setEditable(false);
581             uploadedField.setBounds(750, 225, 200, 50);
582             pane.add(uploadedField, 0);
583             
584             //Progress Bar
585             progressBar = new JProgressBar(0, 100);
586             progressBar.setValue(0);
587             progressBar.setStringPainted(true);
588             progressBar.setVisible(true);
589             progressBar.setForeground(Color.ORANGE);
590             progressBar.setBackground(Color.black);
591             progressBar.setFont(new Font("Aharoni", 0, 25));
592             progressBar.setBounds(20, 450, 980, 50);
593             pane.add(progressBar, 0);
594    	}
595    	public static void setField(OtpErlangLong torrentId, int tag, String value) {
596    		Torrent torrent = getTorrent(torrentId);
597    		switch (tag) {
598    		case 0:
599    			System.out.println("FILENAME: " + value);
600    			torrent.setFileName(value);
601    			torrent.getTorrentButton().setText(value);
602    			torrent.getTorrentButton().setToolTipText("Display torrent: " + torrent.getFileName());
603    		break;
604    		case 1: 
605    			torrent.setFileSize(Long.parseLong(value)); 			
606    		break;
607			case 2:
608				torrent.setTracker(value);
609    		break;
610    		case 3:
611    			torrent.setDownloadSpeed(Integer.parseInt(value));
612    		break;
613    		case 4:
614    			torrent.setUploadSpeed(Integer.parseInt(value));
615    		break;
616    		case 5:
617    			torrent.setSeeders(Integer.parseInt(value));
618    		break;
619    		case 6:
620    			torrent.setLeechers(Integer.parseInt(value));
621    		break;
622    		case 7:
623    			torrent.setDownloaded(Double.parseDouble(value));
624    			torrent.setPercentage((int)(Integer.parseInt(value)/(double)torrent.getFileSize()*100));
625    		break;
626    		case 8:
627    			torrent.setUploaded(Double.parseDouble(value));
628        		break;
629    		case 9:
630    			torrent.setStatus(value);
631    		break;
632    		case 10: 
633    			ArrayList<String> files = torrent.getFiles();
634    			files.add(value);
635    			torrent.setFiles(files);
636    		break;
637    		}
638    		displayTorrent(getActiveTorrent());
639    	}
640
641    	public static void displayTorrent(Torrent torrent) {
642    		if(fileSizeField != null)
643    			fileSizeField.setText("File size: " + torrent.getFileSize()/1048576 + " Mb");  
644    		if(trackerField != null)
645    			trackerField.setText("Tracker: " + torrent.getTracker());
646    		if(downloadSpeedField != null)
647    			downloadSpeedField.setText("Download speed: " + torrent.getDownloadSpeed() +" Kb/s");
648    		if (uploadSpeedField != null)
649    			uploadSpeedField.setText("Upload speed: " + torrent.getUploadSpeed() +" Kb/s");
650    		if (seedersField != null)
651    			seedersField.setText("Seeders: " + torrent.getSeeders());
652    		if (leechersField != null)
653    			leechersField.setText("Leechers: " + torrent.getLeechers());
654    		if (downloadedField != null)
655    			downloadedField.setText("Downloaded: " + torrent.getDownloaded() + " Mb");
656    		if (progressBar != null)
657    			progressBar.setValue(torrent.getPercentage());
658    		if (uploadedField != null)
659    			uploadedField.setText("Uploaded: " + torrent.getUploaded() + " Mb");
660    		if (statusField != null)
661    			statusField.setText("Status: " + torrent.getStatus());
662    		if (fileNameField != null)
663    			fileNameField.setText("Filename: " + torrent.getFileName());
664    	}
665    	
666    	private static Torrent getActiveTorrent() {
667    		Torrent torrent;
668    		for (int i = 0; i < torrents.size(); i++) {
669    			torrent = torrents.get(i);
670    			if(torrent.isActive)
671    				return torrent;
672    		}
673    		return null;
674    	}
675    	
676    	private static Torrent getTorrent(OtpErlangLong torrentId) {
677    		Torrent torrent;
678    		for (int i = 0; i < torrents.size(); i++) {
679    			torrent = torrents.get(i);
680    			if(torrentId.equals(torrent.getId()))
681    				return torrent;
682    		}
683    		return addTorrent(torrentId);
684    	}
685    	
686    	public static Torrent addTorrent(OtpErlangLong torrentId) {
687    		final Torrent newTorrent = new Torrent(torrentId);
688    		torrents.add(newTorrent);
689    		newTorrent.setTorrentButton(newTorrent, torrents.size()-1);
690    		pane.add(newTorrent.getTorrentButton(),1);        
691    		
692            newTorrent.getTorrentButton().addActionListener(new ActionListener() {
693                public void actionPerformed(ActionEvent e) {
694                	setActiveTorrent(newTorrent.getId());
695                	displayTorrent(newTorrent);
696                }
697            });
698            
699            setActiveTorrent(torrentId);
700    		return newTorrent;
701    	}
702    	
703    	public static void setActiveTorrent(OtpErlangLong torrentId) {
704    		for (int i = 0; i < torrents.size(); i++) {
705    			Torrent torrent = torrents.get(i);
706    			if (torrent.getId().equals(torrentId)) {
707    				torrent.setActive(true);
708    				torrent.getTorrentButton().setBackground(Color.ORANGE);
709    			}
710    			else {
711    				torrent.setActive(false);
712    				torrent.getTorrentButton().setBackground(Color.GRAY);
713    			}
714    		}
715    	}
716}