/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.livesub.downloader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
import org.apache.log4j.Logger;
import org.livesub.player.Player;
/**
*
* @author vasilis
*/
public class DownloaderUI extends Application {
private static final Logger LOG = Logger.getLogger(DownloaderUI.class);
private final String DEFAULT_OUTPUT_PATH = System.getProperty("user.home") + "/Desktop/subs";
private final Player player;
private Scene scene;
private ComboBox languageCombo;
private TableView<Subtitle> table;
private TextField movieNameField;
private TextField linkField;
private final ObservableList<Subtitle> data;
private Button searchButton, downloadButton, mountButton;
HashMap<String, String> languages;
SearchForSubs titles;
ArrayList<Subtitle> movieList;
public DownloaderUI(Player player) {
this.player = player;
titles = new SearchForSubs();
languages = new HashMap<String, String>();
data = FXCollections.observableArrayList();
}
@Override
public void start(Stage primaryStage) {
Label movieNameLabel = new Label("Movie Name:");
Label languageLabel = new Label("Language:");
movieNameField = new TextField();
languageCombo = new ComboBox();
searchButton = new Button("Search");
initComboBox();
languageCombo.setMaxWidth(Double.MAX_VALUE);
GridPane.setHgrow(movieNameField, Priority.ALWAYS);
GridPane.setHgrow(languageCombo, Priority.ALWAYS);
GridPane topPane = new GridPane();
topPane.setHgap(5);
topPane.setVgap(10);
topPane.setPadding(new Insets(10, 25, 15, 25));
topPane.add(movieNameLabel, 1, 1);
topPane.add(languageLabel, 1, 2);
topPane.add(movieNameField, 2, 1, 2, 1);
topPane.add(languageCombo, 2, 2);
topPane.add(searchButton, 3, 2);
HBox.setHgrow(topPane, Priority.ALWAYS);
HBox topBox = new HBox();
topBox.getChildren().add(topPane);
table = new TableView<Subtitle>();
table.setEditable(false);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
table.setItems(data);
TableColumn idCol = new TableColumn("ID");
idCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("id"));
TableColumn titleCol = new TableColumn("Title");
titleCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("name"));
TableColumn uploadedCol = new TableColumn("Uploaded");
uploadedCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("uploadedDate"));
TableColumn yearReleasedCol = new TableColumn("Year Released");
yearReleasedCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("year"));
TableColumn kindCol = new TableColumn("Kind");
kindCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("kind"));
TableColumn seasonCol = new TableColumn("Season");
seasonCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("season"));
TableColumn episodeCol = new TableColumn("Episode");
episodeCol.setCellValueFactory(new PropertyValueFactory<Subtitle, String>("episode"));
table.getColumns().addAll(idCol, titleCol, uploadedCol, yearReleasedCol, kindCol, seasonCol, episodeCol);
linkField = new TextField("Download Link");
mountButton = new Button("Mount");
downloadButton = new Button("Download");
linkField.setEditable(false);
HBox.setHgrow(linkField, Priority.ALWAYS);
HBox bottomBox = new HBox(5);
bottomBox.setPadding(new Insets(20, 5, 15, 5));
bottomBox.getChildren().addAll(linkField, mountButton, downloadButton );
BorderPane root = new BorderPane();
root.setTop(topBox);
root.setCenter(table);
root.setBottom(bottomBox);
initListeners();
scene = new Scene(root, 500, 600);
scene.getStylesheets().add("org/livesub/styles/DownloaderUIStyle.css");
primaryStage.setTitle("Find Subtitles Online");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void initListeners(){
searchButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
search();
}
});
table.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if(table.getItems().size() != 0){
Subtitle sub = (Subtitle) table.getSelectionModel().getSelectedItem();
linkField.setText(sub.getLink());
}
}
});
table.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
if(table.getItems().size() != 0){
Subtitle sub = (Subtitle) table.getSelectionModel().getSelectedItem();
linkField.setText(sub.getLink());
}
}
});
mountButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
download(true);
}
});
downloadButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
download(false);
}
});
}
private void initComboBox() {
languages.put("*All", "");
languages.put("Albanian", "alb");
languages.put("Arabic", "ara");
languages.put("Armenian", "arm");
languages.put("Basque", "baq");
languages.put("Bengali", "ben");
languages.put("Bosnian", "bos");
languages.put("Portuguese-BR", "pob");
languages.put("Breton", "bre");
languages.put("Bulgarian", "bul");
languages.put("Catalan", "cat");
languages.put("Chinese", "chi");
languages.put("Croatian", "hrv");
languages.put("Czech", "cze");
languages.put("Danish", "dan");
languages.put("Dutch", "dut");
languages.put("English", "eng");
languages.put("Esperanto", "epo");
languages.put("Estonian", "est");
languages.put("Finnish", "fin");
languages.put("French", "fre");
languages.put("Galician", "glg");
languages.put("Georgian", "geo");
languages.put("German", "ger");
languages.put("Greek", "ell");
languages.put("Hebrew", "heb");
languages.put("Hindi", "hin");
languages.put("Hungarian", "hun");
languages.put("Icelandic", "ice");
languages.put("Indonesian", "ind");
languages.put("Italian", "ita");
languages.put("Japanese", "jpn");
languages.put("Kazakh", "kaz");
languages.put("Khmer", "khm");
languages.put("Korean", "kor");
languages.put("Latvian", "lav");
languages.put("Lithuanian", "lit");
languages.put("Luxembourgish", "ltz");
languages.put("Skopjan", "mac");
languages.put("Malay", "may");
languages.put("Malayalam", "mal");
languages.put("Mongolian", "mon");
languages.put("Norwegian", "nor");
languages.put("Occitan", "oci");
languages.put("Farsi", "per");
languages.put("Polish", "pol");
languages.put("Portuguese", "por");
languages.put("Romanian", "rum");
languages.put("Russian", "rus");
languages.put("Serbian", "scc");
languages.put("Sinhalese", "sin");
languages.put("Slovak", "slo");
languages.put("Slovenian", "slv");
languages.put("Spanish", "spa");
languages.put("Swahili", "swa");
languages.put("Swedish", "swe");
languages.put("Syriac", "syr");
languages.put("Tagalog", "tgl");
languages.put("Telugu", "tel");
languages.put("Thai", "tha");
languages.put("Turkish", "tur");
languages.put("Ukrainian", "ukr");
languages.put("Urdu", "urd");
languages.put("Vietnamese", "vie");
TreeSet<String> sortedLangs = new TreeSet<String>(languages.keySet());
languageCombo.getItems().addAll(sortedLangs);
languageCombo.setValue("*All");
}
public static String getKeysFromValue(Map<String, String> hm, String value) {
for (String o : hm.keySet()) {
if (hm.get(o).equals(value)) {
return o;
}
}
return null;
}
public void search() {
table.getItems().removeAll(table.getItems());
scene.setCursor(Cursor.WAIT);
String movieName = movieNameField.getText();
String lang = languages.get(languageCombo.getValue().toString());
LOG.debug(String.format("Search for %s in %s", movieName, languageCombo.getValue().toString()));
titles.search(movieName, lang);
movieList = titles.getResults();
if (movieList != null) {
for (Subtitle result : movieList) {
if (result.getFormat().equals("srt") || result.getFormat().equals("sub")) {
data.add(result);
}
}
}
scene.setCursor(Cursor.DEFAULT);
}
public void download(boolean load) {
if (!linkField.getText().contains("http")) {
return;
}
scene.setCursor(Cursor.WAIT);
UnZip.unZipIt(linkField.getText(), DEFAULT_OUTPUT_PATH);
scene.setCursor(Cursor.DEFAULT);
if (load) {
player.loadSubtitles(UnZip.firstSubPath);
}
}
}