001    package railo.runtime.chart;
002    
003    import java.awt.Font;
004    import java.awt.FontMetrics;
005    import java.text.AttributedString;
006    
007    import org.jfree.chart.labels.PieSectionLabelGenerator;
008    import org.jfree.data.general.PieDataset;
009    
010    import railo.commons.lang.StringList;
011    import railo.commons.lang.font.FontUtil;
012    import railo.runtime.op.Caster;
013    import railo.runtime.type.List;
014    
015    public class PieSectionLegendLabelGeneratorImpl implements
016                    PieSectionLabelGenerator {
017    
018    
019            private FontMetrics metrics;
020            private int with;
021    
022            public PieSectionLegendLabelGeneratorImpl(Font font,int with) {
023                    this.metrics=FontUtil.getFontMetrics(font);
024                    this.with=with-20;
025            }
026    
027            public AttributedString generateAttributedSectionLabel(PieDataset dataset,
028                            Comparable key) {
029                    return null;
030            }
031    
032            public String generateSectionLabel(PieDataset pd, Comparable c) {
033                    String value=Caster.toString(pd.getKey(pd.getIndex(c)),"");
034                    
035                    StringList list = List.toList(value, '\n');
036                    StringBuffer sb=new StringBuffer();
037                    String line;
038                    int lineLen;
039                    while(list.hasNext()) {
040                            line=list.next();
041                            lineLen=metrics.stringWidth(line);
042                            if(lineLen>with) {
043                                    reorganize(sb,list,new StringBuffer(line));
044                                    break;
045                            }
046                            if(sb.length()>0)sb.append('\n');
047                            sb.append(line);
048                    }
049                    
050                    
051                    
052                    //int strLen = metrics.stringWidth(value);
053                    return sb.toString();//metrics.stringWidth(value)+"-"+with+":"+value;
054                    //return "StringUtil.reverse()";
055            }
056    
057            private void reorganize(StringBuffer sb, StringList list, StringBuffer rest) {
058                    // fill rest
059                    String item;
060                    while(list.hasNext()) {
061                            item=list.next();
062                            rest.append(list.delimeter());
063                            rest.append(item);
064                    }
065                    
066                    StringList words = List.toWordList(rest.toString());
067                    StringBuffer line=new StringBuffer();
068                    
069                    while(words.hasNext()) {
070                            item=words.next();
071                            
072                            if(line.length()>0 && metrics.stringWidth(item.concat(" ").concat(line.toString()))>with) {
073                                    if(sb.length()>0) sb.append('\n');
074                                    sb.append(line);
075                                    //print.out("line:"+line);
076                                    line=new StringBuffer(item);
077                            }
078                            else {
079                                    //item=words.next();
080                                    if(line.length()>0)line.append(words.delimeter()==0?' ':words.delimeter());
081                                    line.append(item);
082                            }
083                    }
084                    if(line.length()>0){
085                            if(sb.length()>0) sb.append('\n');
086                            sb.append(line);
087                    }
088            }
089    }