Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.subreddimages;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import java.awt.Color;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.JTextPane;
- import javax.swing.SwingWorker;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import java.awt.Font;
- import javax.swing.JComboBox;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.border.SoftBevelBorder;
- import javax.swing.border.BevelBorder;
- import javax.swing.JFileChooser;
- import javax.swing.JButton;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- import java.util.logging.Level;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JCheckBox;
- import java.awt.Component;
- import javax.swing.ButtonGroup;
- import javax.swing.JSlider;
- import javax.swing.event.ChangeListener;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import org.apache.commons.lang3.StringUtils;
- import com.subreddimages.GUIComponents.GUIComponents;
- import javax.swing.event.ChangeEvent;
- import javax.swing.JProgressBar;
- import javax.swing.JRadioButton;
- public class MainGUI {
- private GUIComponents.Components cm = new GUIComponents.Components();
- private Font font = new Font("Courier New", Font.PLAIN, 13);
- private JFrame frame;
- private JTextField sub_name;
- private JProgressBar progressBar;
- private JLabel directory_label;
- JPanel sub_options;
- JPanel sub_select;
- String path;
- String path_to_image;
- JButton start;
- Process p = null;
- static JTextPane console_area = new JTextPane();
- ButtonGroup buttons = new ButtonGroup();
- MouseListener startML = new MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- SanityCheck();
- }
- };
- MouseListener endML = new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- try {
- CancelProcess(true);
- } catch (Exception e1) {
- }
- }
- };
- static StringWriter sw = new StringWriter();
- static PrintWriter pw = new PrintWriter(sw);
- SwingWorker python = null;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- startGU();
- }
- });
- }
- public static void startGU() {
- LoggerMain.initialize(console_area);
- LoggerMain.Log(String.format("OS: %s, OSver: %s, OSarch: %s, JAVAver: %s",
- System.getProperty("os.name"),
- System.getProperty("os.version"),
- System.getProperty("os.arch"),
- System.getProperty("java.version")),
- console_area,
- Level.CONFIG);
- MainGUI main = new MainGUI();
- main.frame.setVisible(true);
- LoggerMain.Log("GUI initalized successfuly!", console_area, Level.INFO);
- }
- public MainGUI() {
- LoggerMain.Log("Initializing GUI", console_area, Level.INFO);
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- *
- * @wbp.parser.entryPoint
- */
- private void initialize() {
- frame = cm.JFrame("Subreddimages", font, false, Color.DARK_GRAY, JFrame.EXIT_ON_CLOSE, null, new Integer[] {100, 100, 771, 507});
- // ADDING EVERYTHING TO GUI
- SubredditSelection(frame);
- SubredditOptions(frame);
- Console(frame);
- // START THE PROCESS BUTTON
- start = cm.JButton("Run!", startML, "Starts the process.", Color.BLACK, font, new Integer[] {94, 439, 247, 29});
- frame.getContentPane().add(start);
- HelpfulFunctions.createComponentMap(sub_options);
- }
- public void startPythonThread() {
- // REMOVING OLD MOUSE LISTER SO AS NOT TO TRIGGER THIS FUNCTION AGAIN
- start.removeMouseListener(startML);
- // DISABLING ALL COMPONENTS SO THEY CANNOT BE INTERACTED WITH
- for (Component component : HelpfulFunctions.getComponents(sub_select)) {
- component.setEnabled(false);
- }
- for (Component component : HelpfulFunctions.getComponents(sub_options)) {
- component.setEnabled(false);
- }
- // RE-ENABLING BUTTON
- start.setEnabled(true);
- start.addMouseListener(endML);
- // CHANGING TEXT
- start.setText("Cancel");
- HelpfulFunctions.infoBox("This could take a long time!", "Warning", JOptionPane.WARNING_MESSAGE);
- python = new SwingWorker() // HAVE TO REINSTANTIATE IT EVERY TIME BECAUSE THEY CAN ONLY BE USED ONCE
- {
- @Override
- protected Boolean doInBackground() throws Exception {
- String command = "";
- try {
- command = String.format("python %s %s %s %s %s %s %s %s %s",
- System.getProperty("user.dir") + "/start_process.py", // SCRIPT DIRECTORY
- String.format("-m %s", ((JSlider)HelpfulFunctions.getComponentByName("max_img_slider")).getValue()), // MAXIMUM NUMBER OF IMAGES
- String.format("-s %s", sub_name.getText()), // SUBREDDIT NAME
- String.format("-n %s", StringUtils.capitalize(Boolean.toString(((JCheckBox) HelpfulFunctions.getComponentByName("nsfw_check")).isSelected()))), // NSFW
- String.format("-nc %s", (String)HelpfulFunctions.getSelectedButtonText(buttons)), // NEAREST
- String.format("-so %s", ((JComboBox) HelpfulFunctions.getComponentByName("sort_by")).getSelectedItem().toString().replace(' ', '-')), // SORT
- String.format("-si %s", ((JSlider) HelpfulFunctions.getComponentByName("img_section_size")).getValue()), // IMAGE CROP SIZE
- String.format("-tc %s", path_to_image), // PATH TO IMAGE BEING CREATED
- String.format("-o %s", path)); // OUTPUT PATH
- } catch (Exception e) {
- LoggerMain.FullLog(e, console_area);
- CancelProcess(false);
- }
- LoggerMain.Log(String.format("COMMAND: %s", command), console_area, Level.CONFIG);
- try {
- // RUN COMMAND
- p = Runtime.getRuntime().exec(command);
- LoggerMain.Log("Called python script", console_area, Level.INFO);
- } catch (IOException e) {
- LoggerMain.FullLog(e, console_area);
- }
- BufferedReader in = null;
- try {
- in = new BufferedReader(new InputStreamReader(p.getInputStream()));
- } catch (NullPointerException e) {
- LoggerMain.FullLog(e, console_area);
- }
- String ret = null;
- try {
- while ((ret = in.readLine()) != null) {
- if (ret.contains("LOG")) {
- LoggerMain.Log("Python: " + ret.substring(6, ret.length()), console_area, Level.INFO);
- } else if (ret.contains("WARN")) {
- LoggerMain.Log("Python: " + ret.substring(7, ret.length()), console_area, Level.WARNING);
- } else if (ret.contains("ERR")) {
- LoggerMain.Log("Python: " + ret.substring(6, ret.length()), console_area, Level.SEVERE);
- } else if (ret.contains("END")) {
- LoggerMain.Log("Python: " + ret.substring(4, ret.length()), console_area, Level.SEVERE);
- CancelProcess(false);
- } else {
- LoggerMain.Log("Python: " + ret, console_area, Level.INFO);
- }
- }
- LoggerMain.Log("Read python output successfully!", console_area, Level.INFO);
- } catch (Exception e) {
- LoggerMain.PartialLog(e, console_area);
- }
- try {
- in.close();
- } catch (IOException e) {
- LoggerMain.FullLog(e, console_area);
- }
- return true;
- }
- @Override
- protected void done() {
- LoggerMain.Log("Python finished executing!", console_area, Level.INFO);
- try {
- p.destroyForcibly();
- } catch(NullPointerException e) {
- LoggerMain.FullLog(e, console_area);
- }
- }
- };
- progressBar.setEnabled(true);
- python.execute();
- }
- public void CancelProcess(boolean canceledByMouse) throws Exception {
- int result = 0;
- if (canceledByMouse) {
- result = JOptionPane.showConfirmDialog(null, "Are you sure you want to cancel?", "Cancel",
- JOptionPane.YES_NO_OPTION);
- LoggerMain.Log("Canceling process", console_area, Level.INFO);
- } else {
- result = JOptionPane.YES_OPTION;
- }
- if (result == JOptionPane.YES_OPTION) {
- try {
- p.destroy();
- } catch (Exception e) {}
- for (Component component : HelpfulFunctions.getComponents(sub_select)) {
- component.setEnabled(true);
- }
- for (Component component : HelpfulFunctions.getComponents(sub_options)) {
- component.setEnabled(true);
- }
- start.setText("Run!");
- start.removeMouseListener(endML);
- start.addMouseListener(startML);
- python.cancel(true);
- progressBar.setEnabled(false);
- frame.repaint(); // REFRESH GUI
- } else if (result == JOptionPane.NO_OPTION) {
- LoggerMain.Log("Python not canceled!", console_area, Level.INFO);
- }
- }
- public void SanityCheck() {
- // SANITY CHECK SUB NAME - IS IT EMPTY & IS THERE ANY NUMBERS/SYMBOLS?
- if (!sub_name.getText().equals("")) {
- // USING REGEX TO CHECK FOR ANY NON ALPHABETICAL CHARACTERS
- Pattern reg = Pattern.compile("[^a-zA-Z]");
- Matcher matcher = reg.matcher(sub_name.getText());
- if (matcher.find()) {
- LoggerMain.Log("Subreddit name must only use alphabetical characters!", console_area, Level.WARNING);
- HelpfulFunctions.infoBox("Subreddit name must only use alphabetical characters!", "Error",
- JOptionPane.ERROR_MESSAGE);
- } else {
- // SANITY CHECK DIRECTORY - IS IT SELECTED?
- if (path == null || path.equals("")) {
- LoggerMain.Log("Output path is empty, using current directory!", console_area, Level.WARNING);
- path = System.getProperty("user.dir");
- directory_label.setText(path);
- } else {
- if (path_to_image == null || path_to_image.equals("")) {
- LoggerMain.Log("Image path is empty!", console_area, Level.WARNING);
- HelpfulFunctions.infoBox("Please select an image to create!", "Error", JOptionPane.ERROR_MESSAGE);
- path = System.getProperty("user.dir");
- directory_label.setText(path);
- } else {
- LoggerMain.Log("Passed checks, can start", console_area, Level.INFO);
- startPythonThread();
- }
- }
- }
- } else if (sub_name.getText().equals("")) {
- LoggerMain.Log("Subreddit name cannot be empty!", console_area, Level.WARNING);
- HelpfulFunctions.infoBox("Subreddit name cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- public void SubredditOptions(JFrame frame) {
- // JPANEL TO HOUSE ALL SUBREDDIT OPTIONS
- sub_options = cm.JPanel(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null), Color.DARK_GRAY, null, new Integer[] {6, 200, 415, 235});;
- frame.getContentPane().add(sub_options);
- // SORT BY LABEL
- 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});
- sub_options.add(sort_by_label);
- // SORT BY SELECTION
- JComboBox sort_by = cm.JComboBox("sort_by", "", Color.BLACK, Color.WHITE, font, new DefaultComboBoxModel(
- new String[] { "Past Day", "Past Week", "Past Month", "Past Year", "All time" }), new Integer[] {82, 8, 160, 27});
- sub_options.add(sort_by);
- // NSFW CHECK BOX LABEL
- 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});
- sub_options.add(nsfw_label);
- // NSFW CHECK BOX
- JCheckBox nsfw_check = cm.JCheckBox("nsfw_check", "", new Integer[] {92, 53, 118, 39});
- sub_options.add(nsfw_check);
- // IMAGE SCALE LABEL
- 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});
- sub_options.add(image_sectionS_label);
- // LABEL FOR VALUE OF IMAGE SCALE SLIDER
- JLabel img_sectionS_value = cm.JLabel("", "img_sectionS_value", "", font, Color.WHITE, new Integer[] {346, 146, 52, 16});
- sub_options.add(img_sectionS_value);
- //SLIDER FOR CROP SIZE
- JSlider img_section_size = cm.JSlider("img_section_size", "", null, 200, 100, 400, new Integer[] {161, 136, 190, 39});
- ChangeListener change_size = new ChangeListener() { public void stateChanged(ChangeEvent e) { img_sectionS_value.setText(Integer.toString(img_section_size.getValue())); } };
- img_section_size.addChangeListener(change_size);
- img_sectionS_value.setText(Integer.toString(img_section_size.getValue()));
- sub_options.add(img_section_size);
- //MAX IMAGES SLIDER VALUE
- JLabel max_img_value = cm.JLabel("", "max_img_value", "", font, Color.WHITE, new Integer[] {307, 196, 91, 17});
- sub_options.add(max_img_value);
- //MAX IMAGES LABEL
- 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});
- sub_options.add(max_images);
- //MAX IMAGES SLIDER
- JSlider max_img_slider = cm.JSlider("max_img_slider", "", null, 20000, 1000, 30000, new Integer[] {127, 184, 190, 45});
- ChangeListener change_max = new ChangeListener() { public void stateChanged(ChangeEvent e) { max_img_value.setText(Integer.toString(max_img_slider.getValue())); } };
- max_img_slider.addChangeListener(change_max);
- max_img_value.setText(Integer.toString(max_img_slider.getValue()));
- sub_options.add(max_img_slider);
- //NEAREST BY
- 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});
- sub_options.add(nearest);
- //COLOUR RADIO BUTTON
- JRadioButton nearest_colour = cm.JRadioButton("Colour", "colour", true, Color.WHITE, font, new Integer[] {102, 104, 91, 23}); //////
- sub_options.add(nearest_colour);
- //BRIGHTNESS RADIO BUTTON
- JRadioButton nearest_bright = cm.JRadioButton("Brightness", "brightness", false, Color.WHITE, font, new Integer[] {192, 104, 141, 23}); ////////
- sub_options.add(nearest_bright);
- buttons.add(nearest_colour);
- buttons.add(nearest_bright);
- }
- public void SubredditSelection(JFrame frame) {
- final JFileChooser fc_out = cm.JFileChooser(font, Color.DARK_GRAY, JFileChooser.DIRECTORIES_ONLY, null, false);
- final JFileChooser fc_img = cm.JFileChooser(font, Color.DARK_GRAY, null, new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg", "gif"), false);
- // CREATING JPANEL FOR THE SUBREDDIT SELECTION
- sub_select = cm.JPanel(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null), Color.DARK_GRAY, null, new Integer[] {6, 6, 415, 182});
- frame.getContentPane().add(sub_select);
- // SUBREDDIT NAME TEXT BOX
- sub_name = cm.JTextField(font, Color.WHITE, Color.DARK_GRAY, 10, new Integer[] {90, 18, 319, 33});
- sub_select.add(sub_name);
- // SUBREDDIT NAME TEXT BOX LABEL
- JLabel sub_name_box_label = cm.JLabel("Subreddit:", "subreddit", "The name of the subreddit.", font, Color.WHITE, new Integer[] {6, 26, 101, 16});
- sub_select.add(sub_name_box_label);
- // PROGRESS BAR
- progressBar = cm.JProgressBar(100, Color.WHITE, false, true, new Integer[] {6, 465, 415, 20});
- frame.getContentPane().add(progressBar);
- // OUTPUT DIRECTORY LABEL
- JLabel output_directory = cm.JLabel("Output Directory:", "out_dir", "Output directory for the image.", font, Color.WHITE, new Integer[] {6, 82, 152, 16});
- sub_select.add(output_directory);
- // LABEL THAT SHOWS SELECTED DIRECTORY
- directory_label = cm.JLabel("", "selected_dir", "", font, Color.WHITE, new Integer[] {152, 110, 257, 16});
- sub_select.add(directory_label);
- // BUTTON TO SELECT FILE
- JButton file_chooser = cm.JButton("Choose Folder", null, "", null, font, new Integer[] {142, 77, 136, 29});
- MouseAdapter filechoose = new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- int returnVal = fc_out.showOpenDialog(file_chooser);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- directory_label.setText(fc_out.getSelectedFile().getAbsolutePath());
- path = fc_out.getSelectedFile().getAbsolutePath();
- }}};
- file_chooser.addMouseListener(filechoose);
- sub_select.add(file_chooser);
- JLabel image_dir_label = cm.JLabel("", "im_dir_lbl", "", font, Color.WHITE, new Integer[] {152, 160, 257, 16});
- sub_select.add(image_dir_label);
- 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});
- sub_select.add(to_make_label);
- JButton image_chooser = cm.JButton("Choose Image", null, "", null, font, new Integer[] {142, 132, 136, 29});
- // MOUSE CLICK LISTNER
- MouseAdapter imagechoose = new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- int returnVal = fc_img.showOpenDialog(image_chooser);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- image_dir_label.setText(fc_img.getSelectedFile().getAbsolutePath());
- path_to_image = fc_img.getSelectedFile().getAbsolutePath();
- }}};
- image_chooser.addMouseListener(imagechoose);
- sub_select.add(image_chooser);
- }
- public void Console(JFrame frame) {
- JPanel console = cm.JPanel(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null), Color.DARK_GRAY, null, new Integer[] {433, 6, 332, 473});;
- console_area = cm.JTextPane(font, Color.WHITE, Color.DARK_GRAY, false, "text/html", new Integer[] {6, 6, 320, 461});
- console.add(console_area);
- frame.getContentPane().add(console);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement