Implemented navigation
This commit is contained in:
parent
846ed78252
commit
b3e0757b29
11 changed files with 203 additions and 90 deletions
|
|
@ -1,111 +0,0 @@
|
|||
/*
|
||||
* 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 wa.server.page;
|
||||
|
||||
import wa.server.WAContext;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
import zutil.net.http.HttpPage;
|
||||
import zutil.net.http.HttpPrintStream;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.Templator;
|
||||
import zutil.parser.json.JSONWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-04-02.
|
||||
*/
|
||||
public abstract class AbstractPage implements HttpPage{
|
||||
private static final Logger log = LogUtil.getLogger();
|
||||
private static final String TMPL_FILE = "WebContent/index.tmpl";
|
||||
|
||||
private Templator tmpl;
|
||||
|
||||
public AbstractPage() {
|
||||
try {
|
||||
tmpl = new Templator(FileUtil.find(TMPL_FILE));
|
||||
} catch(IOException e){
|
||||
log.log(Level.SEVERE, null, e);
|
||||
tmpl = new Templator(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final synchronized void respond(HttpPrintStream out,
|
||||
HttpHeaderParser client_info,
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request) throws IOException {
|
||||
WAContext context = (WAContext)session.get("context");
|
||||
if(context == null){
|
||||
context = new WAContext();
|
||||
}
|
||||
|
||||
if(("application/json").equals(client_info.getHeader("ContentType")) ||
|
||||
request.containsKey("json")){
|
||||
DataNode node = jsonResponse(context, client_info, session, cookie, request);
|
||||
if(node != null) {
|
||||
out.setHeader("Content-Type", "application/json");
|
||||
JSONWriter writer = new JSONWriter(out);
|
||||
writer.write(node);
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
else {
|
||||
tmpl.clear();
|
||||
|
||||
tmpl.set("title", "WebAdmin");
|
||||
tmpl.set("top-nav", context.getNavigation());
|
||||
tmpl.set("side-nav-show", true);
|
||||
tmpl.set("side-nav", context.getNavigation().get(0).getSubNav());
|
||||
tmpl.set("alerts", context.getAlerts());
|
||||
//tmpl.set("footer", null);
|
||||
|
||||
Templator content = htmlResponse(context, client_info, session, cookie, request);
|
||||
if(content != null)
|
||||
tmpl.set("content", content.compile());
|
||||
|
||||
out.print(tmpl.compile());
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Templator htmlResponse(WAContext context,
|
||||
HttpHeaderParser client_info,
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request);
|
||||
|
||||
public DataNode jsonResponse(WAContext context,
|
||||
HttpHeaderParser client_info,
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -22,10 +22,10 @@
|
|||
|
||||
package wa.server.page;
|
||||
|
||||
import wa.server.WAAbstractPage;
|
||||
import wa.server.WAContext;
|
||||
import wa.server.page.struct.WANavigation;
|
||||
import wa.server.plugin.WAService;
|
||||
import wa.server.plugin.WAStatus;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.Templator;
|
||||
|
|
@ -37,7 +37,7 @@ import java.util.Map;
|
|||
/**
|
||||
* Created by Ziver on 2015-04-06.
|
||||
*/
|
||||
public class ServicesPage extends AbstractPage{
|
||||
public class ServicesPage implements WAPage {
|
||||
private ArrayList<WAService> plugins;
|
||||
|
||||
public ServicesPage(PluginManager pluginManager){
|
||||
|
|
@ -46,7 +46,7 @@ public class ServicesPage extends AbstractPage{
|
|||
WANavigation nav = new WANavigation("Services");
|
||||
for(WAService plugin : plugins)
|
||||
nav.addSubNav(new WANavigation(plugin.getName()));
|
||||
WANavigation.getRootNav().addSubNav(nav);
|
||||
WANavigation.addRootNav(nav);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
package wa.server.page;
|
||||
|
||||
import wa.server.WAAbstractPage;
|
||||
import wa.server.WAContext;
|
||||
import wa.server.page.struct.WANavigation;
|
||||
import wa.server.plugin.WAStatus;
|
||||
|
|
@ -30,23 +31,22 @@ import zutil.parser.DataNode;
|
|||
import zutil.parser.Templator;
|
||||
import zutil.plugin.PluginManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-04-06.
|
||||
*/
|
||||
public class StatusPage extends AbstractPage{
|
||||
public class StatusPage implements WAPage {
|
||||
private ArrayList<WAStatus> plugins;
|
||||
|
||||
public StatusPage(PluginManager pluginManager){
|
||||
this.plugins = pluginManager.toArray(WAStatus.class);
|
||||
|
||||
WANavigation nav = new WANavigation("Status");
|
||||
WANavigation nav = new WANavigation("Status", this);
|
||||
for(WAStatus plugin : plugins)
|
||||
nav.addSubNav(new WANavigation(plugin.getName()));
|
||||
WANavigation.getRootNav().addSubNav(nav);
|
||||
nav.addSubNav(new WANavigation(plugin.getName(), plugin));
|
||||
WANavigation.addRootNav(nav);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -56,14 +56,14 @@ public class StatusPage extends AbstractPage{
|
|||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request) {
|
||||
WAStatus obj = null;
|
||||
if(request.containsKey("i"))
|
||||
obj = getPlugin(Integer.parseInt(request.get("i")));
|
||||
else
|
||||
obj = getPlugin(0);
|
||||
|
||||
if(obj != null)
|
||||
return new Templator(obj.html());
|
||||
WAStatus obj = getPlugin(context);
|
||||
|
||||
if(obj != null) {
|
||||
Templator tmpl = new Templator(obj.html());
|
||||
tmpl.set("nav", context.getBreadcrumb().get(1));
|
||||
return tmpl;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ public class StatusPage extends AbstractPage{
|
|||
Map<String, String> request){
|
||||
|
||||
if(request.containsKey("i")) {
|
||||
WAStatus obj = getPlugin(Integer.parseInt(request.get("i")));
|
||||
WAStatus obj = getPlugin(context);
|
||||
DataNode root = new DataNode(DataNode.DataType.Map);
|
||||
obj.jsonUpdate(request, root);
|
||||
return root;
|
||||
|
|
@ -82,9 +82,14 @@ public class StatusPage extends AbstractPage{
|
|||
return null;
|
||||
}
|
||||
|
||||
private WAStatus getPlugin(int i){
|
||||
if(0 >= i && i < plugins.size())
|
||||
return plugins.get(i);
|
||||
|
||||
private WAStatus getPlugin(WAContext context){
|
||||
if(context.getBreadcrumb().size() >= 2){
|
||||
int i = plugins.indexOf(context.getBreadcrumb().get(1).getResource());
|
||||
if(i >= 0)
|
||||
return plugins.get(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
49
src/wa/server/page/WAPage.java
Normal file
49
src/wa/server/page/WAPage.java
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* 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 wa.server.page;
|
||||
|
||||
import wa.server.WAContext;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.Templator;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-04-25.
|
||||
*/
|
||||
public interface WAPage {
|
||||
|
||||
public abstract Templator htmlResponse(WAContext context,
|
||||
HttpHeaderParser client_info,
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request);
|
||||
|
||||
public DataNode jsonResponse(WAContext context,
|
||||
HttpHeaderParser client_info,
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request);
|
||||
|
||||
}
|
||||
|
|
@ -22,42 +22,62 @@
|
|||
|
||||
package wa.server.page.struct;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-04-02.
|
||||
*/
|
||||
public class WANavigation {
|
||||
private static WANavigation rootNav;
|
||||
private static int nextId;
|
||||
private static List<WANavigation> root_nav = new ArrayList<WANavigation>();
|
||||
private static HashMap<Integer, WANavigation> nav_map = new HashMap<Integer, WANavigation>();
|
||||
|
||||
private int id;
|
||||
private String url;
|
||||
private String name;
|
||||
private ArrayList<WANavigation> subNavs;
|
||||
private ArrayList<WANavigation> sub_navs;
|
||||
|
||||
private WANavigation parent_nav;
|
||||
private Object resource;
|
||||
|
||||
|
||||
public WANavigation(String name) {
|
||||
this.id = nextId++;
|
||||
this.nav_map.put(this.id, this);
|
||||
this.url = "?i="+this.id;
|
||||
this.name = name;
|
||||
this.subNavs = new ArrayList<>();
|
||||
this.sub_navs = new ArrayList<>();
|
||||
}
|
||||
public WANavigation(String name, WANavigation[] sub_nav) {
|
||||
public WANavigation(String name, Object resource) {
|
||||
this(name);
|
||||
this.subNavs.addAll(Arrays.asList(sub_nav));
|
||||
this.setResource(resource);
|
||||
}
|
||||
|
||||
|
||||
public void addSubNav(WANavigation subNav) {
|
||||
this.subNavs.add(subNav);
|
||||
this.sub_navs.add(subNav);
|
||||
subNav.setParentNav(this );
|
||||
}
|
||||
public List<WANavigation> getSubNav() {
|
||||
return subNavs;
|
||||
return sub_navs;
|
||||
}
|
||||
public Object getSubNav(String name) {
|
||||
int index = subNavs.indexOf(name);
|
||||
int index = sub_navs.indexOf(name);
|
||||
if(index >= 0)
|
||||
return subNavs.get(index);
|
||||
return sub_navs.get(index);
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getResource(){
|
||||
return resource;
|
||||
}
|
||||
public void setResource(Object obj){
|
||||
resource = obj;
|
||||
}
|
||||
|
||||
private void setParentNav(WANavigation nav){
|
||||
this.parent_nav = nav;
|
||||
}
|
||||
|
||||
public boolean equals(Object o){
|
||||
if(o instanceof String)
|
||||
|
|
@ -67,9 +87,22 @@ public class WANavigation {
|
|||
}
|
||||
|
||||
|
||||
public static WANavigation getRootNav(){
|
||||
if(rootNav == null)
|
||||
rootNav = new WANavigation("root_nav");
|
||||
return rootNav;
|
||||
public static void addRootNav(WANavigation nav){
|
||||
getRootNav().add(nav);
|
||||
}
|
||||
public static List<WANavigation> getRootNav(){
|
||||
return root_nav;
|
||||
}
|
||||
|
||||
public static List<WANavigation> getNavResource(Map<String, String> request) {
|
||||
LinkedList list = new LinkedList();
|
||||
if(request.containsKey("i")){
|
||||
WANavigation current = nav_map.get(Integer.parseInt(request.get("i")));
|
||||
while(current != null){
|
||||
list.addFirst(current);
|
||||
current = current.parent_nav;
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue