Skip to content

Instantly share code, notes, and snippets.

@AnEmortalKid
Created September 8, 2015 01:54
Show Gist options
  • Save AnEmortalKid/e693a8dec25e80ec5554 to your computer and use it in GitHub Desktop.
Save AnEmortalKid/e693a8dec25e80ec5554 to your computer and use it in GitHub Desktop.
package com.anemortalkid;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class RedditWindow extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JTextField se, ep;
JButton watch, close;
RedditWindow(String title) {
super(title);
this.setSize(180, 125);
this.init();
this.setVisible(true);
}
private void init() {
setAlwaysOnTop(false);
JLabel lse = new JLabel("What season?");
JLabel lep = new JLabel("What episode?");
se = new JTextField(2);
ep = new JTextField(2);
watch = new JButton("Watch!");
CloseAction ca = new CloseAction();
close = new JButton(ca);
// so button can be pressed using F1 and ENTER
close.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
close.getActionMap().put("Eclosenter", ca);
close.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
"close1");
close.getActionMap().put("close1", ca);
watch.addActionListener(this);
JPanel panel = new JPanel();
panel.add(lse);
panel.add(se);
panel.add(lep);
panel.add(ep);
panel.add(watch);
panel.add(close);
this.add(panel);
}
public void setDefaultCloseOperation(int exitOnClose) {
}
@Override
public void actionPerformed(ActionEvent event) {
boolean exist = false;
String path = null;
if (event.getSource() == watch) {
int s = Integer.parseInt(se.getText());
int e = Integer.parseInt(ep.getText());
Desktop chrome = Desktop.getDesktop();
if (s < 10 && e >= 10) {
path = ("file:///Users/3069264/Documents/Modern%20Family/Season%203/S0" + s + "E" + e);
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
exist = true;
try {
chrome.browse(new URI(path));
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "File does not exist.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (e < 10 && s >= 10) {
path = ("file:///Users/3069264/Documents/Modern%20Family/Season%203/S" + s + "E0" + e);
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
exist = true;
try {
chrome.browse(new URI(path));
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "File does not exist.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (s < 10 && e < 10) {
path = ("file:///Users/3069264/Documents/Modern%20Family/Season%203/S0" + s + "E0" + e);
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
exist = true;
try {
chrome.browse(new URI(path));
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "File does not exist.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (s > 10 && e > 10) {
path = ("file:///Users/3069264/Documents/Modern%20Family/Season%203/S" + s + "E" + e);
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
exist = true;
try {
chrome.browse(new URI(path));
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "File does not exist.", "Error",
JOptionPane.INFORMATION_MESSAGE);
}
}
if (exist == true) {
System.out.println("Playing season " + s + ", episode " + e);
} else if (exist == false) {
System.out.println(path + " does not exist");
}
se.setText("");
ep.setText("");
}
if (event.getSource() == close) {
System.exit(0);
}
}
/**
* Generally you'd want something like this instead of the
* actionListener(THIS). You can assign Actions to those buttons
*
*/
private class CloseAction extends AbstractAction {
public CloseAction(String string) {
super(string);
}
public CloseAction() {
super("close");
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("I should close! but I'm not gonna System.exit(0)");
}
}
public static void main(String[] args) {
new RedditWindow("some title");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment