]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartData.java
Faster bounds calculation for zoom to selection and navigate to target
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / editor / ChartData.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.charts.editor;
13
14 import java.util.concurrent.atomic.AtomicInteger;
15
16 import org.simantics.databoard.annotations.Optional;
17 import org.simantics.db.Resource;
18 import org.simantics.history.Collector;
19 import org.simantics.history.HistoryManager;
20 import org.simantics.simulation.data.Datasource;
21 import org.simantics.simulation.experiment.IExperiment;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public final class ChartData {
27
28     /**
29      * The model for which this ChartData is valid, i.e. the model that contains
30      * {@link #run} if {@link #run} is non<code>non-null</code>
31      */
32     @Optional
33     public Resource       model;
34
35     /**
36      * If just reading data from file system, not the database, this may be
37      * <code>null</code>.
38      */
39     @Optional
40     public Resource       run;
41
42     /**
43      * Used for giving the chart access to the active experiment state.
44      */
45     @Optional
46     public IExperiment    experiment;
47
48     /**
49      * May be null if the chart data is not connected to a running experiment.
50      */
51     @Optional
52     public Datasource     datasource;
53
54     @Optional
55     public HistoryManager history;
56
57     /**
58      * Optional collector, Chart uses this to flush data right before drawing it
59      */
60     @Optional
61     public Collector      collector;
62
63     /**
64      * This (shared instance) is used to track the amount of users of this ChartData
65      * instance. The same instance may end up in many ChartData instances through
66      * {@link #readFrom(ChartData)}.
67      */
68     public AtomicInteger  refCount = new AtomicInteger();
69
70     public ChartData(Resource model, Resource run, IExperiment experiment, Datasource datasource, HistoryManager history, Collector collector) {
71         this.model = model;
72         this.run = run;
73         this.experiment = experiment;
74         this.datasource = datasource;
75         this.history = history;
76         this.collector = collector;
77     }
78     
79     public void readFrom(ChartData other) {
80         if (other==null) {
81                 this.model = null;
82                 this.run = null;
83                 this.experiment = null;
84                 this.datasource = null;
85                 this.history = null;
86                 this.collector = null;
87                 this.refCount = null;
88         } else {
89                 this.model = other.model;
90                 this.run = other.run;
91                 this.experiment = other.experiment;
92                 this.datasource = other.datasource;
93                 this.history = other.history;
94                 this.collector = other.collector;
95                 this.refCount = other.refCount;
96         }
97     }
98
99     /**
100      * Dispose by closing history
101      */
102     public void dispose() {
103         model = null;
104         run = null;
105         experiment = null;
106         datasource = null;
107         if (history != null) {
108                 history.close();
109                 history = null;
110         }
111     }
112
113     public int reference() {
114         AtomicInteger i = refCount;
115         if (i == null)
116             return 0;
117         int result = i.incrementAndGet();
118         //System.out.println(this + ": reference: " + (result-1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
119         return result;
120     }
121
122     public int dereference() {
123         AtomicInteger i = refCount;
124         if (i == null)
125             return 0;
126         int result = i.decrementAndGet();
127         //System.out.println(this + ": dereference: " + (result+1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
128         if (result <= 0) {
129             synchronized (i) {
130                 i.notifyAll();
131             }
132         }
133         return result;
134     }
135
136     public void waitUntilNotReferenced() throws InterruptedException {
137         AtomicInteger i = refCount;
138         if (i == null)
139             return;
140         synchronized (i) {
141             while (i.get() > 0) {
142                 i.wait();
143             }
144         }
145     }
146
147 }