~ubuntu-branches/ubuntu/natty/libswingx-java/natty

« back to all changes in this revision

Viewing changes to src/test/org/jdesktop/swingx/renderer/PainterVisualCheck.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-07-26 12:11:27 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100726121127-k0d3b21nhja0dn93
Tags: 1:1.6.1-1
* New upstream release.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.1: no changes needed.
* Drop Depends on JRE: not requested anymore by new Java Policy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: PainterVisualCheck.java 3513 2009-09-22 11:18:09Z kleopatra $
 
2
 * $Id: PainterVisualCheck.java 3650 2010-04-08 15:16:11Z kleopatra $
3
3
 *
4
4
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
45
45
 
46
46
import javax.swing.Action;
47
47
import javax.swing.DefaultListModel;
48
 
import javax.swing.JCheckBox;
49
 
import javax.swing.JComponent;
50
48
import javax.swing.JLabel;
51
49
import javax.swing.JTable;
52
50
import javax.swing.KeyStroke;
61
59
import org.jdesktop.swingx.JXTable;
62
60
import org.jdesktop.swingx.JXTree;
63
61
import org.jdesktop.swingx.action.AbstractActionExt;
64
 
import org.jdesktop.swingx.action.ActionContainerFactory;
 
62
import org.jdesktop.swingx.color.ColorUtil;
65
63
import org.jdesktop.swingx.decorator.ComponentAdapter;
66
64
import org.jdesktop.swingx.decorator.HighlightPredicate;
67
65
import org.jdesktop.swingx.decorator.Highlighter;
69
67
import org.jdesktop.swingx.decorator.PainterHighlighter;
70
68
import org.jdesktop.swingx.decorator.HighlightPredicate.ColumnHighlightPredicate;
71
69
import org.jdesktop.swingx.decorator.HighlightPredicate.NotHighlightPredicate;
72
 
import org.jdesktop.swingx.painter.AbstractLayoutPainter;
73
70
import org.jdesktop.swingx.painter.AbstractPainter;
74
71
import org.jdesktop.swingx.painter.BusyPainter;
75
72
import org.jdesktop.swingx.painter.ImagePainter;
79
76
import org.jdesktop.swingx.painter.AbstractLayoutPainter.HorizontalAlignment;
80
77
import org.jdesktop.swingx.painter.AbstractLayoutPainter.VerticalAlignment;
81
78
import org.jdesktop.swingx.painter.effects.InnerGlowPathEffect;
 
79
import org.jdesktop.swingx.renderer.RelativePainterHighlighter.NumberRelativizer;
 
80
import org.jdesktop.swingx.renderer.RelativePainterHighlighter.RelativePainter;
 
81
import org.jdesktop.swingx.sort.DefaultSortController;
82
82
import org.jdesktop.swingx.table.ColumnControlButton;
83
83
import org.jdesktop.swingx.test.XTestUtils;
84
84
import org.jdesktop.swingx.treetable.FileSystemModel;
103
103
//      setSystemLF(true);
104
104
      PainterVisualCheck test = new PainterVisualCheck();
