package de.markusc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import java.util.prefs.Preferences;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import de.markusc.controller.ExcelFileManager;
import de.markusc.model.Buch;
import de.markusc.model.BuchListWrapper;
import de.markusc.view.BuchUebersichtController;
import de.markusc.view.BuchhalterController;
import de.markusc.view.RegalBodenController;
import de.markusc.view.RootLayoutController;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private AnchorPane buchHalter;
private ScrollPane buchübersichtScrollpane,regalbodenScrollpane;
private ObservableList<Buch> buchListe= FXCollections.observableArrayList();
private ObservableList<Buch> buchListeX= FXCollections.observableArrayList();
private Vector<ObservableList<Buch>> BuchKategorien;
private Vector<String> vKat;
public Main() {
}
public ObservableList<Buch> getBuchData() {
return buchListe;
}
@Override
public void start(Stage primaryStage) {
try {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("Bibliothek");
initRootLayout();
} catch(Exception e) {
e.printStackTrace();
}
}
public void importXLS(File file) {
try {
if(file!=null) {
ExcelFileManager efM=new ExcelFileManager(file);
buchListe.clear();
buchListe= FXCollections.observableArrayList();
buchListe=efM.readFile();
sortiere(buchListe);
showBooks();
}
}catch(Exception e) {
e.printStackTrace();
}
}
public void exportXLS(File file) {
if(file!=null) {
ExcelFileManager efM=new ExcelFileManager(file);
efM.saveFile(buchListe);
}
}
private void sortiere(ObservableList<Buch> buchListe2) {
System.out.println("Anfang: Methode sortiere ");
boolean enthaelt=false;
vKat=new Vector<String>();
vKat.add(((Buch)buchListe2.get(0)).getKategorie());
for(int i=1;i<buchListe2.size();i++) {
enthaelt=false;
String kategorie=buchListe2.get(i).getKategorie();
for(int j=0; j<vKat.size();j++) {
if(kategorie.equals(vKat.get(j))) {
enthaelt=true;
}
}
if(enthaelt==false&&!kategorie.equals("")) {
vKat.add(kategorie);
enthaelt=false;
}
}
BuchKategorien=new Vector<ObservableList<Buch>>();
for(int i=0; i<vKat.size();i++) {
buchListeX= FXCollections.observableArrayList();
BuchKategorien.add(buchListeX);
}
for(int i=0; i<buchListe2.size();i++) {
Buch buch=(Buch)buchListe2.get(i);
for(int j=0;j<vKat.size();j++) {
ObservableList<Buch> buchListe3=(ObservableList<Buch>) BuchKategorien.get(j);
if(buch.getKategorie().equals(((String)vKat.get(j)))) {
buchListe3.add(buch);
}
}
}
System.out.println("Ende: Methode sortiere ");
}
public void beenden() {
primaryStage.close();
}
public void initRootLayout() {
try {
System.out.println("Anfang: Methode initRootLayout");
FXMLLoader loader= new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout= (BorderPane) loader.load();
Scene scene=new Scene(rootLayout);
String urlString="application.css";
scene.getStylesheets().add(Main.class.getResource(urlString).toExternalForm());
primaryStage.setScene(scene);
RootLayoutController controller = loader.getController();
controller.setMain(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Schritt 1");
File file = getBuchFilePath();
if (file != null) {
System.out.println(file.getAbsolutePath().toString());
loadBuchDataFromFile(file);
}
sortiere(buchListe);
showBooks();
System.out.println("Ende: Methode RootLayout ");
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void showBooks() {
System.out.println("Anfang: Methode ShowBooks ");
try {
FXMLLoader loader2 = new FXMLLoader();
loader2.setLocation(Main.class.getResource("view/BuchUebersicht.fxml"));
AnchorPane buchUebersicht = (AnchorPane) loader2.load();
rootLayout.setCenter(buchUebersicht);
BuchUebersichtController controller = loader2.getController();
controller.setMain(this);
ScrollPane buchübersichtScrollpane =controller.getScrollpane();
VBox vb= new VBox();
Vector<HBox> boxVector=new Vector<HBox>();
HBox hb=null;
for(int j=0; j<BuchKategorien.size(); j++) {
hb= new HBox();
ObservableList<Buch> buchListe=(ObservableList<Buch>)BuchKategorien.get(j);
for(int i=0; i<buchListe.size(); i++) {
int k=i;
FXMLLoader loaderBuchHalter = new FXMLLoader();
loaderBuchHalter.setLocation(Main.class.getResource("view/BuchHalter.fxml"));
buchHalter = (AnchorPane) loaderBuchHalter.load();
buchHalter.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, new CornerRadii(5), BorderStroke.THIN)));
BuchhalterController buchhalterController=loaderBuchHalter.getController();
buchhalterController.setMain(this);
buchhalterController.showBook(buchListe.get(i));
hb.getChildren().add(buchHalter);
buchHalter.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
controller.setBuch(buchhalterController.getBook());
final Canvas canvas=controller.getCanvas();
final GraphicsContext gc=canvas.getGraphicsContext2D();
FileInputStream inputstream=null;
try {
String s=System.getProperty("user.home")+"/"+buchListe.get(k).getCover();
inputstream = new FileInputStream(s);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Image image = new Image(inputstream);
gc.drawImage(image, 0, 0, 200, 200);
}
});
}
boxVector.add(hb);
FXMLLoader loaderRegalboden = new FXMLLoader();
loaderRegalboden.setLocation(Main.class.getResource("view/Regalboden.fxml"));
AnchorPane regalboden = (AnchorPane) loaderRegalboden.load();
RegalBodenController regalbodenController= loaderRegalboden.getController();
regalbodenController.setLabelTxt((String)(vKat.get(j)));
regalbodenController.setMain(this);
ScrollPane regalbodenScrollpane=regalbodenController.getScrollPane();
regalbodenScrollpane.setContent((HBox)boxVector.get(j));
vb.getChildren().add(regalboden);
}
buchübersichtScrollpane.setContent(vb);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Ende: Methode ShowBooks ");
}
public void loadBuchDataFromFile(File file) {
System.out.println("Anfang: Methode load");
try {
JAXBContext context = JAXBContext
.newInstance(BuchListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
BuchListWrapper wrapper = (BuchListWrapper) um.unmarshal(file);
buchListe.clear();
buchListe= FXCollections.observableArrayList();
buchListe.addAll(wrapper.getBuecher());
setBuchFilePath(file);
System.out.println("size: "+buchListe.size());
System.out.println(((Buch)buchListe.get(0)).getClass());
for(int i=0;i<buchListe.size();i++) {
System.out.println(buchListe.getClass());
System.out.println(((Buch)buchListe.get(i)).getTitel());
}
} catch (Exception e) {
e.printStackTrace();
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Could not load data");
alert.setContentText("Could not load data from file:\n" + file.getPath());
alert.showAndWait();
}
System.out.println("Ende: Methode load");
}
public void setBuchFilePath(File file) {
System.out.println("Anfang: Methode setBuchFilepath");
Preferences prefs = Preferences.userNodeForPackage(Main.class);
if (file != null) {
prefs.put("filePath", file.getPath());
primaryStage.setTitle("Bibliothek - " + file.getName());
} else {
prefs.remove("filePath");
primaryStage.setTitle("Bibliothek");
}
System.out.println("Ende: Methode setBuchFilepath");
}
public File getBuchFilePath() {
System.out.println("Anfang: Methode getBuchFilepath");
Preferences prefs = Preferences.userNodeForPackage(Main.class);
String filePath = prefs.get("filePath", null);
if (filePath != null) {
System.out.println("Ende: Methode getBuchFilepath");
return new File(filePath);
} else {
System.out.println("Ende: Methode getBuchFilepath");
return null;
}
}
public void saveBuchDataToFile(File file) {
try {
System.out.println("Anfang: Methode saveBuchFilepath");
JAXBContext context = JAXBContext
.newInstance(BuchListWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
System.out.println(buchListe.size());
BuchListWrapper wrapper = new BuchListWrapper();
wrapper.setBuecher(buchListe);
m.marshal(wrapper, file);
System.out.println("Anfang: Methode saveBuchFilepath");
setBuchFilePath(file);
} catch (Exception e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Could not save data");
alert.setContentText("Could not save data to file:\n" + file.getPath());
alert.showAndWait();
}
}
public static void main(String[] args) {
launch(args);
}
}