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
package timeflow.vis;
 
import java.awt.Color;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
 
import timeflow.data.db.Act;
import timeflow.data.db.ActDB;
import timeflow.data.db.ActList;
import timeflow.data.db.Field;
import timeflow.model.TFModel;
import timeflow.model.VirtualField;
 
public class VisualActFactory
{
        // create one VisualAct per Act
 
        private static java.util.List<VisualAct> create(ActList acts)
        {
                java.util.List<VisualAct> list = new ArrayList<VisualAct>();
                for (Act a : acts)
                {
                        VisualAct v = new TagVisualAct(a);
                        list.add(v);
                }
                return list;
        }
 
        // create one VisualAct per Act/tag combo.
        public static java.util.List<VisualAct> create(ActList acts, Field tagField, boolean multipleColors)
        {
                if (tagField == null || tagField.getType() == String.class)
                {
                        return create(acts);
                }
                java.util.List<VisualAct> list = new ArrayList<VisualAct>();
                for (Act a : acts)
                {
                        String[] tags = a.getTextList(tagField);
                        if (tags == null || tags.length < 2)
                        {
                                VisualAct v = new TagVisualAct(a);
                                if (tags != null && tags.length == 1)
                                {
                                        v.setTrackString(tags[0]);
                                }
                                list.add(v);
                        } else
                        {
                                for (String tag : tags)
                                {
                                        VisualAct v = multipleColors ? new TagVisualAct(a) : new VisualAct(a);
                                        v.setTrackString(tag);
                                        list.add(v);
                                }
                        }
                }
                return list;
        }
 
        public static Collection<VisualAct> makeEmFit(TFModel model, ArrayList<VisualAct> vacts, Rectangle bounds)
        {
                // Does everything fit? Because, if so, we're already good to go.
                int area = bounds.width * bounds.height;
                int room = area / 200;
                if (vacts.size() <= room)
                {
                        return vacts;
                }
 
                ArrayList<VisualAct> results = new ArrayList<VisualAct>();
 
                // OK. If:
                //     * there's room for more than one item, and  
                //     * there's more than one color in use,
                //
                // Then let's see how many colors there are. Maybe we can do one bubble per color.
                ActDB db = model.getDB();
                if (room > 1 && (db.getField(VirtualField.COLOR) != null || db.getField(VirtualField.TRACK) != null))
                {
                        HashMap<Color, ArrayList<VisualAct>> colorGroupings = new HashMap<Color, ArrayList<VisualAct>>();
                        for (VisualAct v : vacts)
                        {
                                Color c = v.color;
                                ArrayList<VisualAct> grouping = colorGroupings.get(c);
                                if (grouping == null)
                                {
                                        grouping = new ArrayList<VisualAct>();
                                        colorGroupings.put(c, grouping);
                                }
                                grouping.add(v);
                        }
 
                        if (colorGroupings.size() <= room) // Great! The colors fit. We now return one group VisualAct per color.
                        {
                                for (Color c : colorGroupings.keySet())
                                {
                                        ArrayList<VisualAct> grouping = colorGroupings.get(c);
                                        if (grouping.size() == 1)
                                        {
                                                results.add(grouping.get(0));
                                        } else if (grouping.size() > 1)
                                        {
                                                results.add(new GroupVisualAct(grouping, false, bounds));
                                        }
                                }
                                return results;
                        }
                }
 
                // OK, too bad, even that doesn't fit. We will just create one fat VisualAct
                // that descibes the aggregate. C'est la vie!
 
                results.add(new GroupVisualAct(vacts, true, bounds));
                return results;
        }
}