Normand Briere
2018-07-07 e416acb9b012b17d1efe49ad2199ea7132d874d1
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package timeflow.vis.timeline;
 
import timeflow.data.db.*;
import timeflow.data.db.filter.*;
import timeflow.data.time.*;
import timeflow.model.*;
import timeflow.vis.*;
 
import java.util.*;
import java.awt.*;
 
/*
 * A VisualEncoding takes the info about which fields to translate to
 * which visual aspects, and applies that to particular Acts.
 */
public class TimelineVisuals
{
 
        private Map<String, TimelineTrack> trackTable = new HashMap<String, TimelineTrack>();
        ArrayList<TimelineTrack> trackList = new ArrayList<TimelineTrack>();
        private TimeScale timeScale = new TimeScale();
        private Rectangle bounds = new Rectangle();
        private boolean frameChanged;
        private int numShown = 0;
        private Interval globalInterval;
 
        public double scale = 1;
        
        public enum Layout
        {
 
                TIGHT, LOOSE, GRAPH
        };
        private Layout layoutStyle = Layout.TIGHT;
        private VisualEncoder encoder;
        private TFModel model;
        private int fullHeight;
 
        public int getFullHeight()
        {
                return fullHeight;
        }
 
        public TimelineVisuals(TFModel model)
        {
                this.model = model;
                encoder = new VisualEncoder(model);
        }
 
        public TimeScale getTimeScale()
        {
                return timeScale;
        }
 
        public Rectangle getBounds()
        {
                return bounds;
        }
 
        public void setBounds(int x, int y, int w, int h)
        {
                bounds.setBounds(x, y, w, h);
                timeScale.setLow(x);
                timeScale.setHigh(x + w);
                frameChanged = true;
        }
 
        public Layout getLayoutStyle()
        {
                return layoutStyle;
        }
 
        public void setLayoutStyle(Layout style)
        {
                layoutStyle = style;
                layout();
        }
 
        public Interval getFitToVisibleRange()
        {
                ActList acts = model.getActs();
 
                // add a little bit to the right so we can see labels...
                ActDB db = getModel().getDB();
                Field endField = db.getField(VirtualField.END);
                Interval i = null;
                if (endField == null)
                {
                        i = DBUtils.range(acts, VirtualField.START);
                } else
                {
                        i = DBUtils.range(acts, new Field[]
                                {
                                        db.getField(VirtualField.START), endField
                                });
                }
                if (i.length() == 0)
                {
                        i.expand(globalInterval.length() / 20);
                }
                i = i.subinterval(-.05, 1.1);
                i.intersection(globalInterval);
                return i;
        }
 
        public void fitToVisible()
        {
                Interval i = getFitToVisibleRange();
                setTimeBounds(i.start, i.end);
        }
 
        public void zoomOut()
        {
                setTimeBounds(globalInterval.start, globalInterval.end);
        }
 
        public void setTimeBounds(long first, long last)
        {
                timeScale.setDateRange(first, last);
                frameChanged = true;
                model.setViewInterval(new Interval(first, last));
        }
 
        public Interval getGlobalInterval()
        {
                if (globalInterval == null && model != null && model.getDB() != null)
                {
                        createGlobalInterval();
                }
                return globalInterval;
        }
 
        public void createGlobalInterval()
        {
                globalInterval = DBUtils.range(model.getDB().all(), VirtualField.START).subinterval(-.05, 1.1);
        }
 
        public Interval getViewInterval()
        {
                return timeScale.getInterval();
        }
 
        public java.util.List<VisualAct> getVisualActs()
        {
                return encoder.getVisualActs();
        }
 
        public void layoutIfChanged()
        {
                if (frameChanged)
                {
                        layout();
                }
        }
 
        public void init(boolean majorChange)
        {
                note(new TFEvent(majorChange ? TFEvent.Type.DATABASE_CHANGE : TFEvent.Type.ACT_CHANGE, null));
        }
 
        public void note(TFEvent e)
        {
                ActList all = null;
                if (e.type == TFEvent.Type.DATABASE_CHANGE)
                {
                        all = model.getDB().all();
                        createGlobalInterval();
                        Interval i = guessInitialViewInterval(all, globalInterval);
                        setTimeBounds(i.start, i.end);
                }
                if (e.affectsRowSet())
                {
                        all = model.getDB().all();
                        encoder.createVisualActs();
                        createGlobalInterval();
                } else
                {
                        encoder.createVisualActs();
                }
                Interval v = model.getViewInterval();
                if (v != null && v.start != timeScale.getInterval().start)
                {
                        timeScale.getInterval().translateTo(v.start);
                }
                updateVisuals();
        }
 
        private Interval guessInitialViewInterval(ActList acts, Interval fullRange)
        {
                if (acts.size() < 50)
                {
                        return fullRange.copy();
                }
 
                Interval best = null;
                int most = -1;
                double d = Math.max(.1, 50. / acts.size());
                d = Math.min(1. / 3, d);
                for (double x = 0; x < 1 - d; x += d / 4)
                {
                        Interval i = fullRange.subinterval(x, x + d);
                        TimeIntervalFilter f = new TimeIntervalFilter(i, getModel().getDB().getField(VirtualField.START));
                        int num = 0;
                        for (Act a : acts)
                        {
                                if (f.accept(a))
                                {
                                        num++;
                                }
                        }
                        if (num > most)
                        {
                                most = num;
                                best = i;
                        }
                }
                return best;
        }
 
        public void updateVisuals()
        {
                scale = 1;
                updateVisualEncoding();
                layout();
        }
 
        public TFModel getModel()
        {
                return model;
        }
 
        public int getNumTracks()
        {
                return trackList.size();
        }
 
        public double layout()
        {
                ActList acts = model.getActs();
                if (acts == null)
                {
                        return scale;
                }
 
                double labelHeight = 30;
                
                double min = bounds.height == 0 ? 0 : labelHeight / bounds.height;
                double cellH = (double)getModel().getDisplay().getInt("timeline.item.height.min") / bounds.height;
                
                double maxCount = 0;
                
                // Set the minimum scale
                for (TimelineTrack t : trackList)
                {
                        //double height = t.size() * scale / (double) numShown;
 
                        maxCount = Math.max(t.size(), maxCount);
                }
 
                scale = Math.min(cellH * numShown, scale);
                scale = Math.max(min * numShown / maxCount, scale);
                
                double top = 0;
                for (TimelineTrack t : trackList)
                {
                        double height = Math.max(min, t.size() * scale / (double) numShown);
                        t.layout(top, height, this);
                        top += height;
                }
                fullHeight = (int) (top * bounds.height);
 
                Collections.sort(trackList);
                frameChanged = false;
                
                return scale;
        }
 
        private void updateVisualEncoding()
        {
                java.util.List<VisualAct> acts = encoder.apply();
 
                // now arrange on tracks
                trackTable = new HashMap<String, TimelineTrack>();
                trackList = new ArrayList<TimelineTrack>();
                numShown = 0;
                for (VisualAct v : acts)
                {
                        if (!v.isVisible())
                        {
                                continue;
                        }
                        numShown++;
                        String s = v.getTrackString();
                        TimelineTrack t = trackTable.get(s);
                        if (t == null)
                        {
                                t = new TimelineTrack(s);
                                trackTable.put(s, t);
                                trackList.add(t);
                        }
                        t.add(v);
                        v.setTrack(t);
                }
 
                /*
                // the following code is no longer used, but could come in handy again one day...
                
                // If there is more than one "small" track, then we will coalesce them into
                // one bigger "miscellaneous" track.
                int minSize=numShown/30;//Math.max(3,numShown/30);
                ArrayList<TimelineTrack> small=new ArrayList<TimelineTrack>();
                for (TimelineTrack t: trackList)
                {
                if (t.size()<minSize)
                small.add(t);
                }
                if (small.size()>1)
                {
                // create a new Track for "miscellaneous."
                TimelineTrack misc=new TimelineTrack(Display.MISC_CODE);
                trackList.add(misc);
                trackTable.put(misc.label, misc);
                
                // remove the old tracks.
                for (TimelineTrack t:small)
                {
                trackList.remove(t);
                trackTable.remove(t.label);
                for (VisualAct v: t.visualActs)
                {
                v.setTrack(misc);
                misc.add(v);
                }
                }
                // sort miscellaneous items in time order.
                //Collections.sort(misc.visualActs);
                }
                 */
 
                for (TimelineTrack t : trackList)
                {
                        Collections.sort(t.visualActs);
                }
        }
}