⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcomponent.java

📁 欢迎使用 FastJsp 开发框架! 编译说明: * 若要生成Api Javadoc文档
💻 JAVA
字号:
// Copyright 2005-2007 onetsoft.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.onetsoft.fastjsp;

import com.onetsoft.fastjsp.request.PageData;
import com.onetsoft.fastjsp.util.ComboKey;
import com.onetsoft.fastjsp.util.ScriptHelper;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.util.Util;
import com.onetsoft.fastjsp.valid.ValidationDelegate;
import com.onetsoft.fastjsp.valid.ValidationException;

import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="mailto:hgw@onetsoft.com">hgw</a>
 */
public abstract class AbstractComponent implements Component {

    AbstractPageCycle cycle = null;
    AbstractPageService service = null;
    private Messages messages = null;
    private PageParamsImpl pageParams = null;

    public AbstractComponent() {
    }

    /**
     * 组件标识符
     * 唯一标识页面内各组件
     * 链接{@link this#getLink(String)}到当前或其它页面时,此uci值为0
     */
    int uci = 0;

    //pool to contains form instance for every page
    private Map pool = null;

    /**
     * Set cycle when initializing the componet
     *
     * @param cycle
     */
    final void setCycle(AbstractPageCycle cycle) {
        this.cycle = cycle;
        this.service = cycle.service;
        this.pageParams = service.servicer.pageParams;
    }

    /**
     * 初始化当前组件
     * 注:调用此方法前,form or url参数携带的各类数据已全部载入{@link this#getData()} 所取得的{@link com.onetsoft.fastjsp.request.PageData}当前实例中
     */
    final void init() {
        initialize();
        finishLoad();
    }


    /**
     * 初始化组件/页面各类数据
     * 注:该方法在页面url,form请求都会执行
     * 警告:调用{@link this#getData()}方法设置页面参数(url,form)数据会覆盖原有相同参数的值
     * 特殊情况下甚至可重设action参数达到更改/选择action的目的
     * 允许此方法抛出{@link ValidationException},确保组件能实例化。抛出此错误时应此组件至少能正常“显示”
     */
    protected abstract void initialize();

    /**
     * 页面结束载入
     * 此方法在{@link this#initialize()}后调用,方便进一步的页面初始化工作。
     */
    protected void finishLoad() {
    }

    /**
     * 初始化表单域
     * 用于页面或组件显示前设置初始化表单数据
     *
     * @param data
     * @throws ApplicationRuntimeException
     */
    protected void initFields(PageData data) {
    }

    public Messages getMessages() {
        if (messages == null) {
            messages = new ComboMessages(service);
        }
        return messages;
    }

    public Component getComponent(Class clazz) {
        return service.componentsManager.getComponent(clazz);
    }

    public ValidationDelegate getValidationDelegate() {
        return service.delegateCombo;
    }

    /**
     * 返回表单
     * 此表单已注入页面对象{@link Page}
     *
     * @param formName
     * @return
     */
    Form getForm(String formName) {
        try {
            if (pool == null)
                pool = new HashMap(2);
            AbstractForm form = (AbstractForm) pool.get(formName);
            if (form == null) {
                form = service.actionsManager.getFormInstance(new ComboKey(uci, this.getClass()), formName);
                form.setPage(service.page);
                pool.put(formName, form);
            }
            return form;
        } catch (NullPointerException e) {
            throw new ApplicationRuntimeException("Cannot get form:" + formName, e);
        }
    }

    /* 注:每form js验证仅可输出一次*/
    public String getScriptSupport(String formName) {
        if (service.page.formScripts == null)
            service.page.formScripts = new HashMap(3);
        String s = StringUtils.EMPTY;
        if (service.page.formScripts.put(formName, StringUtils.EMPTY) == null) {
            s = ScriptHelper.getFormScripts(service.servicer.getModule().getValidationMessageProvider(), getForm(formName));
            if (!service.page.fjScriptRendered) {
                service.page.fjScriptRendered = true;
                s = new StringBuffer(ScriptHelper.getFastJspScripts(service.servicer.servletContextPath)).append(s).toString();
            }
        }
        return s;
    }

    public Page getPage() {
        return cycle.service.page;
    }

    public PageData getData() {
        return service.data;
    }

    public PageCycle getCycle() {
        return cycle;
    }


    public Link getLink() {
        return getLink(pageParams.pageName);
    }


    public Link getLink(String pageName) {
        if (uci == 0 || !pageParams.pageName.equals(pageName))
            return getLinkImpl(null, pageName);
        else
            return getLinkImpl(null, pageName).append(LinkImpl.UCI, uci);
    }

    public Link getLink(PageModule layoutModule, String pageName) {
        if (layoutModule == null)
            layoutModule = Util.EMPTY_PAGE_MODULE;
        if (!pageParams.layoutModule.equals(layoutModule) || !pageParams.pageName.equals(pageName))
            uci = 0;
        if (uci == 0)
            if (service.servicer.module.welcomeFileName.equalsIgnoreCase(pageName))
                return getLinkImpl(layoutModule, StringUtils.EMPTY);
            else
                return getLinkImpl(layoutModule, pageName);
        else
            return getLinkImpl(layoutModule, pageName).append(LinkImpl.UCI, uci);
    }

    private LinkImpl getLinkImpl(PageModule layoutModule, String pageName) {
        LinkImpl o = service.servicer.linkPool.get();
        if (layoutModule == null) {
            o.buf.append(pageParams.pageUrlBase);
        } else {
            pageParams.getUrlBase(o.buf, layoutModule);
        }
        o.setComponent(this);
        o.setPageName(pageName);
        o.init();
        return o;
    }

    /**
     * 取得本组件所含组件列表
     * 注:页面内一个组件只能实际使用一次
     *
     * @return
     */
    protected abstract Class[] getComponents();

}

⌨️ 快捷键说明

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