Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
DateAxis.java
Go to the documentation of this file.
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2013, Christian Schudt
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  *
25  *
26  */
27 package org.sleuthkit.autopsy.timeline.ui.detailview;
28 
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import javafx.beans.property.ObjectProperty;
33 import javafx.beans.property.ObjectPropertyBase;
34 import javafx.beans.property.ReadOnlyDoubleProperty;
35 import javafx.beans.property.ReadOnlyDoubleWrapper;
36 import javafx.scene.chart.Axis;
37 import org.joda.time.DateTime;
38 import org.joda.time.Interval;
41 
52 final class DateAxis extends Axis<DateTime> {
53 
54  private ObjectProperty<DateTime> lowerBound = new ObjectPropertyBase<DateTime>() {
55  @Override
56  protected void invalidated() {
57  if (!isAutoRanging()) {
58  invalidateRange();
59  requestAxisLayout();
60  }
61  }
62 
63  @Override
64  public Object getBean() {
65  return DateAxis.this;
66  }
67 
68  @Override
69  public String getName() {
70  return "lowerBound"; // NON-NLS
71  }
72  };
73 
79  private DateTime maxDate;
80 
86  private DateTime minDate;
87 
88  private RangeDivisionInfo rangeDivisionInfo;
89 
90  private final ReadOnlyDoubleWrapper tickSpacing = new ReadOnlyDoubleWrapper();
91 
92  private final ObjectProperty<DateTime> upperBound = new ObjectPropertyBase<DateTime>() {
93  @Override
94  protected void invalidated() {
95  if (!isAutoRanging()) {
96  invalidateRange();
97  requestAxisLayout();
98  }
99  }
100 
101  @Override
102  public Object getBean() {
103  return DateAxis.this;
104  }
105 
106  @Override
107  public String getName() {
108  return "upperBound"; // NON-NLS
109  }
110  };
111 
114  DateAxis() {
115  setAutoRanging(false);
116  }
117 
118  @Override
119  public double getDisplayPosition(DateTime date) {
120  final double length = -200 + (getSide().isHorizontal() ? getWidth() : getHeight());
121 
122  // Get the difference between the max and min date.
123  double diff = getUpperBound().getMillis() - getLowerBound().getMillis();
124 
125  // Get the actual range of the visible area.
126  // The minimal date should start at the zero position, that's why we subtract it.
127  double range = length - getZeroPosition();
128 
129  // Then get the difference from the actual date to the min date and divide it by the total difference.
130  // We get a value between 0 and 1, if the date is within the min and max date.
131  double d = (date.getMillis() - getLowerBound().getMillis()) / diff;
132 
133  // Multiply this percent value with the range and add the zero offset.
134  if (getSide().isVertical()) {
135  return getHeight() - d * range + getZeroPosition();
136  } else {
137  return d * range + getZeroPosition();
138  }
139  }
140 
148  public final DateTime getLowerBound() {
149  return lowerBound.get();
150  }
151 
159  public final void setLowerBound(DateTime date) {
160  lowerBound.set(date);
161  }
162 
170  public final DateTime getUpperBound() {
171  return upperBound.get();
172  }
173 
181  public final void setUpperBound(DateTime date) {
182  upperBound.set(date);
183  }
184 
185  @Override
186  public DateTime getValueForDisplay(double displayPosition) {
187  final double length = - 200 + (getSide().isHorizontal() ? getWidth() : getHeight());
188 
189  // Get the difference between the max and min date.
190  double diff = getUpperBound().getMillis() - getLowerBound().getMillis();
191 
192  // Get the actual range of the visible area.
193  // The minimal date should start at the zero position, that's why we subtract it.
194  double range = length - getZeroPosition();
195 
196  if (getSide().isVertical()) {
197  // displayPosition = getHeight() - ((date - lowerBound) / diff) * range + getZero
198  // date = displayPosition - getZero - getHeight())/range * diff + lowerBound
199  return new DateTime((long) ((displayPosition - getZeroPosition() - getHeight()) / -range * diff + getLowerBound().getMillis()), TimeLineController.getJodaTimeZone());
200  } else {
201  // displayPosition = ((date - lowerBound) / diff) * range + getZero
202  // date = displayPosition - getZero)/range * diff + lowerBound
203  return new DateTime((long) ((displayPosition - getZeroPosition()) / range * diff + getLowerBound().getMillis()), TimeLineController.getJodaTimeZone());
204  }
205  }
206 
207  @Override
208  public double getZeroPosition() {
209  return 0;
210  }
211 
212  @Override
213  public void invalidateRange(List<DateTime> list) {
214  super.invalidateRange(list);
215 
216  Collections.sort(list);
217  if (list.isEmpty()) {
218  minDate = maxDate = new DateTime();
219  } else if (list.size() == 1) {
220  minDate = maxDate = list.get(0);
221  } else if (list.size() > 1) {
222  minDate = list.get(0);
223  maxDate = list.get(list.size() - 1);
224  }
225  }
226 
227  @Override
228  public boolean isValueOnAxis(DateTime date) {
229  return date.getMillis() > getLowerBound().getMillis() && date.getMillis() < getUpperBound().getMillis();
230  }
231 
232  @Override
233  public double toNumericValue(DateTime date) {
234  return date.getMillis();
235  }
236 
237  @Override
238  public DateTime toRealValue(double v) {
239  return new DateTime((long) v);
240  }
241 
242  @Override
243  protected Object autoRange(double length) {
244  if (isAutoRanging()) {
245  return new Interval(minDate, maxDate);
246  } else {
247  if (getLowerBound() == null || getUpperBound() == null) {
248  return null;
249  }
250  return getRange();
251  }
252  }
253 
254  @Override
255  protected List<DateTime> calculateTickValues(double length, Object range) {
256  List<DateTime> tickDates = new ArrayList<>();
257  if (range == null) {
258  return tickDates;
259  }
260  rangeDivisionInfo = RangeDivisionInfo.getRangeDivisionInfo((Interval) range);
261  final DateTime lowerBound1 = getLowerBound();
262  final DateTime upperBound1 = getUpperBound();
263 
264  if (lowerBound1 == null || upperBound1 == null) {
265  return tickDates;
266  }
267  DateTime lower = lowerBound1.withZone(TimeLineController.getJodaTimeZone());
268  DateTime upper = upperBound1.withZone(TimeLineController.getJodaTimeZone());
269 
270  DateTime current = lower;
271  // Loop as long we exceeded the upper bound.
272  while (current.isBefore(upper)) {
273  tickDates.add(current);
274  current = current.plus(rangeDivisionInfo.getPeriodSize().getPeriod());//.add(interval.interval, interval.amount);
275  }
276 
277  // At last add the upper bound.
278  tickDates.add(upper);
279 
280  // If there are at least three dates, check if the gap between the lower date and the second date is at least half the gap of the second and third date.
281  // Do the same for the upper bound.
282  // If gaps between dates are to small, remove one of them.
283  // This can occur, e.g. if the lower bound is 25.12.2013 and years are shown. Then the next year shown would be 2014 (01.01.2014) which would be too narrow to 25.12.2013.
284  if (tickDates.size() > 2) {
285  DateTime secondDate = tickDates.get(1);
286  DateTime thirdDate = tickDates.get(2);
287  DateTime lastDate = tickDates.get(tickDates.size() - 2);
288  DateTime previousLastDate = tickDates.get(tickDates.size() - 3);
289 
290  // If the second date is too near by the lower bound, remove it.
291  if (secondDate.getMillis() - lower.getMillis() < (thirdDate.getMillis() - secondDate.getMillis()) / 2) {
292  tickDates.remove(lower);
293  }
294 
295  // If difference from the upper bound to the last date is less than the half of the difference of the previous two dates,
296  // we better remove the last date, as it comes to close to the upper bound.
297  if (upper.getMillis() - lastDate.getMillis() < (lastDate.getMillis() - previousLastDate.getMillis()) / 2) {
298  tickDates.remove(lastDate);
299  }
300  }
301 
302  if (tickDates.size() >= 2) {
303  tickSpacing.set(getDisplayPosition(tickDates.get(1)) - getDisplayPosition(tickDates.get(0)));
304  } else if (tickDates.size() >= 4) {
305  tickSpacing.set(getDisplayPosition(tickDates.get(2)) - getDisplayPosition(tickDates.get(1)));
306  }
307  return tickDates;
308  }
309 
310  @Override
311  protected Object getRange() {
312 
313  return new Interval(getLowerBound(), getUpperBound());
314  }
315 
316  @Override
317  protected String getTickMarkLabel(DateTime date) {
318  return rangeDivisionInfo.getTickFormatter().print(date);
319  }
320 
321  @Override
322  protected void layoutChildren() {
323  super.layoutChildren();
324  }
325 
326  @Override
327  protected void setRange(Object range, boolean animating) {
328 
329  rangeDivisionInfo = RangeDivisionInfo.getRangeDivisionInfo((Interval) range);
330 
331  setLowerBound(new DateTime(rangeDivisionInfo.getLowerBound()));
332  setUpperBound(new DateTime(rangeDivisionInfo.getUpperBound()));
333  }
334 
335  ReadOnlyDoubleProperty getTickSpacing() {
336  return tickSpacing.getReadOnlyProperty();
337  }
338 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.