001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: /**
034: * Utility class that manages built-in hyperlink types.
035: *
036: * @author Lucian Chirita (lucianc@users.sourceforge.net)
037: * @version $Id: JRHyperlinkHelper.java 1720 2007-05-07 10:02:56Z lucianc $
038: */
039: public class JRHyperlinkHelper {
040: /**
041: * "None" link type, equivalent to {@link JRHyperlink#HYPERLINK_TYPE_NONE JRHyperlink.HYPERLINK_TYPE_NONE}.
042: */
043: public static final String HYPERLINK_TYPE_NONE = "None";
044:
045: /**
046: * "Reference" link type, equivalent to {@link JRHyperlink#HYPERLINK_TYPE_REFERENCE JRHyperlink.HYPERLINK_TYPE_REFERENCE}.
047: */
048: public static final String HYPERLINK_TYPE_REFERENCE = "Reference";
049:
050: /**
051: * "LocalAnchor" link type, equivalent to {@link JRHyperlink#HYPERLINK_TYPE_LOCAL_ANCHOR JRHyperlink.HYPERLINK_LOCAL_ANCHOR}.
052: */
053: public static final String HYPERLINK_TYPE_LOCAL_ANCHOR = "LocalAnchor";
054:
055: /**
056: * "LocalPage" link type, equivalent to {@link JRHyperlink#HYPERLINK_TYPE_LOCAL_PAGE JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE}.
057: */
058: public static final String HYPERLINK_TYPE_LOCAL_PAGE = "LocalPage";
059:
060: /**
061: * "RemoteAnchor" link type, equivalent to {@link JRHyperlink#HYPERLINK_TYPE_REMOTE_ANCHOR JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR}.
062: */
063: public static final String HYPERLINK_TYPE_REMOTE_ANCHOR = "RemoteAnchor";
064:
065: /**
066: * "RemotePage" link type, equivalent to {@link JRHyperlink#HYPERLINK_TYPE_REMOTE_PAGE JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE}.
067: */
068: public static final String HYPERLINK_TYPE_REMOTE_PAGE = "RemotePage";
069:
070: private static final Map builtinTypes;
071:
072: static {
073: builtinTypes = createBuiltinTypes();
074: }
075:
076: private static Map createBuiltinTypes() {
077: Map types = new HashMap();
078: types.put(HYPERLINK_TYPE_NONE, new Byte(
079: JRHyperlink.HYPERLINK_TYPE_NONE));
080: types.put(HYPERLINK_TYPE_REFERENCE, new Byte(
081: JRHyperlink.HYPERLINK_TYPE_REFERENCE));
082: types.put(HYPERLINK_TYPE_LOCAL_ANCHOR, new Byte(
083: JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR));
084: types.put(HYPERLINK_TYPE_LOCAL_PAGE, new Byte(
085: JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE));
086: types.put(HYPERLINK_TYPE_REMOTE_ANCHOR, new Byte(
087: JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR));
088: types.put(HYPERLINK_TYPE_REMOTE_PAGE, new Byte(
089: JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE));
090: return types;
091: }
092:
093: /**
094: * Returns the built-in hyperlink type, or {@link JRHyperlink#HYPERLINK_TYPE_CUSTOM JRHyperlink.HYPERLINK_TYPE_CUSTOM}
095: * if the type is not a built-in type.
096: *
097: * @param hyperlink the hyperlink object
098: * @return the hyperlink type
099: */
100: public static byte getHyperlinkType(JRHyperlink hyperlink) {
101: return getHyperlinkType(hyperlink.getLinkType());
102: }
103:
104: /**
105: * Returns the built-in hyperlink type, or {@link JRHyperlink#HYPERLINK_TYPE_CUSTOM JRHyperlink.HYPERLINK_TYPE_CUSTOM}
106: * if the type is not a built-in type.
107: *
108: * @param linkType the link type
109: * @return the hyperlink type
110: */
111: public static byte getHyperlinkType(String linkType) {
112: byte type;
113: if (linkType == null) {
114: type = JRHyperlink.HYPERLINK_TYPE_NONE;
115: } else {
116: Byte builtinType = (Byte) builtinTypes.get(linkType);
117: if (builtinType == null) {
118: type = JRHyperlink.HYPERLINK_TYPE_CUSTOM;
119: } else {
120: type = builtinType.byteValue();
121: }
122: }
123: return type;
124: }
125:
126: /**
127: * Returns the link type associated with a built-in type.
128: *
129: * @param hyperlinkType the built-in type
130: * @return the String link type
131: */
132: public static String getLinkType(byte hyperlinkType) {
133: String type;
134: switch (hyperlinkType) {
135: case JRHyperlink.HYPERLINK_TYPE_NULL:
136: case JRHyperlink.HYPERLINK_TYPE_NONE:
137: type = null;
138: break;
139: case JRHyperlink.HYPERLINK_TYPE_REFERENCE:
140: type = HYPERLINK_TYPE_REFERENCE;
141: break;
142: case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR:
143: type = HYPERLINK_TYPE_LOCAL_ANCHOR;
144: break;
145: case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE:
146: type = HYPERLINK_TYPE_LOCAL_PAGE;
147: break;
148: case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR:
149: type = HYPERLINK_TYPE_REMOTE_ANCHOR;
150: break;
151: case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE:
152: type = HYPERLINK_TYPE_REMOTE_PAGE;
153: break;
154: case JRHyperlink.HYPERLINK_TYPE_CUSTOM:
155: throw new JRRuntimeException(
156: "Custom hyperlink types cannot be specified using the byte constant");
157: default:
158: throw new JRRuntimeException("Unknown hyperlink type "
159: + hyperlinkType);
160: }
161: return type;
162: }
163:
164: /**
165: * Decides whether a hyperlink is empty or not.
166: * <p>
167: * The hyperlink is considered empty when it's <code>null</code> or when
168: * its type is {@link JRHyperlink#HYPERLINK_TYPE_NONE HYPERLINK_TYPE_NONE}
169: * and it doesn't include a tooltip expression
170: * </p>
171: * @param hyperlink the hyperlink
172: * @return whether the hyperlink is empty
173: */
174: public static boolean isEmpty(JRHyperlink hyperlink) {
175: return hyperlink == null
176: || (hyperlink.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_NONE && hyperlink
177: .getHyperlinkTooltipExpression() == null);
178: }
179: }
|