1
1
/**
2
2
* Created by pmurray on 6/13/2017.
3
3
*/
4
-
5
4
package org .gephi .plugin .CirclePack ;
6
5
7
6
import java .util .*;
8
7
9
- import javafx .scene .shape .Circle ;
10
8
import org .gephi .graph .api .*;
11
9
import org .gephi .layout .spi .Layout ;
12
10
import org .gephi .layout .spi .LayoutBuilder ;
13
11
import org .gephi .layout .spi .LayoutProperty ;
14
-
12
+ import org . openide . util . Exceptions ;
15
13
16
14
public class CirclePackLayout implements Layout {
17
15
18
- private static Object Bundle ;
19
16
//Architecture
20
17
private final LayoutBuilder builder ;
21
18
private GraphModel graphModel ;
22
19
//Flags
23
20
private boolean executing = false ;
24
21
//Properties
25
22
private int radiusSize ;
26
- private String hierarchy1 ;
27
- private String hierarchy2 ;
28
- private String hierarchy3 ;
29
- private String hierarchy4 ;
30
- private String hierarchy5 ;
23
+ private String hierarchy1 ;
24
+ private String hierarchy2 ;
25
+ private String hierarchy3 ;
26
+ private String hierarchy4 ;
27
+ private String hierarchy5 ;
31
28
32
29
public CirclePackLayout (CirclePackLayoutBuilder builder ) {
33
30
this .builder = builder ;
@@ -43,58 +40,79 @@ public void initAlgo() {
43
40
executing = true ;
44
41
}
45
42
46
-
47
43
@ Override
48
44
public void goAlgo () {
49
- Graph graph = graphModel .getGraphVisible ();
50
- graph .readLock ();
51
- int nodeCount = graph .getNodeCount ();
52
- Node [] nodes = graph .getNodes ().toArray ();
53
-
54
- CircleWrap container = new CircleWrap ();
45
+ ArrayList <String > attributesToUse = new ArrayList <>();
55
46
56
- for (int i = 0 ; i < nodeCount ; i ++){
57
- Node node = nodes [i ];
58
-
59
- ArrayList attributes = new ArrayList <String >();
60
- if (this .hierarchy1 != null && this .hierarchy1 != "No Selection" ){
61
- attributes .add (String .valueOf (node .getAttribute (this .hierarchy1 )));
62
- }
63
- if (this .hierarchy2 != null && this .hierarchy2 != "No Selection" ){
64
- attributes .add (String .valueOf (node .getAttribute (this .hierarchy2 )));
65
- }
66
- if (this .hierarchy3 != null && this .hierarchy3 != "No Selection" ){
67
- attributes .add (String .valueOf (node .getAttribute (this .hierarchy3 )));
68
- }
69
- if (this .hierarchy4 != null && this .hierarchy4 != "No Selection" ){
70
- attributes .add (String .valueOf (node .getAttribute (this .hierarchy4 )));
71
- }
72
- if (this .hierarchy5 != null && this .hierarchy5 != "No Selection" ){
73
- attributes .add (String .valueOf (node .getAttribute (this .hierarchy5 )));
47
+ for (String att : Arrays .asList (hierarchy1 , hierarchy2 , hierarchy3 , hierarchy4 , hierarchy5 )) {
48
+ if (att != null && !att .equals ("No Selection" )) {
49
+ attributesToUse .add (att );
74
50
}
51
+ }
75
52
76
- CircleWrap newCircleWrap = new CircleWrap ((String ) node .getId ());
77
- newCircleWrap .r = node .size ();
53
+ final Graph graph ;
54
+ if (graphModel .isDirected () || graphModel .isMixed ()) {
55
+ graph = graphModel .getDirectedGraphVisible ();
56
+ } else {
57
+ graph = graphModel .getGraphVisible ();
58
+ }
78
59
79
- CircleWrap parentContainer = container ;
80
- for (Object attribute : attributes ) {
81
- parentContainer = parentContainer .getChild ((String ) attribute );
60
+ graph .readLock ();
61
+ try {
62
+ int nodeCount = graph .getNodeCount ();
63
+ Node [] nodes = graph .getNodes ().toArray ();
64
+
65
+ CircleWrap container = new CircleWrap ();
66
+
67
+ for (int i = 0 ; i < nodeCount ; i ++) {
68
+ Node node = nodes [i ];
69
+
70
+ ArrayList <String > nodeAttributes = new ArrayList <>();
71
+
72
+ for (String att : attributesToUse ) {
73
+ switch (att ) {
74
+ case "NodeID" :
75
+ nodeAttributes .add (node .getId ().toString ());
76
+ break ;
77
+ case "Degree" :
78
+ nodeAttributes .add (String .valueOf (graph .getDegree (node )));
79
+ break ;
80
+ case "InDegree" :
81
+ nodeAttributes .add (String .valueOf (((DirectedGraph ) graph ).getInDegree (node )));
82
+ break ;
83
+ case "OutDegree" :
84
+ nodeAttributes .add (String .valueOf (((DirectedGraph ) graph ).getOutDegree (node )));
85
+ break ;
86
+ default :
87
+ nodeAttributes .add (String .valueOf (node .getAttribute (att )));
88
+ break ;
89
+ }
90
+ }
91
+
92
+ CircleWrap newCircleWrap = new CircleWrap (node .getId ().toString ());
93
+ newCircleWrap .r = node .size ();
94
+
95
+ CircleWrap parentContainer = container ;
96
+ for (String attribute : nodeAttributes ) {
97
+ parentContainer = parentContainer .getChild (attribute );
98
+ }
99
+ parentContainer .addChild (node .getId ().toString (), newCircleWrap );
82
100
}
83
- parentContainer .addChild ((String ) node .getId (), newCircleWrap );
84
- }
85
101
86
- CirclePackAlgo PackAlgo = new CirclePackAlgo ();
102
+ CirclePackAlgo PackAlgo = new CirclePackAlgo ();
87
103
88
- PackAlgo .packHierarchyAndShift (container );
89
- setNode (graph , container );
104
+ PackAlgo .packHierarchyAndShift (container );
105
+ setNode (graph , container );
90
106
91
- graph .readUnlock ();
107
+ } finally {
108
+ graph .readUnlockAll ();
109
+ }
92
110
endAlgo ();
93
111
}
94
112
95
- public void setNode (Graph graph , CircleWrap parentCircle ){
113
+ public void setNode (Graph graph , CircleWrap parentCircle ) {
96
114
for (CircleWrap circle : parentCircle .children .values ()) {
97
- if (circle .hasChildren ()){
115
+ if (circle .hasChildren ()) {
98
116
setNode (graph , circle );
99
117
} else {
100
118
Node node = graph .getNode (circle .getId ());
@@ -116,21 +134,20 @@ public boolean canAlgo() {
116
134
117
135
@ Override
118
136
public LayoutProperty [] getProperties () {
119
- List <LayoutProperty > properties = new ArrayList <LayoutProperty >();
120
- final String CIRCLELAYOUT = "Circle Pack Layout" ;
137
+ List <LayoutProperty > properties = new ArrayList <>();
121
138
122
139
try {
123
140
properties .add (LayoutProperty .createProperty (
124
141
this , String .class ,
125
142
"Hierarchy1" ,
126
143
"Hierarchy" ,
127
- "Hierarchy1" ,
144
+ "Hierarchy1" ,
128
145
"getHierarchy1" , "setHierarchy1" , LayoutComboBoxEditor .class ));
129
146
properties .add (LayoutProperty .createProperty (
130
147
this , String .class ,
131
148
"Hierarchy2" ,
132
149
"Hierarchy" ,
133
- "Hierarchy2" ,
150
+ "Hierarchy2" ,
134
151
"getHierarchy2" , "setHierarchy2" , LayoutComboBoxEditor .class ));
135
152
properties .add (LayoutProperty .createProperty (
136
153
this , String .class ,
@@ -150,8 +167,8 @@ public LayoutProperty[] getProperties() {
150
167
"Hierarchy" ,
151
168
"Hierarchy5" ,
152
169
"getHierarchy5" , "setHierarchy5" , LayoutComboBoxEditor .class ));
153
- } catch (Exception e ) {
154
- e .printStackTrace ();
170
+ } catch (NoSuchMethodException e ) {
171
+ Exceptions .printStackTrace (e );
155
172
}
156
173
157
174
return properties .toArray (new LayoutProperty [0 ]);
@@ -167,53 +184,43 @@ public LayoutBuilder getBuilder() {
167
184
return builder ;
168
185
}
169
186
170
- public String getHierarchy1 ()
171
- {
187
+ public String getHierarchy1 () {
172
188
return this .hierarchy1 ;
173
189
}
174
190
175
- public void setHierarchy1 (String attribute )
176
- {
191
+ public void setHierarchy1 (String attribute ) {
177
192
this .hierarchy1 = attribute ;
178
193
}
179
194
180
- public String getHierarchy2 ()
181
- {
195
+ public String getHierarchy2 () {
182
196
return this .hierarchy2 ;
183
197
}
184
198
185
- public void setHierarchy2 (String attribute )
186
- {
199
+ public void setHierarchy2 (String attribute ) {
187
200
this .hierarchy2 = attribute ;
188
201
}
189
202
190
- public void setHierarchy3 (String attribute )
191
- {
203
+ public void setHierarchy3 (String attribute ) {
192
204
this .hierarchy3 = attribute ;
193
205
}
194
206
195
- public String getHierarchy3 ()
196
- {
207
+ public String getHierarchy3 () {
197
208
return this .hierarchy3 ;
198
209
}
199
210
200
- public void setHierarchy4 (String attribute )
201
- {
211
+ public void setHierarchy4 (String attribute ) {
202
212
this .hierarchy4 = attribute ;
203
213
}
204
214
205
- public String getHierarchy4 ()
206
- {
215
+ public String getHierarchy4 () {
207
216
return this .hierarchy4 ;
208
217
}
209
218
210
- public void setHierarchy5 (String attribute )
211
- {
219
+ public void setHierarchy5 (String attribute ) {
212
220
this .hierarchy5 = attribute ;
213
221
}
214
222
215
- public String getHierarchy5 ()
216
- {
223
+ public String getHierarchy5 () {
217
224
return this .hierarchy5 ;
218
225
}
219
226
0 commit comments