Thursday, May 23, 2013

JasperReport HtmlComponent Real Size Issue Fixed












<![CDATA[]]>


<![CDATA[content]]>


<![CDATA[footNote]]>






<![CDATA["[See sub-section (3) of Section 1]
S.No.Name of DistrictArea
(1)(2)(3)
1.GwaliorGwalior Division
Gwalior Corporation Area
"]]>





<![CDATA[($F{footNote} != null && !$F{footNote}.trim().isEmpty())]]>








<![CDATA[$F{footNote}]]>






Wednesday, May 22, 2013

Apache Lucene Search Word Highlighter use LIKE wildcard




public static String highlightField(String searchField, String searchWord,  String text) throws Exception {
        QueryParser parser = new QueryParser(Version.LUCENE_42, searchField, analyzer);
        parser.setAllowLeadingWildcard(true);
        Query query = parser.parse("*"+searchWord+"*");

        TokenStream tokenStream = analyzer.tokenStream(searchField, new StringReader(text));

        // Assuming "", "" used to highlight
        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("", ""); // #F8B270 - Orange
        //SimpleHTMLFormatter formatter = new SimpleHTMLFormatter();
        MyQueryScorer scorer = new MyQueryScorer(query, searchField, searchField);
        Highlighter highlighter = new Highlighter(formatter, scorer);
        highlighter.setTextFragmenter(new SimpleFragmenter(Integer.MAX_VALUE));

        String rv = highlighter.getBestFragments(tokenStream, text, 1, "");
        //String rv = highlighter.getBestFragments(tokenStream, text, 1, "(FIELD TEXT TRUNCATED)");
        return (rv == null || rv.trim().isEmpty()) ? text : rv;
    }


public static class MyWeightedSpanTermExtractor extends WeightedSpanTermExtractor {

        public MyWeightedSpanTermExtractor() {
            super();
        }

        public MyWeightedSpanTermExtractor(String defaultField) {
            super(defaultField);
        }

        @Override
        protected void extractUnknownQuery(Query query,
                Map terms) throws IOException {
            if (query instanceof CustomQuery) {
                extractWeightedTerms(terms, new TermQuery(((CustomQuery) query).term));
            }
        }
    }

    public static class MyQueryScorer extends QueryScorer {

        public MyQueryScorer(Query query, String field, String defaultField) {
            super(query, field, defaultField);
        }

        @Override
        protected WeightedSpanTermExtractor newTermExtractor(String defaultField) {
            return defaultField == null ? new MyWeightedSpanTermExtractor()
                    : new MyWeightedSpanTermExtractor(defaultField);
        }
    }

    public static class CustomQuery extends Query {

        private final Term term;

        public CustomQuery(Term term) {
            super();
            this.term = term;
        }

        @Override
        public String toString(String field) {
            return new TermQuery(term).toString(field);
        }

        @Override
        public Query rewrite(IndexReader reader) throws IOException {
            return new TermQuery(term);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + ((term == null) ? 0 : term.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            CustomQuery other = (CustomQuery) obj;
            if (term == null) {
                if (other.term != null) {
                    return false;
                }
            } else if (!term.equals(other.term)) {
                return false;
            }
            return true;
        }
    }


Monday, May 20, 2013

GradientJTextPane as TableCellRenderer with HTML Tooltip




public class GradientTextPane extends JTextPane {

    private transient Position.Bias[] bias = new Position.Bias[1];

    /**
     * Creates new form GradientLabel
     */
    public GradientTextPane() {
        initComponents();
    }

    public void setBgColor(Color bgColor) {
        start = bgColor;
        repaint();
    }

    @Override
    public String getToolTipText(MouseEvent e) {
        String title = super.getToolTipText(e);
        JTextPane editor = (JTextPane) e.getSource();
        if (!editor.isEditable()) {
            Point pt = e.getPoint();
            int pos = editor.getUI().viewToModel(editor, pt, bias);
            if (bias[0] == Position.Bias.Backward && pos > 0) {
                pos--;
            }
            if (pos >= 0 && (editor.getDocument() instanceof HTMLDocument)) {
                HTMLDocument hdoc = (HTMLDocument) editor.getDocument();
                Element elem = hdoc.getCharacterElement(pos);
                if (elem != null) {
                    AttributeSet a = elem.getAttributes();
                    //AttributeSet span = (AttributeSet) a.getAttribute(HTML.Tag.SPAN);
                    if (a != null) {
                        title = (String) a.getAttribute(HTML.Attribute.TITLE);
                    }
                }
            }
        }
        return title;
    }

    @Override
    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        colors[0] = start;
        colors[1] = start.brighter();
        colors[2] = start;
        // Create the gradient paint
        //GradientPaint paint = new GradientPaint(0, 0, start, width, height, end, true);

        LinearGradientPaint paint = new LinearGradientPaint(0, 0, 0, getHeight(), dist, colors);

        // we need to cast to Graphics2D for this operation
        Graphics2D g2d = (Graphics2D) g;

        // save the old paint
        Paint oldPaint = g2d.getPaint();

        // set the paint to use for this operation
        g2d.setPaint(paint);

        // fill the background using the paint
        g2d.fillRect(0, 0, width, height);

        // restore the original paint
        g2d.setPaint(oldPaint);

        super.paint(g);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        setOpaque(true);
    }//
//GEN-END:initComponents    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private Color start;
    float[] dist = {0.0f, 0.5f, 1.0f};
    private Color[] colors = new Color[3];
}



Sunday, May 19, 2013

Round Robin in JAVA




  1. package test;  
  2.    
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.    
  7. import junit.framework.TestCase;  
  8.    
  9. class Robin {  
  10. private int i;  
  11.    
  12. public Robin(int i) {  
  13. this.i = i;  
  14. }  
  15.    
  16. public int call() {  
  17. return i;  
  18. }  
  19. }  
  20.    
  21. class RoundRobin {  
  22. private Iterator it;  
  23. private List list;  
  24.    
  25. public RoundRobin(List list) {  
  26. this.list = list;  
  27. it = list.iterator();  
  28. }  
  29.   
  30. public int next() {  
  31. // if we get to the end, start again  
  32. if (!it.hasNext()) {  
  33. it = list.iterator();  
  34. }  
  35. Robin robin = it.next();  
  36.   
  37. return robin.call();  
  38. }  
  39. }  
  40.    
  41. public class RoundRobinTest extends TestCase {  
  42. List items = new ArrayList();  
  43. RoundRobin round;  
  44.   
  45. public void testOne() {  
  46. items.add(new Robin(1));  
  47. round = new RoundRobin(items);  
  48. assertEquals(1, round.next());  
  49. assertEquals(1, round.next());  
  50. }  
  51.   
  52. public void testTwo() {  
  53. items.add(new Robin(1));  
  54. items.add(new Robin(2));  
  55. round = new RoundRobin(items);  
  56. assertEquals(1, round.next());  
  57. assertEquals(2, round.next());  
  58. assertEquals(1, round.next());  
  59. assertEquals(2, round.next());  
  60. }  
  61.   
  62. public void testThree() {  
  63. items.add(new Robin(1));  
  64. items.add(new Robin(2));  
  65. items.add(new Robin(3));  
  66. round = new RoundRobin(items);  
  67. assertEquals(1, round.next());  
  68. assertEquals(2, round.next());  
  69. assertEquals(3, round.next());  
  70. assertEquals(1, round.next());  
  71. assertEquals(2, round.next());  
  72. assertEquals(3, round.next());  
  73. }  
  74. }