2
Copyright 2008-2011 Gephi
3
Authors : Daniel Bernardes <daniel.bernardes@polytechnique.edu>
4
Website : http://www.gephi.org
6
This file is part of Gephi.
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.
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.
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/>.
21
package org.gephi.io.exporter.plugin;
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;
41
* @author Daniel Bernardes
43
public class ExporterPajek implements GraphExporter, CharacterExporter, LongTask {
46
private boolean exportPosition = true;
47
private boolean exportEdgeWeight = true;
49
private Workspace workspace;
50
private Writer writer;
51
private boolean exportVisible;
52
private boolean cancel = false;
53
private ProgressTicket progressTicket;
55
public void setExportEdgeWeight(boolean exportEdgeWeight) {
56
this.exportEdgeWeight = exportEdgeWeight;
59
public boolean isExportEdgeWeight() {
60
return exportEdgeWeight;
63
public void setExportPosition(boolean exportPosition) {
64
this.exportPosition = exportPosition;
67
public boolean isExportPosition() {
68
return exportPosition;
71
public boolean cancel() {
76
public void setProgressTicket(ProgressTicket progressTicket) {
77
this.progressTicket = progressTicket;
80
public boolean isExportVisible() {
84
public void setExportVisible(boolean exportVisible) {
85
this.exportVisible = exportVisible;
88
public void setWriter(Writer writer) {
92
public Workspace getWorkspace() {
96
public void setWorkspace(Workspace workspace) {
97
this.workspace = workspace;
100
public String getName() {
101
return NbBundle.getMessage(getClass(), "ExporterPajek_name");
104
public FileType[] getFileTypes() {
105
FileType ft = new FileType(".net", NbBundle.getMessage(getClass(), "fileType_Pajek_Name"));
106
return new FileType[]{ft};
109
public boolean execute() {
110
GraphModel graphModel = workspace.getLookup().lookup(GraphModel.class);
111
HierarchicalGraph graph = null;
113
graph = graphModel.getHierarchicalGraphVisible();
115
graph = graphModel.getHierarchicalGraph();
119
} catch (Exception e) {
120
throw new RuntimeException(e);
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);
130
Progress.start(progressTicket, max);
133
writer.append("*Vertices " + max + "\n");
135
for (Node node : graph.getNodes()) {
136
writer.append(Integer.toString(i));
137
writer.append(" \"" + node.getNodeData().getLabel() + "\"");
139
writer.append(" "+node.getNodeData().x()+" "+node.getNodeData().y()+" "+node.getNodeData().z());
142
idx.put(node.getNodeData().getId(), i++); // assigns Ids from the interval [1..max]
145
if (graph instanceof UndirectedGraph) {
146
writer.append("*Edges\n");
148
writer.append("*Arcs\n");
151
for (Edge edge : graph.getEdgesAndMetaEdges()) {
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());
164
Progress.progress(progressTicket);
167
graph.readUnlockAll();
169
Progress.finish(progressTicket);