mystyle-java-fx-tools/src/main/java/com/zhangmeng/tools/controller/PlayerController.java

284 lines
9.1 KiB
Java

package com.zhangmeng.tools.controller;
import com.zhangmeng.tools.utils.ImagePath;
import com.zhangmeng.tools.utils.ResourcesUtils;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.trace.http.TraceableRequest;
import java.io.IOException;
import java.util.Arrays;
/**
* @author : 芊芊墨客
* @version : 1.0
* @date : 2023-02-16 10:04
*/
@Slf4j
public class PlayerController {
private SimpleDoubleProperty width = new SimpleDoubleProperty(0.0);
private SimpleDoubleProperty height = new SimpleDoubleProperty(0.0);
private AnchorPane root;
private AnchorPane video;
private AnchorPane music;
private AnchorPane vip_parser;
@FXML
private ListView<ResourcesUtils.Player> listView;
@FXML
private SplitPane splitPane;
public static final String color_cell = "#f4f4f4";
@FXML
public void md5_menu_item() {
load_encrypt();
}
@FXML
public void spring_security_menu_item() {
load_encrypt();
}
@FXML
public void video_menu_item() {
boolean flag = false;
if (video != null) {
flag = true;
}
video(flag);
}
@FXML
public void music_menu_item() {
boolean flag = false;
if (music != null) {
flag = true;
}
music(flag);
}
@FXML
public void vip_parser_menu_item() {
boolean flag = false;
if (vip_parser != null) {
flag = true;
}
vip_parser(flag);
}
public void load_encrypt() {
Stage stage = (Stage) splitPane.getScene().getWindow();
AnchorPane fx = null;
try {
fx = FXMLLoader.load(ResourcesUtils.getResource("home"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(fx);
stage.setScene(scene);
}
@FXML
public void initialize() {
init();
listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
boolean flag = false;
if (newValue.getIndex() == 0) {
if (video != null) {
flag = true;
}
video(flag);
}
if (newValue.getIndex() == 1) {
if (music != null) {
flag = true;
}
music(flag);
}
if (newValue.getIndex() == 2) {
if (vip_parser != null) {
flag = true;
}
vip_parser(flag);
}
}
});
}
public static Image getImage(ResourcesUtils.Player player) {
return switch (player) {
case Video -> new Image(ImagePath.path(ImagePath.ImagePathType.VIDEO_PLAYER));
case Music -> new Image(ImagePath.path(ImagePath.ImagePathType.MUSIC_PLAYER));
case VipParser -> new Image(ImagePath.path(ImagePath.ImagePathType.VIP_PLAYER));
};
}
public void init() {
ResourcesUtils.Player[] values = ResourcesUtils.Player.values();
ObservableList<ResourcesUtils.Player> list = FXCollections.observableArrayList();
list.addAll(Arrays.asList(values));
listView.setItems(list);
listView.setFixedCellSize(40);
listView.setCellFactory(new Callback<>() {
private int position;
@Override
public ListCell<ResourcesUtils.Player> call(ListView<ResourcesUtils.Player> playerListView) {
Label label = new Label();
label.setPrefWidth(200);
ListCell<ResourcesUtils.Player> listCell = new ListCell<>() {
@Override
protected void updateItem(ResourcesUtils.Player player, boolean b) {
super.updateItem(player, b);
if (!b) {
HBox hBox = new HBox(30);
hBox.setAlignment(Pos.CENTER);
label.setText(player.getTitle());
label.setTextFill(Paint.valueOf("#000000"));
Image im = getImage(player);
ImageView iv = new ImageView(im);
iv.setPreserveRatio(true);
iv.setFitWidth(15);
hBox.getChildren().add(iv);
hBox.getChildren().add(label);
this.setGraphic(hBox);
}
this.setStyle("-fx-background-color: " + color_cell);
}
};
listCell.hoverProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
if (t1 && !label.getText().equals("")) {
position = playerListView.getItems().indexOf(label.getText());
label.setFont(new Font(16));
playerListView.getFocusModel().focus(position);
listCell.setStyle("-fx-background-color: #369e7d");
} else {
label.setPrefHeight(20);
label.setFont(new Font(13));
listCell.setStyle("-fx-background-color: " + color_cell);
}
}
});
return listCell;
}
});
video(false);
}
private void music(boolean flag) {
listView.getSelectionModel().select(1);
if (!flag) {
try {
root = FXMLLoader.load(ResourcesUtils.getResource("music"));
} catch (IOException e) {
e.printStackTrace();
}
music = root;
} else {
root = music;
}
common_method();
}
private void vip_parser(boolean flag) {
listView.getSelectionModel().select(2);
if (!flag) {
try {
root = FXMLLoader.load(ResourcesUtils.getResource("vip-parser"));
} catch (IOException e) {
e.printStackTrace();
}
vip_parser = root;
} else {
root = vip_parser;
}
common_method();
}
private void video(boolean flag) {
//默认选择第一个
listView.getSelectionModel().select(0);
if (!flag) {
try {
root = FXMLLoader.load(ResourcesUtils.getResource("video"));
} catch (IOException e) {
e.printStackTrace();
}
video = root;
} else {
root = video;
}
common_method();
}
private void common_method() {
splitPane.getItems().remove(1);
splitPane.getItems().add(1, root);
root.widthProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
double width = splitPane.getWidth();
PlayerController.this.width.set(width);
log.info("player:--->width:{}", width);
}
});
root.heightProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
double height = splitPane.getHeight();
PlayerController.this.height.set(height);
log.info("player:--->height:{}", height);
}
});
this.width.addListener((observable, oldValue, newValue) -> {
PlayerController.this.root.setPrefWidth(newValue.doubleValue() - listView.getWidth());
log.info("newValue.doubleValue():{}", newValue.doubleValue());
});
this.height.addListener((observable, oldValue, newValue) -> {
PlayerController.this.root.setPrefHeight(newValue.doubleValue() - listView.getHeight());
log.info("newValue.doubleValue():{}", newValue.doubleValue());
});
}
}