1 | package de.uka.ipd.sdq.sensorframework.visualisation.jfreechartvisualisation.editor; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.List; |
6 | import java.util.Properties; |
7 | |
8 | import org.eclipse.swt.SWT; |
9 | import org.eclipse.swt.layout.GridData; |
10 | import org.eclipse.swt.layout.GridLayout; |
11 | import org.eclipse.swt.layout.RowData; |
12 | import org.eclipse.swt.layout.RowLayout; |
13 | import org.eclipse.swt.widgets.Composite; |
14 | import org.eclipse.swt.widgets.Event; |
15 | import org.eclipse.swt.widgets.Label; |
16 | import org.eclipse.swt.widgets.Listener; |
17 | import org.eclipse.swt.widgets.Text; |
18 | |
19 | import de.uka.ipd.sdq.codegen.simudatavisualisation.datatypes.Histogram; |
20 | import de.uka.ipd.sdq.sensorframework.adapter.DataAdapter; |
21 | import de.uka.ipd.sdq.sensorframework.adapter.TimeSpanToHistogramAdapter; |
22 | import de.uka.ipd.sdq.sensorframework.visualisation.IVisualisation; |
23 | import de.uka.ipd.sdq.sensorframework.visualisation.editor.AbstractReportView; |
24 | import de.uka.ipd.sdq.sensorframework.visualisation.jfreechartvisualisation.AbstractJFreeChartWidthViewer; |
25 | |
26 | /**Report for Histograms. |
27 | * Provides the basic possibilities to generate histograms and change their classes width. |
28 | * @author groenda |
29 | */ |
30 | public abstract class AbstractJFreeChartWidthReport extends AbstractReportView implements IVisualisation<Histogram>{ |
31 | |
32 | AbstractJFreeChartWidthViewer viewer; |
33 | protected Text widthInput; |
34 | protected double histogramWidth = Double.NaN; |
35 | private List<DataAdapter> adapterList = null; |
36 | |
37 | public AbstractJFreeChartWidthReport() { |
38 | super(); |
39 | } |
40 | |
41 | protected abstract AbstractJFreeChartWidthViewer createViewer(Composite parent, int flags); |
42 | |
43 | /** {@inheritDoc} |
44 | */ |
45 | @Override |
46 | protected void createReportControls(Composite parent) { |
47 | GridLayout gridLayout = new GridLayout(); |
48 | gridLayout.numColumns = 1; |
49 | parent.setLayout(gridLayout); |
50 | Composite histogramWidthPanel = new Composite(parent, SWT.NONE); |
51 | GridData data = new GridData(); |
52 | data.grabExcessHorizontalSpace = true; |
53 | histogramWidthPanel.setLayoutData(data); |
54 | RowLayout rowLayout = new RowLayout(); |
55 | rowLayout.wrap = false; |
56 | rowLayout.pack = true; |
57 | histogramWidthPanel.setLayout(rowLayout); |
58 | Label label = new Label(histogramWidthPanel, SWT.CENTER); |
59 | label.setText("Sampling Rate"); |
60 | label.setLayoutData(new RowData(SWT.DEFAULT, SWT.DEFAULT)); |
61 | widthInput = new Text(histogramWidthPanel, SWT.BORDER); |
62 | widthInput.setText(Double.toString(histogramWidth)); |
63 | widthInput.setLayoutData(new RowData(60, SWT.DEFAULT)); |
64 | Listener listener = new Listener() { |
65 | |
66 | public void handleEvent(Event event) { |
67 | switch (event.type) { |
68 | case SWT.KeyDown: |
69 | if (event.character == SWT.CR) |
70 | updateHistogramWidth(Double.parseDouble(widthInput |
71 | .getText())); |
72 | break; |
73 | case SWT.FocusOut: |
74 | updateHistogramWidth(Double.parseDouble(widthInput |
75 | .getText())); |
76 | break; |
77 | } |
78 | } |
79 | |
80 | }; |
81 | widthInput.addListener(SWT.KeyDown, listener); |
82 | widthInput.addListener(SWT.FocusOut, listener); |
83 | |
84 | viewer = createViewer(parent, SWT.NONE); |
85 | GridData data2 = new GridData(); |
86 | data2.grabExcessHorizontalSpace = true; |
87 | data2.grabExcessVerticalSpace = true; |
88 | data2.verticalAlignment = GridData.FILL; |
89 | data2.horizontalAlignment = GridData.FILL; |
90 | viewer.setLayoutData(data2); |
91 | histogramWidthPanel.setLayoutData(data); |
92 | } |
93 | |
94 | /**Updates the histogram width after a change on the gui. |
95 | * @param newWidth The new width to set. |
96 | */ |
97 | private void updateHistogramWidth(double newWidth) { |
98 | this.histogramWidth = newWidth; |
99 | |
100 | setIgnoreDataSettingsChanged(true); |
101 | // Set new histogram width on all adapters of this report |
102 | for (DataAdapter adapter : adapterList) { |
103 | Properties adapterProperties = adapter.getProperties(); |
104 | adapterProperties.put(TimeSpanToHistogramAdapter.HISTOGRAM_WIDTH, |
105 | histogramWidth); |
106 | adapter.setProperties(adapterProperties); |
107 | } |
108 | setIgnoreDataSettingsChanged(false); |
109 | generateVisualization(adapterList); |
110 | } |
111 | |
112 | /** {@inheritDoc} |
113 | */ |
114 | public void setInput(Collection<Histogram> histograms) { |
115 | viewer.setData(histograms); |
116 | this.widthInput.setText(""+histogramWidth); |
117 | } |
118 | |
119 | /** {@inheritDoc} |
120 | */ |
121 | @Override |
122 | protected void generateVisualization(List<DataAdapter> newList) { |
123 | if (newList == null || newList.isEmpty()) |
124 | throw new RuntimeException("Histogram reports must have at least one data input."); |
125 | Properties adapterProperties = null; |
126 | // Determine width of "old" histogram, if existing |
127 | double newWidth = Double.NaN; |
128 | if (adapterList != null && adapterList.isEmpty() == false) { |
129 | DataAdapter adapter = adapterList.get(0); |
130 | adapterProperties = adapter.getProperties(); |
131 | Double test = (Double) adapterProperties.get(TimeSpanToHistogramAdapter.HISTOGRAM_WIDTH); |
132 | newWidth = test; |
133 | } else { |
134 | // this is the first data added |
135 | DataAdapter adapter = newList.get(0); |
136 | adapterProperties = adapter.getProperties(); |
137 | newWidth = (Double) adapterProperties.get(TimeSpanToHistogramAdapter.HISTOGRAM_WIDTH); |
138 | } |
139 | |
140 | boolean allSameWidth = isSameWidth(newList); |
141 | |
142 | /* Set all histograms to the same histogram width and add histograms |
143 | * to the current view. |
144 | */ |
145 | ArrayList<Histogram> newHistogramList = new ArrayList<Histogram>(); |
146 | for (DataAdapter adapter : newList) { |
147 | if (!allSameWidth) { |
148 | adapterProperties = adapter.getProperties(); |
149 | adapterProperties.put(TimeSpanToHistogramAdapter.HISTOGRAM_WIDTH, newWidth); |
150 | // Note: The property change will notify all listeners, so be careful |
151 | adapter.setProperties(adapterProperties); |
152 | } |
153 | newHistogramList.add((Histogram) adapter.getAdaptedObject()); |
154 | } |
155 | histogramWidth = newWidth; // set view-local cached version to new value |
156 | adapterList = newList; |
157 | |
158 | this.setInput(newHistogramList); |
159 | } |
160 | |
161 | /**Checks if the adapters all have the same histogram width. |
162 | * @param adapterList The list of adapters to check. |
163 | * @return <code>true</code> if all have the same width. |
164 | */ |
165 | private boolean isSameWidth(List<DataAdapter> adapterList) { |
166 | Properties adapterProperties; |
167 | boolean allSameWidth = true; |
168 | double width = Double.NaN; |
169 | double currentWidth = Double.NaN; |
170 | for (DataAdapter adapter : adapterList) { |
171 | adapterProperties = adapter.getProperties(); |
172 | currentWidth = (Double) adapterProperties.get(TimeSpanToHistogramAdapter.HISTOGRAM_WIDTH); |
173 | if (Double.isNaN(width)) { |
174 | width = currentWidth; |
175 | } |
176 | if (width != currentWidth) { |
177 | allSameWidth = false; |
178 | } |
179 | } |
180 | return allSameWidth; |
181 | } |
182 | |
183 | |
184 | /** {@inheritDoc} |
185 | */ |
186 | @Override |
187 | public void setFocus() { |
188 | viewer.setFocus(); |
189 | } |
190 | |
191 | /** {@inheritDoc} |
192 | */ |
193 | public void addInput(Collection<Histogram> c) { |
194 | // The implementation is not necessary. |
195 | } |
196 | |
197 | /** {@inheritDoc} |
198 | */ |
199 | public void deleteInput(Collection<Histogram> c) { |
200 | // The implementation is not necessary. |
201 | } |
202 | } |