2014-03-01 11:13:32 +01:00
|
|
|
package com.rarchives.ripme.ui;
|
|
|
|
|
|
|
|
import java.awt.BorderLayout;
|
2014-03-02 03:08:16 +01:00
|
|
|
import java.awt.Desktop;
|
2014-03-01 11:13:32 +01:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.io.File;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Observable;
|
|
|
|
import java.util.Observer;
|
|
|
|
|
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
import javax.swing.JLabel;
|
2014-03-02 03:08:16 +01:00
|
|
|
import javax.swing.JPanel;
|
2014-03-01 11:13:32 +01:00
|
|
|
import javax.swing.JTextField;
|
2014-03-02 03:08:16 +01:00
|
|
|
import javax.swing.SwingConstants;
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
2014-03-01 11:13:32 +01:00
|
|
|
|
|
|
|
import com.rarchives.ripme.ripper.AbstractRipper;
|
2014-03-02 03:08:16 +01:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
2014-03-01 11:13:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
public class MainWindow implements Runnable {
|
|
|
|
|
2014-03-02 03:08:16 +01:00
|
|
|
private static final Logger logger = Logger.getLogger(MainWindow.class);
|
|
|
|
|
2014-03-01 11:13:32 +01:00
|
|
|
private static final String WINDOW_TITLE = "RipMe";
|
|
|
|
|
|
|
|
private static JFrame mainFrame;
|
2014-03-02 03:08:16 +01:00
|
|
|
private static JPanel ripPanel;
|
2014-03-01 11:13:32 +01:00
|
|
|
private static JTextField ripTextfield;
|
|
|
|
private static JButton ripButton;
|
|
|
|
|
2014-03-02 03:08:16 +01:00
|
|
|
private static JPanel statusPanel;
|
|
|
|
private static JLabel statusLabel;
|
|
|
|
private static JButton statusButton;
|
2014-03-01 11:13:32 +01:00
|
|
|
|
|
|
|
public MainWindow() {
|
|
|
|
createUI();
|
|
|
|
setupHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createUI() {
|
|
|
|
mainFrame = new JFrame(WINDOW_TITLE);
|
|
|
|
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
2014-03-02 03:08:16 +01:00
|
|
|
ripPanel = new JPanel();
|
2014-03-01 11:13:32 +01:00
|
|
|
ripTextfield = new JTextField("", 20);
|
|
|
|
ripButton = new JButton("rip");
|
2014-03-02 03:08:16 +01:00
|
|
|
ripPanel.add(ripTextfield, BorderLayout.WEST);
|
|
|
|
ripPanel.add(ripButton, BorderLayout.EAST);
|
|
|
|
mainFrame.getContentPane().add(ripPanel, BorderLayout.NORTH);
|
|
|
|
|
|
|
|
statusPanel = new JPanel();
|
|
|
|
statusLabel = new JLabel("inactive", SwingConstants.LEADING);
|
|
|
|
statusButton = new JButton("open dir");
|
|
|
|
statusButton.setVisible(false);
|
|
|
|
statusPanel.add(statusLabel, BorderLayout.WEST);
|
|
|
|
statusPanel.add(statusButton, BorderLayout.EAST);
|
|
|
|
mainFrame.getContentPane().add(statusPanel, BorderLayout.SOUTH);
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setupHandlers() {
|
|
|
|
ripButton.addActionListener(new RipButtonHandler());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
mainFrame.pack();
|
|
|
|
mainFrame.setLocationRelativeTo(null);
|
|
|
|
mainFrame.setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void status(String text) {
|
2014-03-02 03:08:16 +01:00
|
|
|
statusLabel.setText(text);
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class RipButtonHandler implements ActionListener {
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
2014-03-02 03:08:16 +01:00
|
|
|
statusButton.setVisible(false);
|
2014-03-01 11:13:32 +01:00
|
|
|
try {
|
|
|
|
URL url = new URL(ripTextfield.getText());
|
|
|
|
AbstractRipper ripper = AbstractRipper.getRipper(url);
|
|
|
|
ripper.setObserver(new RipStatusHandler());
|
2014-03-01 11:43:47 +01:00
|
|
|
Thread t = new Thread(ripper);
|
|
|
|
t.start();
|
2014-03-01 11:13:32 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
status("Error: " + e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RipStatusHandler implements Observer {
|
|
|
|
public void update(Observable observable, Object object) {
|
|
|
|
RipStatusMessage msg = (RipStatusMessage) object;
|
|
|
|
System.err.println("Observer update, object: " + object.toString());
|
|
|
|
switch(msg.getStatus()) {
|
|
|
|
case LOADING_RESOURCE:
|
|
|
|
case DOWNLOAD_STARTED:
|
|
|
|
case DOWNLOAD_COMPLETE:
|
|
|
|
case DOWNLOAD_ERRORED:
|
|
|
|
status((String) msg.getObject());
|
|
|
|
break;
|
|
|
|
case RIP_COMPLETE:
|
|
|
|
File f = (File) msg.getObject();
|
2014-03-02 03:08:16 +01:00
|
|
|
statusButton.setActionCommand(f.toString());
|
|
|
|
statusButton.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
|
|
|
try {
|
|
|
|
Desktop.getDesktop().open(new File(event.getActionCommand()));
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
statusButton.setVisible(true);
|
|
|
|
status("Finished: " + Utils.removeCWD(f));
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|