diff --git a/Classes/ApiCall.java b/Classes/ApiCall.java new file mode 100644 index 0000000..895318f --- /dev/null +++ b/Classes/ApiCall.java @@ -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 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(); + } + +} diff --git a/Interfaces/Callback.java b/Interfaces/Callback.java new file mode 100644 index 0000000..09de622 --- /dev/null +++ b/Interfaces/Callback.java @@ -0,0 +1,7 @@ +package Interfaces; + +public interface Callback { + + public void call(String response); + +} diff --git a/controller/HeaderMenu.java b/controller/HeaderMenu.java index 07f6ccf..8eeb1bd 100644 --- a/controller/HeaderMenu.java +++ b/controller/HeaderMenu.java @@ -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 items; + private ArrayList 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(); + this.items = new ArrayList(); + this.observer = observer; } - - - public void addItem(HeaderMenuItem p_item) { - this.items.add(p_item); + + public void addItem(String p_name, String p_image_uri) { + + 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() { + @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); } } diff --git a/controller/RootLayout.java b/controller/RootLayout.java index 84d40da..f508bb1 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -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 headers = new HashMap(); + 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"); } diff --git a/lib/org.json.jar b/lib/org.json.jar new file mode 100644 index 0000000..40a325d Binary files /dev/null and b/lib/org.json.jar differ diff --git a/model/HeaderMenuItem.java b/model/HeaderMenuItem.java deleted file mode 100644 index 93b10a1..0000000 --- a/model/HeaderMenuItem.java +++ /dev/null @@ -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 The name of the menu item - * @param p_image_uri 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() { - @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; - } - -}