-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGanttDataUtil.java
221 lines (198 loc) · 6.72 KB
/
GanttDataUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/****************************************************************************
**
** This demo file is part of yFiles for JavaFX 3.6.
**
** Copyright (c) 2000-2023 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for JavaFX functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for JavaFX version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for JavaFX powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for JavaFX
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
package viewer.ganttchart;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.List;
/**
* Stores a list of tasks in a project schedule and provides methods
* for mapping graph coordinates to dates and tasks (and vice versa).
*/
public class GanttDataUtil {
/**
* The width in the graph coordinate system that corresponds to one day.
*/
public static final int DAY_WIDTH = 80;
/**
* The spacing between tasks.
*/
public static final int TASK_SPACING = 10;
/**
* The spacing between activities.
*/
public static final int ACTIVITY_SPACING = 20;
/**
* The height of an activity.
*/
public static final int ACTIVITY_HEIGHT = 40;
/**
* The date from which we start.
*/
private final LocalDateTime originDate;
private final List<Task> tasks;
private final DateTimeFormatter dMHms;
private final DateTimeFormatter dMy;
/**
* Creates a new {@code GanttDataUtil} instance.
*/
public GanttDataUtil() {
originDate = LocalDateTime.of(LocalDate.parse(Activity.ORIGIN_DATE), LocalTime.MIN);
tasks = new ArrayList<Task>();
dMHms = DateTimeFormatter.ofPattern("d 'of' MMMM HH:mm:ss");
dMy = DateTimeFormatter.ofPattern("d 'of' MMMM yyyy");
}
/**
* Returns a human-readable representation of the given date-time.
*/
public String getDateString( LocalDateTime ldt ) {
return ldt.format(dMy);
}
/**
* Returns a human-readable representation of given date-time that
* corresponds to the given x-coordinate.
*/
public String getDateTimeString( double x ) {
return getDate(x).format(dMHms);
}
/**
* Returns the x-coordinate that corresponds to the given date-time.
*/
public int getX( LocalDateTime date ) {
long hours = originDate.until(date, ChronoUnit.MINUTES) / 60;
long x = getDayOffset(hours) * GanttDataUtil.DAY_WIDTH;
return (int) x;
}
private long getDayOffset( long hours ) {
long rest = hours % 24;
return hours / 24 - (hours > -1 || rest == 0 ? 0 : 1);
}
/**
* Returns the date-time that corresponds to the given x-coordinate.
*/
public LocalDateTime getDate( double x ) {
double duration = x / GanttDataUtil.DAY_WIDTH;
double durationMin = duration * 24 * 60;
return originDate.plusMinutes((long) durationMin);
}
/**
* Returns the number of days or months between the given date and
* {@link #originDate}.
*/
public long untilOriginDate( LocalDateTime date, TemporalUnit temporalUnit ) {
LocalDate origin =
temporalUnit == ChronoUnit.MONTHS
// months until originDate
? originDate.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate()
// days until originDate
: originDate.toLocalDate();
return Math.abs(date.toLocalDate().atTime(LocalTime.MIN).until(
origin.atTime(LocalTime.MIN), temporalUnit));
}
/**
* Returns the y-coordinate for the given activity.
*/
public int getActivityY( Activity activity ) {
Task task = activity.getTask();
int idx = activity.getSubrowIndex();
return getTaskY(task) + idx * ACTIVITY_HEIGHT + (idx + 1) * ACTIVITY_SPACING;
}
/**
* Returns the y-coordinate for the given task.
*/
public int getTaskY( Task task ) {
int y = TASK_SPACING;
for (Task pred : tasks) {
if (pred == task) {
break;
} else {
y += getCompleteTaskHeight(pred) + TASK_SPACING;
}
}
return y;
}
/**
* Gets the task at the given y coordinate.
*/
public Task getTask( double y ) {
int currentY = 0;
for (Task task : tasks) {
currentY += getCompleteTaskHeight(task) + GanttDataUtil.TASK_SPACING;
if (currentY > y) {
return task;
}
}
return tasks.get(tasks.size() - 1);
}
/**
* Returns all tasks for this project schedule.
*/
public List<Task> getTasks() {
return tasks;
}
/**
* Adds a tasks to this project schedule.
*/
public void addTask( Task task ) {
tasks.add(task);
}
/**
* Calculates the height of the given tasks.
*/
public double getCompleteTaskHeight( Task task ) {
int rows = Math.max(1, task.getSubrowCount());
return rows * (GanttDataUtil.ACTIVITY_HEIGHT
+ GanttDataUtil.ACTIVITY_SPACING) + GanttDataUtil.ACTIVITY_SPACING;
}
/**
* Calculates the total activity duration in hours.
*/
public static double getTotalActivityDuration( Activity activity ) {
long duration = activity.getStartDate().until(activity.getEndDate(), ChronoUnit.HOURS);
return (duration + (activity.getLeadTime()) + (activity.getFollowUpTime()));
}
/**
* Calculates the length in world coordinates from the given duration in hours.
*/
public static double hoursToWorldLength( double hours ) {
return hours / 24.0 * GanttDataUtil.DAY_WIDTH;
}
/**
* Calculates the duration in hours from the given length in world coordinates.
*/
public static double worldLengthToHours( double worldLength ) {
return (worldLength * 24 / GanttDataUtil.DAY_WIDTH);
}
}