~gephi.team/gephi/0.8.1

« back to all changes in this revision

Viewing changes to TimelineModule/src/org/gephi/timeline/ui/FakeTimelineDataModel.java

  • Committer: Mathieu Bastian
  • Date: 2009-07-23 14:26:08 UTC
  • mfrom: (166.1.9)
  • Revision ID: git-v1:13fd133fa30075e587d20f75d45ceaabd45ca316
Merge with Julian's Timeline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2008 WebAtlas
 
3
Authors : Mathieu Bastian, Mathieu Jacomy, Julian Bilcke
 
4
Website : http://www.gephi.org
 
5
 
 
6
This file is part of Gephi.
 
7
 
 
8
Gephi is free software: you can redistribute it and/or modify
 
9
it under the terms of the GNU General Public License as published by
 
10
the Free Software Foundation, either version 3 of the License, or
 
11
(at your option) any later version.
 
12
 
 
13
Gephi is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU General Public License
 
19
along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
package org.gephi.timeline.ui;
 
22
 
 
23
import java.util.ArrayList;
 
24
import java.util.List;
 
25
import java.util.Random;
 
26
import org.gephi.timeline.TimelineQuartzImpl;
 
27
import org.gephi.timeline.api.TimelineDataModel;
 
28
import org.gephi.timeline.api.TimelineDataModelListener;
 
29
import org.gephi.timeline.api.TimelineQuartz;
 
30
import org.gephi.timeline.api.TimelineQuartzListener;
 
31
 
 
32
/**
 
33
 *
 
34
 * @author Julian Bilcke
 
35
 */
 
36
public class FakeTimelineDataModel implements TimelineDataModel, TimelineQuartzListener {
 
37
 
 
38
    private Random random;
 
39
    private List<TimelineDataModelListener> listeners;
 
40
    private Float from;
 
41
    private Float to;
 
42
    private TimelineQuartz quartz;
 
43
 
 
44
    // FAKE DATASET SIZE
 
45
    private List<Float> data;
 
46
    private int data_getNbOfFakeRevisions = 800000;
 
47
    private Float speed;
 
48
 
 
49
   public enum PlayMode {
 
50
        OLDEST,
 
51
        YOUNGEST,
 
52
        BOTH
 
53
    }
 
54
    PlayMode playMode = PlayMode.YOUNGEST;
 
55
    
 
56
    public FakeTimelineDataModel() {
 
57
        
 
58
        // defaults
 
59
        from = 0.15f;
 
60
        to = 0.85f;
 
61
        speed = 0.0001f;
 
62
 
 
63
        listeners = new ArrayList<TimelineDataModelListener>();
 
64
 
 
65
        // WE GENERATE OUR FAKE DATASET
 
66
        random = new Random();
 
67
        data = new ArrayList<Float>();
 
68
        for (int i = 0; i < data_getNbOfFakeRevisions; i++) {
 
69
            data.add(0.15f + random.nextFloat() * 0.6f);
 
70
        }
 
71
 
 
72
        quartz = new TimelineQuartzImpl();
 
73
        quartz.addTimelineQuartzListener(this);
 
74
    }
 
75
 
 
76
    public List<Float> getOverviewSample(int resolution) {
 
77
        // TODO put a call to the timeline engine here ?
 
78
 
 
79
        List<Float> tmp = new ArrayList<Float>();
 
80
 
 
81
        int unit = data.size() / resolution; // eg.  16 = 10000 / 600
 
82
        for (int i = 0; i < resolution; i++) {
 
83
            tmp.add(data.get(i * unit)); // eg. 600 * chunks of 16
 
84
        }
 
85
        return tmp;
 
86
    }
 
87
 
 
88
    public void addTimelineDataModelListener(TimelineDataModelListener listener) {
 
89
        listeners.add(listener);
 
90
    }
 
91
 
 
92
    public List<Float> getZoomSample(int resolution) {
 
93
 
 
94
        if (resolution < 1) resolution = 1;
 
95
        
 
96
        // TODO put a call to the timeline engine here ?
 
97
        // get size from the timeline
 
98
        int size = data.size(); // eg 200000000
 
99
 
 
100
        int totalf = (int) (from * (float) size); // eg. 12000
 
101
        int totalt = (int) (to * (float) size); // eg. 12000000
 
102
        System.out.println("totalf: " + totalf);
 
103
        System.out.println("totalt: " + totalt);
 
104
 
 
105
        // get tmp from the timeline getFromRange(..,..)
 
106
        List<Float> tmp = new ArrayList<Float>();
 
107
 
 
108
        int unit = (totalt - totalf) / resolution; // eg.  16 = 10000 / 600
 
109
        if (unit < 1) unit = 1;
 
110
        System.out.println("unit: " + unit);
 
111
 
 
112
        for (int i = 0; i < resolution; i++) {
 
113
            tmp.add(data.get(totalf + i * unit)); // eg. 600 * chunks of 16
 
114
        }
 
115
        return tmp;
 
116
    }
 
117
 
 
118
    public void selectInterval(Float from, Float to) {
 
119
        if (0.0f < to && to < 1.0f && 0.0f < from && from < 1.0f && from < to) {
 
120
            this.to = to;
 
121
            this.from = from;
 
122
        }
 
123
    }
 
124
    public void selectTo(Float to) {
 
125
        if (0.0f < to && to < 1.0f && from < to) {
 
126
            this.to = to;
 
127
        }
 
128
    }
 
129
 
 
130
    public void selectFrom(Float from) {
 
131
        if (0.0f < from && from < 1.0f && from < to) {
 
132
            this.from = from;
 
133
        }
 
134
    }
 
135
 
 
136
    public Comparable getFirstComparable() {
 
137
        return data.get(0);
 
138
    }
 
139
    public Comparable getLastComparable() {
 
140
        return data.get(data.size()-1);
 
141
    }
 
142
 
 
143
    public Float getSelectionFrom() {
 
144
        return from;
 
145
    }
 
146
 
 
147
    public Comparable getSelectionFromAsComparable() {
 
148
        return data.get((int) (from * (float)data.size()));
 
149
    }
 
150
    public Float getSelectionTo() {
 
151
        return to;
 
152
    }
 
153
    public Comparable getSelectionToAsComparable() {
 
154
        return data.get((int) (to * (float)data.size()));
 
155
    }
 
156
 
 
157
    public void play() {
 
158
        quartz.start();
 
159
    }
 
160
 
 
161
    public void pause() {
 
162
        quartz.stop();
 
163
    }
 
164
 
 
165
    public void isPlaying() {
 
166
        quartz.isRunning();
 
167
    }
 
168
    public void quartzTick(long delay) {
 
169
        // delay is the current "resolution" of the quartz
 
170
        // this is provided ases convenience, in case you would need it for your
 
171
        // calculations
 
172
        switch(playMode) {
 
173
            case OLDEST:
 
174
                selectFrom(getSelectionFrom() + speed);
 
175
                break;
 
176
            case BOTH:
 
177
                selectInterval(getSelectionFrom() + speed,
 
178
                               getSelectionTo() + speed);
 
179
                break;
 
180
            case YOUNGEST:
 
181
                selectTo(getSelectionTo() + speed);
 
182
                break;
 
183
        }
 
184
    }
 
185
}