/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

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