navigation.java

来自「tbuy1.1.5是在netbeans环境下用JSF技术编写的一个论坛tbuy1」· Java 代码 · 共 218 行

JAVA
218
字号
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的,如有技术问题请与本人联系! *  * BBS的导航条 */package biz.tbuy.bbs.bean;import biz.tbuy.bbs.ForumModel;import biz.tbuy.bbs.TopicAction;import biz.tbuy.bbs.TopicModel;import biz.tbuy.bbs.ZoneModel;import java.util.List;import javax.faces.component.UIComponent;import javax.faces.component.UIPanel;import javax.faces.component.UIOutput;import javax.faces.component.html.HtmlOutputLink;import javax.faces.component.html.HtmlOutputText;import javax.faces.context.FacesContext;import javax.servlet.http.HttpServletRequest;/** * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class Navigation extends BaseBean{    /**     * 页面URL中的ID标识,如 /topic/11.faces 中的 11     * /bbs/zone/x.faces     * /bbs/forum/x.faces     * /bbs/topic/x.faces     */     private int id;    private boolean inZone;     // 表示当前所在的页面是否为 /bbs/zone/...    private boolean inForum;    // 表示当前所在的页面是否为 /bbs/forum/..    private boolean inTopic;    // 表示当前所在的页面是否为 /bbs/topic/...    private boolean inReply;    // 表示当前所在的页面是否为 /bbs/reply/...    private UIPanel uiPanel;    // 页面导航条 绑定于页面 navigation.jsp 中        public Navigation() {        String view;        try {            FacesContext fc = getFacesContext();             view = fc.getViewRoot().getViewId();        } catch (Exception e) {            HttpServletRequest request = (HttpServletRequest)                 getFacesContext().getExternalContext().getRequest();            view = request.getRequestURL().toString();            //out("Exception:" + e.getMessage());        }        //out("viewNav=" + view);        parseURI(view);    }        public void setId(int id) {        this.id = id;    }        public int getId() {        return id;    }        public void setInZone(boolean inZone) {        this.inZone = inZone;    }        public boolean getInZone() {        return inZone;    }        public void setInForum(boolean inForum) {        this.inForum = inForum;    }        public boolean getInForum() {        return inForum;    }        public void setInTopic(boolean inTopic) {        this.inTopic = inTopic;    }        public boolean getInTopic() {        return inTopic;    }        // 页面的导航组件    public void setUiPanel(UIPanel uiPanel) {        this.uiPanel = uiPanel;    }        /* 设置页面导航组件 */    public UIPanel getUiPanel() {        uiPanel = new UIPanel();        try {            List<UIComponent> comList = uiPanel.getChildren();            comList.add(getZonesLink(true));            if (inZone) {                ZoneModel zone = getBBSApplication().getZonesMap().get(id);                comList.add(getOutput());                comList.add(getZoneLink(zone, false));            } else if (inForum) {                ForumModel forum = getBBSApplication().getForumsMap().get(id);                ZoneModel zone = getBBSApplication().getZonesMap().get(forum.getByZone());                comList.add(getOutput());                comList.add(getZoneLink(zone, true));                comList.add(getOutput());                comList.add(getForumLink(forum, false));            } else if (inTopic) {                TopicModel topic = TopicAction.getTopicsById(id);                ForumModel forum = getBBSApplication().getForumsMap().get(topic.getByForum());                ZoneModel zone = getBBSApplication().getZonesMap().get(forum.getByZone());                comList.add(getOutput());                comList.add(getZoneLink(zone, true));                comList.add(getOutput());                comList.add(getForumLink(forum, true));                comList.add(getOutput());                comList.add(getTopicLink(topic, true));            }        } catch (Exception e) {            System.out.println("Exception in Navigation/" + e.getMessage());        }        return uiPanel;    }        /**     * get UIComponents     */     private UIComponent getZonesLink(boolean showLink) {        UIOutput link = (HtmlOutputLink)                getApplication().createComponent(HtmlOutputLink.COMPONENT_TYPE);        UIOutput text = (HtmlOutputText)                getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);        link.setValue( getContextPath() + "/bbs/forums.faces");        // 取得个性化网站名称        text.setValue(getComApplication().getMySetting().get("webname"));        link.getChildren().add(text);        if (showLink) {            return link;        } else {            return text;        }    }        /**     * get UIComponents     */     private UIComponent getZoneLink(ZoneModel zone, boolean showLink) {        UIOutput link = (HtmlOutputLink)                getApplication().createComponent(HtmlOutputLink.COMPONENT_TYPE);        UIOutput text = (HtmlOutputText)                getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);        link.setValue(getContextPath() + "/bbs/zone/" + zone.getNum() + ".faces");        text.setValue(zone.getName());        link.getChildren().add(text);        return showLink ? link : text;    }        private UIComponent getForumLink(ForumModel forum, boolean showLink) {        UIOutput link = (HtmlOutputLink)                getApplication().createComponent(HtmlOutputLink.COMPONENT_TYPE);        UIOutput text = (HtmlOutputText)                getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);        link.setValue(getContextPath() + "/bbs/forum/" + forum.getNum() + ".faces");        text.setValue(forum.getName());        link.getChildren().add(text);        return showLink ? link : text;    }        private UIComponent getTopicLink(TopicModel topic, boolean showLink) {        UIOutput link = (HtmlOutputLink)                getApplication().createComponent(HtmlOutputLink.COMPONENT_TYPE);        UIOutput text = (HtmlOutputText)                getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);        link.setValue(getContextPath() + "/bbs/topic/" + topic.getNum() + ".faces");        text.setValue(topic.getTitle());        link.getChildren().add(text);        return showLink ? link : text;    }        private UIComponent getOutput() {        UIOutput nextText = (HtmlOutputText)                getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);        nextText.setValue(" >> ");        return nextText;    }        /**     * 分析当前请求的URL是否于哪一个页面中     */     private void parseURI(String uri) {        // 尝试分析URI中的字串以用于navigation,zone,forum,topic        try {            // System.out.println("uri=" + uri);            if (uri.indexOf("/bbs/zone/") != -1) {                this.inZone = true;            } else if (uri.indexOf("/bbs/forum/") != -1) {                this.inForum = true;            } else if (uri.indexOf("/bbs/topic/") != -1) {                this.inTopic = true;            } else if (uri.indexOf("/bbs/reply/") != -1) {                this.inReply = true;            }            int start = uri.lastIndexOf("/");            int end = uri.lastIndexOf(".");            id = Integer.parseInt(uri.substring(start + 1, end));        } catch (NumberFormatException nfe) {            //System.out.println("NumberFormatException:in Navigation"             //+ nfe.getMessage());        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?