Project

General

Profile

Download (5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
10

    
11
import java.util.List;
12

    
13
import org.eclipse.jface.window.ToolTip;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.graphics.Point;
16
import org.eclipse.swt.layout.FillLayout;
17
import org.eclipse.swt.layout.GridData;
18
import org.eclipse.swt.layout.GridLayout;
19
import org.eclipse.swt.widgets.Composite;
20
import org.eclipse.swt.widgets.Event;
21
import org.eclipse.swt.widgets.Label;
22
import org.swtchart.Chart;
23
import org.swtchart.IAxis;
24
import org.swtchart.IBarSeries;
25
import org.swtchart.ISeries.SeriesType;
26
import org.swtchart.ISeriesLabel;
27

    
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.description.State;
30
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
31

    
32
/**
33
 * @author pplitzner
34
 * @since Jul 6, 2018
35
 *
36
 */
37
public class CategoricalChartTooltip extends ToolTip {
38

    
39
    static final String TOOLTIP_NOT_AVAILABLE = Messages.CategoricalChartTooltip_TOOLTIP_NOT_AVAILABLE;
40

    
41
    private CharacterMatrix matrix;
42

    
43
    public CategoricalChartTooltip(CharacterMatrix matrix) {
44
        super(matrix.getNatTable(), ToolTip.NO_RECREATE, false);
45
        this.matrix = matrix;
46
    }
47

    
48
    @Override
49
    protected Object getToolTipArea(Event event) {
50
        int col = matrix.getNatTable().getColumnPositionByX(event.x);
51
        int row = matrix.getNatTable().getRowPositionByY(event.y);
52
        return new Point(col, row);
53
    }
54

    
55
    @Override
56
    protected Composite createToolTipContentArea(Event event,
57
            Composite parent) {
58

    
59
        int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
60
        int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
61
        Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
62
        CategoricalDataHistogram histogram = matrix.getFeatureToHistogramMap().get(feature);
63

    
64
        Composite tooltip;
65
        if(histogram!=null && !histogram.getStates().isEmpty()){
66
            parent.setLayout(new GridLayout());
67
            tooltip = createTooltip(event, parent);
68
        }
69
        else{
70
            tooltip = new Composite(parent, SWT.NONE);
71
            tooltip.setLayout(new FillLayout());
72
            Label label = new Label(tooltip, SWT.NONE);
73
            label.setText(TOOLTIP_NOT_AVAILABLE);
74
        }
75
        return tooltip;
76
    }
77

    
78
    private Composite createTooltip(Event event, Composite parent) {
79
        Chart chart = new Chart(parent, SWT.NONE);
80
        chart.setLayoutData(new GridData(600, 300));
81

    
82
        //get histogram for column
83
        int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
84
        int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
85
        Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
86
        CategoricalDataHistogram histogram = matrix.getFeatureToHistogramMap().get(feature);
87

    
88
        // set chart title
89
        chart.getTitle().setText(feature.getLabel());
90
        IAxis xAxis = chart.getAxisSet().getXAxis(0);
91

    
92
        //initalize x and y axis values
93
        List<State> statesList = histogram.getStates();
94
        String[] states = new String[statesList.size()];
95
        double[] counts = new double[statesList.size()];
96

    
97
        int index = 0;
98
        for (State state : statesList) {
99
            states[index] = state.getLabel();
100
            counts[index] = histogram.getCount(state);
101
            index++;
102
        }
103
        xAxis.setCategorySeries(states);
104
        xAxis.enableCategory(true);
105
        xAxis.getTitle().setVisible(false);
106
        xAxis.getTick().setTickLabelAngle(25);
107

    
108
        IAxis yAxis = chart.getAxisSet().getYAxis(0);
109
        yAxis.getTitle().setVisible(false);
110

    
111
        chart.getLegend().setVisible(false);
112

    
113
        IBarSeries barSeries = (IBarSeries) chart.getSeriesSet()
114
            .createSeries(SeriesType.BAR, "state counts"); //$NON-NLS-1$
115
        ISeriesLabel seriesLabel = barSeries.getLabel();
116
        seriesLabel.setVisible(true);
117
        barSeries.setYSeries(counts);
118

    
119
        chart.getAxisSet().adjustRange();
120

    
121
        return chart;
122
    }
123

    
124
    @Override
125
    protected boolean shouldCreateToolTip(Event event) {
126
        //Differentiation between "index" and "position" is important here
127
        //"position" is the current visible index
128
        //"index" refers to the underlying data model
129
        int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
130
        int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
131
        int rowPos = matrix.getNatTable().getRowPositionByY(event.y);
132
        Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
133

    
134
        if(matrix.isShowTooltips()
135
                && rowPos==1
136
                && colIndex>=CharacterMatrix.LEADING_COLUMN_COUNT
137
                && feature!=null
138
                && feature.isSupportsCategoricalData()) {
139
            return true;
140
        }
141
        return false;
142
    }
143

    
144
}
(2-2/21)