package timeBench.demo.vis;

import ieg.prefuse.data.DataHelper;

import javax.swing.BorderFactory;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import prefuse.Constants;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.ShapeAction;
import prefuse.action.layout.AxisLabelLayout;
import prefuse.action.layout.AxisLayout;
import prefuse.controls.ToolTipControl;
import prefuse.render.AxisRenderer;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.ShapeRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.InGroupPredicate;
import prefuse.visual.expression.VisiblePredicate;
import prefuse.visual.sort.ItemSorter;
import timeBench.action.layout.TimeAxisLayout;
import timeBench.action.layout.timescale.AdvancedTimeScale;
import timeBench.action.layout.timescale.RangeAdapter;
import timeBench.data.TemporalDataException;
import timeBench.data.TemporalDataset;
import timeBench.ui.TimeAxisDisplay;

/**
 * Simple demo of a dot plot showing a numerical variable over time. 
 * @author Rind
 */
public class DotPlotDemo {

    private static final String LABELS = "ylab";
    private static final String GROUP = "data";
    private static final String DATA_COL = "value";

    /**
     * @param args
     * @throws TemporalDataException
     */
    public static void main(String[] args) throws TemporalDataException {
        TemporalDataset tmpds = DemoEnvironmentFactory
                .generateRandomNumericalInstantData(100, DATA_COL);
        DataHelper.printTable(System.out, tmpds.getNodeTable());

        final Visualization vis = new Visualization();
        final TimeAxisDisplay display = new TimeAxisDisplay(vis);
        // display width must be set before the time scale
        // otherwise the initial layout does not match the display width
        display.setSize(700, 450);

        // --------------------------------------------------------------------
        // STEP 1: setup the visualized data & time scale
        vis.addTable(GROUP, tmpds.getTemporalObjectTable());

        long border = (tmpds.getSup() - tmpds.getInf()) / 20;
        final AdvancedTimeScale timeScale = new AdvancedTimeScale(
                tmpds.getInf() - border, tmpds.getSup() + border,
                display.getWidth() - 1);
        AdvancedTimeScale overviewTimeScale = new AdvancedTimeScale(timeScale);
        RangeAdapter rangeAdapter = new RangeAdapter(overviewTimeScale,
                timeScale);

        timeScale.setAdjustDateRangeOnResize(true);
        timeScale.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                vis.run(DemoEnvironmentFactory.ACTION_UPDATE);
            }
        });

        // --------------------------------------------------------------------
        // STEP 2: set up renderers for the visual data
        ShapeRenderer dotRenderer = new ShapeRenderer(8);
        DefaultRendererFactory rf = new DefaultRendererFactory(dotRenderer);
        rf.add(new InGroupPredicate(LABELS), new AxisRenderer(
                Constants.FAR_LEFT, Constants.CENTER));
        vis.setRendererFactory(rf);

        // --------------------------------------------------------------------
        // STEP 3: create actions to process the visual data

        TimeAxisLayout time_axis = new TimeAxisLayout(GROUP, timeScale);

        AxisLayout y_axis = new AxisLayout(GROUP, DATA_COL, Constants.Y_AXIS,
                VisiblePredicate.TRUE);

        // add value axis labels and horizontal grid lines 
        AxisLabelLayout y_labels = new AxisLabelLayout(LABELS, y_axis);

        // color must be set -> otherwise nothing displayed
        ColorAction color = new ColorAction(GROUP, VisualItem.FILLCOLOR,
                ColorLib.rgb(100, 100, 255));

        ShapeAction shape = new ShapeAction(GROUP, Constants.SHAPE_ELLIPSE);

        // runs on layout updates (e.g., window resize, pan)
        ActionList update = new ActionList();
        update.add(time_axis);
        update.add(y_axis);
        update.add(y_labels);
        update.add(new RepaintAction());
        vis.putAction(DemoEnvironmentFactory.ACTION_UPDATE, update);

        // runs once (at startup)
        ActionList draw = new ActionList();
        draw.add(update);
        draw.add(color);
        draw.add(shape);
        draw.add(new RepaintAction());
        vis.putAction(DemoEnvironmentFactory.ACTION_INIT, draw);

        // --------------------------------------------------------------------
        // STEP 4: set up a display and controls

        // enable anti-aliasing
        display.setHighQuality(true);
        
        // ensure there is space on left for tick mark label (FAR_LEFT setting)
        display.setBorder(BorderFactory.createEmptyBorder(7, 25, 7, 0));

        // ensure (horizontal) grid lines are in back of data items 
        display.setItemSorter(new ItemSorter() {
            public int score(VisualItem item) {
                int score = super.score(item);
                if (item.isInGroup(LABELS))
                    score--;
                return score;
            }
        });

        // show value in tooltip 
        display.addControlListener(new ToolTipControl(DATA_COL));

        // --------------------------------------------------------------------
        // STEP 5: launching the visualization

        DemoEnvironmentFactory env = new DemoEnvironmentFactory("dot plot");
        env.setPaintWeekends(false);
        env.show(display, rangeAdapter);
    }
}

@@ -1323,24 +1323,29 @@
 ing 
-LABELS = %22ylab%22;
+COL_DATA = %22value%22;%0D%0A
 %0D%0A  
@@ -1379,16 +1379,21 @@
 ng GROUP
+_DATA
  = %22data
@@ -1432,29 +1432,41 @@
 ing 
-DATA_COL = %22value%22;%0D%0A
+GROUP_AXIS_LABELS = %22ylab%22;%0D%0A    
 %0D%0A  
@@ -1610,24 +1610,85 @@
 xception %7B%0D%0A
+//        java.util.Locale.setDefault(java.util.Locale.US);%0D%0A
         Temp
@@ -1788,24 +1788,24 @@
 ta(100, 
+COL_
 DATA
-_COL
 );%0D%0A    
@@ -2329,16 +2329,21 @@
 le(GROUP
+_DATA
 , tmpds.
@@ -3370,16 +3370,27 @@
 edicate(
+GROUP_AXIS_
 LABELS),
@@ -3709,16 +3709,21 @@
 ut(GROUP
+_DATA
 , timeSc
@@ -3783,18 +3783,23 @@
 ROUP
-, 
+_
 DATA
-_
+, 
 COL
+_DATA
 , Co
@@ -3974,16 +3974,27 @@
 lLayout(
+GROUP_AXIS_
 LABELS, 
@@ -4115,16 +4115,21 @@
 on(GROUP
+_DATA
 , Visual
@@ -4243,16 +4243,21 @@
 on(GROUP
+_DATA
 , Consta
@@ -5485,16 +5485,27 @@
 InGroup(
+GROUP_AXIS_
 LABELS))
@@ -5690,16 +5690,16 @@
 rol(
+COL_
 DATA
-_COL
 ));%0D

rename constants for groups and columns