001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.io;
017
018import org.opengion.fukurou.util.StringUtil;
019
020import org.jfree.chart.urls.CategoryURLGenerator;
021import org.jfree.chart.urls.PieURLGenerator;
022import org.jfree.chart.urls.XYURLGenerator;
023
024import org.jfree.data.category.CategoryDataset;
025import org.jfree.data.general.PieDataset;
026import org.jfree.data.xy.XYDataset;
027
028/**
029 * A URL generator that can be assigned to a
030 * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
031 */
032public class HybsURLGenerator implements CategoryURLGenerator, PieURLGenerator, XYURLGenerator {
033
034        /** The prefix. */
035        private final String prefix ;
036
037        /** The category parameter name. */
038        private String categoryName = "category";
039
040        /** The pie index parameter name. */
041        private String indexName = "pieIndex";
042
043        /** Series parameter name to go in each URL */
044        private String seriesName = "series" ;
045
046        /**
047         * クリッカブル・マップ用URLを指定します。
048         *
049         * 画像に、クリッカブル・マップを作成する場合の、URL を指定します。
050         * これは、画像上にエリア指定でリンク引数を作成することが可能です。
051         * URL 自身は、? 付きで固定値の引数を連結することが可能です。
052         * クリックしたエリアのカテゴリやインデックスの値(引数)は、自動的に
053         * 設定されます。(指定しない場合はチャートによって異なります)
054         * ※ 本家 jFreeChart とは並び順やキーが異なります。
055         * <pre>
056         * ・Pie      :category、pieIndex
057         * ・XY       :category、series
058         * ・Category :category、series
059         * </pre>
060         * この引数の URL の名称を変更したい場合は、URL に続けて、カンマ(,) で、
061         * 名称を記述してください。
062         * 例:link.jsp,BLOCK
063         *
064         * @param       imageMapUrl     クリッカブル・マップ用URL
065         */
066        public HybsURLGenerator( final String imageMapUrl ) {
067                final boolean first = imageMapUrl.indexOf( '?' ) < 0 ;  // 含まなければ true
068
069                final int adrs = imageMapUrl.indexOf( ',' );
070                if( adrs < 0 ) {        // 引数が 0個
071                        prefix = imageMapUrl + ( first ? "?" : "&" );
072                }
073                else {
074                        // 一番目の引数設定
075                        prefix = imageMapUrl.substring( 0,adrs ) + ( first ? "?" : "&" ) ;
076                        final int adrs2 = imageMapUrl.indexOf( ',',adrs+1 );
077                        if( adrs2 < 0 ) {       // 引数が 1個確定
078                                categoryName = imageMapUrl.substring( adrs+1 );
079                        }
080                        else {
081                                categoryName = imageMapUrl.substring( adrs+1,adrs2 );
082                                seriesName   = imageMapUrl.substring( adrs2+1 );
083                                indexName    = seriesName;
084                        }
085                }
086        }
087
088        /**
089         * カテゴリDataset,シリーズ番号,カテゴリ番号を指定して、URL文字列を作成します。
090         *
091         * Generates a URL for a particular item within a series.
092         *
093         * @param       dataset         カテゴリDataset
094         * @param       series          シリーズ番号
095         * @param       category        カテゴリ番号
096         *
097         * @return      作成されたURL文字列
098         */
099        public String generateURL( final CategoryDataset dataset, final int series, final int category ) {
100                final Comparable<?> seriesKey   = dataset.getRowKey(series);                    // 4.3.3.6 (2008/11/15) Generics警告対応
101                final Comparable<?> categoryKey = dataset.getColumnKey(category);               // 4.3.3.6 (2008/11/15) Generics警告対応
102
103                final String url = prefix
104                                                + categoryName + "="
105                                                + StringUtil.urlEncode(categoryKey.toString() )
106                                                + "&" + seriesName + "="
107                                                + StringUtil.urlEncode( seriesKey.toString() );
108                return url;
109        }
110
111        /**
112         * パイDataset,アイテムキー,インデックス番号を指定して、URL文字列を作成します。
113         *
114         * Generates a URL.
115         *
116         * @param       dataset         パイDataset
117         * @param       key                     アイテムキー
118         * @param       pieIndex        インデックス番号
119         *
120         * @return      作成されたURL文字列
121         */
122        @SuppressWarnings("rawtypes")
123        public String generateURL( final PieDataset dataset, final Comparable key, final int pieIndex ) {
124                final String url = prefix
125                                                + categoryName + "="
126                                                + StringUtil.urlEncode(key.toString() )
127                                                + "&" + indexName + "="
128                                                + pieIndex;
129
130                return url;
131        }
132
133        /**
134         * エックスワイDataset,シリーズ番号,アイテム番号を指定して、URL文字列を作成します。
135         *
136         * Generates a URL for a particular item within a series.
137         *
138         * @param       dataset         エックスワイDataset
139         * @param       series          シリーズ番号
140         * @param       item            アイテム番号
141         *
142         * @return      作成されたURL文字列
143         */
144        public String generateURL( final XYDataset dataset, final int series, final int item ) {
145                final String url = prefix
146                                                + categoryName + "=" + item
147                                                + "&" + seriesName + "=" + series;
148                return url;
149        }
150}