Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.04 KB | None | 0 0
  1. package com.subreddimages;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import java.awt.Color;
  7. import javax.swing.JPanel;
  8. import javax.swing.JTextField;
  9. import javax.swing.JTextPane;
  10. import javax.swing.SwingWorker;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13.  
  14. import java.awt.Font;
  15. import javax.swing.JComboBox;
  16. import javax.swing.DefaultComboBoxModel;
  17. import javax.swing.border.SoftBevelBorder;
  18. import javax.swing.border.BevelBorder;
  19. import javax.swing.JFileChooser;
  20. import javax.swing.JButton;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23. import java.awt.event.MouseListener;
  24. import java.io.BufferedReader;
  25. import java.io.IOException;
  26. import java.io.InputStreamReader;
  27. import java.io.PrintWriter;
  28. import java.io.StringWriter;
  29. import java.util.logging.Level;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32. import javax.swing.JCheckBox;
  33. import java.awt.Component;
  34.  
  35. import javax.swing.ButtonGroup;
  36. import javax.swing.JSlider;
  37. import javax.swing.event.ChangeListener;
  38. import javax.swing.filechooser.FileNameExtensionFilter;
  39.  
  40. import org.apache.commons.lang3.StringUtils;
  41.  
  42. import com.subreddimages.GUIComponents.GUIComponents;
  43.  
  44. import javax.swing.event.ChangeEvent;
  45. import javax.swing.JProgressBar;
  46. import javax.swing.JRadioButton;
  47.  
  48. public class MainGUI {
  49.    
  50.     private GUIComponents.Components cm = new GUIComponents.Components();  
  51.     private Font font = new Font("Courier New", Font.PLAIN, 13);
  52.     private JFrame frame;
  53.     private JTextField sub_name;
  54.     private JProgressBar progressBar;
  55.     private JLabel directory_label;
  56.    
  57.     JPanel sub_options;
  58.     JPanel sub_select;
  59.     String path;
  60.     String path_to_image;
  61.     JButton start;
  62.     Process p = null;
  63.     static JTextPane console_area = new JTextPane();
  64.     ButtonGroup buttons = new ButtonGroup();
  65.    
  66.     MouseListener startML = new MouseAdapter() {
  67.         public void mouseClicked(java.awt.event.MouseEvent evt) {
  68.             SanityCheck();
  69.         }
  70.     };
  71.     MouseListener endML = new MouseAdapter() {
  72.         @Override
  73.         public void mouseClicked(MouseEvent e) {
  74.             try {
  75.                 CancelProcess(true);
  76.             } catch (Exception e1) {
  77.             }
  78.         }
  79.     };
  80.     static StringWriter sw = new StringWriter();
  81.     static PrintWriter pw = new PrintWriter(sw);
  82.     SwingWorker python = null;
  83.  
  84.     public static void main(String[] args) {
  85.         EventQueue.invokeLater(new Runnable() {
  86.             public void run() {
  87.                 startGU();
  88.             }
  89.         });
  90.     }
  91.  
  92.     public static void startGU() {
  93.  
  94.         LoggerMain.initialize(console_area);
  95.        
  96.         LoggerMain.Log(String.format("OS: %s, OSver: %s, OSarch: %s, JAVAver: %s",
  97.                 System.getProperty("os.name"),
  98.                 System.getProperty("os.version"),
  99.                 System.getProperty("os.arch"),
  100.                 System.getProperty("java.version")),
  101.                 console_area,
  102.                 Level.CONFIG);
  103.  
  104.         MainGUI main = new MainGUI();
  105.         main.frame.setVisible(true);
  106.  
  107.         LoggerMain.Log("GUI initalized successfuly!", console_area, Level.INFO);
  108.  
  109.     }
  110.  
  111.     public MainGUI() {
  112.         LoggerMain.Log("Initializing GUI", console_area, Level.INFO);
  113.         initialize();
  114.     }
  115.  
  116.     /**
  117.      * Initialize the contents of the frame.
  118.      *
  119.      * @wbp.parser.entryPoint
  120.      */
  121.     private void initialize() {
  122.  
  123.         frame = cm.JFrame("Subreddimages", font, false, Color.DARK_GRAY, JFrame.EXIT_ON_CLOSE, null, new Integer[] {100, 100, 771, 507});
  124.  
  125.         // ADDING EVERYTHING TO GUI
  126.         SubredditSelection(frame);
  127.         SubredditOptions(frame);
  128.         Console(frame);
  129.  
  130.         // START THE PROCESS BUTTON
  131.         start = cm.JButton("Run!", startML, "Starts the process.", Color.BLACK, font, new Integer[] {94, 439, 247, 29});
  132.         frame.getContentPane().add(start);
  133.  
  134.         HelpfulFunctions.createComponentMap(sub_options);
  135.    
  136.        
  137.     }
  138.  
  139.     public void startPythonThread() {
  140.         // REMOVING OLD MOUSE LISTER SO AS NOT TO TRIGGER THIS FUNCTION AGAIN
  141.         start.removeMouseListener(startML);
  142.         // DISABLING ALL COMPONENTS SO THEY CANNOT BE INTERACTED WITH
  143.         for (Component component : HelpfulFunctions.getComponents(sub_select)) {
  144.             component.setEnabled(false);
  145.         }
  146.         for (Component component : HelpfulFunctions.getComponents(sub_options)) {
  147.             component.setEnabled(false);
  148.         }
  149.         // RE-ENABLING BUTTON
  150.         start.setEnabled(true);
  151.         start.addMouseListener(endML);
  152.         // CHANGING TEXT
  153.         start.setText("Cancel");
  154.         HelpfulFunctions.infoBox("This could take a long time!", "Warning", JOptionPane.WARNING_MESSAGE);
  155.  
  156.         python = new SwingWorker() // HAVE TO REINSTANTIATE IT EVERY TIME BECAUSE THEY CAN ONLY BE USED ONCE
  157.         {
  158.             @Override
  159.             protected Boolean doInBackground() throws Exception {
  160.                
  161.                 String command = "";
  162.                
  163.                 try {
  164.  
  165.                 command = String.format("python %s %s %s %s %s %s %s %s %s",
  166.                         System.getProperty("user.dir") + "/start_process.py", // SCRIPT DIRECTORY
  167.                         String.format("-m %s", ((JSlider)HelpfulFunctions.getComponentByName("max_img_slider")).getValue()), // MAXIMUM NUMBER OF IMAGES
  168.                         String.format("-s %s", sub_name.getText()), // SUBREDDIT NAME
  169.                         String.format("-n %s", StringUtils.capitalize(Boolean.toString(((JCheckBox) HelpfulFunctions.getComponentByName("nsfw_check")).isSelected()))), // NSFW
  170.                         String.format("-nc %s", (String)HelpfulFunctions.getSelectedButtonText(buttons)), // NEAREST
  171.                         String.format("-so %s", ((JComboBox) HelpfulFunctions.getComponentByName("sort_by")).getSelectedItem().toString().replace(' ', '-')), // SORT
  172.                         String.format("-si %s", ((JSlider) HelpfulFunctions.getComponentByName("img_section_size")).getValue()), // IMAGE CROP SIZE
  173.                         String.format("-tc %s", path_to_image), // PATH TO IMAGE BEING CREATED
  174.                         String.format("-o %s", path)); // OUTPUT PATH
  175.                
  176.                 } catch (Exception e) {
  177.                     LoggerMain.FullLog(e, console_area);
  178.                     CancelProcess(false);
  179.                 }
  180.  
  181.                 LoggerMain.Log(String.format("COMMAND: %s", command), console_area, Level.CONFIG);
  182.  
  183.                 try {
  184.                     // RUN COMMAND
  185.                     p = Runtime.getRuntime().exec(command);
  186.                     LoggerMain.Log("Called python script", console_area, Level.INFO);
  187.                 } catch (IOException e) {
  188.                     LoggerMain.FullLog(e, console_area);
  189.                 }
  190.                 BufferedReader in = null;
  191.                 try {
  192.                     in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  193.                 } catch (NullPointerException e) {
  194.                     LoggerMain.FullLog(e, console_area);
  195.                 }
  196.  
  197.                 String ret = null;
  198.                 try {
  199.                     while ((ret = in.readLine()) != null) {
  200.                         if (ret.contains("LOG")) {
  201.                             LoggerMain.Log("Python: " + ret.substring(6, ret.length()), console_area, Level.INFO);
  202.                         } else if (ret.contains("WARN")) {
  203.                             LoggerMain.Log("Python: " + ret.substring(7, ret.length()), console_area, Level.WARNING);
  204.                         } else if (ret.contains("ERR")) {
  205.                             LoggerMain.Log("Python: " + ret.substring(6, ret.length()), console_area, Level.SEVERE);
  206.                         } else if (ret.contains("END")) {
  207.                             LoggerMain.Log("Python: " + ret.substring(4, ret.length()), console_area, Level.SEVERE);
  208.                             CancelProcess(false);
  209.                         } else {
  210.                             LoggerMain.Log("Python: " + ret, console_area, Level.INFO);
  211.                         }
  212.                     }
  213.                     LoggerMain.Log("Read python output successfully!", console_area, Level.INFO);
  214.                 } catch (Exception e) {
  215.                     LoggerMain.PartialLog(e, console_area);
  216.                 }
  217.                 try {
  218.                     in.close();
  219.                 } catch (IOException e) {
  220.                     LoggerMain.FullLog(e, console_area);
  221.                 }
  222.                 return true;
  223.             }
  224.  
  225.             @Override
  226.             protected void done() {
  227.                 LoggerMain.Log("Python finished executing!", console_area, Level.INFO);
  228.                 try {
  229.                     p.destroyForcibly();
  230.                 } catch(NullPointerException e) {
  231.                     LoggerMain.FullLog(e, console_area);
  232.                 }
  233.             }
  234.         };
  235.  
  236.         progressBar.setEnabled(true);
  237.  
  238.         python.execute();
  239.     }
  240.  
  241.     public void CancelProcess(boolean canceledByMouse) throws Exception {
  242.         int result = 0;
  243.         if (canceledByMouse) {
  244.             result = JOptionPane.showConfirmDialog(null, "Are you sure you want to cancel?", "Cancel",
  245.                     JOptionPane.YES_NO_OPTION);
  246.             LoggerMain.Log("Canceling process", console_area, Level.INFO);
  247.         } else {
  248.             result = JOptionPane.YES_OPTION;
  249.         }
  250.         if (result == JOptionPane.YES_OPTION) {
  251.             try {
  252.                 p.destroy();
  253.             } catch (Exception e) {}
  254.             for (Component component : HelpfulFunctions.getComponents(sub_select)) {
  255.                 component.setEnabled(true);
  256.             }
  257.             for (Component component : HelpfulFunctions.getComponents(sub_options)) {
  258.                 component.setEnabled(true);
  259.             }
  260.             start.setText("Run!");
  261.             start.removeMouseListener(endML);
  262.             start.addMouseListener(startML);
  263.  
  264.             python.cancel(true);
  265.             progressBar.setEnabled(false);
  266.             frame.repaint(); // REFRESH GUI
  267.         } else if (result == JOptionPane.NO_OPTION) {
  268.             LoggerMain.Log("Python not canceled!", console_area, Level.INFO);
  269.         }
  270.     }
  271.    
  272.  
  273.     public void SanityCheck() {
  274.         // SANITY CHECK SUB NAME - IS IT EMPTY & IS THERE ANY NUMBERS/SYMBOLS?
  275.         if (!sub_name.getText().equals("")) {
  276.             // USING REGEX TO CHECK FOR ANY NON ALPHABETICAL CHARACTERS
  277.             Pattern reg = Pattern.compile("[^a-zA-Z]");
  278.             Matcher matcher = reg.matcher(sub_name.getText());
  279.             if (matcher.find()) {
  280.                 LoggerMain.Log("Subreddit name must only use alphabetical characters!", console_area, Level.WARNING);
  281.                 HelpfulFunctions.infoBox("Subreddit name must only use alphabetical characters!", "Error",
  282.                         JOptionPane.ERROR_MESSAGE);
  283.             } else {
  284.                 // SANITY CHECK DIRECTORY - IS IT SELECTED?
  285.                 if (path == null || path.equals("")) {
  286.                     LoggerMain.Log("Output path is empty, using current directory!", console_area, Level.WARNING);
  287.                     path = System.getProperty("user.dir");
  288.                     directory_label.setText(path);
  289.                 } else {
  290.                     if (path_to_image == null || path_to_image.equals("")) {
  291.                         LoggerMain.Log("Image path is empty!", console_area, Level.WARNING);
  292.                         HelpfulFunctions.infoBox("Please select an image to create!", "Error", JOptionPane.ERROR_MESSAGE);
  293.                         path = System.getProperty("user.dir");
  294.                         directory_label.setText(path);
  295.                     } else {
  296.                         LoggerMain.Log("Passed checks, can start", console_area, Level.INFO);
  297.                         startPythonThread();
  298.                     }
  299.                 }
  300.             }
  301.         } else if (sub_name.getText().equals("")) {
  302.             LoggerMain.Log("Subreddit name cannot be empty!", console_area, Level.WARNING);
  303.             HelpfulFunctions.infoBox("Subreddit name cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
  304.         }
  305.  
  306.     }
  307.  
  308.     public void SubredditOptions(JFrame frame) {
  309.         // JPANEL TO HOUSE ALL SUBREDDIT OPTIONS
  310.         sub_options = cm.JPanel(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null), Color.DARK_GRAY, null, new Integer[] {6, 200, 415, 235});;
  311.         frame.getContentPane().add(sub_options);
  312.  
  313.         // SORT BY LABEL
  314.         JLabel sort_by_label = cm.JLabel("Sort by:", "sort_by_label", "The sort of the subreddit.", font, Color.WHITE, new Integer[] {6, 12, 64, 16});
  315.         sub_options.add(sort_by_label);
  316.  
  317.         // SORT BY SELECTION
  318.         JComboBox sort_by = cm.JComboBox("sort_by", "", Color.BLACK, Color.WHITE, font, new DefaultComboBoxModel(
  319.                 new String[] { "Past Day", "Past Week", "Past Month", "Past Year", "All time" }), new Integer[] {82, 8, 160, 27});
  320.         sub_options.add(sort_by);
  321.  
  322.         // NSFW CHECK BOX LABEL
  323.         JLabel nsfw_label = cm.JLabel("NSFW / L:", "nsfw_label", "Whether NSFW/L posts should be included.", font, Color.WHITE, new Integer[] {6, 65, 74, 16});
  324.         sub_options.add(nsfw_label);
  325.        
  326.         // NSFW CHECK BOX
  327.         JCheckBox nsfw_check = cm.JCheckBox("nsfw_check", "", new Integer[] {92, 53, 118, 39});
  328.         sub_options.add(nsfw_check);
  329.  
  330.         // IMAGE SCALE LABEL
  331.         JLabel image_sectionS_label = cm.JLabel("Image section size:", "image_sectionS_label", "The size of the section of the image the program will take.", font, Color.WHITE, new Integer[] {6, 146, 160, 16});
  332.         sub_options.add(image_sectionS_label);
  333.  
  334.         // LABEL FOR VALUE OF IMAGE SCALE SLIDER
  335.         JLabel img_sectionS_value = cm.JLabel("", "img_sectionS_value", "", font, Color.WHITE, new Integer[] {346, 146, 52, 16});
  336.         sub_options.add(img_sectionS_value);
  337.  
  338.         //SLIDER FOR CROP SIZE
  339.         JSlider img_section_size = cm.JSlider("img_section_size", "", null, 200, 100, 400, new Integer[] {161, 136, 190, 39});
  340.         ChangeListener change_size = new ChangeListener() { public void stateChanged(ChangeEvent e) { img_sectionS_value.setText(Integer.toString(img_section_size.getValue())); } };
  341.         img_section_size.addChangeListener(change_size);
  342.         img_sectionS_value.setText(Integer.toString(img_section_size.getValue()));
  343.         sub_options.add(img_section_size);
  344.  
  345.         //MAX IMAGES SLIDER VALUE
  346.         JLabel max_img_value = cm.JLabel("", "max_img_value", "", font, Color.WHITE, new Integer[] {307, 196, 91, 17});
  347.         sub_options.add(max_img_value);
  348.  
  349.         //MAX IMAGES LABEL
  350.         JLabel max_images = cm.JLabel("Maximum images:", "max_images", "Maximum number of images allowed to be downloaded.", font, Color.WHITE, new Integer[] {6, 196, 128, 16});
  351.         sub_options.add(max_images);
  352.  
  353.         //MAX IMAGES SLIDER
  354.         JSlider max_img_slider = cm.JSlider("max_img_slider", "", null, 20000, 1000, 30000, new Integer[] {127, 184, 190, 45});
  355.         ChangeListener change_max = new ChangeListener() { public void stateChanged(ChangeEvent e) { max_img_value.setText(Integer.toString(max_img_slider.getValue())); } };
  356.         max_img_slider.addChangeListener(change_max);
  357.         max_img_value.setText(Integer.toString(max_img_slider.getValue()));
  358.         sub_options.add(max_img_slider);
  359.        
  360.         //NEAREST BY
  361.         JLabel nearest = cm.JLabel("Nearest by:", "nearest_by", "Whether it should pick the image depending on it's brightntess or colour.", font, Color.WHITE, new Integer[] {6, 107, 103, 16});
  362.         sub_options.add(nearest);
  363.  
  364.         //COLOUR RADIO BUTTON
  365.         JRadioButton nearest_colour = cm.JRadioButton("Colour", "colour", true, Color.WHITE, font, new Integer[] {102, 104, 91, 23}); //////
  366.         sub_options.add(nearest_colour);
  367.  
  368.         //BRIGHTNESS RADIO BUTTON
  369.         JRadioButton nearest_bright = cm.JRadioButton("Brightness", "brightness", false, Color.WHITE, font, new Integer[] {192, 104, 141, 23}); ////////
  370.         sub_options.add(nearest_bright);
  371.        
  372.         buttons.add(nearest_colour);
  373.         buttons.add(nearest_bright);
  374.  
  375.     }
  376.  
  377.     public void SubredditSelection(JFrame frame) {     
  378.         final JFileChooser fc_out = cm.JFileChooser(font, Color.DARK_GRAY, JFileChooser.DIRECTORIES_ONLY, null, false);
  379.         final JFileChooser fc_img = cm.JFileChooser(font, Color.DARK_GRAY, null, new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg", "gif"), false);
  380.         // CREATING JPANEL FOR THE SUBREDDIT SELECTION
  381.         sub_select = cm.JPanel(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null), Color.DARK_GRAY, null, new Integer[] {6, 6, 415, 182});
  382.         frame.getContentPane().add(sub_select);
  383.  
  384.         // SUBREDDIT NAME TEXT BOX
  385.         sub_name = cm.JTextField(font, Color.WHITE, Color.DARK_GRAY, 10, new Integer[] {90, 18, 319, 33});
  386.         sub_select.add(sub_name);
  387.  
  388.         // SUBREDDIT NAME TEXT BOX LABEL
  389.         JLabel sub_name_box_label = cm.JLabel("Subreddit:", "subreddit", "The name of the subreddit.", font, Color.WHITE, new Integer[] {6, 26, 101, 16});
  390.         sub_select.add(sub_name_box_label);
  391.  
  392.         // PROGRESS BAR
  393.         progressBar = cm.JProgressBar(100, Color.WHITE, false, true, new Integer[] {6, 465, 415, 20});
  394.         frame.getContentPane().add(progressBar);
  395.  
  396.         // OUTPUT DIRECTORY LABEL
  397.         JLabel output_directory = cm.JLabel("Output Directory:", "out_dir", "Output directory for the image.", font, Color.WHITE, new Integer[] {6, 82, 152, 16});
  398.         sub_select.add(output_directory);
  399.  
  400.         // LABEL THAT SHOWS SELECTED DIRECTORY
  401.         directory_label = cm.JLabel("", "selected_dir", "", font, Color.WHITE, new Integer[] {152, 110, 257, 16});
  402.         sub_select.add(directory_label);
  403.  
  404.         // BUTTON TO SELECT FILE
  405.         JButton file_chooser = cm.JButton("Choose Folder", null, "", null, font, new Integer[] {142, 77, 136, 29});
  406.         MouseAdapter filechoose = new MouseAdapter() {
  407.             @Override
  408.             public void mouseClicked(MouseEvent e) {
  409.                 int returnVal = fc_out.showOpenDialog(file_chooser);
  410.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  411.                     directory_label.setText(fc_out.getSelectedFile().getAbsolutePath());
  412.                     path = fc_out.getSelectedFile().getAbsolutePath();
  413.                 }}};
  414.         file_chooser.addMouseListener(filechoose);
  415.         sub_select.add(file_chooser);
  416.  
  417.         JLabel image_dir_label = cm.JLabel("", "im_dir_lbl", "", font, Color.WHITE, new Integer[] {152, 160, 257, 16});
  418.         sub_select.add(image_dir_label);
  419.  
  420.         JLabel to_make_label = cm.JLabel("Image to create:", "to_make_lbl", "The image you want created.", font, Color.WHITE, new Integer[] {6, 137, 136, 16});
  421.         sub_select.add(to_make_label);
  422.  
  423.         JButton image_chooser = cm.JButton("Choose Image", null, "", null, font, new Integer[] {142, 132, 136, 29});
  424.         // MOUSE CLICK LISTNER
  425.         MouseAdapter imagechoose = new MouseAdapter() {
  426.             @Override
  427.             public void mouseClicked(MouseEvent e) {
  428.                 int returnVal = fc_img.showOpenDialog(image_chooser);
  429.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  430.                     image_dir_label.setText(fc_img.getSelectedFile().getAbsolutePath());
  431.                     path_to_image = fc_img.getSelectedFile().getAbsolutePath();
  432.                 }}};
  433.         image_chooser.addMouseListener(imagechoose);
  434.         sub_select.add(image_chooser);
  435.  
  436.     }
  437.    
  438.     public void Console(JFrame frame) {
  439.         JPanel console = cm.JPanel(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null), Color.DARK_GRAY, null, new Integer[] {433, 6, 332, 473});;
  440.        
  441.         console_area = cm.JTextPane(font, Color.WHITE, Color.DARK_GRAY, false, "text/html", new Integer[] {6, 6, 320, 461});
  442.        
  443.         console.add(console_area);
  444.  
  445.         frame.getContentPane().add(console);
  446.     }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement