Implémentation des appels d'API + refactor du menu
This commit is contained in:
parent
f544e03132
commit
24fb79745d
|
@ -0,0 +1,75 @@
|
|||
package Classes;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import Interfaces.Callback;
|
||||
|
||||
public class ApiCall implements Runnable {
|
||||
|
||||
private Callback callback;
|
||||
private HttpURLConnection connection;
|
||||
private String method;
|
||||
|
||||
public ApiCall(String URL, String method, Callback call) {
|
||||
this.callback = call;
|
||||
this.method = method;
|
||||
try {
|
||||
URL url = new URL(URL);
|
||||
this.connection = (HttpURLConnection) url.openConnection();
|
||||
this.connection.setRequestMethod(method);
|
||||
this.connection.setDoInput(true);
|
||||
this.connection.setDoOutput(true);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Impossible d'ouvrir l'URL: "+URL);
|
||||
}
|
||||
}
|
||||
|
||||
public void addHeaders(HashMap<String,String> map) {
|
||||
Iterator it = map.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
this.connection.setRequestProperty((String)pair.getKey(), (String)pair.getValue());
|
||||
it.remove(); // avoids a ConcurrentModificationException
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
//Send request
|
||||
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
|
||||
wr.flush ();
|
||||
wr.close ();
|
||||
|
||||
//Get Response
|
||||
InputStream is = connection.getInputStream();
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
StringBuffer response = new StringBuffer();
|
||||
while((line = rd.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
rd.close();
|
||||
|
||||
this.callback.call(response.toString());
|
||||
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void send() {
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package Interfaces;
|
||||
|
||||
public interface Callback {
|
||||
|
||||
public void call(String response);
|
||||
|
||||
}
|
|
@ -2,27 +2,46 @@ package controller;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import Interfaces.EventObserver;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.FlowPane;
|
||||
import model.HeaderMenuItem;
|
||||
|
||||
public class HeaderMenu{
|
||||
|
||||
/* Data */
|
||||
private ArrayList<HeaderMenuItem> items;
|
||||
private ArrayList<ImageView> items;
|
||||
private FlowPane parent;
|
||||
private EventObserver observer;
|
||||
|
||||
|
||||
/* Constructor */
|
||||
public HeaderMenu(FlowPane p_parent){
|
||||
public HeaderMenu(FlowPane p_parent, EventObserver observer){
|
||||
this.parent = p_parent;
|
||||
this.items = new ArrayList<HeaderMenuItem>();
|
||||
this.items = new ArrayList<ImageView>();
|
||||
this.observer = observer;
|
||||
}
|
||||
|
||||
public void addItem(String p_name, String p_image_uri) {
|
||||
|
||||
public void addItem(HeaderMenuItem p_item) {
|
||||
this.items.add(p_item);
|
||||
ImageView menuItem = new ImageView();
|
||||
|
||||
menuItem.setImage(new Image(p_image_uri));
|
||||
menuItem.setId("header_menu_item_"+p_name);
|
||||
menuItem.getStyleClass().add("header_menu_item");
|
||||
|
||||
menuItem.setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||
@Override
|
||||
public void handle(MouseEvent event) {
|
||||
HeaderMenu.this.observer.handleEvent(new Classes.Event(menuItem.getId(),"click"));
|
||||
}
|
||||
});
|
||||
|
||||
this.items.add(menuItem);
|
||||
// Add to parent
|
||||
parent.getChildren().add(p_item);
|
||||
parent.getChildren().add(menuItem);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import Classes.ApiCall;
|
||||
import Classes.css.user.ContextBuilder;
|
||||
import Classes.css.user.Header;
|
||||
import Classes.css.user.MenuContainer;
|
||||
import Interfaces.Callback;
|
||||
import Interfaces.Event;
|
||||
import Interfaces.EventObserver;
|
||||
import javafx.application.Application;
|
||||
|
@ -13,7 +18,6 @@ import javafx.scene.Scene;
|
|||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.FlowPane;
|
||||
import javafx.stage.Stage;
|
||||
import model.HeaderMenuItem;
|
||||
|
||||
public class RootLayout extends Application implements EventObserver {
|
||||
|
||||
|
@ -41,12 +45,12 @@ public class RootLayout extends Application implements EventObserver {
|
|||
/* (1) HEADER
|
||||
-------------------------------------*/
|
||||
/* (1) Create header menu */
|
||||
HeaderMenu hm = new HeaderMenu((FlowPane) this.root_scene.lookup("#header_menu"));
|
||||
HeaderMenu hm = new HeaderMenu((FlowPane) this.root_scene.lookup("#header_menu"),this);
|
||||
|
||||
hm.addItem( new HeaderMenuItem("notification", "/src/header-notif.png",this) );
|
||||
hm.addItem( new HeaderMenuItem("mail", "/src/header-mail.png",this) );
|
||||
hm.addItem( new HeaderMenuItem("search", "/src/header-search.png",this) );
|
||||
hm.addItem( new HeaderMenuItem("menu", "/src/header-menu.png",this) );
|
||||
hm.addItem("notification", "/src/header-notif.png");
|
||||
hm.addItem("mail", "/src/header-mail.png");
|
||||
hm.addItem("search", "/src/header-search.png");
|
||||
hm.addItem("menu", "/src/header-menu.png");
|
||||
|
||||
|
||||
/* (2) CSS
|
||||
|
@ -98,6 +102,22 @@ public class RootLayout extends Application implements EventObserver {
|
|||
|
||||
@Override
|
||||
public void handleEvent(Event e) {
|
||||
HashMap<String,String> headers = new HashMap<String,String>();
|
||||
headers.put("Referer", "http://www.wordreference.com");
|
||||
|
||||
ApiCall call = new ApiCall("http://api.wordreference.com/1/json/enfr/grin","GET",new Callback() {
|
||||
|
||||
@Override
|
||||
public void call(String response) {
|
||||
JSONObject json = new JSONObject(response);
|
||||
System.out.println(json.toString());
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
call.addHeaders(headers);
|
||||
call.send();
|
||||
|
||||
System.out.println("Object "+e.getObjectId()+" fired a "+e.getEventType()+" event");
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -1,46 +0,0 @@
|
|||
package model;
|
||||
|
||||
import Interfaces.Event;
|
||||
import Interfaces.EventObserver;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
|
||||
public class HeaderMenuItem extends ImageView{
|
||||
|
||||
private final String name;
|
||||
private EventObserver parentHandler;
|
||||
|
||||
|
||||
/* Constructor
|
||||
*
|
||||
* @param p_name<String> The name of the menu item
|
||||
* @param p_image_uri<String> The URI of the image
|
||||
*
|
||||
*/
|
||||
public HeaderMenuItem(String p_name, String p_image_uri, EventObserver parent){
|
||||
this.name = p_name;
|
||||
this.parentHandler = parent;
|
||||
this.setImage(new Image(p_image_uri));
|
||||
this.setId("header_menu_item_"+p_name);
|
||||
this.getStyleClass().add("header_menu_item");
|
||||
|
||||
this.setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||
@Override
|
||||
public void handle(MouseEvent event) {
|
||||
HeaderMenuItem.this.sendEvent(new Classes.Event(HeaderMenuItem.this.getId(),"click"));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void sendEvent(Event e) {
|
||||
this.parentHandler.handleEvent(e);
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue