2017-11-19 11:18:26 +00:00
|
|
|
package controller;
|
|
|
|
|
2017-12-20 15:51:52 +00:00
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.datatransfer.Clipboard;
|
|
|
|
import java.awt.datatransfer.StringSelection;
|
2017-11-19 11:18:26 +00:00
|
|
|
import java.io.IOException;
|
2017-12-13 14:35:39 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
2017-11-19 11:18:26 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
2017-12-19 16:06:15 +00:00
|
|
|
import java.util.Iterator;
|
2017-11-19 13:44:40 +00:00
|
|
|
import Classes.Category;
|
2017-12-20 15:51:52 +00:00
|
|
|
import Classes.Toast;
|
2017-11-19 11:18:26 +00:00
|
|
|
import Interfaces.EventObserver;
|
2017-11-19 13:44:40 +00:00
|
|
|
import javafx.application.Platform;
|
2017-12-20 15:51:52 +00:00
|
|
|
import javafx.event.EventHandler;
|
2017-11-19 11:18:26 +00:00
|
|
|
import javafx.fxml.FXMLLoader;
|
2017-12-19 16:06:15 +00:00
|
|
|
import javafx.scene.Node;
|
|
|
|
import javafx.scene.control.TextField;
|
2017-11-19 13:44:40 +00:00
|
|
|
import javafx.scene.image.Image;
|
|
|
|
import javafx.scene.image.ImageView;
|
2017-12-20 15:51:52 +00:00
|
|
|
import javafx.scene.input.MouseEvent;
|
2017-11-19 11:18:26 +00:00
|
|
|
import javafx.scene.layout.AnchorPane;
|
|
|
|
import javafx.scene.layout.FlowPane;
|
|
|
|
import javafx.scene.layout.HBox;
|
|
|
|
import javafx.scene.text.Text;
|
2017-12-20 15:51:52 +00:00
|
|
|
import javafx.stage.Stage;
|
2017-11-19 13:44:40 +00:00
|
|
|
import model.NewsModel;
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
public class Article{
|
|
|
|
|
|
|
|
/* Data */
|
|
|
|
private ArrayList<AnchorPane> items;
|
|
|
|
private FlowPane parent;
|
|
|
|
private EventObserver observer;
|
2017-12-19 16:06:15 +00:00
|
|
|
private AnchorPane root;
|
|
|
|
public Boolean activated = false;
|
2017-12-20 15:51:52 +00:00
|
|
|
private Stage stage;
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Constructor */
|
2017-12-20 15:51:52 +00:00
|
|
|
public Article(FlowPane p_parent, EventObserver observer, AnchorPane root_layout, Stage stage){
|
2017-11-19 11:18:26 +00:00
|
|
|
this.parent = p_parent;
|
|
|
|
this.items = new ArrayList<AnchorPane>();
|
|
|
|
this.observer = observer;
|
2017-12-19 16:06:15 +00:00
|
|
|
this.root = root_layout;
|
2017-12-20 15:51:52 +00:00
|
|
|
this.stage = stage;
|
2017-12-19 16:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void addAllItem(ArrayList<NewsModel> models) throws IOException {
|
|
|
|
/* (9) Add to the controller local */
|
|
|
|
TextField tx = (TextField) this.root.lookup("#mag_searchbar");
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for(NewsModel news : models) {
|
|
|
|
|
|
|
|
tx.textProperty().set("Chargement... "+i+"/"+models.size());
|
|
|
|
|
|
|
|
this.items.add(this.buildItem(news));
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (11) Add to parent (graphics) */
|
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
public void run(){
|
|
|
|
if(Article.this.activated) {
|
|
|
|
Article.this.parent.getChildren().addAll(Article.this.items);
|
|
|
|
tx.textProperty().set("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-19 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:06:15 +00:00
|
|
|
public AnchorPane buildItem(NewsModel model) throws IOException {
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (1) Load the article_disp.fxml */
|
|
|
|
FXMLLoader loader = new FXMLLoader();
|
|
|
|
loader.setLocation(getClass().getResource("/fxml/article_disp.fxml"));
|
|
|
|
|
|
|
|
/* (2) Get the loaded item*/
|
|
|
|
AnchorPane item = (AnchorPane) loader.load();
|
|
|
|
|
|
|
|
/* (3) Set content */
|
2017-11-19 13:44:40 +00:00
|
|
|
this.gsetContent( model.getDescription(), item );
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (4) Set date */
|
2017-11-19 13:44:40 +00:00
|
|
|
this.gsetDate( model.getDate(), item );
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (5) Set title */
|
|
|
|
HBox headerContainer = (HBox) item.getChildren().get(3);
|
2017-11-19 13:44:40 +00:00
|
|
|
this.gsetTitle( model.getTitle(), headerContainer );
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (6) Set tags */
|
2017-11-19 13:44:40 +00:00
|
|
|
this.gsetTags( model.getTags(), headerContainer );
|
|
|
|
|
|
|
|
/* (7) Set image */
|
|
|
|
this.gsetImage( model.getImageURL(), item );
|
|
|
|
|
|
|
|
/* (8) Bind event */
|
2017-12-20 15:51:52 +00:00
|
|
|
item.setOnMouseClicked(new EventHandler<MouseEvent>() {
|
|
|
|
@Override
|
|
|
|
public void handle(MouseEvent event) {
|
|
|
|
StringSelection selection = new StringSelection(model.getNewsURL());
|
|
|
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
clipboard.setContents(selection, selection);
|
|
|
|
|
|
|
|
Toast.makeText(Article.this.stage, "Adresse de l'article copié", 2000, 200, 200);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2017-11-19 11:18:26 +00:00
|
|
|
|
2017-11-19 13:44:40 +00:00
|
|
|
/* (10) On bind au width du parent */
|
2017-11-19 11:18:26 +00:00
|
|
|
item.prefWidthProperty().bind(this.parent.widthProperty());
|
|
|
|
item.maxWidthProperty().bind(this.parent.widthProperty());
|
|
|
|
|
2017-12-19 16:06:15 +00:00
|
|
|
return item;
|
2017-11-19 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:06:15 +00:00
|
|
|
public synchronized void clearContent() {
|
|
|
|
this.items.clear();
|
2017-12-12 12:40:22 +00:00
|
|
|
Platform.runLater(new Runnable(){
|
|
|
|
public void run(){
|
2017-12-19 16:06:15 +00:00
|
|
|
Iterator<Node> it = Article.this.parent.getChildren().iterator();
|
|
|
|
while(it.hasNext()) {
|
|
|
|
Node n = it.next();
|
|
|
|
if(n.getId() != "mag_searchbar") {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 12:40:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void gsetContent(String p_content, AnchorPane p_parent){
|
|
|
|
/* (1) Get node */
|
|
|
|
Text g_content = (Text) p_parent.getChildren().get(1);
|
|
|
|
|
|
|
|
/* (2) Update content */
|
|
|
|
g_content.setText(p_content);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-19 13:44:40 +00:00
|
|
|
public void gsetDate(Date p_date, AnchorPane p_parent){
|
2017-11-19 11:18:26 +00:00
|
|
|
/* (1) Get node */
|
|
|
|
Text g_date = (Text) p_parent.getChildren().get(2);
|
|
|
|
|
|
|
|
/* (2) Update content */
|
2017-12-13 14:35:39 +00:00
|
|
|
g_date.setText(new SimpleDateFormat("dd-MM-yyyy HH:mm").format(p_date));
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void gsetTitle(String p_title, HBox p_parent){
|
|
|
|
/* (1) Get node */
|
|
|
|
Text g_title = (Text) p_parent.getChildren().get(0);
|
|
|
|
|
|
|
|
/* (2) Update title */
|
|
|
|
g_title.setText(p_title);
|
|
|
|
}
|
2017-11-19 13:44:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void gsetImage(String p_uri, AnchorPane p_parent){
|
|
|
|
/* (1) Get node */
|
|
|
|
ImageView g_image = (ImageView) p_parent.getChildren().get(0);
|
|
|
|
|
|
|
|
/* (2) Update title */
|
2017-12-12 13:39:34 +00:00
|
|
|
try {
|
|
|
|
g_image.setImage(new Image(p_uri));
|
|
|
|
}catch(Exception e) {
|
|
|
|
g_image.setImage(new Image("http://lorempicsum.com/futurama/255/200/2"));
|
|
|
|
}
|
2017-11-19 13:44:40 +00:00
|
|
|
}
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
|
2017-11-19 13:44:40 +00:00
|
|
|
public void gsetTags(ArrayList<Category> p_tags, HBox p_parent) throws IOException{
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (1) For each tag -> load a new one */
|
2017-11-19 13:44:40 +00:00
|
|
|
for( Category tag : p_tags ){
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (1.1) Create the container */
|
|
|
|
FXMLLoader loader = new FXMLLoader();
|
|
|
|
loader.setLocation(getClass().getResource("/fxml/article_tag_disp.fxml"));
|
|
|
|
|
|
|
|
/* (1.2) Load the tag elements */
|
2017-11-19 13:44:40 +00:00
|
|
|
FlowPane g_tag = (FlowPane) loader.load();
|
|
|
|
Text g_tagText = (Text) g_tag.getChildren().get(0);
|
2017-11-19 11:18:26 +00:00
|
|
|
|
|
|
|
/* (1.3) Update the tag name */
|
|
|
|
g_tagText.setText(tag.getLabel());
|
|
|
|
|
|
|
|
/* (1.4) Set the custom color */
|
|
|
|
g_tag.setStyle("-fx-background-color: "+tag.getColor());
|
|
|
|
|
|
|
|
/* (1.5) Ajout au parent*/
|
|
|
|
p_parent.getChildren().add(g_tag);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|