/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors.. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.core.metadata;
import java.awt.Image;
import java.awt.Toolkit;
import java.beans.BeanInfo;
import java.io.Serializable;
import java.net.URL;
import java.util.Locale;
import java.util.MissingResourceException;
import org.pentaho.reporting.libraries.base.util.Messages;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
import org.pentaho.reporting.libraries.xmlns.common.ParserUtil;
/**
* Todo: Document Me
*
* @author Thomas Morgner
*/
public class AbstractMetaData implements Serializable, MetaData
{
private transient Image[] icons;
private String name;
private String bundleLocation;
private String keyPrefix;
private boolean expert;
private boolean preferred;
private boolean hidden;
private boolean deprecated;
private transient Locale lastLocale;
private transient Messages lastMessages;
private String displayNameKey;
private String descriptionKey;
private String groupingKey;
private String groupingOrdinalKey;
private String itemOrdinalKey;
private String keyPrefixAndName;
private final String deprecationKey;
private boolean experimental;
public AbstractMetaData(final String name,
final String bundleLocation,
final String keyPrefix,
final boolean expert,
final boolean preferred,
final boolean hidden,
final boolean deprecated)
{
this(name, bundleLocation, keyPrefix, expert, preferred, hidden, deprecated, false);
}
public AbstractMetaData(final String name,
final String bundleLocation,
final String keyPrefix,
final boolean expert,
final boolean preferred,
final boolean hidden,
final boolean deprecated,
final boolean experimental)
{
if (name == null)
{
throw new NullPointerException();
}
if (bundleLocation == null)
{
throw new NullPointerException();
}
if (keyPrefix == null)
{
throw new NullPointerException();
}
this.experimental = experimental;
this.name = name;
this.bundleLocation = bundleLocation;
this.keyPrefix = keyPrefix;
this.expert = expert;
this.preferred = preferred;
this.hidden = hidden;
this.deprecated = deprecated;
keyPrefixAndName = this.keyPrefix + this.name;
displayNameKey = keyPrefixAndName + ".display-name";
descriptionKey = keyPrefixAndName + ".description";
itemOrdinalKey = keyPrefixAndName + ".ordinal";
groupingKey = keyPrefixAndName + ".grouping";
groupingOrdinalKey = groupingKey + ".ordinal";
deprecationKey = keyPrefixAndName + ".deprecated";
}
public boolean isExperimental()
{
return experimental;
}
public boolean isDeprecated()
{
return deprecated;
}
public String getName()
{
return name;
}
public Messages getBundle(final Locale locale)
{
if (locale == null)
{
throw new NullPointerException();
}
if (lastMessages == null || ObjectUtilities.equal(locale, lastLocale) == false)
{
lastMessages = new Messages(locale, bundleLocation,
ObjectUtilities.getClassLoader(AbstractMetaData.class));
lastLocale = locale;
}
return lastMessages;
}
public String getMetaAttribute(final String attributeName, final Locale locale)
{
try
{
final String key = keyPrefixAndName + '.' + attributeName;
return getBundle(locale).strictString(key);
}
catch (MissingResourceException mre)
{
return null;
}
}
public String getDisplayName(final Locale locale)
{
return getBundle(locale).getString(displayNameKey);
}
public String getDescription(final Locale locale)
{
return getBundle(locale).getString(descriptionKey);
}
public String getGrouping(final Locale locale)
{
return getBundle(locale).getString(groupingKey);
}
public int getGroupingOrdinal(final Locale locale)
{
final String strOrd = getBundle(locale).getString(groupingOrdinalKey);
return ParserUtil.parseInt(strOrd, Integer.MAX_VALUE);
}
public int getItemOrdinal(final Locale locale)
{
final String strOrd = getBundle(locale).getString(itemOrdinalKey);
return ParserUtil.parseInt(strOrd, Integer.MAX_VALUE);
}
public String getDeprecationMessage(final Locale locale)
{
return getBundle(locale).getString(deprecationKey);
}
public boolean isExpert()
{
return expert;
}
public boolean isPreferred()
{
return preferred;
}
public boolean isHidden()
{
return hidden;
}
public String getKeyPrefix()
{
return keyPrefix;
}
public String getBundleLocation()
{
return bundleLocation;
}
public synchronized Image getIcon(final Locale locale, final int iconKind)
{
if (iconKind < 1 || iconKind > 4)
{
throw new IllegalArgumentException();
}
if (icons != null)
{
final Image cachedIcon = icons[iconKind - 1];
if (cachedIcon != null)
{
return cachedIcon;
}
}
final String iconName;
switch (iconKind)
{
case BeanInfo.ICON_COLOR_16x16:
iconName = getBundle(locale).getString(getKeyPrefix() + getName() + ".icon-color-16");
break;
case BeanInfo.ICON_COLOR_32x32:
iconName = getBundle(locale).getString(getKeyPrefix() + getName() + ".icon-color-32");
break;
case BeanInfo.ICON_MONO_16x16:
iconName = getBundle(locale).getString(getKeyPrefix() + getName() + ".icon-mono-16");
break;
case BeanInfo.ICON_MONO_32x32:
iconName = getBundle(locale).getString(getKeyPrefix() + getName() + ".icon-mono-32");
break;
default:
throw new IllegalArgumentException();
}
URL url = ObjectUtilities.getResource(iconName, DefaultElementMetaData.class);
if (url == null)
{
final String fallbackIcon = getBundle(locale).getString(getKeyPrefix() + getName() + ".icon");
url = ObjectUtilities.getResource(fallbackIcon, DefaultElementMetaData.class);
if (url == null)
{
return null;
}
}
if (icons == null)
{
icons = new Image[4];
}
final Image retval = Toolkit.getDefaultToolkit().createImage(url);
icons[iconKind - 1] = retval;
return retval;
}
}