2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.descriptiveDataSet
.matrix
;
11 import java
.util
.List
;
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
;
28 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
29 import eu
.etaxonomy
.cdm
.model
.description
.State
;
30 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
37 public class CategoricalChartTooltip
extends ToolTip
{
39 static final String TOOLTIP_NOT_AVAILABLE
= Messages
.CategoricalChartTooltip_TOOLTIP_NOT_AVAILABLE
;
41 private CharacterMatrix matrix
;
43 public CategoricalChartTooltip(CharacterMatrix matrix
) {
44 super(matrix
.getNatTable(), ToolTip
.NO_RECREATE
, false);
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
);
56 protected Composite
createToolTipContentArea(Event event
,
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
);
65 if(histogram
!=null && !histogram
.getStates().isEmpty()){
66 parent
.setLayout(new GridLayout());
67 tooltip
= createTooltip(event
, parent
);
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
);
78 private Composite
createTooltip(Event event
, Composite parent
) {
79 Chart chart
= new Chart(parent
, SWT
.NONE
);
80 chart
.setLayoutData(new GridData(600, 300));
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
);
89 chart
.getTitle().setText(feature
.getLabel());
90 IAxis xAxis
= chart
.getAxisSet().getXAxis(0);
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()];
98 for (State state
: statesList
) {
99 states
[index
] = state
.getLabel();
100 counts
[index
] = histogram
.getCount(state
);
103 xAxis
.setCategorySeries(states
);
104 xAxis
.enableCategory(true);
105 xAxis
.getTitle().setVisible(false);
106 xAxis
.getTick().setTickLabelAngle(25);
108 IAxis yAxis
= chart
.getAxisSet().getYAxis(0);
109 yAxis
.getTitle().setVisible(false);
111 chart
.getLegend().setVisible(false);
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
);
119 chart
.getAxisSet().adjustRange();
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
);
134 if(matrix
.isShowTooltips()
136 && colIndex
>=CharacterMatrix
.LEADING_COLUMN_COUNT
138 && feature
.isSupportsCategoricalData()) {