daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Jan 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package application;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.Arrays;
  11. import java.util.concurrent.TimeUnit;
  12. import java.util.logging.Logger;
  13.  
  14. import javafx.application.Platform;
  15. import javafx.fxml.FXML;
  16. import javafx.fxml.FXMLLoader;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Label;
  19. import javafx.scene.image.Image;
  20. import javafx.scene.image.ImageView;
  21. import javafx.stage.Stage;
  22.  
  23. public class BondController
  24. {
  25.     Stage stgBond;
  26.     Compound comp;
  27.  
  28.     @FXML
  29.     private ImageView imgStructure;
  30.     @FXML
  31.     private Label lblFormula;
  32.     @FXML
  33.     private Label lblName;
  34.     @FXML
  35.     private Label lblType;
  36.     @FXML
  37.     private Label lblPolarity;
  38.     @FXML
  39.     private Label lblMoleculeShape;
  40.  
  41.     public BondController(Compound c)
  42.     {
  43.         stgBond = new Stage();
  44.         comp = c;
  45.         imgStructure = new ImageView();
  46.         try
  47.         {
  48.             stgBond.getIcons().add(new Image(this.getClass().getResourceAsStream("icon.png")));
  49.             FXMLLoader loader = new FXMLLoader(getClass().getResource("Bond Info.fxml"));
  50.  
  51.             loader.setController(this);
  52.             stgBond.setScene(new Scene(loader.load()));
  53.             stgBond.setTitle("Bond");
  54.         }
  55.         catch(Exception e)
  56.         {
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.  
  61.     public void initialize()
  62.     {
  63.         display(findCID());
  64.  
  65.         lblFormula.setText(Compound.subscript(comp.getFormula()));
  66.         lblName.setText(comp.getName()[0]);
  67.  
  68.         if(lblName.getText().equals("Hydrogen Fluoride"))
  69.             lblName.setText("Hydrofluoric Acid");
  70.  
  71.         lblType.setText(comp.getBondType());
  72.         lblPolarity.setText(comp.getBondPolarity());
  73.         lblMoleculeShape.setText(comp.getMoleculeShape());
  74.  
  75.         Platform.runLater( () -> { File file = new File("bin/resources/structure.png"); Image image = new Image(file.toURI().toString()); imgStructure.setImage(image); } );
  76.     }
  77.  
  78.     public String findCID()
  79.     {
  80.         org.jsoup.nodes.Document doc = null, doc1 = null,doc2 = null;
  81.  
  82.         try
  83.         {
  84.             String url = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/" + comp.getName()[0].replace(" ","%20").trim() + "/record/SDF/?record_type=2d&response_type=display";
  85.             doc = org.jsoup.Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0").timeout(0).get();
  86.             return doc.getAllElements().text().split(" ")[0];
  87.         }
  88.         catch (Exception e)
  89.         {
  90.             try
  91.             {
  92.                 doc1 = org.jsoup.Jsoup.connect("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/formula/" + comp.getFormula() + "/txt").userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0").timeout(3000).get();
  93.                 String listkey = doc1.getAllElements().text().split("Your request is running ListKey: ")[1].trim();
  94.                 TimeUnit.SECONDS.sleep(4);
  95.                 try
  96.                 {
  97.                     doc2 = org.jsoup.Jsoup.connect("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/listkey/" + listkey + "/cids/txt").userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36").referrer("http://www.google.com").timeout(0).get();
  98.                     String[] arr = doc2.getAllElements().text().split("\\s+");
  99.                     return String.valueOf(sorting(arr)[0]);
  100.                 }
  101.                 catch (IOException e3)
  102.                 {
  103.                     System.out.println("broke");
  104.                     e3.printStackTrace();
  105.                 }
  106.                 return "No Results!";
  107.             }
  108.             catch(Exception e1)
  109.             {
  110.                 e1.printStackTrace();
  111.             }
  112.         }
  113.         return "No Results!";
  114.     }
  115.  
  116.     public void display(String CID)
  117.     {
  118.         InputStream inputStream = null;
  119.         OutputStream outputStream = null;
  120.  
  121.         try
  122.         {
  123.             URL url = new URL("https://pubchem.ncbi.nlm.nih.gov/image/imagefly.cgi?cid=" + CID + "&width=500&height=500");
  124.             String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
  125.             URLConnection con = url.openConnection();
  126.             con.setRequestProperty("User-Agent", USER_AGENT);
  127.             inputStream = con.getInputStream();
  128.             String path = BondController.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/resources";
  129.             new File(path + "/structure.png").delete();
  130.             outputStream = new FileOutputStream(new java.io.File(path + "/structure.png"), false);
  131.  
  132.             byte[] buffer = new byte[2048];
  133.  
  134.             int length;
  135.             while ((length = inputStream.read(buffer)) != -1)
  136.             {
  137.                 outputStream.write(buffer, 0, length);
  138.             }
  139.             outputStream.close();
  140.             inputStream.close();
  141.         }
  142.         catch (Exception ex)
  143.         {
  144.             Logger.getLogger("Compound Image Not Available");
  145.         }
  146.     }
  147.  
  148.     public int[] sorting(String[] arr)
  149.     {
  150.         String[] temp = arr.clone();
  151.         int[] temp1 = new int[temp.length];
  152.         for(int i = 0; i < arr.length; i++)
  153.         {
  154.             if(temp[i].matches("-?\\d+"))
  155.             {
  156.                 temp1[i] = Integer.parseInt(temp[i].trim());
  157.             }
  158.         }
  159.         Arrays.sort(temp1);
  160.         return temp1;
  161.     }
  162.  
  163.     public void showStage()
  164.     {
  165.         stgBond.show();
  166.     }
  167.  
  168.     public void loadBack()
  169.     {
  170.         BondTableController ctrlBondTable = new BondTableController();
  171.         ctrlBondTable.showStage();
  172.         stgBond.close();
  173.     }
  174.  
  175.     public void loadMenu()
  176.     {
  177.         MainMenuController ctrlMainMenu = new MainMenuController();
  178.         ctrlMainMenu.showStage();
  179.         stgBond.close();
  180.     }
  181.  
  182. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top