~keheliya-gallaba/gephi/maven-build

« back to all changes in this revision

Viewing changes to exporter.plugin/src/main/java/org/gephi/io/exporter/plugin/ExporterPajek.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-2011 Gephi
 
3
Authors : Daniel Bernardes <daniel.bernardes@polytechnique.edu>
 
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.io.exporter.plugin;
 
22
 
 
23
import java.io.Writer;
 
24
import java.util.HashMap;
 
25
import org.gephi.graph.api.Edge;
 
26
import org.gephi.graph.api.GraphModel;
 
27
import org.gephi.graph.api.HierarchicalGraph;
 
28
import org.gephi.graph.api.Node;
 
29
import org.gephi.graph.api.UndirectedGraph;
 
30
import org.gephi.io.exporter.api.FileType;
 
31
import org.gephi.io.exporter.spi.CharacterExporter;
 
32
import org.gephi.io.exporter.spi.GraphExporter;
 
33
import org.gephi.project.api.Workspace;
 
34
import org.gephi.utils.longtask.spi.LongTask;
 
35
import org.gephi.utils.progress.Progress;
 
36
import org.gephi.utils.progress.ProgressTicket;
 
37
import org.openide.util.NbBundle;
 
38
 
 
39
/**
 
40
 *
 
41
 * @author Daniel Bernardes
 
42
 */
 
43
public class ExporterPajek implements GraphExporter, CharacterExporter, LongTask {
 
44
 
 
45
    // Options
 
46
    private boolean exportPosition = true;
 
47
    private boolean exportEdgeWeight = true;
 
48
    // Architecture
 
49
    private Workspace workspace;
 
50
    private Writer writer;
 
51
    private boolean exportVisible;
 
52
    private boolean cancel = false;
 
53
    private ProgressTicket progressTicket;
 
54
    
 
55
    public void setExportEdgeWeight(boolean exportEdgeWeight) {
 
56
        this.exportEdgeWeight = exportEdgeWeight;
 
57
    }
 
58
    
 
59
    public boolean isExportEdgeWeight() {
 
60
        return exportEdgeWeight;
 
61
    }
 
62
 
 
63
    public void setExportPosition(boolean exportPosition) {
 
64
        this.exportPosition = exportPosition;
 
65
    }
 
66
    
 
67
    public boolean isExportPosition() {
 
68
        return exportPosition;
 
69
    }
 
70
    
 
71
    public boolean cancel() {
 
72
        cancel = true;
 
73
        return true;
 
74
    }
 
75
 
 
76
    public void setProgressTicket(ProgressTicket progressTicket) {
 
77
        this.progressTicket = progressTicket;
 
78
    }
 
79
 
 
80
    public boolean isExportVisible() {
 
81
        return exportVisible;
 
82
    }
 
83
 
 
84
    public void setExportVisible(boolean exportVisible) {
 
85
        this.exportVisible = exportVisible;
 
86
    }
 
87
 
 
88
    public void setWriter(Writer writer) {
 
89
        this.writer = writer;
 
90
    }
 
91
 
 
92
    public Workspace getWorkspace() {
 
93
        return workspace;
 
94
    }
 
95
 
 
96
    public void setWorkspace(Workspace workspace) {
 
97
        this.workspace = workspace;
 
98
    }
 
99
    
 
100
    public String getName() {
 
101
        return NbBundle.getMessage(getClass(), "ExporterPajek_name");
 
102
    }
 
103
 
 
104
    public FileType[] getFileTypes() {
 
105
        FileType ft = new FileType(".net", NbBundle.getMessage(getClass(), "fileType_Pajek_Name"));
 
106
        return new FileType[]{ft};
 
107
    }
 
108
 
 
109
    public boolean execute() {
 
110
        GraphModel graphModel = workspace.getLookup().lookup(GraphModel.class);
 
111
        HierarchicalGraph graph = null;
 
112
        if (exportVisible) {
 
113
            graph = graphModel.getHierarchicalGraphVisible();
 
114
        } else {
 
115
            graph = graphModel.getHierarchicalGraph();
 
116
        }
 
117
        try {
 
118
            exportData(graph);
 
119
        } catch (Exception e) {
 
120
            throw new RuntimeException(e);
 
121
        }
 
122
 
 
123
        return !cancel;
 
124
    }
 
125
 
 
126
    private void exportData(HierarchicalGraph graph) throws Exception {
 
127
        int max = graph.getNodeCount(), i=1;
 
128
        HashMap<String, Integer> idx = new HashMap<String, Integer>(3*max/2+1);
 
129
 
 
130
        Progress.start(progressTicket, max);
 
131
        graph.readLock();
 
132
 
 
133
        writer.append("*Vertices " + max + "\n");
 
134
 
 
135
        for (Node node : graph.getNodes()) {
 
136
            writer.append(Integer.toString(i));
 
137
            writer.append(" \"" + node.getNodeData().getLabel() + "\"");
 
138
            if(exportPosition) {
 
139
                writer.append(" "+node.getNodeData().x()+" "+node.getNodeData().y()+" "+node.getNodeData().z());
 
140
            }
 
141
            writer.append("\n");
 
142
            idx.put(node.getNodeData().getId(), i++); // assigns Ids from the interval [1..max]
 
143
        }
 
144
 
 
145
        if (graph instanceof UndirectedGraph) {
 
146
            writer.append("*Edges\n");
 
147
        } else {
 
148
            writer.append("*Arcs\n");
 
149
        }
 
150
 
 
151
        for (Edge edge : graph.getEdgesAndMetaEdges()) {
 
152
            if (cancel) {
 
153
                break;
 
154
            }
 
155
            if (edge != null) {
 
156
                writer.append(Integer.toString(idx.get(edge.getSource().getNodeData().getId())) + " ");
 
157
                writer.append(Integer.toString(idx.get(edge.getTarget().getNodeData().getId())));
 
158
                if (exportEdgeWeight) {
 
159
                    writer.append(" " + edge.getWeight());
 
160
                }
 
161
                writer.append("\n");
 
162
            }
 
163
 
 
164
            Progress.progress(progressTicket);
 
165
        }
 
166
 
 
167
        graph.readUnlockAll();
 
168
 
 
169
        Progress.finish(progressTicket);
 
170
    }
 
171
 
 
172
}