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                // 7.2.9.5 (2020/11/28) PMD:Consider simply returning the value vs storing it in local variable 'XXXX'
104                return prefix
105//              final String url = prefix
106                                                + categoryName + "="
107                                                + StringUtil.urlEncode(categoryKey.toString() )
108                                                + "&" + seriesName + "="
109                                                + StringUtil.urlEncode( seriesKey.toString() );
110//              return url;
111        }
112
113        /**
114         * パイDataset,アイテムキー,インデックス番号を指定して、URL文字列を作成します。
115         *
116         * Generates a URL.
117         *
118         * @param       dataset         パイDataset
119         * @param       key                     アイテムキー
120         * @param       pieIndex        インデックス番号
121         *
122         * @return      作成されたURL文字列
123         */
124        @SuppressWarnings("rawtypes")
125        public String generateURL( final PieDataset dataset, final Comparable key, final int pieIndex ) {
126                // 7.2.9.5 (2020/11/28) PMD:Consider simply returning the value vs storing it in local variable 'XXXX'
127                return prefix
128//              final String url = prefix
129                                                + categoryName + "="
130                                                + StringUtil.urlEncode(key.toString() )
131                                                + "&" + indexName + "="
132                                                + pieIndex;
133
134//              return url;
135        }
136
137        /**
138         * エックスワイDataset,シリーズ番号,アイテム番号を指定して、URL文字列を作成します。
139         *
140         * Generates a URL for a particular item within a series.
141         *
142         * @param       dataset         エックスワイDataset
143         * @param       series          シリーズ番号
144         * @param       item            アイテム番号
145         *
146         * @return      作成されたURL文字列
147         */
148        public String generateURL( final XYDataset dataset, final int series, final int item ) {
149                // 7.2.9.5 (2020/11/28) PMD:Consider simply returning the value vs storing it in local variable 'XXXX'
150                return prefix
151//              final String url = prefix
152                                                + categoryName + "=" + item
153                                                + "&" + seriesName + "=" + series;
154//              return url;
155        }
156}