105
105
      try {
106
 
        test.runInteractiveTests();
107
 
//         test.runInteractiveTests("interactive.*Gradient.*");
 
106
//        test.runInteractiveTests();
 
107
         test.runInteractiveTests("interactive.*ValueBasedG.*");
108
108
//         test.runInteractiveTests("interactive.*Icon.*");
109
109
//        test.runInteractiveTests("interactive.*Animated.*");
110
110
      } catch (Exception e) {
297
297
     * Use highlighter with image painter which is positioned relative to 
298
298
     * cell value. 
299
299
     */
300
 
    public void interactivePositionedIconPainterHighlight()  {
 
300
    public void interactiveValueBasedRelativePainterHighlight()  {
301
301
        TableModel model = new AncientSwingTeam();
302
302
        JXTable table = new JXTable(model);
303
303
        final ImagePainter imagePainter = new ImagePainter(XTestUtils.loadDefaultImage("green-orb.png"));
304
304
        imagePainter.setHorizontalAlignment(HorizontalAlignment.RIGHT);
305
305
        imagePainter.setAreaEffects(new InnerGlowPathEffect());
306
 
        ValueBasedPositionalHighlighter iconHighlighter = new ValueBasedPositionalHighlighter();
307
 
        iconHighlighter.setDelegatePainter(imagePainter);
 
306
        RelativePainterHighlighter iconHighlighter = new RelativePainterHighlighter(imagePainter);
308
307
        iconHighlighter.setHorizontalAlignment(HorizontalAlignment.LEFT);
 
308
        iconHighlighter.setRelativizer(new NumberRelativizer(100));
309
309
        table.getColumnExt(3).addHighlighter(iconHighlighter);
310
310
        // re-use component controller and highlighter in a JXList
311
311
        JXList list = new JXList(createListNumberModel(), true);
312
312
        list.setCellRenderer(new DefaultListRenderer(new LabelProvider(JLabel.RIGHT)));
313
313
        list.addHighlighter(iconHighlighter);
 
314
        list.setComparator(DefaultSortController.COMPARABLE_COMPARATOR);
314
315
        list.toggleSortOrder();
315
316
        showWithScrollingInFrame(table, list, 
316
 
                "value-based image position highlighting");
 
317
            "value-based image position (with relativePainterHighlighter)");
317
318
    }
318
 
 
 
319
    
319
320
    
320
321
    
321
322
    /**
322
 
     * Use highlighter with image painter which is positioned relative to 
323
 
     * cell value. 
 
323
     * Use highlighter with image painter which is marching across the 
 
324
     * cell range (same for all, independent of value).
324
325
     */
325
326
    public void interactiveAnimatedIconPainterHighlight()  {
326
327
        TableModel model = new AncientSwingTeam();
533
534
     * with SwingX extended default renderer. Shared by table and list with
534
535
     * striping.
535
536
     */
536
 
    public void interactiveNumberProportionalGradientHighlightPlusStriping() {
 
537
    public void interactiveValueBasedGradientHighlightPlusStriping() {
537
538
        TableModel model = new AncientSwingTeam();
538
539
        JXTable table = new JXTable(model);
539
540
        ComponentProvider<JLabel> controller = new LabelProvider(
540
541
                JLabel.RIGHT) ;
541
 
        final ValueBasedGradientHighlighter gradientHighlighter = createTransparentGradientHighlighter();
542
 
        gradientHighlighter.setHorizontalAlignment(HorizontalAlignment.RIGHT);
 
542
        RelativePainterHighlighter gradientHighlighter = 
 
543
            createRelativeGradientHighlighter(HorizontalAlignment.RIGHT, 100);
 
544
        table.addHighlighter(gradientHighlighter);
543
545
        Highlighter alternateRowHighlighter = HighlighterFactory.createSimpleStriping();
544
546
        table.addHighlighter(alternateRowHighlighter);
545
547
        table.addHighlighter(gradientHighlighter);
554
556
        addStatusMessage(frame,
555
557
                "uses a PainterAwareLabel in renderer");
556
558
        // crude binding to play with options - the factory is incomplete...
557
 
        addStatusComponent(frame, createTransparencyToggle(gradientHighlighter));
558
 
        show(frame);
 
559
//        addStatusComponent(frame, createTransparencyToggle(gradientHighlighter));
 
560
//        show(frame);
559
561
    }
560
562
 
561
563
    /**
563
565
     * with SwingX extended default renderer. Shared by table and list with
564
566
     * background color.
565
567
     */
566
 
    public void interactiveNumberProportionalGradientHighlight() {
 
568
    public void interactiveValueBasedGradientHighlight() {
567
569
        TableModel model = new AncientSwingTeam();
568
570
        JXTable table = new JXTable(model);
569
571
        table.setBackground(HighlighterFactory.LEDGER);
571
573
                JLabel.RIGHT);
572
574
//        table.setDefaultRenderer(Number.class, new DefaultTableRenderer(
573
575
//                controller));
574
 
        ValueBasedGradientHighlighter gradientHighlighter = createTransparentGradientHighlighter();
575
 
        gradientHighlighter.setHorizontalAlignment(HorizontalAlignment.RIGHT);
 
576
        RelativePainterHighlighter gradientHighlighter = 
 
577
            createRelativeGradientHighlighter(HorizontalAlignment.RIGHT, 100);
576
578
        table.addHighlighter(gradientHighlighter);
 
579
        
577
580
        // re-use component controller and highlighter in a JXList
578
581
        JXList list = new JXList(createListNumberModel(), true);
579
582
        list.setBackground(table.getBackground());
581
584
        list.addHighlighter(gradientHighlighter);
582
585
        list.toggleSortOrder();
583
586
        JXFrame frame = wrapWithScrollingInFrame(table, list,
584
 
                "transparent value relative highlighting");
585
 
        addStatusMessage(frame,
586
 
                "uses the default painter-aware label in renderer");
 
587
            "transparent value relative highlighting (with RelativePH)");
 
588
//        addStatusMessage(frame,
 
589
//        "uses the default painter-aware label in renderer");
587
590
        // crude binding to play with options - the factory is incomplete...
588
 
        addStatusComponent(frame, createTransparencyToggle(gradientHighlighter));
 
591
//        addStatusComponent(frame, createTransparencyToggle(gradientHighlighter));
589
592
        show(frame);
590
593
    }
591
 
 
592
 
    //--------- hack around missing size proportional painters
593
 
    
594
 
    public static class RelativePainter<T> extends AbstractLayoutPainter<T> {
595
 
 
596
 
        private Painter<? super T> painter;
597
 
        private double xFactor;
598
 
        private double yFactor;
599
 
 
600
 
        public RelativePainter() {
601
 
            this(null);
602
 
        }
603
 
        
604
 
        public void setPainter(Painter<? super T> painter) {
605
 
            Object old = getPainter();
606
 
            this.painter = painter;
607
 
            firePropertyChange("painter", old, getPainter());
608
 
        }
609
 
        
610
 
        public Painter<? super T> getPainter() {
611
 
            return painter;
612
 
        }
613
 
        
614
 
        public RelativePainter(Painter<? super T> delegate) {
615
 
            this.painter = delegate;
616
 
        }
617
 
        
618
 
        public void setXFactor(double xPercent) {
619
 
            double old = getXFactor();
620
 
            this.xFactor = xPercent;
621
 
            firePropertyChange("xFactor", old, getXFactor());
622
 
        }
623
 
        
624
 
        /**
625
 
         * @return
626
 
         */
627
 
        private double getXFactor() {
628
 
            return xFactor;
629
 
        }
630
 
 
631
 
        public void setYFactor(double yPercent) {
632
 
            this.yFactor = yPercent;
633
 
        }
634
 
        @Override
635
 
        protected void doPaint(Graphics2D g, T object, int width, int height) {
636
 
            if (painter == null) return;
637
 
            // use epsilon
638
 
            if (xFactor != 0.0) {
639
 
                int oldWidth = width;
640
 
                width = (int) (xFactor * width);
641
 
                if (getHorizontalAlignment() == HorizontalAlignment.RIGHT) {
642
 
                    g.translate(oldWidth - width, 0);
643
 
                }
644
 
            }
645
 
            if (yFactor != 0.0) {
646
 
                int oldHeight = height;
647
 
                height = (int) (yFactor * height);
648
 
                if (getVerticalAlignment() == VerticalAlignment.BOTTOM) {
649
 
                    g.translate(0, oldHeight - height);
650
 
                }
651
 
            }
652
 
            
653
 
            painter.paint(g, object, width, height);
654
 
        }
655
 
        
656
 
    }
657
 
    // -------------------- Value-based transparent gradient highlighter
658
 
 
659
 
    /**
660
 
     * A Highlighter which applies a value-proportional gradient to PainterAware
661
 
     * rendering components if the value is a Number. The gradient is a simple
662
 
     * red to red.brigther paint. The red can be toggled to
663
 
     * half-transparent.<p>
664
 
     * 
665
 
     */
666
 
    public static class ValueBasedGradientHighlighter extends
667
 
            PainterHighlighter {
668
 
        float maxValue = 100;
669
 
 
670
 
        private boolean yellowTransparent;
671
 
 
672
 
        public ValueBasedGradientHighlighter() {
673
 
            this(false);
674
 
        }
675
 
        
676
 
        /**
677
 
         * @param b
678
 
         */
679
 
        public ValueBasedGradientHighlighter(boolean b) {
680
 
            super(new RelativePainter<JComponent>());
681
 
            setYellowTransparent(b);
682
 
        }
683
 
        
684
 
        /**
685
 
         * Overridden to do nothing if it's not a RelativePainter. We 
686
 
         * roll our own.
687
 
         */
688
 
        @Override
689
 
        public void setPainter(Painter painter) {
690
 
            if (!(painter instanceof RelativePainter)) return; 
691
 
            super.setPainter(painter);
692
 
        }
693
 
 
694
 
        @Override
695
 
        public RelativePainter getPainter() {
696
 
            return (RelativePainter) super.getPainter();
697
 
        }
698
 
 
699
 
        @Override
700
 
        protected Component doHighlight(Component renderer,
701
 
                ComponentAdapter adapter) {
702
 
            float end = getEndOfGradient((Number) adapter.getValue());
703
 
            if (end > 1) {
704
 
                renderer.setBackground(Color.RED.darker());
705
 
                renderer.setForeground(Color.WHITE);
706
 
            } else if (end > 0.02) {
707
 
                Painter painter = getPainter(end);
708
 
                ((PainterAware) renderer).setPainter(painter);
709
 
            }
710
 
            return renderer;
711
 
        }
712
 
 
713
 
        
714
 
        @Override
715
 
        protected boolean canHighlight(Component component,
716
 
                ComponentAdapter adapter) {
717
 
            return (adapter.getValue() instanceof Number) && 
718
 
                super.canHighlight(component, adapter);
719
 
        }
720
 
 
721
 
        /**
722
 
         * @param yellowTransparent
723
 
         */
724
 
        public void setYellowTransparent(boolean yellowTransparent) {
725
 
            if (this.yellowTransparent == yellowTransparent) return;
726
 
            this.yellowTransparent = yellowTransparent;
727
 
            getPainter().setPainter(null);
728
 
            fireStateChanged();
729
 
        }
730
 
 
731
 
        public void setHorizontalAlignment(HorizontalAlignment align) {
732
 
            getPainter().setHorizontalAlignment(align);
733
 
            fireStateChanged();
734
 
        }
735
 
        
736
 
        public HorizontalAlignment getHorizontalAlignment() {
737
 
            return getPainter().getHorizontalAlignment();
738
 
        }
739
 
        
740
 
        private Painter getPainter(float end) {
741
 
            if (getPainter().getPainter() == null) {
742
 
                Color startColor = getTransparentColor(Color.RED,
743
 
                        yellowTransparent ? 125 : 254);
744
 
                Color endColor = getTransparentColor(Color.RED.brighter(), 0);
745
 
                boolean isRightAligned = HorizontalAlignment.RIGHT == getHorizontalAlignment();
746
 
                GradientPaint paint = new GradientPaint(new Point2D.Double(0, 0),
747
 
                        isRightAligned ? endColor : startColor, 
748
 
                        new Point2D.Double(100, 0), 
749
 
                        isRightAligned ? startColor : endColor);
750
 
                MattePainter painter = new MattePainter(paint);
751
 
                painter.setPaintStretched(true);
752
 
                getPainter().setPainter(painter);
753
 
            } 
754
 
            getPainter().setXFactor(end);
755
 
            return getPainter();
756
 
        }
757
 
 
758
 
 
759
 
        private Color getTransparentColor(Color base, int transparency) {
760
 
            return new Color(base.getRed(), base.getGreen(), base.getBlue(),
761
 
                    transparency);
762
 
        }
763
 
 
764
 
        private float getEndOfGradient(Number number) {
765
 
            float end = number.floatValue() / maxValue;
766
 
            return end;
767
 
        }
768
 
       
769
 
    }
770
 
 
771
 
    
772
 
    
773
 
    /**
774
 
     * A Highlighter which applies a value-proportional gradient to PainterAware
775
 
     * rendering components if the value is a Number. The gradient is a simple
776
 
     * red to red.brigther paint. The red can be toggled to
777
 
     * half-transparent.<p>
778
 
     * 
779
 
     */
780
 
    public static class ValueBasedPositionalHighlighter extends
781
 
            PainterHighlighter {
782
 
        float maxValue = 100;
783
 
 
784
 
        public ValueBasedPositionalHighlighter() {
785
 
            super(new RelativePainter<JComponent>());
786
 
        }
787
 
        
788
 
        /**
789
 
         * Overridden to do nothing if it's not a RelativePainter. We 
790
 
         * roll our own.
791
 
         */
792
 
        @Override
793
 
        public void setPainter(Painter painter) {
794
 
            if (!(painter instanceof RelativePainter)) return; 
795
 
            super.setPainter(painter);
796
 
        }
797
 
        
798
 
        @Override
799
 
        public RelativePainter getPainter() {
800
 
            return (RelativePainter) super.getPainter();
801
 
        }
802
 
 
803
 
        public void setDelegatePainter(Painter painter) {
804
 
            getPainter().setPainter(painter);
805
 
        }
806
 
        
807
 
        @Override
808
 
        protected Component doHighlight(Component renderer,
809
 
                ComponentAdapter adapter) {
810
 
            float end = getEndOfGradient((Number) adapter.getValue());
811
 
            if (end > 1) {
812
 
                renderer.setBackground(Color.RED.darker());
813
 
                renderer.setForeground(Color.WHITE);
814
 
            } else if (end > 0.02) {
815
 
                Painter painter = getPainter(end);
816
 
                ((PainterAware) renderer).setPainter(painter);
817
 
            }
818
 
            return renderer;
819
 
        }
820
 
 
821
 
        @Override
822
 
        protected boolean canHighlight(Component component,
823
 
                ComponentAdapter adapter) {
824
 
            return (adapter.getValue() instanceof Number) && 
825
 
                super.canHighlight(component, adapter);
826
 
        }
827
 
 
828
 
        public void setHorizontalAlignment(HorizontalAlignment align) {
829
 
            getPainter().setHorizontalAlignment(align);
830
 
            fireStateChanged();
831
 
        }
832
 
        
833
 
        public HorizontalAlignment getHorizontalAlignment() {
834
 
            return getPainter().getHorizontalAlignment();
835
 
        }
836
 
        
837
 
        private Painter getPainter(float end) {
838
 
            getPainter().setXFactor(end);
839
 
            return getPainter();
840
 
        }
841
 
        private float getEndOfGradient(Number number) {
842
 
            float end = number.floatValue() / maxValue;
843
 
            return end;
844
 
        }
845
 
        
846
 
    }
847
 
 
848
 
    /**
849
 
     * creates and returns a highlighter with a value-based transparent gradient
850
 
     * if the cell content type is a Number.
851
 
     * 
852
 
     * @return
853
 
     */
854
 
    private ValueBasedGradientHighlighter createTransparentGradientHighlighter() {
855
 
        return new ValueBasedGradientHighlighter();
856
 
    }
857
 
 
858
 
    /**
859
 
     * Creates and returns a checkbox to toggle the gradient's yellow
860
 
     * transparency.
861
 
     * 
862
 
     * @param gradientHighlighter
863
 
     * @return
864
 
     */
865
 
    private JCheckBox createTransparencyToggle(
866
 
            final ValueBasedGradientHighlighter gradientHighlighter) {
867
 
        ActionContainerFactory factory = new ActionContainerFactory();
868
 
        // toggle opaque optimatization
869
 
        AbstractActionExt toggleTransparent = new AbstractActionExt(
870
 
                "yellow transparent") {
871
 
 
872
 
            public void actionPerformed(ActionEvent e) {
873
 
                gradientHighlighter.setYellowTransparent(isSelected());
874
 
            }
875
 
 
876
 
        };
877
 
        toggleTransparent.setStateAction();
878
 
        JCheckBox box = new JCheckBox();
879
 
        factory.configureButton(box, toggleTransparent, null);
880
 
        return box;
881
 
    }
 
594
    
 
595
    /**
 
596
     * @param right
 
597
     * @return
 
598
     */
 
599
    private RelativePainterHighlighter createRelativeGradientHighlighter(
 
600
            HorizontalAlignment right, Number max) {
 
601
        Color startColor = ColorUtil.setAlpha(Color.RED, 130);
 
602
        Color endColor = ColorUtil.setAlpha(Color.RED.brighter(), 0);
 
603
        boolean isRightAligned = HorizontalAlignment.RIGHT == right;
 
604
        GradientPaint paint = new GradientPaint(new Point2D.Double(0, 0),
 
605
                isRightAligned ? endColor : startColor, 
 
606
                new Point2D.Double(100, 0), 
 
607
                isRightAligned ? startColor : endColor);
 
608
        MattePainter painter = new MattePainter(paint);
 
609
        painter.setPaintStretched(true);
 
610
        RelativePainterHighlighter p = new RelativePainterHighlighter(painter);
 
611
        p.setHorizontalAlignment(right);
 
612
        p.setRelativizer(new NumberRelativizer(max));
 
613
        return p;
 
614
    }
 
615
 
882
616
 
883
617
//----------------- Utility    
884
618
    /**