2018-05-02 05:21:44 +03:00
|
|
|
package mplayer4anime.appPanes;
|
|
|
|
|
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.ObservableList;
|
|
|
|
import javafx.fxml.FXML;
|
|
|
|
import javafx.fxml.Initializable;
|
2018-11-28 07:10:35 +03:00
|
|
|
import javafx.scene.control.Label;
|
2018-05-02 05:21:44 +03:00
|
|
|
import javafx.scene.control.ListCell;
|
|
|
|
import javafx.scene.control.ListView;
|
|
|
|
import javafx.scene.input.KeyEvent;
|
|
|
|
import javafx.stage.DirectoryChooser;
|
2018-11-28 07:10:35 +03:00
|
|
|
import javafx.stage.FileChooser;
|
2018-05-02 05:21:44 +03:00
|
|
|
import javafx.util.Callback;
|
2018-11-28 07:10:35 +03:00
|
|
|
import mplayer4anime.AppPreferences;
|
2018-05-02 05:21:44 +03:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Arrays;
|
2018-11-28 07:10:35 +03:00
|
|
|
import java.util.List;
|
2018-05-02 05:21:44 +03:00
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
2018-11-28 07:10:35 +03:00
|
|
|
public class ControllerPane implements Initializable {
|
2018-05-02 05:21:44 +03:00
|
|
|
|
2018-05-02 15:28:32 +03:00
|
|
|
private ResourceBundle resourceBundle;
|
2018-05-02 05:21:44 +03:00
|
|
|
// use folderToOpen same variable in all panes
|
|
|
|
private static String folderToOpen;
|
|
|
|
|
2018-11-28 07:10:35 +03:00
|
|
|
private AppPreferences appPreferences;
|
|
|
|
|
2018-05-02 05:21:44 +03:00
|
|
|
@FXML
|
2018-11-25 18:13:09 +03:00
|
|
|
private ListView<File> paneListView;
|
|
|
|
private ObservableList<File> paneFileList = FXCollections.observableArrayList();
|
2018-05-02 05:21:44 +03:00
|
|
|
|
2018-11-28 07:10:35 +03:00
|
|
|
@FXML
|
|
|
|
private Label paneLbl;
|
|
|
|
private String paneType;
|
|
|
|
|
|
|
|
|
2018-05-02 05:21:44 +03:00
|
|
|
@Override
|
|
|
|
public void initialize(URL url, ResourceBundle resBundle) {
|
|
|
|
SetCellFactory(paneListView);
|
|
|
|
resourceBundle = resBundle;
|
2018-11-28 07:10:35 +03:00
|
|
|
appPreferences = new AppPreferences();
|
|
|
|
}
|
|
|
|
public void setPaneType(String paneType){
|
|
|
|
this.paneType = paneType;
|
|
|
|
|
|
|
|
switch (paneType) {
|
|
|
|
case "Video":
|
|
|
|
paneLbl.setText(resourceBundle.getString("lbl_VideoPane"));
|
|
|
|
break;
|
|
|
|
case "Audio":
|
|
|
|
paneLbl.setText(resourceBundle.getString("lbl_AudioPane"));
|
|
|
|
break;
|
|
|
|
case "Subtitles":
|
|
|
|
paneLbl.setText(resourceBundle.getString("lbl_SubsPane"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
paneLbl.setText(resourceBundle.getString("?"));
|
|
|
|
break;
|
|
|
|
}
|
2018-05-02 05:21:44 +03:00
|
|
|
}
|
|
|
|
|
2018-11-25 18:13:09 +03:00
|
|
|
/** Get index of the selected in pane element */
|
|
|
|
public int getElementSelectedIndex(){
|
|
|
|
return this.paneListView.getSelectionModel().getSelectedIndex();
|
|
|
|
}
|
|
|
|
/** Select element name (full path) using index recieved */
|
|
|
|
public String getElementSelected(){
|
|
|
|
if (this.paneListView.getSelectionModel().getSelectedItem() != null) {
|
|
|
|
return this.paneFileList.get(this.getElementSelectedIndex()).toPath().toString();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** Select element in pane using index recieved */
|
|
|
|
public void setElementSelectedByIndex(int index){
|
|
|
|
this.paneListView.getSelectionModel().select(index);
|
|
|
|
}
|
|
|
|
/** Get number of elements loaded into the pane */
|
|
|
|
public int getElementsCount(){
|
|
|
|
return this.paneFileList.size();
|
|
|
|
}
|
|
|
|
/** Check if there are any elements loaded */
|
|
|
|
public boolean isElementsListEmpty(){
|
|
|
|
return paneFileList.isEmpty();
|
|
|
|
}
|
|
|
|
/** Get all elements
|
|
|
|
* Used in Json playlist writer only */
|
|
|
|
public String[] getElementsAll(){
|
|
|
|
String[] elementsArray = new String[this.getElementsCount()];
|
|
|
|
for (int i = 0; i < elementsArray.length; i++){
|
|
|
|
elementsArray[i] = paneFileList.get(i).toString();
|
|
|
|
}
|
|
|
|
return elementsArray;
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:21:44 +03:00
|
|
|
private void SetCellFactory(ListView<File> lv) {
|
|
|
|
lv.setCellFactory(new Callback<ListView<File>, ListCell<File>>() {
|
|
|
|
@Override
|
|
|
|
public ListCell<File> call(ListView<File> fileListView) {
|
|
|
|
return new ListCell<File>(){
|
|
|
|
@Override
|
|
|
|
public void updateItem(File item, boolean empty){
|
|
|
|
// have to call super here
|
|
|
|
super.updateItem(item, empty);
|
|
|
|
|
|
|
|
String trimmedName;
|
|
|
|
|
|
|
|
if (item == null || empty){
|
|
|
|
setText(null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
trimmedName = item.getName();
|
|
|
|
setText(trimmedName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-11-25 18:13:09 +03:00
|
|
|
/**
|
|
|
|
* Open file selector (Open folder button in UI).
|
|
|
|
* */
|
2018-11-28 07:10:35 +03:00
|
|
|
@FXML
|
|
|
|
void openDirChooser(){
|
|
|
|
String[] filesExtension;
|
|
|
|
switch (paneType) {
|
|
|
|
case "Video":
|
|
|
|
filesExtension = appPreferences.getVideoExtensionsList();
|
|
|
|
break;
|
|
|
|
case "Audio":
|
|
|
|
filesExtension = appPreferences.getAudioExtensionsList();
|
|
|
|
break;
|
|
|
|
case "Subtitles":
|
|
|
|
filesExtension = appPreferences.getSubsExtensionsList();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
filesExtension = new String[]{"*"};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:21:44 +03:00
|
|
|
File directoryReceived; // Store files (folder) received from selector
|
|
|
|
DirectoryChooser dirSelect;
|
|
|
|
|
|
|
|
// Show directory selector
|
|
|
|
dirSelect = new DirectoryChooser();
|
|
|
|
dirSelect.setTitle(resourceBundle.getString("SelectDirectoryTitle"));
|
|
|
|
if (folderToOpen == null)
|
|
|
|
dirSelect.setInitialDirectory(new File(System.getProperty("user.home")));
|
|
|
|
else
|
|
|
|
dirSelect.setInitialDirectory(new File(folderToOpen));
|
2018-11-25 18:13:09 +03:00
|
|
|
directoryReceived = dirSelect.showDialog(paneListView.getScene().getWindow());
|
2018-05-02 05:21:44 +03:00
|
|
|
|
2018-11-25 18:13:09 +03:00
|
|
|
// GET LIST OF FILES from directory
|
2018-05-02 05:21:44 +03:00
|
|
|
if (directoryReceived != null) {
|
2018-11-28 07:10:35 +03:00
|
|
|
File[] files; // Store files mkv/mka
|
|
|
|
|
|
|
|
files = directoryReceived.listFiles(new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file, String Name) {
|
|
|
|
if (Name.lastIndexOf('.') > 0) {
|
|
|
|
int lastindex = Name.lastIndexOf('.');
|
|
|
|
String ext = Name.substring(lastindex);
|
|
|
|
for (String key : filesExtension){
|
|
|
|
if (ext.equals(key.substring(1)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
displayFiles(files);
|
2018-05-02 05:21:44 +03:00
|
|
|
} else {
|
2018-11-28 07:10:35 +03:00
|
|
|
System.out.println("No folder selected");
|
2018-05-02 05:21:44 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 07:10:35 +03:00
|
|
|
/**
|
|
|
|
* Open file selector (Open files button in UI).
|
|
|
|
* */
|
|
|
|
@FXML
|
|
|
|
void openFilesChooser(){
|
|
|
|
String[] filesExtension;
|
|
|
|
switch (paneType) {
|
|
|
|
case "Video":
|
|
|
|
filesExtension = appPreferences.getVideoExtensionsList();
|
|
|
|
break;
|
|
|
|
case "Audio":
|
|
|
|
filesExtension = appPreferences.getAudioExtensionsList();
|
|
|
|
break;
|
|
|
|
case "Subtitles":
|
|
|
|
filesExtension = appPreferences.getSubsExtensionsList();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
filesExtension = new String[]{"*"};
|
|
|
|
break;
|
|
|
|
}
|
2018-05-02 05:21:44 +03:00
|
|
|
|
2018-11-28 07:10:35 +03:00
|
|
|
List<File> filesRecievedList;
|
2018-05-02 05:21:44 +03:00
|
|
|
|
2018-11-28 07:10:35 +03:00
|
|
|
FileChooser fc = new FileChooser();
|
|
|
|
fc.setTitle(resourceBundle.getString("SelectFile"));
|
|
|
|
if (folderToOpen == null)
|
|
|
|
fc.setInitialDirectory(new File(System.getProperty("user.home")));
|
|
|
|
else
|
|
|
|
fc.setInitialDirectory(new File(folderToOpen));
|
|
|
|
fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter(paneType, filesExtension));
|
2018-05-02 05:21:44 +03:00
|
|
|
|
2018-11-28 07:10:35 +03:00
|
|
|
filesRecievedList = fc.showOpenMultipleDialog(paneListView.getScene().getWindow());
|
|
|
|
if (filesRecievedList != null){
|
|
|
|
File[] filesRecieved = new File[filesRecievedList.size()];
|
|
|
|
filesRecievedList.toArray(filesRecieved);
|
|
|
|
displayFiles(filesRecieved);
|
|
|
|
} else {
|
|
|
|
System.out.println("No files selected");
|
|
|
|
}
|
2018-11-25 18:13:09 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set files using lists. Used if playlist loaded
|
|
|
|
* */
|
|
|
|
public void setFilesFromList(String[] fileLocations){
|
|
|
|
if (fileLocations != null && fileLocations.length != 0) {
|
|
|
|
File[] files = new File[fileLocations.length];
|
|
|
|
for (int i=0; i < fileLocations.length; i++)
|
|
|
|
files[i] = new File(fileLocations[i]);
|
|
|
|
displayFiles(files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void displayFiles(File[] files){
|
2018-05-02 05:21:44 +03:00
|
|
|
if (files != null && files.length > 0) {
|
|
|
|
// spiced java magic
|
|
|
|
Arrays.sort(files);
|
2018-11-25 18:13:09 +03:00
|
|
|
|
2018-05-02 05:21:44 +03:00
|
|
|
// Remember the folder used for MKV and reuse it when user opens MKA/subs folder (as new default path instead of user.home)
|
|
|
|
folderToOpen = files[0].getParent();
|
2018-11-25 18:13:09 +03:00
|
|
|
//System.out.println(folderToOpen);
|
2018-05-02 05:21:44 +03:00
|
|
|
|
|
|
|
paneFileList.addAll(files);
|
|
|
|
paneListView.setItems(paneFileList);
|
|
|
|
paneListView.getSelectionModel().select(0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
System.out.println("\tNo files in this folder");
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 07:10:35 +03:00
|
|
|
@FXML
|
|
|
|
public void cleanList(){
|
|
|
|
paneListView.getItems().clear(); // wipe elements from ListView
|
|
|
|
}
|
|
|
|
|
2018-05-02 05:21:44 +03:00
|
|
|
@FXML
|
|
|
|
private void Up(){
|
|
|
|
int index;
|
|
|
|
index = paneListView.getSelectionModel().getSelectedIndex();
|
|
|
|
if (index >0){
|
|
|
|
paneFileList.add(index-1, paneListView.getSelectionModel().getSelectedItem());
|
|
|
|
paneFileList.remove(index+1);
|
|
|
|
paneListView.getSelectionModel().select(index-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@FXML
|
|
|
|
private void Down(){
|
|
|
|
int index;
|
|
|
|
index = paneListView.getSelectionModel().getSelectedIndex();
|
|
|
|
if (index+1 < paneFileList.size() ){
|
|
|
|
paneFileList.add(index+2, paneListView.getSelectionModel().getSelectedItem());
|
|
|
|
paneFileList.remove(index);
|
|
|
|
paneListView.getSelectionModel().select(index+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@FXML
|
|
|
|
private void Del(){ paneFileList.remove(paneListView.getSelectionModel().getSelectedItem()); }
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private void KeyPressed(KeyEvent event){
|
|
|
|
if (event.getCode().toString().equals("DELETE"))
|
|
|
|
Del();
|
|
|
|
}
|
2018-11-25 18:13:09 +03:00
|
|
|
}
|