tableproperties.java

来自「displaytag-1.0修正版」· Java 代码 · 共 1,131 行 · 第 1/3 页

JAVA
1,131
字号
    public boolean getAddExport(MediaTypeEnum exportType)
    {
        return getBooleanProperty(PROPERTY_EXPORT_PREFIX + SEP + exportType.getName());
    }

    /**
     * Should headers be included in given export type?
     * @param exportType instance of MediaTypeEnum
     * @return boolean true if export should include headers
     */
    public boolean getExportHeader(MediaTypeEnum exportType)
    {
        return getBooleanProperty(PROPERTY_EXPORT_PREFIX
            + SEP
            + exportType.getName()
            + SEP
            + EXPORTPROPERTY_BOOLEAN_EXPORTHEADER);
    }

    /**
     * Returns the label for the given export option.
     * @param exportType instance of MediaTypeEnum
     * @return String label
     */
    public String getExportLabel(MediaTypeEnum exportType)
    {
        return getProperty(PROPERTY_EXPORT_PREFIX + SEP + exportType.getName() + SEP + EXPORTPROPERTY_STRING_LABEL);
    }

    /**
     * Returns the file name for the given media. Can be null
     * @param exportType instance of MediaTypeEnum
     * @return String filename
     */
    public String getExportFileName(MediaTypeEnum exportType)
    {
        return getProperty(PROPERTY_EXPORT_PREFIX + SEP + exportType.getName() + SEP + EXPORTPROPERTY_STRING_FILENAME);
    }

    /**
     * Getter for the <code>PROPERTY_BOOLEAN_EXPORTDECORATED</code> property.
     * @return boolean <code>true</code> if decorators should be used in exporting
     */
    public boolean getExportDecorated()
    {
        return getBooleanProperty(PROPERTY_BOOLEAN_EXPORTDECORATED);
    }

    /**
     * Getter for the <code>PROPERTY_STRING_EXPORTBANNER</code> property.
     * @return String
     */
    public String getExportBanner()
    {
        return getProperty(PROPERTY_STRING_EXPORTBANNER);
    }

    /**
     * Getter for the <code>PROPERTY_STRING_EXPORTBANNER_SEPARATOR</code> property.
     * @return String
     */
    public String getExportBannerSeparator()
    {
        return getProperty(PROPERTY_STRING_EXPORTBANNER_SEPARATOR);
    }

    /**
     * Getter for the <code>PROPERTY_BOOLEAN_SHOWHEADER</code> property.
     * @return boolean
     */
    public boolean getShowHeader()
    {
        return getBooleanProperty(PROPERTY_BOOLEAN_SHOWHEADER);
    }

    /**
     * Getter for the <code>PROPERTY_STRING_EMPTYLIST_MESSAGE</code> property.
     * @return String
     */
    public String getEmptyListMessage()
    {
        return getProperty(PROPERTY_STRING_EMPTYLIST_MESSAGE);
    }

    /**
     * Getter for the <code>PROPERTY_STRING_EMPTYLISTROW_MESSAGE</code> property.
     * @return String
     */
    public String getEmptyListRowMessage()
    {
        return getProperty(PROPERTY_STRING_EMPTYLISTROW_MESSAGE);
    }

    /**
     * Getter for the <code>PROPERTY_BOOLEAN_EMPTYLIST_SHOWTABLE</code> property.
     * @return boolean <code>true</code> if table should be displayed also if no items are found
     */
    public boolean getEmptyListShowTable()
    {
        return getBooleanProperty(PROPERTY_BOOLEAN_EMPTYLIST_SHOWTABLE);
    }

    /**
     * Getter for the <code>PROPERTY_STRING_EXPORTAMOUNT</code> property.
     * @return boolean <code>true</code> if <code>export.amount</code> is <code>list</code>
     */
    public boolean getExportFullList()
    {
        return "list".equals(getProperty(PROPERTY_STRING_EXPORTAMOUNT)); //$NON-NLS-1$
    }

    /**
     * Getter for the <code>PROPERTY_STRING_SORTAMOUNT</code> property.
     * @return boolean <code>true</code> if <code>sort.amount</code> is <code>list</code>
     */
    public boolean getSortFullList()
    {
        return "list".equals(getProperty(PROPERTY_STRING_SORTAMOUNT)); //$NON-NLS-1$
    }

    /**
     * Should paging banner be added before the table?
     * @return boolean
     */
    public boolean getAddPagingBannerTop()
    {
        String placement = getProperty(PROPERTY_STRING_BANNER_PLACEMENT);
        return "top".equals(placement) || "both".equals(placement); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Should paging banner be added after the table?
     * @return boolean
     */
    public boolean getAddPagingBannerBottom()
    {
        String placement = getProperty(PROPERTY_STRING_BANNER_PLACEMENT);
        return "bottom".equals(placement) || "both".equals(placement); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Returns the appropriate css class for a table row.
     * @param rowNumber row number
     * @return the value of <code>PROPERTY_CSS_TR_EVEN</code> if rowNumber is even or <code>PROPERTY_CSS_TR_ODD</code>
     * if rowNumber is odd.
     */
    public String getCssRow(int rowNumber)
    {
        return getProperty((rowNumber % 2 == 0) ? PROPERTY_CSS_TR_ODD : PROPERTY_CSS_TR_EVEN);
    }

    /**
     * Returns the appropriate css class for a sorted column header.
     * @param ascending <code>true</code> if column is sorded in ascending order.
     * @return the value of <code>PROPERTY_CSS_TH_SORTED_ASCENDING</code> if column is sorded in ascending order or
     * <code>PROPERTY_CSS_TH_SORTED_DESCENDING</code> if column is sorded in descending order.
     */
    public String getCssOrder(boolean ascending)
    {
        return getProperty(ascending ? PROPERTY_CSS_TH_SORTED_ASCENDING : PROPERTY_CSS_TH_SORTED_DESCENDING);
    }

    /**
     * Returns the configured css class for a sorted column header.
     * @return the value of <code>PROPERTY_CSS_TH_SORTED</code>
     */
    public String getCssSorted()
    {
        return getProperty(PROPERTY_CSS_TH_SORTED);
    }

    /**
     * Returns the configured css class for the main table tag.
     * @return the value of <code>PROPERTY_CSS_TABLE</code>
     */
    public String getCssTable()
    {
        return getProperty(PROPERTY_CSS_TABLE);
    }

    /**
     * Returns the configured css class for a sortable column header.
     * @return the value of <code>PROPERTY_CSS_TH_SORTABLE</code>
     */
    public String getCssSortable()
    {
        return getProperty(PROPERTY_CSS_TH_SORTABLE);
    }

    /**
     * Returns the configured list of media.
     * @return the value of <code>PROPERTY_EXPORTTYPES</code>
     */
    public String[] getExportTypes()
    {
        String list = getProperty(PROPERTY_EXPORTTYPES);
        if (list == null)
        {
            return new String[0];
        }

        return StringUtils.split(list);
    }

    /**
     * Returns the class responsible for the given export.
     * @param exportName export name
     * @return String classname
     */
    public String getExportClass(String exportName)
    {
        return getProperty(PROPERTY_EXPORT_PREFIX + SEP + exportName + SEP + EXPORTPROPERTY_STRING_CLASS);
    }

    /**
     * Returns an instance of configured requestHelperFactory.
     * @return RequestHelperFactory instance.
     * @throws FactoryInstantiationException if unable to load or instantiate the configurated class.
     */
    public RequestHelperFactory getRequestHelperFactoryInstance() throws FactoryInstantiationException
    {
        Object loadedObject = getClassPropertyInstance(PROPERTY_CLASS_REQUESTHELPERFACTORY);

        // should not be null, but avoid errors just in case... see DISPL-148
        if (loadedObject == null)
        {
            return new DefaultRequestHelperFactory();
        }

        try
        {
            return (RequestHelperFactory) loadedObject;
        }
        catch (ClassCastException e)
        {
            throw new FactoryInstantiationException(getClass(), PROPERTY_CLASS_REQUESTHELPERFACTORY, loadedObject
                .getClass()
                .getName(), e);
        }
    }

    /**
     * Returns the configured resource provider instance. If necessary instantiate the resource provider from config and
     * then keep a cached instance.
     * @return I18nResourceProvider instance.
     * @see I18nResourceProvider
     */
    public I18nResourceProvider geResourceProvider()
    {
        String className = getProperty(PROPERTY_CLASS_LOCALEPROVIDER);

        if (resourceProvider == null)
        {
            if (className != null)
            {
                try
                {
                    Class classProperty = ReflectHelper.classForName(className);
                    resourceProvider = (I18nResourceProvider) classProperty.newInstance();

                    log.info(Messages.getString("TableProperties.classinitializedto", //$NON-NLS-1$
                        new Object[]{ClassUtils.getShortClassName(I18nResourceProvider.class), className}));
                }
                catch (Throwable e)
                {
                    log.warn(Messages.getString("TableProperties.errorloading", //$NON-NLS-1$
                        new Object[]{
                            ClassUtils.getShortClassName(I18nResourceProvider.class),
                            e.getClass().getName(),
                            e.getMessage()}));
                }
            }
            else
            {
                log.info(Messages.getString("TableProperties.noconfigured", //$NON-NLS-1$
                    new Object[]{ClassUtils.getShortClassName(I18nResourceProvider.class)}));
            }

            // still null?
            if (resourceProvider == null)
            {
                // fallback provider, no i18n
                resourceProvider = new I18nResourceProvider()
                {

                    // Always returns null
                    public String getResource(String titleKey, String property, Tag tag, PageContext context)
                    {
                        return null;
                    }
                };
            }
        }

        return resourceProvider;
    }

    /**
     * Reads a String property.
     * @param key property name
     * @return property value or <code>null</code> if property is not found
     */
    private String getProperty(String key)
    {
        return this.properties.getProperty(key);
    }

    /**
     * Sets a property.
     * @param key property name
     * @param value property value
     */
    public void setProperty(String key, String value)
    {
        this.properties.setProperty(key, value);
    }

    /**
     * Reads a boolean property.
     * @param key property name
     * @return boolean <code>true</code> if the property value is "true", <code>false</code> for any other value.
     */
    private boolean getBooleanProperty(String key)
    {
        return Boolean.TRUE.toString().equals(getProperty(key));
    }

    /**
     * Returns an instance of a configured Class. Returns a configured Class instantiated
     * callingClass.forName([configuration value]).
     * @param key configuration key
     * @return instance of configured class
     * @throws FactoryInstantiationException if unable to load or instantiate the configurated class.
     */
    private Object getClassPropertyInstance(String key) throws FactoryInstantiationException
    {
        String className = getProperty(key);

        // shouldn't be null, but better check it
        if (className == null)
        {
            return null;
        }

        try
        {
            Class classProperty = ReflectHelper.classForName(className);
            return classProperty.newInstance();
        }
        catch (Exception e)
        {
            throw new FactoryInstantiationException(getClass(), key, className, e);
        }
    }

    /**
     * Reads an int property.
     * @param key property name
     * @param defaultValue default value returned if property is not found or not a valid int value
     * @return property value
     */
    private int getIntProperty(String key, int defaultValue)
    {
        try
        {
            return Integer.parseInt(getProperty(key));
        }
        catch (NumberFormatException e)
        {
            // Don't care, use default
            log.warn(Messages.getString("TableProperties.invalidvalue", //$NON-NLS-1$
                new Object[]{key, getProperty(key), new Integer(defaultValue)}));
        }

        return defaultValue;
    }
}

⌨️ 快捷键说明

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