Skip to content

Commit 332237b

Browse files
committed
FXML-friendly constructor.
1 parent 26363d8 commit 332237b

1 file changed

Lines changed: 51 additions & 33 deletions

File tree

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.brunomnsilva.smartgraph.graph.Vertex;
3030
import javafx.animation.AnimationTimer;
3131
import javafx.application.Platform;
32+
import javafx.beans.NamedArg;
3233
import javafx.beans.property.BooleanProperty;
3334
import javafx.beans.property.SimpleBooleanProperty;
3435
import javafx.geometry.BoundingBox;
@@ -82,6 +83,7 @@ public class SmartGraphPanel<V, E> extends Pane {
8283
*/
8384
private final SmartGraphProperties graphProperties;
8485

86+
private static final String DEFAULT_CSS_FILE = "smartgraph.css";
8587
/*
8688
INTERNAL DATA STRUCTURE
8789
*/
@@ -127,16 +129,19 @@ THESE HAVE PRIORITY OVER ANY MODEL ANNOTATIONS (E.G., SmartLabelSource)
127129
* <code>theGraph</code>, using default properties, default circular
128130
* placement of vertices, default automatic spring gravity layout strategy
129131
* and styling from smartgraph.css.
132+
* @see Graph
133+
* @see SmartGraphProperties
134+
* @see SmartCircularSortedPlacementStrategy
135+
* @see ForceDirectedSpringGravityLayoutStrategy
130136
*
131137
* @param theGraph underlying graph
132-
*
133-
* @see Graph
138+
* @throws IllegalArgumentException if <code>theGraph</code> is <code>null</code>
134139
*/
135140
public SmartGraphPanel(Graph<V, E> theGraph) {
136141
this(theGraph,
137142
new SmartGraphProperties(),
138143
new SmartCircularSortedPlacementStrategy(),
139-
null,
144+
new File(DEFAULT_CSS_FILE).toURI(),
140145
new ForceDirectedSpringGravityLayoutStrategy<>()
141146
);
142147
}
@@ -148,14 +153,14 @@ public SmartGraphPanel(Graph<V, E> theGraph) {
148153
*
149154
* @param theGraph underlying graph
150155
* @param layoutStrategy the automatic layout strategy
151-
* @see Graph
156+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
152157
*/
153158
public SmartGraphPanel(Graph<V, E> theGraph, ForceDirectedLayoutStrategy<V> layoutStrategy) {
154159
this(theGraph,
155-
new SmartGraphProperties(),
156-
new SmartCircularSortedPlacementStrategy(),
157-
null,
158-
layoutStrategy
160+
new SmartGraphProperties(),
161+
new SmartCircularSortedPlacementStrategy(),
162+
new File(DEFAULT_CSS_FILE).toURI(),
163+
layoutStrategy
159164
);
160165
}
161166

@@ -166,12 +171,13 @@ public SmartGraphPanel(Graph<V, E> theGraph, ForceDirectedLayoutStrategy<V> layo
166171
*
167172
* @param theGraph underlying graph
168173
* @param properties custom properties
174+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
169175
*/
170176
public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties) {
171177
this(theGraph,
172178
properties,
173179
new SmartCircularSortedPlacementStrategy(),
174-
null,
180+
new File(DEFAULT_CSS_FILE).toURI(),
175181
new ForceDirectedSpringGravityLayoutStrategy<>()
176182
);
177183
}
@@ -183,13 +189,14 @@ public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties) {
183189
* @param theGraph underlying graph
184190
* @param placementStrategy placement strategy
185191
* @param layoutStrategy the automatic layout strategy
192+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
186193
*/
187194
public SmartGraphPanel(Graph<V, E> theGraph, SmartPlacementStrategy placementStrategy,
188195
ForceDirectedLayoutStrategy<V> layoutStrategy) {
189196
this(theGraph,
190197
new SmartGraphProperties(),
191198
placementStrategy,
192-
null,
199+
new File(DEFAULT_CSS_FILE).toURI(),
193200
layoutStrategy
194201
);
195202
}
@@ -202,13 +209,14 @@ public SmartGraphPanel(Graph<V, E> theGraph, SmartPlacementStrategy placementStr
202209
*
203210
* @param theGraph underlying graph
204211
* @param placementStrategy placement strategy, null for default
212+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
205213
*/
206214
public SmartGraphPanel(Graph<V, E> theGraph, SmartPlacementStrategy placementStrategy) {
207215
this(theGraph,
208-
new SmartGraphProperties(),
209-
placementStrategy,
210-
null,
211-
new ForceDirectedSpringGravityLayoutStrategy<>()
216+
new SmartGraphProperties(),
217+
placementStrategy,
218+
new File(DEFAULT_CSS_FILE).toURI(),
219+
new ForceDirectedSpringGravityLayoutStrategy<>()
212220
);
213221
}
214222

@@ -221,14 +229,15 @@ public SmartGraphPanel(Graph<V, E> theGraph, SmartPlacementStrategy placementStr
221229
* @param theGraph underlying graph
222230
* @param properties custom properties, null for default
223231
* @param placementStrategy placement strategy, null for default
232+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
224233
*/
225234
public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties,
226235
SmartPlacementStrategy placementStrategy) {
227236

228237
this(theGraph,
229238
properties,
230239
placementStrategy,
231-
null,
240+
new File(DEFAULT_CSS_FILE).toURI(),
232241
new ForceDirectedSpringGravityLayoutStrategy<>()
233242
);
234243
}
@@ -242,6 +251,7 @@ public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties,
242251
* @param properties custom properties, null for default
243252
* @param placementStrategy placement strategy, null for default
244253
* @param cssFile alternative css file, instead of default 'smartgraph.css'
254+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
245255
*/
246256
public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties,
247257
SmartPlacementStrategy placementStrategy, URI cssFile) {
@@ -257,20 +267,34 @@ public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties,
257267
/**
258268
* Constructs a visualization of the graph referenced by
259269
* <code>theGraph</code>, using custom parameters.
270+
* <br/>
271+
* This is the only FXML-friendly constructor (there can only be one). If you need to instantiate the default
272+
* parameters (besides <code>graph</code>), they are the following:
273+
* <ul>
274+
* <li>properties - <code>new SmartGraphProperties()</code></li>
275+
* <li>placementStrategy - <code>new SmartCircularSortedPlacementStrategy()</code></li>
276+
* <li>cssFileURI - <code>new File("smartgraph.css").toURI()</code></li>
277+
* <li>automaticLayoutStrategy - <code>new ForceDirectedSpringGravityLayoutStrategy()</code></li>
278+
* </ul>
260279
*
261280
* @param theGraph underlying graph
262-
* @param properties custom properties, null for default
263-
* @param placementStrategy placement strategy, null for default
281+
* @param properties custom properties
282+
* @param placementStrategy placement strategy
264283
* @param cssFile alternative css file, instead of default 'smartgraph.css'
265284
* @param layoutStrategy the automatic layout strategy to use
285+
* @throws IllegalArgumentException if any of the arguments is <code>null</code>
266286
*/
267-
public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties,
268-
SmartPlacementStrategy placementStrategy, URI cssFile,
269-
ForceDirectedLayoutStrategy<V> layoutStrategy) {
287+
public SmartGraphPanel(@NamedArg("graph") Graph<V, E> theGraph,
288+
@NamedArg("properties") SmartGraphProperties properties,
289+
@NamedArg("placementStrategy") SmartPlacementStrategy placementStrategy,
290+
@NamedArg("cssFileURI") URI cssFile,
291+
@NamedArg("automaticLayoutStrategy") ForceDirectedLayoutStrategy<V> layoutStrategy) {
270292

271293
Args.requireNotNull(theGraph, "theGraph");
272294
Args.requireNotNull(properties, "properties");
273295
Args.requireNotNull(placementStrategy, "placementStrategy");
296+
Args.requireNotNull(cssFile, "cssFile");
297+
Args.requireNotNull(layoutStrategy, "layoutStrategy");
274298

275299
this.theGraph = theGraph;
276300
this.graphProperties = properties;
@@ -285,7 +309,7 @@ public SmartGraphPanel(Graph<V, E> theGraph, SmartGraphProperties properties,
285309
this.connections = new HashMap<>();
286310

287311
//set stylesheet and class
288-
loadStylesheet(cssFile);
312+
loadAndApplyStylesheet(cssFile);
289313

290314
initNodes();
291315

@@ -347,8 +371,8 @@ public void init() throws IllegalStateException {
347371
this.heightProperty().doubleValue(),
348372
this);
349373
} else {
350-
//apply random placement
351-
new SmartRandomPlacementStrategy().place(this.widthProperty().doubleValue(),
374+
//apply circular placement, I think it's a better initial state for automatic layout
375+
new SmartCircularSortedPlacementStrategy().place(this.widthProperty().doubleValue(),
352376
this.heightProperty().doubleValue(),
353377
this);
354378

@@ -1247,20 +1271,14 @@ public SmartStylableNode getStylableLabel(Edge<E,V> e) {
12471271
/**
12481272
* Loads the stylesheet and applies the .graph class to this panel.
12491273
*/
1250-
private void loadStylesheet(URI cssFile) {
1274+
private void loadAndApplyStylesheet(URI cssFile) {
12511275
try {
1252-
String css;
1253-
if( cssFile != null ) {
1254-
css = cssFile.toURL().toExternalForm();
1255-
} else {
1256-
File f = new File("smartgraph.css");
1257-
css = f.toURI().toURL().toExternalForm();
1258-
}
1259-
1276+
String css = cssFile.toURL().toExternalForm();
12601277
getStylesheets().add(css);
12611278
this.getStyleClass().add("graph");
12621279
} catch (MalformedURLException ex) {
1263-
Logger.getLogger(SmartGraphPanel.class.getName()).log(Level.SEVERE, null, ex);
1280+
String msg = String.format("Error loading stylesheet from URI = %s", cssFile);
1281+
Logger.getLogger(SmartGraphPanel.class.getName()).log(Level.SEVERE, msg, ex);
12641282
}
12651283
}
12661284

0 commit comments

Comments
 (0)