package saveReddit.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import saveReddit.main.MainApplication;
public class MainWindow extends JFrame implements ActionListener{
/**
* @author Pascal Romahn
* @Version Version 0.1
*/
private static final long serialVersionUID = -2971951429304504221L;
/**
* @param args
*/
public static void main(String[] args) {
final MainWindow mw = new MainWindow();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out.println("In shutdown hook");
mw.getMainApplication().writeSettings();
}
}, "Shutdown-thread"));
}
private MainApplication main;
private JScrollPane mainOutputScrollPane;
private JTextArea mainOutput;
private JLabel userLabel;
private JComboBox<String> userBox;
private JButton newUserButton;
private JButton removeUserButton;
private JCheckBox overwriteCheckBox;
private JButton startButton;
private JDialog dialog;
private JLabel label;
private JButton saveButton;
public MainWindow() {
super("SaveReddit");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getRootPane().setDoubleBuffered(true);
GridBagConstraints c = new GridBagConstraints();
mainOutput = new JTextArea();
mainOutput.setEditable(false);
mainOutput.setRows(10);
mainOutputScrollPane = new JScrollPane(mainOutput);
new SmartScroller(mainOutputScrollPane);
main = new MainApplication(this);
userLabel = new JLabel();
userLabel.setText("User");
userLabel.setHorizontalAlignment(SwingConstants.CENTER);
userBox = new JComboBox<String>();
userBox.addActionListener(this);
this.refreshUserBox();
newUserButton = new JButton("Add User");
newUserButton.addActionListener(this);
removeUserButton = new JButton("Remove User");
removeUserButton.addActionListener(this);
overwriteCheckBox = new JCheckBox("Overwrite existing files", false);
overwriteCheckBox.addActionListener(this);
startButton = new JButton("Start");
startButton.addActionListener(this);
//Layout
setLayout(new GridBagLayout());
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
c.gridx = 0;
c.gridy = 0;
//c.weightx = 2;
c.weighty = 1;
c.gridwidth = 3;
getContentPane().add(mainOutputScrollPane, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.gridwidth = 1;
getContentPane().add(userLabel, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
getContentPane().add(userBox, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
getContentPane().add(newUserButton, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
getContentPane().add(removeUserButton, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
getContentPane().add(overwriteCheckBox, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_END;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 3;
getContentPane().add(startButton, c);
this.setSize(250, 350);
setVisible(true);
}
public MainApplication getMainApplication() {
return main;
}
public String getMainOutput() {
return mainOutput.getText();
}
public void setMainOutput(String string) {
mainOutput.setText(string);
mainOutput.updateUI();
}
public void mainOutputAddLine(String string) {
mainOutput.append("\n" + string);
System.out.println(string);
//mainOutput.updateUI();
}
public void mainOutputReplaceLastLine(String string) {
int lastNewLine = this.getMainOutput().lastIndexOf('\n');
this.setMainOutput(this.getMainOutput().substring(0, lastNewLine));
this.mainOutputAddLine(string);
//mainOutput.updateUI();
}
private void refreshUserBox() {
userBox.removeAllItems();
LinkedList<String> list = main.getUsers();
while(list.peekFirst() != null) {
userBox.addItem(list.poll());
}
}
public void startLastSaveBooleanFalseDialog() {
LastSaveBooleanFalseDialog dialog = new LastSaveBooleanFalseDialog(this, main);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(startButton)) {
this.setButtonActiv(startButton, false);
main.runParser();
} else if(e.getSource().equals(newUserButton)) {
//Disable newUserButton
this.setButtonActiv(newUserButton, false);
//Setup dialog
AddUserDialog dialog = new AddUserDialog(this, "New User");
} else if(e.getSource().equals(saveButton)) {
System.out.println("Saving user");
dialog.dispose();
this.setButtonActiv(newUserButton, true);
} else if(e.getSource().equals(removeUserButton)) {
this.mainOutputAddLine("Deleting user " + userBox.getSelectedItem());
main.removeUser((String) userBox.getSelectedItem());
this.refreshUserBox();
} else if(e.getSource().equals(userBox)) {
main.setCurrentUser((String) userBox.getSelectedItem());
main.getCurrentLastSave();
main.getLastSaveBoolean();
System.out.println("Set user to: " + userBox.getSelectedItem());
} else if(e.getSource().equals(overwriteCheckBox)) {
main.setOverwrite(overwriteCheckBox.isSelected());
}
}
public void setButtonActiv(JButton button, boolean value) {
button.setEnabled(value);
}
public void setStartButtonActiv(boolean value) {
startButton.setEnabled(value);
}
public void setNewUserButtonActiv(boolean value) {
newUserButton.setEnabled(value);
}
public void addUser(String name, String url) {
main.addUser(name, url);
userBox.addItem(name);
}
public String getSelectedUser() {
return (String) userBox.getSelectedItem();
}
}