/
WorkflowXMLBuilder.java
827 lines (683 loc) · 27.5 KB
/
WorkflowXMLBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/**
* Copyright (C) 2008-2010 Daniel Senff
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package de.danielsenff.imageflow.controller;
import java.awt.Point;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.zip.DataFormatException;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import visualap.Node;
import de.danielsenff.imageflow.ImageFlow;
import de.danielsenff.imageflow.models.MacroElement;
import de.danielsenff.imageflow.models.connection.Connection;
import de.danielsenff.imageflow.models.connection.ConnectionList;
import de.danielsenff.imageflow.models.connection.Input;
import de.danielsenff.imageflow.models.connection.Output;
import de.danielsenff.imageflow.models.datatype.DataType;
import de.danielsenff.imageflow.models.datatype.ImageDataType;
import de.danielsenff.imageflow.models.delegates.UnitDescription;
import de.danielsenff.imageflow.models.parameter.BooleanParameter;
import de.danielsenff.imageflow.models.parameter.ChoiceParameter;
import de.danielsenff.imageflow.models.parameter.IntegerParameter;
import de.danielsenff.imageflow.models.parameter.Parameter;
import de.danielsenff.imageflow.models.unit.CommentNode;
import de.danielsenff.imageflow.models.unit.GroupUnitElement;
import de.danielsenff.imageflow.models.unit.UnitElement;
import de.danielsenff.imageflow.models.unit.UnitFactory;
import de.danielsenff.imageflow.models.unit.UnitList;
/**
* Read a workflow from ifw-file.
* @author Daniel Senff
*
*/
public class WorkflowXMLBuilder {
private GraphController graphController;
private Collection<ConnectionDelegate> connectionDelegates;
private Collection<GroupUnitElement> groupUnits;
private HashMap<Integer, UnitElement> newNodes;
/**
* Create a builder object based on a {@link UnitList}
* @param graphController
*/
public WorkflowXMLBuilder(final GraphController graphController) {
this.graphController = graphController;
this.connectionDelegates = new Vector<ConnectionDelegate>();
this.newNodes = new HashMap<Integer, UnitElement>();
this.groupUnits = new Vector<GroupUnitElement>();
}
/**
* Reads the contents of a flow-XML input stream.
* @param url
*/
public void read(URL url) {
try {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(url);
Element root = doc.getRootElement();
// read units
Element unitsElement = root.getChild("Units");
readUnits(url, unitsElement);
// at this point we have all units in the workflow, however no connections
// groups are missing their inputs and outputs
// read ConnectionDelegates
for (ConnectionDelegate conndel : connectionDelegates) {
conndel.list.add(readConnection(newNodes, conndel));
}
// read Groups
for (GroupUnitElement unit : groupUnits) {
unit.dealWithConnections(getConnectionList());
}
// read Connections
Element connectionsElement = root.getChild("Connections");
readConnections(newNodes, connectionsElement);
// read Widgets
Element widgetsElement = root.getChild("Widgets");
readWidgets(widgetsElement);
}
catch (Exception e) {
System.err.println("Invalid XML-File!");
e.printStackTrace();
JOptionPane.showMessageDialog(((ImageFlow)ImageFlow.getInstance()).getMainFrame(),
"The selected workflow could not be loaded.",
"Workflow not loaded",
JOptionPane.INFORMATION_MESSAGE);
}
}
private void readWidgets(Element widgetsElement) {
if (widgetsElement != null) {
List<Element> widgetsList = widgetsElement.getChildren();
Iterator<Element> widgetsIterator = widgetsList.iterator();
// loop over all Widgets
Element actualWidgetElement;
Node node;
while (widgetsIterator.hasNext()) {
actualWidgetElement = widgetsIterator.next();
readWidget(actualWidgetElement);
}
}
}
private void readWidget(Element actualWidgetElement) {
int xPos = Integer.parseInt(actualWidgetElement.getChild("XPos").getValue());
int yPos = Integer.parseInt(actualWidgetElement.getChild("YPos").getValue());
// id of the unit this widget is attached to
int id = Integer.parseInt(actualWidgetElement.getChild("UnitID").getValue());
Point position = new Point(xPos, yPos);
UnitElement unit = newNodes.get(id);
if (actualWidgetElement.getName().equalsIgnoreCase("PreviewWidget"))
graphController.addPreviewWidget(unit, position);
else if (actualWidgetElement.getName().equalsIgnoreCase("Widget"))
graphController.addWidget(unit, position);
}
private void readUnits(URL url, Element unitsElement) {
if (unitsElement != null) {
List<Element> unitsList = unitsElement.getChildren();
Iterator<Element> unitsIterator = unitsList.iterator();
// loop over all Units
Element actualUnitElement;
Node node;
while (unitsIterator.hasNext()) {
actualUnitElement = unitsIterator.next();
node = readNode(actualUnitElement, url);
graphController.addNode(node, node.getOrigin());
}
}
}
/**
* @param actualUnitElement
* @param url
* @return
*/
protected Node readNode(final Element actualUnitElement, final URL url) {
int xPos = Integer.parseInt(actualUnitElement.getChild("XPos").getValue());
int yPos = Integer.parseInt(actualUnitElement.getChild("YPos").getValue());
Point position = new Point(xPos, yPos);
String label = "";
if(actualUnitElement.getChild("Label") != null)
label = actualUnitElement.getChild("Label").getValue();
Element unitDescriptionElement = actualUnitElement.getChild("UnitDescription");
Node node;
if(actualUnitElement.getChild("UnitID") != null
&& unitDescriptionElement != null) {
node = createUnitElement(actualUnitElement, url, position, label, unitDescriptionElement);
} else {
node = createCommentNode(position, label);
}
return node;
}
/**
* Create the unit element described in the workflow xml file.
* the Unit definition is the same as the XML Unit Definition.
* @param actualUnitElement
* @param url
* @param position
* @param label
* @param unitDescriptionElement
* @return
*/
protected Node createUnitElement(final Element actualUnitElement,
final URL url,
final Point position,
final String label,
final Element unitDescriptionElement) {
int unitID = Integer.parseInt(actualUnitElement.getChild("UnitID").getValue());
UnitDescription unitDescription = new UnitDescription(url);
try {
unitDescription.readXML(unitDescriptionElement);
} catch (DataFormatException e) {
e.printStackTrace();
}
Element unitsElement = unitDescriptionElement.getChild("Units");
UnitElement unitElement;
Element internalConnectionsElement = unitDescriptionElement.getChild("InternalConnections");
Element externalConnectionsElement = unitDescriptionElement.getChild("ExternalConnections");
Element originalConnectionsElement = unitDescriptionElement.getChild("OriginalConnections");
if(unitsElement != null
&& internalConnectionsElement != null
&& externalConnectionsElement != null
&& originalConnectionsElement != null) {
//create GroupUnit
unitElement = readGroup(url, position, label, unitID,
unitDescription, unitsElement,
internalConnectionsElement,
externalConnectionsElement,
originalConnectionsElement);
} else {
// create UnitElement
unitElement = createProcessingUnit(position, label, unitID, unitDescription);
}
return unitElement;
}
private Node createCommentNode(final Point position, String label) {
return new CommentNode(position, label);
}
/**
* Create a regular processing {@link UnitElement}.
* @param position
* @param label
* @param unitID
* @param unitDescription
* @return
*/
protected UnitElement createProcessingUnit(final Point position,
final String label,
final int unitID,
final UnitDescription unitDescription) {
UnitElement unitElement = UnitFactory.createProcessingUnit(unitDescription, position);
unitElement.setDisplay(unitDescription.getIsDisplayUnit());
unitElement.setHelpString(unitDescription.helpString);
unitElement.setLabel(label);
newNodes.put(unitID, unitElement);
return unitElement;
}
/**
* Read {@link GroupUnitElement} from XML.
* @param url
* @param position
* @param label
* @param unitID
* @param unitDescription
* @param unitsElement
* @param internalConnectionsElement
* @param externalConnectionsElement
* @param originalConnectionsElement
* @return
*/
protected UnitElement readGroup(final URL url,
final Point position,
final String label,
final int unitID,
final UnitDescription unitDescription,
final Element unitsElement,
final Element internalConnectionsElement,
final Element externalConnectionsElement,
final Element originalConnectionsElement) {
UnitElement unitElement = new GroupUnitElement(position, label);
HashMap<Integer, UnitElement> embeddedNodes = new HashMap<Integer, UnitElement>();
unitElement.setHelpString(unitDescription.helpString);
newNodes.put(unitID, unitElement);
groupUnits.add((GroupUnitElement) unitElement);
/*for (int i = 0; i < unitDescription.numInputs; i++) {
unitElement.addInput(UnitFactory.createInput(unitDescription.input[i+1], unitElement, i+1));
}
for (int i = 0; i < unitDescription.numOutputs; i++) {
unitElement.addOutput(UnitFactory.createOutput(unitDescription.output[i+1], unitElement, i+1));
}*/
if(unitsElement != null) {
List<Element> unitsList = unitsElement.getChildren();
Iterator<Element> unitsIterator = unitsList.iterator();
// loop over all Units
while (unitsIterator.hasNext()) {
Element actualEmbeddedUnitElement = unitsIterator.next();
Node embeddedNode = readNode(actualEmbeddedUnitElement, url);
((GroupUnitElement)unitElement).getNodes().add(embeddedNode);
if(embeddedNode instanceof UnitElement) {
System.out.println("put in embeddedNodes: " + embeddedNode
+ " unitid: "+ ((UnitElement)embeddedNode).getNodeID());
newNodes.put(((UnitElement)embeddedNode).getNodeID(), (UnitElement) embeddedNode);
embeddedNodes.put(((UnitElement)embeddedNode).getNodeID(), (UnitElement) embeddedNode);
}
}
}
if(internalConnectionsElement != null) {
List<Element> connectionsList = internalConnectionsElement.getChildren();
Collection<Connection> groupInternalConnections =
((GroupUnitElement)unitElement).getInternalConnections();
readGroupConnections(connectionsList, groupInternalConnections);
}
if(originalConnectionsElement != null) {
// FIXME WTF, why internal connection here? is this a bug?
List<Element> connectionsList = internalConnectionsElement.getChildren();
Collection<Connection> groupOriginalConnections =
((GroupUnitElement)unitElement).getOriginalConnections();
readGroupConnections(connectionsList, groupOriginalConnections);
}
if(externalConnectionsElement != null) {
List<Element> connectionsList = externalConnectionsElement.getChildren();
Collection<Connection> groupExternalConnections =
((GroupUnitElement)unitElement).getExternalConnections();
readGroupConnections(connectionsList, groupExternalConnections);
}
return unitElement;
}
private void readGroupConnections(List<Element> connectionsList,
Collection<Connection> groupExternalConnections) {
Iterator<Element> connectionsIterator = connectionsList.iterator();
// external connections require the hashmap with all embedded and existing units
// we need all units of the workflow :/
ConnectionDelegate conDelegate;
while (connectionsIterator.hasNext()) {
conDelegate = new ConnectionDelegate(connectionsIterator.next(), groupExternalConnections);
connectionDelegates.add(conDelegate);
}
}
private class ConnectionDelegate {
public int fromUnitID, fromUnitOutput;
public int toUnitID, toUnitInput;
// to add this list
public Collection<Connection> list;
public ConnectionDelegate(int fromUnitID, int fromUnitOutput,
int toUnitID, int toUnitInput, Collection<Connection> list) {
this.fromUnitID = fromUnitID;
this.fromUnitOutput = fromUnitOutput;
this.toUnitID = toUnitID;
this.toUnitInput = toUnitInput;
this.list = list;
}
public ConnectionDelegate(Element connectionElement, Collection<Connection> list) {
this(Integer.parseInt(connectionElement.getChild("FromUnitID").getValue()),
Integer.parseInt(connectionElement.getChild("FromOutputNumber").getValue()),
Integer.parseInt(connectionElement.getChild("ToUnitID").getValue()),
Integer.parseInt(connectionElement.getChild("ToInputNumber").getValue()),
list
);
}
}
private void readConnections(HashMap<Integer, UnitElement> newNodes, Element connectionsElement) {
//unitsElement != null &&
if ( connectionsElement != null) {
List<Element> connectionsList = connectionsElement.getChildren();
Iterator<Element> connectionsIterator = connectionsList.iterator();
while (connectionsIterator.hasNext()) {
getUnitList().addConnection(readConnection(newNodes, connectionsIterator.next()));
}
}
}
private static Connection readConnection(HashMap<Integer, UnitElement> newNodes,
Element connectionElement) {
int fromUnitID =
Integer.parseInt(connectionElement.getChild("FromUnitID").getValue());
int fromOutputNumber =
Integer.parseInt(connectionElement.getChild("FromOutputNumber").getValue());
int toUnitID =
Integer.parseInt(connectionElement.getChild("ToUnitID").getValue());
int toInputNumber =
Integer.parseInt(connectionElement.getChild("ToInputNumber").getValue());
Connection con = null;
try {
con = new Connection(newNodes.get(fromUnitID), fromOutputNumber,
newNodes.get(toUnitID), toInputNumber);
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println("fromUnitID " + fromUnitID
+ " fromOutputNumber" + fromOutputNumber
+ " toUnitId " + toUnitID
+ " toInputNumber " + toInputNumber);
}
return con;
}
private static Connection readConnection(HashMap<Integer, UnitElement> newNodes,
ConnectionDelegate connDel) {
int fromUnitID = connDel.fromUnitID;
int fromOutputNumber = connDel.fromUnitOutput;
int toUnitID = connDel.toUnitID;
int toInputNumber = connDel.toUnitInput;
Connection con = null;
try {
con = new Connection(newNodes.get(fromUnitID), fromOutputNumber,
newNodes.get(toUnitID), toInputNumber);
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println("fromUnitID " + fromUnitID
+ " fromOutputNumber" + fromOutputNumber
+ " toUnitId " + toUnitID
+ " toInputNumber " + toInputNumber);
}
return con;
}
/**
* @param file
* @throws IOException
*/
public void write(final File file) throws IOException {
Element root = new Element("FlowDescription");
Document flowGraph = new Document(root);
// Unit Definitions
Element units = new Element("Units");
Element unitElement;
root.addContent(units);
for (Node node : getUnitList()) {
unitElement = new Element("Unit");
units.addContent(unitElement);
writeXMLNode(node, unitElement);
}
// Connection Definitions
Element connectionsElement = new Element("Connections");
root.addContent(connectionsElement);
for (Connection connection : getConnectionList()) {
writeXMLConnection(connectionsElement, connection);
}
// Dash Widget Definitions
Element widgets = new Element("Widgets");
root.addContent(widgets);
Element widgetElement;
for (Node node : getUnitList()) {
if(node instanceof UnitElement) {
UnitElement unit = (UnitElement) node;
if(unit.hasWidget()) { // properties widget
widgetElement = new Element("Widget");
writeXMLWidget(unit, widgetElement, unit.getWidget());
widgets.addContent(widgetElement);
} else if(unit.hasPreviewWidget()) { // preview widget
widgetElement = new Element("PreviewWidget");
writeXMLWidget(unit, widgetElement, unit.getPreviewWidget());
widgets.addContent(widgetElement);
}
}
}
// output
FileOutputStream fos = new FileOutputStream(file);
new XMLOutputter(Format.getPrettyFormat()).output(flowGraph, fos);
}
private void writeXMLWidget(UnitElement unit, Element widgetElement, JComponent widget) {
// id of the unit this widget is attached to
Element unitID = new Element("UnitID");
unitID.addContent(unit.getNodeID()+"");
widgetElement.addContent(unitID);
// id of the unit this widget is attached to
Element xPos = new Element("XPos");
xPos.addContent(widget.getLocation().x+"");
widgetElement.addContent(xPos);
Element yPos = new Element("YPos");
yPos.addContent(widget.getLocation().y+"");
widgetElement.addContent(yPos);
}
private void writeXMLNode(Node node, Element unitElement) {
// unit details
Element xPos = new Element("XPos");
xPos.addContent(node.getOrigin().x+"");
unitElement.addContent(xPos);
Element yPos = new Element("YPos");
yPos.addContent(node.getOrigin().y+"");
unitElement.addContent(yPos);
Element label = new Element("Label");
label.addContent(node.getLabel());
unitElement.addContent(label);
if(node instanceof UnitElement) {
UnitElement unit = (UnitElement) node;
Element unitDescription = writeXMLUnitElement(unit, unitElement);
if(node instanceof GroupUnitElement) {
GroupUnitElement group = (GroupUnitElement)node;
// groupUnits have additional fields for
// unitlist included in group
Element embeddedUnitsElement = new Element("Units");
unitDescription.addContent(embeddedUnitsElement);
Element embeddedUnitElement;
for (Node embeddedNode : group.getNodes()) {
embeddedUnitElement = new Element("Unit");
embeddedUnitsElement.addContent(embeddedUnitElement);
writeXMLNode(embeddedNode, embeddedUnitElement);
}
// list of internal connections
Element internalConnectionsElement = new Element("InternalConnections");
unitDescription.addContent(internalConnectionsElement);
for (Connection connection : group.getInternalConnections()) {
writeXMLConnection(internalConnectionsElement, connection);
}
// list of external connections
Element externalConnectionsElement = new Element("ExternalConnections");
unitDescription.addContent(externalConnectionsElement);
for (Connection connection : group.getExternalConnections()) {
writeXMLConnection(externalConnectionsElement, connection);
}
// list of original connections
Element originalConnectionsElement = new Element("OriginalConnections");
unitDescription.addContent(originalConnectionsElement);
for (Connection connection : group.getOriginalConnections()) {
writeXMLConnection(originalConnectionsElement, connection);
}
}
} // end unitelement
}
private Element writeXMLUnitElement(UnitElement unit, Element unitElement) {
Element unitID = new Element("UnitID");
unitID.addContent(unit.getNodeID()+"");
unitElement.addContent(unitID);
// unit description analog to the Unit-XML
Element unitDescription = new Element("UnitDescription");
unitElement.addContent(unitDescription);
Element general = new Element("General");
unitDescription.addContent(general);
Element unitName = new Element("UnitName");
unitName.addContent(unit.getUnitName());
general.addContent(unitName);
Element pathToIcon = new Element("PathToIcon");
pathToIcon.addContent(unit.getIconPath());
general.addContent(pathToIcon);
Element imageJSyntax = new Element("ImageJSyntax");
imageJSyntax.addContent(((MacroElement)unit.getObject()).getImageJSyntax());
general.addContent(imageJSyntax);
Element color = new Element("Color");
String colorHex = Integer.toHexString( unit.getColor().getRGB() );
color.addContent("0x"+colorHex.substring(2));
general.addContent(color);
Element iconSize = new Element("IconSize");
iconSize.addContent(unit.getCompontentSize().toString());
general.addContent(iconSize);
Element helpStringU = new Element("HelpString");
helpStringU.addContent(unit.getHelpString());
general.addContent(helpStringU);
// display shows the result in a new window
Element doDisplayU = new Element("DoDisplay");
doDisplayU.addContent(unit.isDisplay() ? "true" : "false");
general.addContent(doDisplayU);
// silent display will not show the result at all, but still store it internally
Element doDisplaySilentU = new Element("DoDisplaySilent");
doDisplaySilentU.addContent(unit.isDisplaySilent() ? "true" : "false");
general.addContent(doDisplaySilentU);
// deal with all parameters
Element parameters = new Element("Parameters");
unitDescription.addContent(parameters);
for (Parameter parameter : unit.getParameters()) {
Element parameterElement = new Element("Parameter");
parameters.addContent(parameterElement);
Element name = new Element("Name");
name.addContent(parameter.getDisplayName());
parameterElement.addContent(name);
Element dataType = new Element("DataType");
dataType.addContent(parameter.getParaType());
parameterElement.addContent(dataType);
// FIXME look why getOptions can return null
if(parameter.getOptions() != null && parameter.getOptions().get("as") != null) {
String dataTypeAs = (String) parameter.getOptions().get("as");
dataType.setAttribute("as", dataTypeAs);
}
Element readOnlyP = new Element("ReadOnly");
readOnlyP.addContent(parameter.isReadOnly() ? "true" : "false");
parameterElement.addContent(readOnlyP);
Element hiddenP = new Element("Hidden");
hiddenP.addContent(parameter.isHidden() ? "true" : "false");
parameterElement.addContent(hiddenP);
Element value = new Element("Value");
value.addContent(parameter.getValue()+"");
Element helpStringP = new Element("HelpString");
helpStringP.addContent(parameter.getHelpString());
parameterElement.addContent(helpStringP);
//special parameters
if(parameter instanceof ChoiceParameter) {
value = new Element("Value");
ChoiceParameter choiceParameter = (ChoiceParameter)parameter;
String choiceString = choiceParameter.getChoicesString();
value.addContent(choiceString);
Element choiceNumber = new Element("ChoiceNumber");
choiceNumber.addContent(choiceParameter.getChoiceIndex()+"");
parameterElement.addContent(choiceNumber);
}
parameterElement.addContent(value);
if(parameter instanceof IntegerParameter) {
if(parameter.getOptions().get("min") != null) {
int min = (Integer) parameter.getOptions().get("min");
value.setAttribute("min", Integer.toString(min));
}
if(parameter.getOptions().get("max") != null) {
int max = (Integer) parameter.getOptions().get("max");
value.setAttribute("max", Integer.toString(max));
}
}
if(parameter instanceof BooleanParameter) {
Element trueString = new Element("TrueString");
trueString.addContent(((BooleanParameter)parameter).getTrueString());
parameterElement.addContent(trueString);
}
}
// deal with inputs
Element inputs = new Element("Inputs");
unitDescription.addContent(inputs);
for (Input input : unit.getInputs()) {
writeXMLInput(inputs, input);
}
// deal with outputs
Element outputs = new Element("Outputs");
unitDescription.addContent(outputs);
for (Output output : unit.getOutputs()) {
writeXMLOutput(outputs, output);
}
return unitDescription;
}
private void writeXMLInput(Element inputs, Input input) {
Element inputElement = new Element("Input");
inputs.addContent(inputElement);
Element name = new Element("Name");
name.addContent(input.getDisplayName());
inputElement.addContent(name);
Element shortName = new Element("ShortName");
shortName.addContent(input.getShortDisplayName());
inputElement.addContent(shortName);
Element required = new Element("Required");
required.addContent(input.isRequired() ? "true" : "false");
inputElement.addContent(required);
Element dataTypeElement = new Element("DataType");
DataType dataType = input.getDataType();
dataTypeElement.addContent(dataType.getSimpleName());
inputElement.addContent(dataTypeElement);
if(dataType instanceof ImageDataType) {
Element imageType = new Element("ImageType");
imageType.addContent(""+((ImageDataType)dataType).getImageBitDepth());
inputElement.addContent(imageType);
}
Element needToCopyInput = new Element("NeedToCopyInput");
String boolNeedCopy = input.isNeedToCopyInput() ? "true" : "false";
needToCopyInput.addContent(boolNeedCopy);
inputElement.addContent(needToCopyInput);
}
private void writeXMLOutput(Element outputs, Output output) {
Element outputElement = new Element("Output");
outputs.addContent(outputElement);
Element name = new Element("Name");
name.addContent(output.getDisplayName());
outputElement.addContent(name);
Element shortName = new Element("ShortName");
shortName.addContent(output.getShortDisplayName());
outputElement.addContent(shortName);
Element dataType = new Element("DataType");
dataType.addContent(output.getDataType().getSimpleName());
outputElement.addContent(dataType);
if(output.getDataType() instanceof ImageDataType) {
Element imageType = new Element("ImageType");
imageType.addContent(""+((ImageDataType)output.getDataType()).getImageBitDepth());
outputElement.addContent(imageType);
}
// In case of plugins with multiple outputs
// I want to leave the option to display only selected outputs
Element doDisplay = new Element("DoDisplay");
String boolIsDisplay = output.isDoDisplay() ? "true" : "false";
doDisplay.addContent(boolIsDisplay);
outputElement.addContent(doDisplay);
Element doDisplaySilent = new Element("DoDisplaySilent");
String boolIsDisplaySilent = output.isDoDisplaySilent() ? "true" : "false";
doDisplay.addContent(boolIsDisplaySilent);
outputElement.addContent(doDisplaySilent);
}
private void writeXMLConnection(Element connectionsElement, Connection edge) {
Connection conn = (Connection) edge;
Element connectionElement = new Element("Connection");
connectionsElement.addContent(connectionElement);
Element fromUnitID = new Element("FromUnitID");
fromUnitID.addContent(""+ ((UnitElement)conn.getFromUnit()).getNodeID());
connectionElement.addContent(fromUnitID);
Element fromOutputNumber = new Element("FromOutputNumber");
fromOutputNumber.addContent(""+conn.getOutput().getIndex());
connectionElement.addContent(fromOutputNumber);
Element toUnitID = new Element("ToUnitID");
toUnitID.addContent(""+((UnitElement)conn.getToUnit()).getNodeID());
connectionElement.addContent(toUnitID);
Element toInputNumber = new Element("ToInputNumber");
toInputNumber.addContent(""+conn.getInput().getIndex());
connectionElement.addContent(toInputNumber);
}
public UnitList getUnitList() {
return this.graphController.getUnitElements();
}
/**
* Returns the Connections in the UnitList processed by this Builder.
* @return
*/
public ConnectionList getConnectionList() {
return getUnitList().getConnections();
}
}