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                boolean first = imageMapUrl.indexOf( '?' ) < 0 ;     // 含まなければ true
068
069                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                        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         * Generates a URL for a particular item within a series.
090         *
091         * @param       dataset         カテゴリDataset
092         * @param       series          シリーズ番号
093         * @param       category        カテゴリ番号
094         *
095         * @return      作成されたURL文字列
096         */
097        public String generateURL( final CategoryDataset dataset, final int series, final int category ) {
098                Comparable<?> seriesKey   = dataset.getRowKey(series);                    // 4.3.3.6 (2008/11/15) Generics警告対応
099                Comparable<?> categoryKey = dataset.getColumnKey(category);               // 4.3.3.6 (2008/11/15) Generics警告対応
100
101                String url = prefix
102                                                + categoryName + "="
103                                                + StringUtil.urlEncode(categoryKey.toString() )
104                                                + "&" + seriesName + "="
105                                                + StringUtil.urlEncode( seriesKey.toString() );
106                return url;
107        }
108
109        /**
110         * Generates a URL.
111         *
112         * @param       dataset         パイDataset
113         * @param       key                     アイテムキー
114         * @param       pieIndex        インデックス番号
115         *
116         * @return      作成されたURL文字列
117         */
118        @SuppressWarnings("rawtypes")
119        public String generateURL( final PieDataset dataset, final Comparable key, final int pieIndex ) {
120                String url = prefix
121                                                + categoryName + "="
122                                                + StringUtil.urlEncode(key.toString() )
123                                                + "&" + indexName + "="
124                                                + pieIndex;
125
126                return url;
127        }
128
129        /**
130         * Generates a URL for a particular item within a series.
131         *
132         * @param       dataset         エックスワイDataset
133         * @param       series          シリーズ番号
134         * @param       item            アイテム番号
135         *
136         * @return      作成されたURL文字列
137         */
138        public String generateURL( final XYDataset dataset, final int series, final int item ) {
139                String url = prefix
140                                                + categoryName + "=" + item
141                                                + "&" + seriesName + "=" + series;
142                return url;
143        }
144}