~keheliya-gallaba/gephi/maven-build

« back to all changes in this revision

Viewing changes to filters/src/main/java/org/gephi/filters/FilterAutoRefreshor.java

  • Committer: Keheliya Gallaba
  • Date: 2011-08-01 13:01:30 UTC
  • Revision ID: keheliya.gallaba@gmail.com-20110801130130-0u2qgcufi8bvqwxv
Adding Export Plugin, Export Plugin UI, Filters Impl

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2008-2010 Gephi
 
3
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
 
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 Affero General Public License as
 
10
published by the Free Software Foundation, either version 3 of the
 
11
License, or (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 Affero General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU Affero General Public License
 
19
along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
package org.gephi.filters;
 
22
 
 
23
import java.util.concurrent.atomic.AtomicBoolean;
 
24
import org.gephi.graph.api.GraphEvent;
 
25
import org.gephi.graph.api.GraphListener;
 
26
import org.gephi.graph.api.GraphModel;
 
27
import org.openide.util.Exceptions;
 
28
 
 
29
/**
 
30
 *
 
31
 * @author Mathieu Bastian
 
32
 */
 
33
public class FilterAutoRefreshor extends Thread implements GraphListener {
 
34
 
 
35
    private static final int TIMER = 1000;
 
36
    private final GraphModel graphModel;
 
37
    private final FilterModelImpl filterModel;
 
38
    private boolean running = true;
 
39
    private AtomicBoolean refresh = new AtomicBoolean(false);
 
40
 
 
41
    public FilterAutoRefreshor(FilterModelImpl filterModel, GraphModel graphModel) {
 
42
        super("Filter Auto-Refresh");
 
43
        setDaemon(true);
 
44
        this.graphModel = graphModel;
 
45
        this.filterModel = filterModel;
 
46
    }
 
47
 
 
48
    @Override
 
49
    public void run() {
 
50
        while (running) {
 
51
            try {
 
52
                if (refresh.compareAndSet(true, false)) {
 
53
                    manualRefresh();
 
54
                }
 
55
                Thread.sleep(TIMER);
 
56
            } catch (InterruptedException ex) {
 
57
                Exceptions.printStackTrace(ex);
 
58
            }
 
59
        }
 
60
    }
 
61
 
 
62
    public void setEnable(boolean enable) {
 
63
        if (enable) {
 
64
            graphModel.addGraphListener(this);
 
65
        } else {
 
66
            graphModel.removeGraphListener(this);
 
67
            refresh.set(false);
 
68
        }
 
69
        if (!isAlive()) {
 
70
            start();
 
71
        }
 
72
    }
 
73
 
 
74
    public void graphChanged(GraphEvent event) {
 
75
        if (event.getSource().isMainView() && event.is(GraphEvent.EventType.ADD_EDGES,
 
76
                GraphEvent.EventType.ADD_NODES,
 
77
                GraphEvent.EventType.MOVE_NODES,
 
78
                GraphEvent.EventType.REMOVE_EDGES,
 
79
                GraphEvent.EventType.REMOVE_NODES)) {
 
80
            refresh.set(true);
 
81
            //System.out.println("set refresh true");
 
82
        }
 
83
    }
 
84
 
 
85
    public void setRunning(boolean running) {
 
86
        this.running = running;
 
87
        if (!running) {
 
88
            graphModel.removeGraphListener(this);
 
89
            refresh.set(false);
 
90
        }
 
91
    }
 
92
 
 
93
    public void manualRefresh() {
 
94
        if (filterModel.getFilterThread() != null && filterModel.getCurrentQuery() != null) {
 
95
            filterModel.getFilterThread().setRootQuery((AbstractQueryImpl) filterModel.getCurrentQuery());
 
96
        }
 
97
    }
 
98
}