Article fetch [ok]
This commit is contained in:
parent
fee05db7f4
commit
217d64e44e
|
@ -1,5 +1,25 @@
|
||||||
package Classes;
|
package Classes;
|
||||||
|
|
||||||
public enum Category {
|
public enum Category {
|
||||||
business, entertainment, gaming, general, healthAndMedical, music, politics, scienceAndNature, sport, technology, all
|
// business, entertainment, gaming, general, healthAndMedical, music, politics, scienceAndNature, sport, technology, all
|
||||||
|
|
||||||
|
all("all", "black"),
|
||||||
|
business("business", "red"),
|
||||||
|
entertainment("entertainment", "skyblue"),
|
||||||
|
gaming("gaming", "green"),
|
||||||
|
health("health", "yellow"),
|
||||||
|
music("music", "purple"),
|
||||||
|
sport("sport", "brown"),
|
||||||
|
science("science", "#f14405"),
|
||||||
|
nature("nature", "#16c668"),
|
||||||
|
economics("economics", "#d1991b"),
|
||||||
|
politics("politics", "#6825f4"),
|
||||||
|
technology("technology", "#1e7ebe");
|
||||||
|
|
||||||
|
protected String color;
|
||||||
|
protected String label;
|
||||||
|
Category(String label, String color){ this.label = label; this.color = color; }
|
||||||
|
|
||||||
|
public String getLabel(){ return this.label; }
|
||||||
|
public String getColor(){ return this.color; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
package Classes;
|
|
||||||
|
|
||||||
public enum TagType {
|
|
||||||
science("science", "#f14405"),
|
|
||||||
nature("nature", "#16c668"),
|
|
||||||
economics("economics", "#d1991b"),
|
|
||||||
politics("politics", "#6825f4"),
|
|
||||||
technology("technology", "#1e7ebe");
|
|
||||||
|
|
||||||
protected String color;
|
|
||||||
protected String label;
|
|
||||||
TagType(String label, String color){ this.label = label; this.color = color; }
|
|
||||||
|
|
||||||
public String getLabel(){ return this.label; }
|
|
||||||
public String getColor(){ return this.color; }
|
|
||||||
|
|
||||||
}
|
|
|
@ -4,21 +4,17 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import Classes.TagType;
|
import Classes.Category;
|
||||||
import Interfaces.EventObserver;
|
import Interfaces.EventObserver;
|
||||||
import javafx.event.EventHandler;
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.geometry.Insets;
|
import javafx.scene.image.Image;
|
||||||
import javafx.scene.input.MouseEvent;
|
import javafx.scene.image.ImageView;
|
||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.layout.Background;
|
|
||||||
import javafx.scene.layout.BackgroundFill;
|
|
||||||
import javafx.scene.layout.CornerRadii;
|
|
||||||
import javafx.scene.layout.FlowPane;
|
import javafx.scene.layout.FlowPane;
|
||||||
import javafx.scene.layout.HBox;
|
import javafx.scene.layout.HBox;
|
||||||
import javafx.scene.layout.Pane;
|
|
||||||
import javafx.scene.paint.Paint;
|
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
|
import model.NewsModel;
|
||||||
|
|
||||||
public class Article{
|
public class Article{
|
||||||
|
|
||||||
|
@ -35,7 +31,7 @@ public class Article{
|
||||||
this.observer = observer;
|
this.observer = observer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addItem(String p_title, String p_content, Date p_publ, ArrayList<TagType> p_tags) throws IOException {
|
public void addItem(NewsModel model) throws IOException {
|
||||||
|
|
||||||
/* (1) Load the article_disp.fxml */
|
/* (1) Load the article_disp.fxml */
|
||||||
FXMLLoader loader = new FXMLLoader();
|
FXMLLoader loader = new FXMLLoader();
|
||||||
|
@ -45,35 +41,42 @@ public class Article{
|
||||||
AnchorPane item = (AnchorPane) loader.load();
|
AnchorPane item = (AnchorPane) loader.load();
|
||||||
|
|
||||||
/* (3) Set content */
|
/* (3) Set content */
|
||||||
this.gsetContent( p_content, item );
|
this.gsetContent( model.getDescription(), item );
|
||||||
|
|
||||||
/* (4) Set date */
|
/* (4) Set date */
|
||||||
this.gsetDate( p_publ.toString(), item );
|
this.gsetDate( model.getDate(), item );
|
||||||
|
|
||||||
/* (5) Set title */
|
/* (5) Set title */
|
||||||
HBox headerContainer = (HBox) item.getChildren().get(3);
|
HBox headerContainer = (HBox) item.getChildren().get(3);
|
||||||
this.gsetTitle( p_title, headerContainer );
|
this.gsetTitle( model.getTitle(), headerContainer );
|
||||||
|
|
||||||
/* (6) Set tags */
|
/* (6) Set tags */
|
||||||
this.gsetTags( p_tags, headerContainer );
|
this.gsetTags( model.getTags(), headerContainer );
|
||||||
|
|
||||||
/* (7) Bind event */
|
/* (7) Set image */
|
||||||
item.setOnMousePressed(new EventHandler<MouseEvent>() {
|
this.gsetImage( model.getImageURL(), item );
|
||||||
@Override
|
|
||||||
public void handle(MouseEvent event) {
|
|
||||||
Article.this.observer.handleEvent(new Classes.Event(item.getId(),"changeMainLayout"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/* (8) Add to the controller local */
|
/* (8) Bind event */
|
||||||
|
// item.setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||||
|
// @Override
|
||||||
|
// public void handle(MouseEvent event) {
|
||||||
|
// Article.this.observer.handleEvent(new Classes.Event(item.getId(),"changeMainLayout"));
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
/* (9) Add to the controller local */
|
||||||
this.items.add(item);
|
this.items.add(item);
|
||||||
|
|
||||||
/* (9) On bind au width du parent */
|
/* (10) On bind au width du parent */
|
||||||
item.prefWidthProperty().bind(this.parent.widthProperty());
|
item.prefWidthProperty().bind(this.parent.widthProperty());
|
||||||
item.maxWidthProperty().bind(this.parent.widthProperty());
|
item.maxWidthProperty().bind(this.parent.widthProperty());
|
||||||
|
|
||||||
/* (10) Add to parent (graphics) */
|
/* (11) Add to parent (graphics) */
|
||||||
this.parent.getChildren().add(item);
|
Platform.runLater(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
Article.this.parent.getChildren().add(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,12 +90,12 @@ public class Article{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void gsetDate(String p_date, AnchorPane p_parent){
|
public void gsetDate(Date p_date, AnchorPane p_parent){
|
||||||
/* (1) Get node */
|
/* (1) Get node */
|
||||||
Text g_date = (Text) p_parent.getChildren().get(2);
|
Text g_date = (Text) p_parent.getChildren().get(2);
|
||||||
|
|
||||||
/* (2) Update content */
|
/* (2) Update content */
|
||||||
g_date.setText(p_date);
|
g_date.setText(p_date.toString());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,20 +107,29 @@ public class Article{
|
||||||
/* (2) Update title */
|
/* (2) Update title */
|
||||||
g_title.setText(p_title);
|
g_title.setText(p_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void gsetImage(String p_uri, AnchorPane p_parent){
|
||||||
|
/* (1) Get node */
|
||||||
|
ImageView g_image = (ImageView) p_parent.getChildren().get(0);
|
||||||
|
|
||||||
|
/* (2) Update title */
|
||||||
|
g_image.setImage(new Image(p_uri));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void gsetTags(ArrayList<TagType> p_tags, HBox p_parent) throws IOException{
|
public void gsetTags(ArrayList<Category> p_tags, HBox p_parent) throws IOException{
|
||||||
|
|
||||||
/* (1) For each tag -> load a new one */
|
/* (1) For each tag -> load a new one */
|
||||||
for( TagType tag : p_tags ){
|
for( Category tag : p_tags ){
|
||||||
|
|
||||||
/* (1.1) Create the container */
|
/* (1.1) Create the container */
|
||||||
FXMLLoader loader = new FXMLLoader();
|
FXMLLoader loader = new FXMLLoader();
|
||||||
loader.setLocation(getClass().getResource("/fxml/article_tag_disp.fxml"));
|
loader.setLocation(getClass().getResource("/fxml/article_tag_disp.fxml"));
|
||||||
|
|
||||||
/* (1.2) Load the tag elements */
|
/* (1.2) Load the tag elements */
|
||||||
Pane g_tag = (Pane) loader.load();
|
FlowPane g_tag = (FlowPane) loader.load();
|
||||||
Text g_tagText = (Text) g_tag.getChildren().get(0);
|
Text g_tagText = (Text) g_tag.getChildren().get(0);
|
||||||
|
|
||||||
/* (1.3) Update the tag name */
|
/* (1.3) Update the tag name */
|
||||||
g_tagText.setText(tag.getLabel());
|
g_tagText.setText(tag.getLabel());
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class HeaderMenu{
|
||||||
menuItem.setOnMousePressed(new EventHandler<MouseEvent>() {
|
menuItem.setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(MouseEvent event) {
|
public void handle(MouseEvent event) {
|
||||||
HeaderMenu.this.observer.handleEvent(new Classes.Event(menuItem.getId(),"changeMainLayout"));
|
HeaderMenu.this.observer.handleEvent(new Classes.Event(menuItem.getId(), "changeMainLayout"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,9 @@ package controller;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import Classes.Category;
|
import Classes.Category;
|
||||||
import Classes.SortTypes;
|
import Classes.SortTypes;
|
||||||
import Classes.TagType;
|
|
||||||
import Classes.css.user.ContextBuilder;
|
import Classes.css.user.ContextBuilder;
|
||||||
import Classes.css.user.HeaderIconStyleSheet;
|
import Classes.css.user.HeaderIconStyleSheet;
|
||||||
import Classes.css.user.HeaderStyleSheet;
|
import Classes.css.user.HeaderStyleSheet;
|
||||||
|
@ -21,6 +19,7 @@ import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.layout.FlowPane;
|
import javafx.scene.layout.FlowPane;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import model.NewsListModel;
|
import model.NewsListModel;
|
||||||
|
import model.NewsModel;
|
||||||
|
|
||||||
public class RootLayout extends Application implements EventObserver {
|
public class RootLayout extends Application implements EventObserver {
|
||||||
|
|
||||||
|
@ -28,7 +27,7 @@ public class RootLayout extends Application implements EventObserver {
|
||||||
private Scene root_scene;
|
private Scene root_scene;
|
||||||
private AnchorPane root_layout;
|
private AnchorPane root_layout;
|
||||||
private FlowPane main_container;
|
private FlowPane main_container;
|
||||||
|
private Article articles;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -61,12 +60,7 @@ public class RootLayout extends Application implements EventObserver {
|
||||||
|
|
||||||
|
|
||||||
/* (2) Create container */
|
/* (2) Create container */
|
||||||
Article artCtl = new Article(this.main_container, this);
|
this.articles = new Article(this.main_container, this);
|
||||||
ArrayList<TagType> tags = new ArrayList<TagType>();
|
|
||||||
tags.add(TagType.science); tags.add(TagType.economics);
|
|
||||||
|
|
||||||
artCtl.addItem("Article 1", "blabla bloblo blibli", new Date(12), tags);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* (2) CSS
|
/* (2) CSS
|
||||||
|
@ -141,16 +135,34 @@ public class RootLayout extends Application implements EventObserver {
|
||||||
call.send();*/
|
call.send();*/
|
||||||
|
|
||||||
switch(e.getEventType()){
|
switch(e.getEventType()){
|
||||||
|
|
||||||
case "changeMainLayout":
|
case "changeMainLayout":
|
||||||
this.handleMainLayoutChange(e.getObjectId());
|
this.handleMainLayoutChange(e.getObjectId());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "NewsQuerySuccess":
|
case "NewsQuerySuccess":
|
||||||
System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé");
|
System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé");
|
||||||
if(NewsListModel.getInstance().getNews().size() != 0) {
|
|
||||||
System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription());
|
// For each news
|
||||||
|
for( NewsModel news : NewsListModel.getInstance().getNews() ){
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
this.articles.addItem( news );
|
||||||
|
|
||||||
|
}catch(Exception e1){
|
||||||
|
|
||||||
|
System.out.println("Cannot fetch article data");
|
||||||
|
e1.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if( NewsListModel.getInstance().getNews().size() != 0 )
|
||||||
|
System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription());
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "NewsQueryFailed":
|
case "NewsQueryFailed":
|
||||||
System.out.println("une erreur est survenue");
|
System.out.println("une erreur est survenue");
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -149,12 +149,14 @@ public class NewsListModel implements Observable{
|
||||||
for(int i = 0;i<arr.length();i++) {
|
for(int i = 0;i<arr.length();i++) {
|
||||||
JSONObject jsonNews = (JSONObject)arr.get(i);
|
JSONObject jsonNews = (JSONObject)arr.get(i);
|
||||||
NewsModel news = new NewsModel();
|
NewsModel news = new NewsModel();
|
||||||
|
|
||||||
news.setAuthor(jsonNews.getString("author"))
|
news.setAuthor(jsonNews.getString("author"))
|
||||||
.setDescription(jsonNews.getString("description"))
|
.setDescription(jsonNews.getString("description"))
|
||||||
.setTitle(jsonNews.getString("title"))
|
.setTitle(jsonNews.getString("title"))
|
||||||
.setNewsURL(jsonNews.getString("url"))
|
.setNewsURL(jsonNews.getString("url"))
|
||||||
.setImageURL(jsonNews.getString("urlToImage"))
|
.setImageURL(jsonNews.getString("urlToImage"))
|
||||||
.setSource(jsonNews.getJSONObject("source").getString("name"));
|
.setSource(jsonNews.getJSONObject("source").getString("name"))
|
||||||
|
.addTag(NewsListModel.this.cat);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
news.setDate(jsonNews.getString("publishedAt"));
|
news.setDate(jsonNews.getString("publishedAt"));
|
||||||
|
|
|
@ -3,8 +3,11 @@ package model;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import Classes.Category;
|
||||||
|
|
||||||
public class NewsModel {
|
public class NewsModel {
|
||||||
|
|
||||||
private String author;
|
private String author;
|
||||||
|
@ -13,8 +16,16 @@ public class NewsModel {
|
||||||
private String newsURL;
|
private String newsURL;
|
||||||
private String title;
|
private String title;
|
||||||
private String description;
|
private String description;
|
||||||
|
private ArrayList<Category> tags;
|
||||||
private Date date;
|
private Date date;
|
||||||
|
|
||||||
|
public NewsModel(){
|
||||||
|
this.tags = new ArrayList<Category>();
|
||||||
|
}
|
||||||
|
public NewsModel addTag(Category tag) {
|
||||||
|
this.tags.add(tag);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public String getAuthor() {
|
public String getAuthor() {
|
||||||
return author;
|
return author;
|
||||||
|
@ -40,6 +51,9 @@ public class NewsModel {
|
||||||
public String getNewsURL() {
|
public String getNewsURL() {
|
||||||
return newsURL;
|
return newsURL;
|
||||||
}
|
}
|
||||||
|
public ArrayList<Category> getTags() {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
public NewsModel setNewsURL(String newsURL) {
|
public NewsModel setNewsURL(String newsURL) {
|
||||||
this.newsURL = newsURL;
|
this.newsURL = newsURL;
|
||||||
return this;
|
return this;
|
||||||
|
|
Loading…
Reference in New Issue