2014-03-01 11:13:32 +01:00
|
|
|
package com.rarchives.ripme.ui;
|
|
|
|
|
2014-03-02 10:12:20 +01:00
|
|
|
import java.awt.Color;
|
2014-03-02 08:28:37 +01:00
|
|
|
import java.awt.Container;
|
2014-03-02 03:08:16 +01:00
|
|
|
import java.awt.Desktop;
|
2014-03-02 10:12:20 +01:00
|
|
|
import java.awt.Dimension;
|
2014-03-02 08:28:37 +01:00
|
|
|
import java.awt.GridBagConstraints;
|
|
|
|
import java.awt.GridBagLayout;
|
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 10:12:20 +01:00
|
|
|
import javax.swing.JList;
|
2014-03-02 03:08:16 +01:00
|
|
|
import javax.swing.JPanel;
|
2014-03-02 08:28:37 +01:00
|
|
|
import javax.swing.JProgressBar;
|
|
|
|
import javax.swing.JScrollPane;
|
2014-03-01 11:13:32 +01:00
|
|
|
import javax.swing.JTextField;
|
2014-03-02 08:28:37 +01:00
|
|
|
import javax.swing.JTextPane;
|
|
|
|
import javax.swing.border.EmptyBorder;
|
2014-03-02 10:12:20 +01:00
|
|
|
import javax.swing.text.BadLocationException;
|
|
|
|
import javax.swing.text.SimpleAttributeSet;
|
|
|
|
import javax.swing.text.StyleConstants;
|
|
|
|
import javax.swing.text.StyledDocument;
|
2014-03-02 03:08:16 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
private static JTextField ripTextfield;
|
|
|
|
private static JButton ripButton;
|
|
|
|
|
2014-03-02 03:08:16 +01:00
|
|
|
private static JLabel statusLabel;
|
2014-03-02 08:28:37 +01:00
|
|
|
private static JButton openButton;
|
|
|
|
private static JProgressBar statusProgress;
|
|
|
|
|
2014-03-02 10:12:20 +01:00
|
|
|
// Log
|
|
|
|
private static JButton optionLog;
|
|
|
|
private static JPanel logPanel;
|
|
|
|
private static JTextPane logText;
|
|
|
|
private static JScrollPane logTextScroll;
|
2014-03-01 11:13:32 +01:00
|
|
|
|
2014-03-02 10:12:20 +01:00
|
|
|
// History
|
|
|
|
private static JButton optionHistory;
|
|
|
|
private static JPanel historyPanel;
|
|
|
|
private static JList historyList;
|
|
|
|
private static JScrollPane historyListScroll;
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
private static JButton optionConfiguration;
|
|
|
|
private static JPanel configurationPanel;
|
|
|
|
// TODO Configuration components
|
|
|
|
|
2014-03-01 11:13:32 +01:00
|
|
|
public MainWindow() {
|
|
|
|
mainFrame = new JFrame(WINDOW_TITLE);
|
|
|
|
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
2014-03-02 10:12:20 +01:00
|
|
|
//mainFrame.setPreferredSize(new Dimension(400, 180));
|
|
|
|
//mainFrame.setResizable(false);
|
2014-03-02 08:28:37 +01:00
|
|
|
mainFrame.setLayout(new GridBagLayout());
|
|
|
|
|
|
|
|
createUI(mainFrame.getContentPane());
|
|
|
|
setupHandlers();
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02 08:28:37 +01:00
|
|
|
mainFrame.pack();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createUI(Container pane) {
|
|
|
|
EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5);
|
|
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
2014-03-02 10:12:20 +01:00
|
|
|
gbc.fill = GridBagConstraints.BOTH;
|
|
|
|
gbc.weightx = 2; gbc.ipadx = 2; gbc.gridx = 0;
|
|
|
|
gbc.weighty = 2; gbc.ipady = 2; gbc.gridy = 0;
|
2014-03-02 08:28:37 +01:00
|
|
|
|
|
|
|
ripTextfield = new JTextField("", 20);
|
|
|
|
ripButton = new JButton("rip");
|
|
|
|
JPanel ripPanel = new JPanel(new GridBagLayout());
|
|
|
|
ripPanel.setBorder(emptyBorder);
|
|
|
|
|
2014-03-02 10:12:20 +01:00
|
|
|
ripPanel.add(new JLabel("URL:", JLabel.RIGHT), gbc);
|
2014-03-02 08:28:37 +01:00
|
|
|
gbc.gridx = 1;
|
|
|
|
ripPanel.add(ripTextfield, gbc);
|
|
|
|
gbc.gridx = 2;
|
|
|
|
ripPanel.add(ripButton, gbc);
|
|
|
|
|
|
|
|
statusLabel = new JLabel("Inactive");
|
2014-03-02 10:12:20 +01:00
|
|
|
statusLabel.setHorizontalAlignment(JLabel.CENTER);
|
2014-03-02 08:28:37 +01:00
|
|
|
openButton = new JButton();
|
|
|
|
openButton.setVisible(false);
|
|
|
|
JPanel statusPanel = new JPanel(new GridBagLayout());
|
|
|
|
statusPanel.setBorder(emptyBorder);
|
2014-03-02 10:12:20 +01:00
|
|
|
|
|
|
|
gbc.gridx = 0;
|
2014-03-02 08:28:37 +01:00
|
|
|
statusPanel.add(statusLabel, gbc);
|
|
|
|
statusPanel.add(openButton, gbc);
|
|
|
|
|
|
|
|
JPanel progressPanel = new JPanel(new GridBagLayout());
|
|
|
|
progressPanel.setBorder(emptyBorder);
|
|
|
|
statusProgress = new JProgressBar(0, 100);
|
|
|
|
progressPanel.add(statusProgress, gbc);
|
2014-03-02 10:12:20 +01:00
|
|
|
|
|
|
|
JPanel optionsPanel = new JPanel(new GridBagLayout());
|
|
|
|
optionsPanel.setBorder(emptyBorder);
|
|
|
|
optionLog = new JButton("Log");
|
|
|
|
optionHistory = new JButton("History");
|
|
|
|
optionConfiguration = new JButton("Configuration");
|
|
|
|
gbc.gridx = 0;
|
|
|
|
optionsPanel.add(optionLog, gbc);
|
|
|
|
gbc.gridx = 1;
|
|
|
|
optionsPanel.add(optionHistory, gbc);
|
|
|
|
gbc.gridx = 2;
|
|
|
|
optionsPanel.add(optionConfiguration, gbc);
|
|
|
|
|
|
|
|
logPanel = new JPanel(new GridBagLayout());
|
2014-03-02 08:28:37 +01:00
|
|
|
logPanel.setBorder(emptyBorder);
|
2014-03-02 10:12:20 +01:00
|
|
|
logText = new JTextPaneNoWrap();
|
|
|
|
logTextScroll = new JScrollPane(logText);
|
|
|
|
logPanel.setVisible(false);
|
|
|
|
logPanel.setPreferredSize(new Dimension(300, 300));
|
|
|
|
logPanel.add(logTextScroll, gbc);
|
|
|
|
|
|
|
|
historyPanel = new JPanel(new GridBagLayout());
|
|
|
|
historyPanel.setBorder(emptyBorder);
|
|
|
|
historyList = new JList();
|
|
|
|
historyListScroll = new JScrollPane(historyList);
|
|
|
|
historyPanel.setVisible(false);
|
|
|
|
historyPanel.setPreferredSize(new Dimension(300, 300));
|
|
|
|
historyPanel.add(historyListScroll, gbc);
|
|
|
|
|
|
|
|
configurationPanel = new JPanel(new GridBagLayout());
|
|
|
|
configurationPanel.setBorder(emptyBorder);
|
|
|
|
configurationPanel.setVisible(false);
|
|
|
|
configurationPanel.setPreferredSize(new Dimension(300, 300));
|
|
|
|
// TODO Configuration components
|
2014-03-02 08:28:37 +01:00
|
|
|
|
|
|
|
pane.add(ripPanel, gbc);
|
|
|
|
gbc.gridy = 1;
|
|
|
|
pane.add(statusPanel, gbc);
|
|
|
|
gbc.gridy = 2;
|
|
|
|
pane.add(progressPanel, gbc);
|
|
|
|
gbc.gridy = 3;
|
2014-03-02 10:12:20 +01:00
|
|
|
pane.add(optionsPanel, gbc);
|
|
|
|
gbc.gridy = 4;
|
2014-03-02 08:28:37 +01:00
|
|
|
pane.add(logPanel, gbc);
|
2014-03-02 10:12:20 +01:00
|
|
|
gbc.gridy = 5;
|
|
|
|
pane.add(historyPanel, gbc);
|
|
|
|
gbc.gridy = 5;
|
|
|
|
pane.add(configurationPanel, gbc);
|
2014-03-02 08:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setupHandlers() {
|
|
|
|
ripButton.addActionListener(new RipButtonHandler());
|
2014-03-02 10:12:20 +01:00
|
|
|
optionLog.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
|
|
|
logPanel.setVisible(!logPanel.isVisible());
|
|
|
|
historyPanel.setVisible(false);
|
|
|
|
configurationPanel.setVisible(false);
|
|
|
|
mainFrame.pack();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
optionHistory.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
|
|
|
logPanel.setVisible(false);
|
|
|
|
historyPanel.setVisible(!historyPanel.isVisible());
|
|
|
|
configurationPanel.setVisible(false);
|
|
|
|
mainFrame.pack();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
optionConfiguration.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
|
|
|
logPanel.setVisible(false);
|
|
|
|
historyPanel.setVisible(false);
|
|
|
|
configurationPanel.setVisible(!configurationPanel.isVisible());
|
|
|
|
mainFrame.pack();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void appendLog(String text, Color color) {
|
|
|
|
SimpleAttributeSet sas = new SimpleAttributeSet();
|
|
|
|
StyleConstants.setForeground(sas, color);
|
|
|
|
|
|
|
|
StyledDocument sd = logText.getStyledDocument();
|
|
|
|
try {
|
|
|
|
sd.insertString(sd.getLength(), text + "\n", sas);
|
|
|
|
} catch (BadLocationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class RipButtonHandler implements ActionListener {
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
2014-03-02 08:28:37 +01:00
|
|
|
openButton.setVisible(false);
|
|
|
|
statusLabel.setVisible(true);
|
|
|
|
mainFrame.pack();
|
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;
|
2014-03-02 10:12:20 +01:00
|
|
|
|
|
|
|
int completedPercent = ((AbstractRipper) observable).getCompletionPercentage();
|
|
|
|
statusProgress.setValue(completedPercent);
|
|
|
|
status( ((AbstractRipper)observable).getStatusText() );
|
|
|
|
|
2014-03-01 11:13:32 +01:00
|
|
|
switch(msg.getStatus()) {
|
|
|
|
case LOADING_RESOURCE:
|
|
|
|
case DOWNLOAD_STARTED:
|
2014-03-02 10:12:20 +01:00
|
|
|
appendLog( "File Downloading: " + (String) msg.getObject(), Color.BLACK);
|
2014-03-01 11:13:32 +01:00
|
|
|
case DOWNLOAD_COMPLETE:
|
2014-03-02 10:12:20 +01:00
|
|
|
appendLog( "File Completed: " + (String) msg.getObject(), Color.GREEN);
|
|
|
|
break;
|
2014-03-01 11:13:32 +01:00
|
|
|
case DOWNLOAD_ERRORED:
|
2014-03-02 10:12:20 +01:00
|
|
|
appendLog( "File Errored: " + (String) msg.getObject(), Color.RED);
|
2014-03-01 11:13:32 +01:00
|
|
|
break;
|
2014-03-02 08:28:37 +01:00
|
|
|
|
2014-03-01 11:13:32 +01:00
|
|
|
case RIP_COMPLETE:
|
2014-03-02 08:28:37 +01:00
|
|
|
statusProgress.setValue(100);
|
|
|
|
statusLabel.setVisible(false);
|
|
|
|
openButton.setVisible(true);
|
2014-03-01 11:13:32 +01:00
|
|
|
File f = (File) msg.getObject();
|
2014-03-02 10:12:20 +01:00
|
|
|
String prettyFile = Utils.removeCWD(f);
|
|
|
|
openButton.setText("Open " + prettyFile);
|
|
|
|
appendLog( "Rip complete, saved to " + prettyFile, Color.GREEN);
|
2014-03-02 08:28:37 +01:00
|
|
|
openButton.setActionCommand(f.toString());
|
|
|
|
openButton.addActionListener(new ActionListener() {
|
2014-03-02 03:08:16 +01:00
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
|
|
|
try {
|
|
|
|
Desktop.getDesktop().open(new File(event.getActionCommand()));
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-03-02 08:28:37 +01:00
|
|
|
mainFrame.pack();
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 10:12:20 +01:00
|
|
|
|
|
|
|
class JTextPaneNoWrap extends JTextPane {
|
|
|
|
public boolean getScrollableTracksViewportWidth() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 11:13:32 +01:00
|
|
|
}
|