~gephi.team/gephi/0.8.1

« back to all changes in this revision

Viewing changes to RankingAPI/src/org/gephi/ranking/RankingModelImpl.java

  • Committer: Mathieu Bastian
  • Date: 2011-07-19 04:14:33 UTC
  • Revision ID: git-v1:fc8aece8315f8e214fdb55d8cd035682fd1ca44b
Major refactoring of the Ranking modules, and creation of SPIs for ranking builders and transformers. Creation of a RankingPlugin module with new ranking and transformers implementations. Simplification of the RankingAPI module and fit to conventions. Update of the DesktopRanking module with cleaner code and introduction of a RankingUIController and RankingUIModel to manage states. Deletion of the RankingResult mechanism and introduction of RankingEvent and RankingListener interfaces to manages ranking events. Documentation of the API and SPI, ready to be declared stable.

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.ranking;
 
22
 
 
23
import java.awt.event.ActionEvent;
 
24
import java.awt.event.ActionListener;
 
25
import org.gephi.ranking.api.Interpolator;
 
26
import org.gephi.ranking.api.RankingModel;
 
27
import org.gephi.ranking.api.Ranking;
 
28
import java.util.ArrayList;
 
29
import java.util.Collection;
 
30
import java.util.Collections;
 
31
import java.util.List;
 
32
import javax.swing.Timer;
 
33
import org.gephi.data.attributes.api.AttributeController;
 
34
import org.gephi.data.attributes.api.AttributeEvent;
 
35
import org.gephi.data.attributes.api.AttributeListener;
 
36
import org.gephi.project.api.Workspace;
 
37
import org.gephi.ranking.api.RankingEvent;
 
38
import org.gephi.ranking.api.RankingListener;
 
39
import org.gephi.ranking.spi.RankingBuilder;
 
40
import org.openide.util.Lookup;
 
41
 
 
42
/**
 
43
 * Implementation of the <code>RankingModel</code> interface.
 
44
 * 
 
45
 * @author Mathieu Bastian
 
46
 */
 
47
public class RankingModelImpl implements RankingModel, AttributeListener {
 
48
    
 
49
    private final Workspace workspace;
 
50
    private final List<RankingListener> listeners;
 
51
    private Interpolator interpolator;
 
52
    
 
53
    public RankingModelImpl(Workspace workspace) {
 
54
        this.workspace = workspace;
 
55
        this.listeners = Collections.synchronizedList(new ArrayList<RankingListener>());
 
56
        this.interpolator = Interpolator.LINEAR;
 
57
    }
 
58
    
 
59
    public void select() {
 
60
        AttributeController attributeController = Lookup.getDefault().lookup(AttributeController.class);
 
61
        attributeController.getModel(workspace).addAttributeListener(this);
 
62
    }
 
63
    
 
64
    public void unselect() {
 
65
        AttributeController attributeController = Lookup.getDefault().lookup(AttributeController.class);
 
66
        attributeController.getModel(workspace).removeAttributeListener(this);
 
67
    }
 
68
    private Timer refreshTimer; //hack
 
69
 
 
70
    @Override
 
71
    public void attributesChanged(AttributeEvent event) {
 
72
        if (event.getEventType().equals(AttributeEvent.EventType.ADD_COLUMN) || event.getEventType().equals(AttributeEvent.EventType.REMOVE_COLUMN)) {
 
73
            if (refreshTimer != null) {
 
74
                refreshTimer.restart();
 
75
            } else {
 
76
                refreshTimer = new Timer(500, new ActionListener() {
 
77
                    
 
78
                    @Override
 
79
                    public void actionPerformed(ActionEvent e) {
 
80
                        RankingEvent rankingEvent = new RankingEventImpl(RankingEvent.EventType.REFRESH_RANKING, RankingModelImpl.this);
 
81
                        fireRankingListener(rankingEvent);
 
82
                    }
 
83
                });
 
84
                refreshTimer.setRepeats(false);
 
85
                refreshTimer.start();
 
86
            }
 
87
        }
 
88
    }
 
89
    
 
90
    public Workspace getWorkspace() {
 
91
        return workspace;
 
92
    }
 
93
    
 
94
    public Ranking[] getNodeRankings() {
 
95
        return getRankings(Ranking.EDGE_ELEMENT);
 
96
    }
 
97
    
 
98
    public Ranking[] getEdgeRankings() {
 
99
        return getRankings(Ranking.EDGE_ELEMENT);
 
100
    }
 
101
    
 
102
    public Ranking[] getRankings(String elementType) {
 
103
        List<Ranking> rankings = new ArrayList<Ranking>();
 
104
        Collection<? extends RankingBuilder> builders = Lookup.getDefault().lookupAll(RankingBuilder.class);
 
105
        for (RankingBuilder builder : builders) {
 
106
            Ranking[] builtRankings = builder.buildRanking(this);
 
107
            for (Ranking r : builtRankings) {
 
108
                if (r.getElementType().equals(elementType)) {
 
109
                    rankings.add(r);
 
110
                }
 
111
            }
 
112
        }
 
113
        return rankings.toArray(new Ranking[0]);
 
114
    }
 
115
    
 
116
    public Ranking getRanking(String elementType, String name) {
 
117
        Ranking[] rankings = getRankings(elementType);
 
118
        for (Ranking r : rankings) {
 
119
            if (r.getName().equals(name)) {
 
120
                return r;
 
121
            }
 
122
        }
 
123
        return null;
 
124
    }
 
125
 
 
126
    public Interpolator getInterpolator() {
 
127
        return interpolator;
 
128
    }
 
129
 
 
130
    public void setInterpolator(Interpolator interpolator) {
 
131
        if(interpolator == null) {
 
132
            throw new NullPointerException();
 
133
        }
 
134
        this.interpolator = interpolator;
 
135
    }
 
136
    
 
137
    public void addRankingListener(RankingListener listener) {
 
138
        if (!listeners.contains(listener)) {
 
139
            listeners.add(listener);
 
140
        }
 
141
    }
 
142
    
 
143
    public void removeRankingListener(RankingListener listener) {
 
144
        listeners.remove(listener);
 
145
    }
 
146
    
 
147
    private void fireRankingListener(RankingEvent rankingEvent) {
 
148
        for (RankingListener listener : listeners) {
 
149
            listener.rankingChanged(rankingEvent);
 
150
        }
 
151
    }
 
152
}