Advertisement
Guest User

App.java

a guest
May 27th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.38 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Vector;
  10.  
  11. public class App {
  12.     private JList Directiories;
  13.     private JFormattedTextField directorySelector;
  14.     private JButton goButton;
  15.     private JPanel panel;
  16.     private JButton readButton;
  17.     private JButton loginButton;
  18.     private JButton Logout;
  19.     private JButton backButton;
  20.     public Session session = null;
  21.     public String lastPath;
  22.     List<String> shared = new LinkedList<>();
  23.     List<FileDSM> filese = new LinkedList<>();
  24.     List<String> temp = new LinkedList<>();
  25.     public App() {
  26.         loginButton.addActionListener(new ActionListener() {
  27.             @Override
  28.             public void actionPerformed(ActionEvent actionEvent) {
  29.                 Login log = new Login();
  30.                 session = log.sess;
  31.                 Vector<String> v = new Vector<>();
  32.                 List<FileDSM> f = new LinkedList<>();
  33.                 v.clear();
  34.  
  35.                 try {
  36.                     for (FileStationList.SharedFolder i : new FileStationList(session).listShared()) {
  37.                         shared.add(i.path);
  38.                         v.add(i.name);
  39.                         f.add(new FileDSM(i.path, i.name, true));
  40.                         lastPath = i.path.substring(0, i.path.lastIndexOf("/"));
  41.                         System.out.println(lastPath);
  42.                     }
  43.                     filese = f;
  44.                 } catch (IOException e) {
  45.                     e.printStackTrace();
  46.                 }
  47.                 Directiories.setListData(v);
  48.  
  49.             }
  50.         });
  51.         Logout.addActionListener(new ActionListener() {
  52.             @Override
  53.             public void actionPerformed(ActionEvent actionEvent) {
  54.                 try {
  55.                     logout();
  56.                 } catch (IOException e) {
  57.                     e.printStackTrace();
  58.                 }
  59.             }
  60.         });
  61.         goButton.addActionListener(new ActionListener() {
  62.             @Override
  63.             public void actionPerformed(ActionEvent actionEvent) {
  64.                 try {
  65.                     int index = Directiories.getSelectedIndex();
  66.                     List<FileDSM> files = new FileStationList(session).list(filese.get(index).path);
  67.  
  68.                     Vector<String> s = new Vector<>();
  69.                     for (FileDSM i : files) {
  70.                         s.add(i.path);
  71.                     }
  72.                     lastPath = files.get(0).path.substring(0, files.get(0).path.lastIndexOf("/"));
  73.                     filese = files;
  74.                     Directiories.setListData(s);
  75.                     System.out.println(lastPath);
  76.                 } catch (IOException e) {
  77.                     e.printStackTrace();
  78.                 }
  79.             }
  80.         });
  81.         backButton.addActionListener(new ActionListener() {
  82.             @Override
  83.             public void actionPerformed(ActionEvent actionEvent) {
  84.                 try {
  85.                     List<FileDSM> files = null;
  86.                     if(shared.contains(lastPath)){
  87.                         List<FileStationList.SharedFolder> filess = new FileStationList(session).listShared();
  88.                         files = new LinkedList<>();
  89.                         for (FileStationList.SharedFolder i : filess){
  90.                             files.add(new FileDSM(i.path, i.name, true));
  91.                         }
  92.                     } else {
  93.                         files = new FileStationList(session).list(lastPath);
  94.                     }
  95.                     Vector<String> s = new Vector<>();
  96.                     for (FileDSM i : files) {
  97.                         s.add(i.path);
  98.  
  99.                     }
  100.                     lastPath = files.get(0).path.substring(0, files.get(0).path.lastIndexOf("/"));
  101.                     System.out.println(lastPath);
  102.  
  103.                     filese = files;
  104.                     Directiories.setListData(s);
  105.                 } catch (IOException e) {
  106.                     e.printStackTrace();
  107.                 }
  108.             }
  109.         });
  110.         readButton.addActionListener(new ActionListener() {
  111.             @Override
  112.             public void actionPerformed(ActionEvent actionEvent) {
  113.                 int index = Directiories.getSelectedIndex();
  114.                 Runnable run = new Runnable() {
  115.                     @Override
  116.                     public void run() {
  117.                         try {
  118.                             byte[] file = new FileStationDownload(session).download(filese.get(index).path);
  119.                             File file1 = File.createTempFile("muz", ".tmp");
  120.                             FileOutputStream  f = new FileOutputStream(file1);
  121.                             f.write(file);
  122.                             temp.add(file1.getAbsolutePath());
  123.                             Desktop.getDesktop().open(file1);
  124.                         } catch (IOException e) {
  125.                             e.printStackTrace();
  126.                         }
  127.                     }
  128.                 };
  129.                 Thread t = new Thread(run);
  130.                 t.start();
  131.                 try {
  132.                     t.join();
  133.                 } catch (InterruptedException e) {
  134.                     e.printStackTrace();
  135.                 }
  136.             }
  137.         });
  138.     }
  139.     public void logout() throws IOException {
  140.         Auth.logout(session);
  141.     }
  142.     public void cleanTemp(){
  143.         for(String i : temp){
  144.             new File(i).delete();
  145.         }
  146.     }
  147.     public static void main(String[] args) throws Exception {
  148.         JFrame frame = new JFrame();
  149.         App app = new App();
  150.         frame.setContentPane(app.panel);
  151.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  152.         frame.pack();
  153.         frame.setVisible(true);
  154.         Thread.sleep(2000);
  155.         WindowListener exitListene = new WindowAdapter() {
  156.             @Override
  157.             public void windowClosing(WindowEvent windowEvent) {
  158.                 super.windowClosing(windowEvent);
  159.                 try {
  160.                     app.logout();
  161.                     app.cleanTemp();
  162.                 } catch (IOException e) {
  163.                     e.printStackTrace();
  164.                 }
  165.             }
  166.         };
  167.         frame.addWindowListener(exitListene);
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement