From fce0a0e9393ebf041390a224f3ed24703047ffe1 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 11 Jan 2016 19:46:43 +0100 Subject: [PATCH] Added navigation object Former-commit-id: c6fef444bc1d4fa3389206ac3609fad1f2a41b9c --- src/se/hal/HalServer.java | 10 +- src/se/hal/intf/HalHttpPage.java | 34 ++++-- src/se/hal/page/EventConfigHttpPage.java | 12 +- src/se/hal/page/HalNavigation.java | 138 ++++++++++++++++++++++ src/se/hal/page/PCHeatMapHttpPage.java | 1 + src/se/hal/page/PCOverviewHttpPage.java | 1 + src/se/hal/page/SensorConfigHttpPage.java | 1 + src/se/hal/page/UserConfigHttpPage.java | 1 + src/se/hal/plugin/tellstick/plugin.json | 4 +- web-resource/event_config.tmpl | 6 +- web-resource/index.tmpl | 23 ++-- web-resource/sensor_config.tmpl | 2 +- 12 files changed, 194 insertions(+), 39 deletions(-) create mode 100755 src/se/hal/page/HalNavigation.java diff --git a/src/se/hal/HalServer.java b/src/se/hal/HalServer.java index ae4d44f0..4eb4164f 100755 --- a/src/se/hal/HalServer.java +++ b/src/se/hal/HalServer.java @@ -7,10 +7,7 @@ import se.hal.deamon.PCDataSynchronizationClient; import se.hal.deamon.PCDataSynchronizationDaemon; import se.hal.intf.HalDaemon; import se.hal.intf.HalHttpPage; -import se.hal.page.SensorConfigHttpPage; -import se.hal.page.PCHeatMapHttpPage; -import se.hal.page.PCOverviewHttpPage; -import se.hal.page.UserConfigHttpPage; +import se.hal.page.*; import se.hal.struct.Event; import se.hal.struct.Sensor; import zutil.db.DBConnection; @@ -72,17 +69,20 @@ public class HalServer { // init http server + HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors")); + HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events")); pages = new HalHttpPage[]{ new PCOverviewHttpPage(), new PCHeatMapHttpPage(), new SensorConfigHttpPage(), + new EventConfigHttpPage(), new UserConfigHttpPage(), }; HttpServer http = new HttpServer(HalContext.getIntegerProperty("http_port")); http.setDefaultPage(new HttpFilePage(FileUtil.find("web-resource/"))); http.setPage("/", pages[0]); for(HalHttpPage page : pages){ - http.setPage(page.getURL(), page); + http.setPage(page.getId(), page); } http.start(); } diff --git a/src/se/hal/intf/HalHttpPage.java b/src/se/hal/intf/HalHttpPage.java index 4b7dc967..03dd951d 100755 --- a/src/se/hal/intf/HalHttpPage.java +++ b/src/se/hal/intf/HalHttpPage.java @@ -1,6 +1,7 @@ package se.hal.intf; import se.hal.HalContext; +import se.hal.page.HalNavigation; import se.hal.struct.User; import zutil.db.DBConnection; import zutil.io.file.FileUtil; @@ -17,26 +18,25 @@ import java.util.Map; * Created by Ziver on 2015-12-10. */ public abstract class HalHttpPage implements HttpPage{ + private static final String TEMPLATE = "web-resource/index.tmpl"; + private static HalNavigation rootNav = new HalNavigation(); + private static HalNavigation userNav = new HalNavigation(); - private static ArrayList pages = new ArrayList<>(); + private HalNavigation nav; - private final String name; - private final String id; public HalHttpPage(String name, String id){ - this.name = name; - this.id = id; - pages.add(this); + this.nav = new HalNavigation(id, name); } public String getName(){ - return name; + return nav.getName(); } public String getId(){ - return id; + return nav.getId(); } - public String getURL(){ - return "/" + this.id; + public HalNavigation getNav(){ + return nav; } @@ -48,9 +48,11 @@ public abstract class HalHttpPage implements HttpPage{ try { DBConnection db = HalContext.getDB(); - Templator tmpl = new Templator(FileUtil.find("web-resource/index.tmpl")); + Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); tmpl.set("user", User.getLocalUser(db)); - tmpl.set("navigation", pages); + tmpl.set("nav", nav.getNavBreadcrumb().get(1)); + tmpl.set("rootNav", rootNav); + tmpl.set("userNav", userNav); tmpl.set("content", httpRespond(session, cookie, request)); out.print(tmpl.compile()); @@ -60,6 +62,14 @@ public abstract class HalHttpPage implements HttpPage{ } + public static HalNavigation getRootNav(){ + return rootNav; + } + public static HalNavigation getUserNav(){ + return userNav; + } + + public abstract Templator httpRespond( Map session, Map cookie, diff --git a/src/se/hal/page/EventConfigHttpPage.java b/src/se/hal/page/EventConfigHttpPage.java index d95571be..0e908f00 100755 --- a/src/se/hal/page/EventConfigHttpPage.java +++ b/src/se/hal/page/EventConfigHttpPage.java @@ -4,7 +4,6 @@ import se.hal.ControllerManager; import se.hal.HalContext; import se.hal.intf.HalHttpPage; import se.hal.struct.Event; -import se.hal.struct.Sensor; import se.hal.struct.User; import zutil.db.DBConnection; import zutil.io.file.FileUtil; @@ -26,9 +25,10 @@ public class EventConfigHttpPage extends HalHttpPage { public EventConfigHttpPage() { super("Configuration", "event_config"); + super.getRootNav().getSubNav("events").addSubNav(super.getNav()); eventConfigurations = new EventDataParams[ - ControllerManager.getInstance().getAvailableSensors().size()]; + ControllerManager.getInstance().getAvailableEvents().size()]; int i=0; for(Class c : ControllerManager.getInstance().getAvailableEvents()){ eventConfigurations[i] = new EventDataParams(); @@ -53,12 +53,12 @@ public class EventConfigHttpPage extends HalHttpPage { int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1); Event event; switch(request.get("action")) { - // Local Sensors + // Local events case "create_local_event": event = new Event(); event.setName(request.get("name")); event.setType(request.get("type")); - //sensor.setConfig(request.get("config")); + //event.setConfig(request.get("config")); event.setUser(localUser); event.save(db); case "modify_local_event": @@ -66,7 +66,7 @@ public class EventConfigHttpPage extends HalHttpPage { if(event != null){ event.setName(request.get("name")); event.setType(request.get("type")); - //sensor.setConfig(request.get("config")); + //event.setConfig(request.get("config")); event.setUser(localUser); event.save(db); } @@ -86,7 +86,7 @@ public class EventConfigHttpPage extends HalHttpPage { tmpl.set("localEventConf", eventConfigurations); - tmpl.set("availableSensors", ControllerManager.getInstance().getAvailableSensors()); + tmpl.set("availableEvents", ControllerManager.getInstance().getAvailableEvents()); return tmpl; diff --git a/src/se/hal/page/HalNavigation.java b/src/se/hal/page/HalNavigation.java new file mode 100755 index 00000000..4a34b4fe --- /dev/null +++ b/src/se/hal/page/HalNavigation.java @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2015 Ziver + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package se.hal.page; + +import se.hal.intf.HalHttpPage; + +import java.util.*; + +/** + * Created by Ziver on 2015-04-02. + */ +public class HalNavigation implements Iterable{ + private static HashMap navMap = new HashMap<>(); + + private final String id; + private String name; + private HalNavigation parentNav; + private ArrayList subNav; + private HalHttpPage resource; + + + /** + * Create a root navigation object + */ + public HalNavigation() { + this(null, null); + } + /** + * Create a sub navigation object with no resource + */ + public HalNavigation(String id, String name) { + this.id = id; + this.name = name; + this.subNav = new ArrayList<>(); + navMap.put(id, this); + } + /** + * Create a sub navigation object + */ +/* public HalNavigation(HalHttpPage page) { + this.id = page.getId(); + this.name = page.getName(); + this.subNav = new ArrayList<>(); + this.resource = page; + navMap.put(id, this); + } +*/ + + @Override + public Iterator iterator() { + return subNav.iterator(); + } + public List getSubNavs() { + return subNav; + } + public HalNavigation getSubNav(String id) { + for(HalNavigation nav : subNav) { + if(nav.equals(id)) + return nav; + } + return null; + } + + public HalNavigation addSubNav(HalNavigation nav) { + nav.setParentNav(this); + subNav.add(nav); + return nav; + } + + + private void setParentNav(HalNavigation nav){ + this.parentNav = nav; + } + + @Override + public boolean equals(Object o){ + if(o instanceof String) + return this.id.equals(o); + return this == o || + (o != null && this.id.equals(((HalNavigation)o).id)); + } + + public List getNavBreadcrumb() { + LinkedList list = new LinkedList(); + + HalNavigation current = this; + while(current != null && id != null){ + list.addFirst(current); + current = current.parentNav; + } + + return list; + } + + + public String getURL(){ + return "/" + this.id; + } + + + public String getId(){ + return id; + } + public String getName(){ + return name; + } + public void setName(String name){ + this.name = name; + } + public HalNavigation getParent(){ + return parentNav; + } + + + public static HalNavigation getNav(String id){ + return navMap.get(id); + } +} diff --git a/src/se/hal/page/PCHeatMapHttpPage.java b/src/se/hal/page/PCHeatMapHttpPage.java index 927964e5..59d337f6 100755 --- a/src/se/hal/page/PCHeatMapHttpPage.java +++ b/src/se/hal/page/PCHeatMapHttpPage.java @@ -12,6 +12,7 @@ public class PCHeatMapHttpPage extends HalHttpPage { public PCHeatMapHttpPage() { super("Heatmap", "pc_heatmap"); + super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); } @Override diff --git a/src/se/hal/page/PCOverviewHttpPage.java b/src/se/hal/page/PCOverviewHttpPage.java index 07722d96..4f43cc8f 100755 --- a/src/se/hal/page/PCOverviewHttpPage.java +++ b/src/se/hal/page/PCOverviewHttpPage.java @@ -20,6 +20,7 @@ public class PCOverviewHttpPage extends HalHttpPage { public PCOverviewHttpPage() { super("Power;Challenge", "pc_overview"); + super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); } @Override diff --git a/src/se/hal/page/SensorConfigHttpPage.java b/src/se/hal/page/SensorConfigHttpPage.java index b431807a..11937e99 100755 --- a/src/se/hal/page/SensorConfigHttpPage.java +++ b/src/se/hal/page/SensorConfigHttpPage.java @@ -25,6 +25,7 @@ public class SensorConfigHttpPage extends HalHttpPage { public SensorConfigHttpPage() { super("Configuration", "sensor_config"); + super.getRootNav().getSubNav("sensors").addSubNav(super.getNav()); sensorConfigurations = new SensorDataParams[ ControllerManager.getInstance().getAvailableSensors().size()]; diff --git a/src/se/hal/page/UserConfigHttpPage.java b/src/se/hal/page/UserConfigHttpPage.java index d1310969..fbeaa35a 100755 --- a/src/se/hal/page/UserConfigHttpPage.java +++ b/src/se/hal/page/UserConfigHttpPage.java @@ -19,6 +19,7 @@ public class UserConfigHttpPage extends HalHttpPage { public UserConfigHttpPage() { super("Profile", "user_profile"); + super.getUserNav().addSubNav(super.getNav()); } @Override diff --git a/src/se/hal/plugin/tellstick/plugin.json b/src/se/hal/plugin/tellstick/plugin.json index 6a223bfd..77051c2e 100755 --- a/src/se/hal/plugin/tellstick/plugin.json +++ b/src/se/hal/plugin/tellstick/plugin.json @@ -2,7 +2,7 @@ "version": 1.0, "name": "Tellstick", "interfaces": [ - {"se.koc.hal.intf.HalSensor": "se.koc.hal.plugin.tellstick.protocols.Oregon0x1A2D"}, - {"se.koc.hal.intf.HalEvent": "se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning"} + {"se.hal.intf.HalSensor": "se.hal.plugin.tellstick.protocols.Oregon0x1A2D"}, + {"se.hal.intf.HalEvent": "se.hal.plugin.tellstick.protocols.NexaSelfLearning"} ] } \ No newline at end of file diff --git a/web-resource/event_config.tmpl b/web-resource/event_config.tmpl index e1361795..924909f1 100755 --- a/web-resource/event_config.tmpl +++ b/web-resource/event_config.tmpl @@ -123,7 +123,7 @@ - +
-
- - -

diff --git a/web-resource/index.tmpl b/web-resource/index.tmpl index 2cc04dd0..1f831f01 100755 --- a/web-resource/index.tmpl +++ b/web-resource/index.tmpl @@ -26,21 +26,28 @@ - HAL + HAL
@@ -52,8 +59,8 @@ diff --git a/web-resource/sensor_config.tmpl b/web-resource/sensor_config.tmpl index deed4893..0a4a661e 100755 --- a/web-resource/sensor_config.tmpl +++ b/web-resource/sensor_config.tmpl @@ -239,7 +239,7 @@ - +