Merge branch 'master' of https://git.xdrm.io/MTI/javafx-p1
This commit is contained in:
commit
832fb73ab8
|
@ -3,12 +3,15 @@
|
|||
*/
|
||||
package Interfaces;
|
||||
|
||||
import Classes.Event;
|
||||
|
||||
/**
|
||||
* @author lucas
|
||||
*
|
||||
*/
|
||||
public interface Observable {
|
||||
|
||||
public void notifyObservers(Event e);
|
||||
public void addObserver(String key , EventObserver o);
|
||||
public void removeObserver(String key);
|
||||
|
||||
|
|
|
@ -18,6 +18,10 @@ import javafx.scene.Scene;
|
|||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.FlowPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import model.DictionaryModel;
|
||||
import model.LangModel;
|
||||
|
||||
import model.NewsListModel;
|
||||
import model.NewsModel;
|
||||
|
||||
|
@ -142,6 +146,9 @@ public class RootLayout extends Application implements EventObserver {
|
|||
|
||||
case "NewsQuerySuccess":
|
||||
System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé");
|
||||
if(NewsListModel.getInstance().getNews().size() != 0) {
|
||||
System.out.println("Le titre du premier article est: "+NewsListModel.getInstance().getNews().get(0).getTitle());
|
||||
System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription());
|
||||
|
||||
// For each news
|
||||
for( NewsModel news : NewsListModel.getInstance().getNews() ){
|
||||
|
@ -156,25 +163,32 @@ public class RootLayout extends Application implements EventObserver {
|
|||
e1.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if( NewsListModel.getInstance().getNews().size() != 0 )
|
||||
System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription());
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "NewsQueryFailed":
|
||||
System.out.println("une erreur est survenue");
|
||||
break;
|
||||
|
||||
case "WordQuerySuccess":
|
||||
System.out.println(DictionaryModel.getInstance().getTranslations().size()+" Traductions ont été trouvés");
|
||||
System.out.println(DictionaryModel.getInstance().getUsages().size()+" Exemples ont été trouvés");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void handleMainLayoutChange(String layout) {
|
||||
|
||||
NewsListModel.getInstance().addObserver("MainClass", this);
|
||||
NewsListModel.getInstance().setCategory(Category.business);
|
||||
NewsListModel.getInstance().setSortType(SortTypes.publishedAt);
|
||||
NewsListModel.getInstance().query("bitcoin");
|
||||
NewsListModel.getInstance().setCategory(Category.gaming);
|
||||
NewsListModel.getInstance().setSortType(SortTypes.relevancy);
|
||||
NewsListModel.getInstance().query("the evil within");
|
||||
DictionaryModel.getInstance().addObserver("MainClass", this);
|
||||
DictionaryModel.getInstance().query("maison");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,152 @@
|
|||
package model;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import Classes.ApiCall;
|
||||
import Classes.Event;
|
||||
import Interfaces.Callback;
|
||||
import Interfaces.EventObserver;
|
||||
import Interfaces.Observable;
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class DictionaryModel implements Observable {
|
||||
|
||||
private HashMap<String,EventObserver> observers;
|
||||
private static DictionaryModel instance;
|
||||
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> translations;
|
||||
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> usages;
|
||||
|
||||
private DictionaryModel() {
|
||||
this.observers = new HashMap<String,EventObserver>();
|
||||
}
|
||||
|
||||
public static DictionaryModel getInstance() {
|
||||
if(DictionaryModel.instance == null) {
|
||||
DictionaryModel.instance = new DictionaryModel();
|
||||
}
|
||||
|
||||
return DictionaryModel.instance;
|
||||
}
|
||||
|
||||
public void query(String search) {
|
||||
|
||||
final String encodedQuery;
|
||||
try {
|
||||
encodedQuery = java.net.URLEncoder.encode(search,"UTF-8");
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
this.notifyObservers(new Event("NewsModel","NewsQueryFailed"));
|
||||
return;
|
||||
}
|
||||
|
||||
HashMap<String,String> headers = new HashMap<String,String>();
|
||||
//si on ne met pas ce header l'API refuse de répondre
|
||||
headers.put("Referer", "http://www.wordreference.com");
|
||||
|
||||
String dic = LangModel.getInstance().getFromLang().name()+LangModel.getInstance().getToLang().name();
|
||||
|
||||
ApiCall call = new ApiCall("http://api.wordreference.com/1/json/"+dic+"/"+encodedQuery,"GET",new Callback() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(JSONObject response) {
|
||||
//on reset le contenu du modele
|
||||
DictionaryModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
|
||||
DictionaryModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
|
||||
|
||||
if(response.has("Error")) {
|
||||
//pas de résultat
|
||||
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryNoResults"));
|
||||
}else{
|
||||
//Traitement des traductions principales
|
||||
DictionaryModel.this.translations.addAll(
|
||||
DictionaryModel.this.computeJSON(
|
||||
response.getJSONObject("term0").getJSONObject("PrincipalTranslations")
|
||||
)
|
||||
);
|
||||
//Traitement des traductions secondaires
|
||||
DictionaryModel.this.translations.addAll(
|
||||
DictionaryModel.this.computeJSON(
|
||||
response.getJSONObject("term0").getJSONObject("AdditionalTranslations")
|
||||
)
|
||||
);
|
||||
//traitement des usages
|
||||
DictionaryModel.this.usages.addAll(
|
||||
DictionaryModel.this.computeJSON(
|
||||
response.getJSONObject("original").getJSONObject("Compounds")
|
||||
)
|
||||
);
|
||||
|
||||
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError() {
|
||||
DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
|
||||
}
|
||||
|
||||
});
|
||||
call.addHeaders(headers);
|
||||
call.send();
|
||||
}
|
||||
|
||||
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> computeJSON(JSONObject container){
|
||||
ArrayList<Pair<WordTraductionModel,WordTraductionModel>> returned = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
|
||||
for (Object key : container.keySet()) {
|
||||
//get original term
|
||||
WordTraductionModel term = new WordTraductionModel();
|
||||
JSONObject jterm = container.getJSONObject((String)key).getJSONObject("OriginalTerm");
|
||||
term.setWord(jterm.getString("term"))
|
||||
.setPOS(jterm.getString("POS"))
|
||||
.setSense(jterm.getString("sense"))
|
||||
.setUsage(jterm.getString("term"));
|
||||
|
||||
//get traduction
|
||||
WordTraductionModel trad = new WordTraductionModel();
|
||||
jterm = container.getJSONObject((String)key).getJSONObject("FirstTranslation");
|
||||
term.setWord(jterm.getString("term"))
|
||||
.setPOS(jterm.getString("POS"))
|
||||
.setSense(jterm.getString("sense"))
|
||||
.setUsage(jterm.getString("term"));
|
||||
|
||||
returned.add(new Pair<WordTraductionModel,WordTraductionModel>(term,trad));
|
||||
}
|
||||
|
||||
return returned;
|
||||
}
|
||||
|
||||
public ArrayList<Pair<WordTraductionModel,WordTraductionModel>> getTranslations(){
|
||||
return this.translations;
|
||||
}
|
||||
|
||||
public ArrayList<Pair<WordTraductionModel,WordTraductionModel>> getUsages(){
|
||||
return this.usages;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addObserver(String key, EventObserver o) {
|
||||
this.observers.put(key, o);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeObserver(String key) {
|
||||
this.observers.remove(key);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyObservers(Event e) {
|
||||
for(Object key : this.observers.keySet().toArray()) {
|
||||
this.observers.get(key).handleEvent(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package model;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
@ -124,9 +125,17 @@ public class NewsListModel implements Observable{
|
|||
this.apiError = false;
|
||||
return;
|
||||
}
|
||||
|
||||
final String encodedQuery;
|
||||
try {
|
||||
encodedQuery = java.net.URLEncoder.encode(q,"UTF-8");
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
this.notifyObservers(new Event("NewsModel","NewsQueryFailed"));
|
||||
return;
|
||||
}
|
||||
//on créer l'URL de l'appel
|
||||
String lang = LangModel.getInstance().getToLang().name();
|
||||
String URL = "http://beta.newsapi.org/v2/everything?apiKey="+this.APIKey+"&language="+lang+"&q="+q+"&sortBy="+this.sortType.name();
|
||||
String URL = "http://beta.newsapi.org/v2/everything?apiKey="+this.APIKey+"&language="+lang+"&q="+encodedQuery+"&sortBy="+this.sortType.name();
|
||||
|
||||
//on ajoute la liste des sources a l'URL de la requete
|
||||
if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) {
|
||||
|
@ -199,7 +208,7 @@ public class NewsListModel implements Observable{
|
|||
|
||||
String lang = LangModel.getInstance().getToLang().name();
|
||||
String URL = "http://beta.newsapi.org/v2/everything?apiKey="+NewsListModel.this.APIKey+"&language="+lang
|
||||
+"&q="+q+"&sortBy="+NewsListModel.this.sortType.name();
|
||||
+"&q="+encodedQuery+"&sortBy="+NewsListModel.this.sortType.name();
|
||||
|
||||
//on ajoute la liste des sources a l'URL de la requete
|
||||
if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) {
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package model;
|
||||
|
||||
public class WordTraductionModel {
|
||||
|
||||
private String word;
|
||||
private String POS;
|
||||
private String sense;
|
||||
private String usage;
|
||||
|
||||
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
public WordTraductionModel setWord(String word) {
|
||||
this.word = word;
|
||||
return this;
|
||||
}
|
||||
public String getPOS() {
|
||||
return POS;
|
||||
}
|
||||
public WordTraductionModel setPOS(String pOS) {
|
||||
POS = pOS;
|
||||
return this;
|
||||
}
|
||||
public String getSense() {
|
||||
return sense;
|
||||
}
|
||||
public WordTraductionModel setSense(String sense) {
|
||||
this.sense = sense;
|
||||
return this;
|
||||
}
|
||||
public String getUsage() {
|
||||
return usage;
|
||||
}
|
||||
public WordTraductionModel setUsage(String usage) {
|
||||
this.usage = usage;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue