~tapaal-maintainers/tapaal/tapaal-dev

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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
package dk.aau.cs.gui.smartDraw;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.tapaal.swinghelpers.CustomJSpinner;
import pipe.gui.CreateGui;
import pipe.gui.graphicElements.PetriNetObject;

public class SmartDrawDialog extends JDialog {
	private static final long serialVersionUID = 6116530047981607501L;
	
	
	private static String getHelpMessage(){ 
		// There is automatic word wrapping in the control that displays the text, so you don't need line breaks in paragraphs.
		StringBuffer buffer = new StringBuffer();
		buffer.append("<html>");
		buffer.append("<b>Automatic net layout options</b><br/>");
		buffer.append("<br/><br/>");
		buffer.append("<b>Search Option</b><br/>\n");
                buffer.append("You can choose between drawing in a Depth First (DFS) or a Breadth First (BFS)\n");
                buffer.append("manner. This may yield a difference as an object is only place once and thus\n");
                buffer.append("reserves its given position. This choice affects which objects are placed first.\n");
		buffer.append("<br/><br/>");
		buffer.append("<b>Functionality</b><br/>");
	        buffer.append("The automatic layout works by choosing a starting object.\n");
	        buffer.append("From there we try different positions around the starting object and choose the position with lowest <em>penalty</em>.\n");
	    buffer.append("<br/><br/>");
	        buffer.append("We try each multiple of x-spacing between <em>parent.x + x-spacing * layer</em> and <em>parent.x - x-spacing * layer</em>\n");
	        buffer.append("<em>parent.x - x-spacing * layer</em> with each multiple of y-spacing\n");
	        buffer.append("between <em>y-spacing * layer</em> and <em>-y-spacing * layer</em>.\n");
	        buffer.append("The value of <em>layer</em> will increment with each iteration and start with value 1.\n");
	        buffer.append("As such choosing the minimum iterations will decide how many layers we try.\n");
	        buffer.append("Minimum Iterations heavily affects how long it will take to do the layout, so a small number is recommended.\n");
	        buffer.append("It is also recommended that x-spacing = y-spacing.\n");
        buffer.append("<br/><br/>");
		buffer.append("<b>Penalties</b><br/>");
		buffer.append("The penalties should be seen as punishments for choosing a position.\n");
                buffer.append("The higher the penalty the higher the punishment; the position with the lowest\n");
                buffer.append("accumulated penalty is where the object will be placed.");
		buffer.append("<br/><br/>");
			buffer.append("<em>Straight arc penalty</em> is a penalty punishing going straight\n");
			buffer.append("i.e. if the candidate position's x or y equals the parent's x or y\n");
			buffer.append("the <em>straight penalty * layer</em> is added to the total penalty.\n");
			buffer.append("If not <em>Diagonal arc penalty * layer</em> is added instead.");
		buffer.append("<br/><br/>");
			buffer.append("<em>Distance penalty</em> punishes candidate positions depending on\n");
			buffer.append("how far away from the starting point they are. As such a higher\n");
			buffer.append("distance penalty will make more compact nets.");
		buffer.append("<br/><br/>");
			buffer.append("<em>Overlapping arc penalty</em> punishes arcs laying directly on top of each other");
			buffer.append("pointing to the same object.");
		buffer.append("<br/><br/>");
			buffer.append("<b>Example:</b><br/>");
		buffer.append("<br/><br/>");
			buffer.append("<img src=\"" + Thread.currentThread().getContextClassLoader().getResource(CreateGui.imgPath + "SmartDrawExampleWithLayers.png") +"\" />");
		buffer.append("<br/><br/>");
		buffer.append("This layout was created with the default values.\n");
		buffer.append("On the figure the numbers and boxes describe the layer. Furthermore, the effect of the <em>Overlapping arc penalty</em> can be seen\n");
		buffer.append("as the objects in layer 2 prefer going diagonal rather than overlap due to the penaltys.");
		buffer.append("</html>");
		return buffer.toString(); 
	}
	
	JPanel mainPanel;
	String startingObject = "Random";
	//String template;
	String[] objectNames;
	//String[] templateNames;
	JComboBox<String> objectDropdown = new JComboBox<String>();
	JCheckBox randomStartObjectCheckBox;
	//JComboBox<Object> templateSelector = new JComboBox<Object>();
	JDialog loadingDialogFrame;
	//JDialog choiceModal;
	SmartDrawWorker worker;
	JLabel timerLabel = new JLabel("Time elapsed: ");
	JLabel progressLabel = new JLabel("Objects placed: ");
	JLabel statusLabel;
	JSpinner xSpinner;
	JButton drawButton;
	long startTimeMs;
	int objectsProgress;

	
	int xSpacing = 80;
	int ySpacing = 80;
	String searchOption = "DFS";
	int straightWeight = 5;
	int diagonalWeight = 8;
	int distanceWeight = 10;
	int overlappingArcWeight = 100;
	int minimumIterations = 3;
	
	private Timer timer = new Timer(1000, new AbstractAction() {
		private static final long serialVersionUID = 2126744033604698592L;

		public void actionPerformed(ActionEvent e) {
			timerLabel.setText("Time elapsed: " + (System.currentTimeMillis() - startTimeMs) / 1000 + " s");
		}
	});
	
	public static SmartDrawDialog smartDrawDialog;
	public static void showSmartDrawDialog() {
		
		if(smartDrawDialog == null){
			smartDrawDialog = new SmartDrawDialog(CreateGui.getApp(), "Smart Draw", true);
			smartDrawDialog.pack();
			smartDrawDialog.setPreferredSize(smartDrawDialog.getSize());
			smartDrawDialog.setMinimumSize(new Dimension(smartDrawDialog.getWidth(), smartDrawDialog.getHeight()));
			smartDrawDialog.setLocationRelativeTo(null);
			smartDrawDialog.setResizable(false);
		}
		smartDrawDialog.updateLists();
		smartDrawDialog.enableButtons();
		smartDrawDialog.setEnabled(true);
		smartDrawDialog.setVisible(true);
	}

	private SmartDrawDialog(Frame frame, String title, boolean modal) {
		super(frame, title, modal);
		initComponents();
	}
	
	private void updateLists() {
		objectDropdown.removeAllItems();
		for(String name : getObjectNames()) {
			objectDropdown.addItem(name);
		}
		
		/*templateSelector.removeAllItems();
		for(String name : getTemplatesAsString()) {
			templateSelector.addItem(name);
		}*/
	}
	
	private void initComponents() {
		
		setLayout(new FlowLayout());
		mainPanel = new JPanel(new GridBagLayout());
		
		initSpacingSelecters();
		initCheckBoxes();
		initAdvancedOptionsPanel();
		//initChoiceModal();
		
		/*templateSelector.setEnabled(false);
		
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		mainPanel.add(templateSelector, gbc);*/
		JButton helpButton = new JButton("Help");
		helpButton.setToolTipText("Help with the different options");
		helpButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(CreateGui.getAppGui(), getMessageComponent(), "Help", JOptionPane.INFORMATION_MESSAGE);
				}
			});
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.SOUTHWEST;
		mainPanel.add(helpButton, gbc);
		
		
		drawButton = new JButton("Smart Draw");
		drawButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				initLoadingFrame();
				worker = new SmartDrawWorker(xSpacing, ySpacing, CreateGui.getDrawingSurface(), searchOption, 
						straightWeight, diagonalWeight, distanceWeight, overlappingArcWeight, startingObject, minimumIterations);
				worker.addSmartDrawListener(new SmartDrawListener() {
					
					@Override
					public void fireStatusChanged(int objectsPlaced) {
						progressLabel.setText("Objects placed: " + objectsPlaced +"/" + CreateGui.getDrawingSurface().getGuiModel().getPlaceTransitionObjects().size());
						
					}
					
					@Override
					public void fireStartDraw() {
						statusLabel.setText("Working...");
						if (timer.isRunning())
							timer.restart();
						else
							timer.start();
						startTimeMs = System.currentTimeMillis();
						
					}
					
					@Override
					public void fireDone(boolean cancelled) {
						if(!(cancelled)) {
							loadingDialogFrame.dispose();
							CreateGui.getAppGui().toFront();
							CreateGui.getAppGui().requestFocus();
						} else {
							statusLabel.setText("Cancelling/Undoing");
						}
					}
				});
				worker.execute();
				smartDrawDialog.setVisible(false);
				loadingDialogFrame.setVisible(true);
				/*loadingDialogFrame.toFront();
				loadingDialogFrame.requestFocus();
				loadingDialogFrame.setAlwaysOnTop(true);*/
			}
		});
		
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.SOUTHEAST;
		mainPanel.add(drawButton, gbc);
		
		
		this.getRootPane().setDefaultButton(drawButton);
		drawButton.requestFocus();
		
		setContentPane(mainPanel);
	}
	
	private void initAdvancedOptionsPanel() {
		JPanel advancedOptionsPanel = new JPanel(new GridBagLayout());
		advancedOptionsPanel.setBorder(new TitledBorder("Advanced Options"));
		
		JLabel comboBoxLabel = new JLabel("Choose Initial Object:");
		comboBoxLabel.setToolTipText("Choose a starting  object");
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(comboBoxLabel, gbc);
		
		objectDropdown.setEnabled(false);
		objectDropdown.setToolTipText("Choose a starting object");
		objectDropdown.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(objectDropdown.getSelectedItem() != null && objectDropdown.isEnabled())
					startingObject = objectDropdown.getSelectedItem().toString();
			}
		});

		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(objectDropdown, gbc);
		
		randomStartObjectCheckBox = new JCheckBox("Random Initial Object:", true);
		randomStartObjectCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
		randomStartObjectCheckBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(randomStartObjectCheckBox.isSelected()) {
					startingObject = "Random";
					objectDropdown.setEnabled(false);
				} else {
					objectDropdown.setEnabled(true);
					startingObject = objectDropdown.getSelectedItem().toString();
				}
					
				
			}
		});
		
		gbc = new GridBagConstraints();
		gbc.gridx = 2;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(randomStartObjectCheckBox, gbc);
		
		
		JLabel straightWeightLabel = new JLabel("Straight Arc Penalty:");
		straightWeightLabel.setToolTipText("Higher number decreases the number of horizontal and vertical arcs");
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(straightWeightLabel, gbc);
		
		final JSpinner straightWeightSpinner = new CustomJSpinner(straightWeight);
		straightWeightSpinner.setToolTipText("Higher number decreases the number of horizontal and vertical arcs");
		straightWeightSpinner.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				straightWeight = (Integer) straightWeightSpinner.getValue();
			}
		});
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(straightWeightSpinner, gbc);
		
		JLabel diagonalWeightLabel = new JLabel("Diagonal Arc Penalty:");
		diagonalWeightLabel.setToolTipText("Higher number decreases the number of diagonal arcs");
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(diagonalWeightLabel, gbc);
		
		final JSpinner diagonalWeightSpinner = new CustomJSpinner(diagonalWeight);
		diagonalWeightSpinner.setToolTipText("Higher number decreases the number of diagonal arcs");
		diagonalWeightSpinner.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				diagonalWeight = (Integer) diagonalWeightSpinner.getValue();
			}
		});
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(diagonalWeightSpinner, gbc);
		
		JLabel distanceWeightLabel = new JLabel("Distance Penalty:");
		distanceWeightLabel.setToolTipText("Higher penalty will make the layout more compact");
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(distanceWeightLabel, gbc);
		
		final JSpinner distanceWeightSpinner = new CustomJSpinner(distanceWeight);
		distanceWeightSpinner.setToolTipText("Higher penalty will make the layout more compact");
		distanceWeightSpinner.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				distanceWeight = (Integer) distanceWeightSpinner.getValue();
			}
		});
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(distanceWeightSpinner, gbc);
		
		JLabel overlappingWeightLabel = new JLabel("Overlapping Arc Penalty:");
		overlappingWeightLabel.setToolTipText("Higher penalty will decrease the number of arcs that cross other objects");
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(overlappingWeightLabel, gbc);
		
		final JSpinner overlappingWeightSpinner = new CustomJSpinner(overlappingArcWeight);
		overlappingWeightSpinner.setToolTipText("Higher penalty will decrease the number of arcs that cross other objects");
		overlappingWeightSpinner.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				overlappingArcWeight = (Integer) overlappingWeightSpinner.getValue();
			}
		});
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 4;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(overlappingWeightSpinner, gbc);
		
		JLabel minimumIterationsLabel = new JLabel("Minimum Iterations:");
		minimumIterationsLabel.setToolTipText("Higher number increases the number of positions tried for each object");
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 5;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(minimumIterationsLabel, gbc);
		
		final JSpinner minimumIterationSpinner = new CustomJSpinner(minimumIterations);
		minimumIterationSpinner.setToolTipText("Higher number increases the number of positions tried for each object");
		minimumIterationSpinner.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				minimumIterations = (Integer) minimumIterationSpinner.getValue();
			}
		});
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 5;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		advancedOptionsPanel.add(minimumIterationSpinner, gbc);
		
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.gridheight = 3;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		mainPanel.add(advancedOptionsPanel, gbc);
		
	}
	private void initCheckBoxes() {
		JPanel checkBoxPanel = new JPanel(new GridBagLayout());
		checkBoxPanel.setBorder(new TitledBorder("Search Option"));
		
		JRadioButton DFS = new JRadioButton("DFS:");
		DFS.setToolTipText("Draw in a depth first manner from start object");
		DFS.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				searchOption = "DFS";
			}
		});
		JRadioButton BFS = new JRadioButton("BFS:");
		BFS.setToolTipText("Draw in a breadth first manner from start object");

		BFS.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				searchOption = "BFS";
			}
		});
		
		/*JRadioButton randomSearch = new JRadioButton("Random:");
		randomSearch.setEnabled(false);
		randomSearch.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				searchOption = "Random";
			}
		});*/
		
	    ButtonGroup group = new ButtonGroup();
	    group.add(DFS);
	    group.add(BFS);
	    //group.add(randomSearch);
	    
	    DFS.setSelected(true);
	    DFS.setHorizontalTextPosition(SwingConstants.LEFT);
	    GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		checkBoxPanel.add(DFS, gbc);
		
	    BFS.setHorizontalTextPosition(SwingConstants.LEFT);
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		checkBoxPanel.add(BFS, gbc);
		
		/*randomSearch.setHorizontalTextPosition(SwingConstants.LEFT);
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		checkBoxPanel.add(randomSearch, gbc);*/
		
		
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		mainPanel.add(checkBoxPanel, gbc);

	}
	
	private void initSpacingSelecters(){
		JPanel spacingPanel = new JPanel(new GridBagLayout());
		spacingPanel.setBorder(new TitledBorder("Spacing"));
		
		
		JLabel xLabel = new JLabel("Spacing on the x-axis:");
		xLabel.setToolTipText("Set the distance there should be between objects on the x-axis");
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 10, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		spacingPanel.add(xLabel, gbc);
		
		xSpinner = new CustomJSpinner(xSpacing);
		xSpinner.setToolTipText("Set the distance there should be between objects on the x-axis");

		xSpinner.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				xSpacing = (Integer)xSpinner.getValue();
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 10, 10, 10);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		spacingPanel.add(xSpinner, gbc);
		
		JLabel yLabel = new JLabel("Spacing on the y-axis:");
		yLabel.setToolTipText("Set the distance there should be between objects on the y-axis");
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 10, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		spacingPanel.add(yLabel, gbc);
		
		final JSpinner ySpinner = new CustomJSpinner(ySpacing);
		ySpinner.setToolTipText("Set the distance there should be between objects on the y-axis");
		ySpinner.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				ySpacing = (Integer)ySpinner.getValue();
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 10, 10, 10);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		spacingPanel.add(ySpinner, gbc);
		
		
		
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10, 20, 10, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		mainPanel.add(spacingPanel, gbc);
	}
	
	private String[] getTemplatesAsString() {
		String[] templateNames = {"Choose Template"};
		Iterator<pipe.dataLayer.Template> iterator = CreateGui.getCurrentTab().activeTemplates().iterator();
		int i = 0;
		while(iterator.hasNext()) {
			pipe.dataLayer.Template template = iterator.next();
			templateNames[i] = template.model().name();
		}
		return templateNames;
	}
	
	static private String[] getObjectNames() {
		ArrayList<String> names = new ArrayList<String>();
		for(PetriNetObject object : CreateGui.getDrawingSurface().getGuiModel().getPlaceTransitionObjects()) {
			names.add(object.getName());
		}
		return Arrays.copyOf(names.toArray(), names.toArray().length, String[].class);
	}
	
	private void initLoadingFrame() {
		loadingDialogFrame = new JDialog(CreateGui.getApp(), "Working...", true);
		loadingDialogFrame.setLayout(new GridBagLayout());
		loadingDialogFrame.setType(Window.Type.POPUP);
		ImageIcon loadingGIF = new ImageIcon(CreateGui.imgPath + "ajax-loader.gif");
		
		JLabel workingLabel = new JLabel("<html><div style='text-align: center;'>Currently doing layout...<br/>This may take several minutes depending on the size of the net...</div></html>", SwingConstants.CENTER);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		loadingDialogFrame.add(workingLabel, gbc);
		
		
		statusLabel = new JLabel("Working... ", loadingGIF, JLabel.CENTER);
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		loadingDialogFrame.add(statusLabel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 10, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		loadingDialogFrame.add(timerLabel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 10, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		loadingDialogFrame.add(progressLabel, gbc);
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelWorker();
				CreateGui.getDrawingSurface().getUndoManager().undo();
				CreateGui.getDrawingSurface().repaintAll();
				loadingDialogFrame.setVisible(false);
				//smartDrawDialog.setVisible(true);
				CreateGui.getAppGui().toFront();
				CreateGui.getAppGui().requestFocus();
				
			}
		});
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		loadingDialogFrame.add(cancelButton, gbc);
		

		loadingDialogFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		loadingDialogFrame.setSize(400, 300);
		loadingDialogFrame.setVisible(false);
		loadingDialogFrame.setLocationRelativeTo(CreateGui.getAppGui());
		loadingDialogFrame.pack();
	}
	
	
	
	
	/*
	 * Asks if you want to keep, revert or try again
	 * is not used currently
	 */
	/*private void initChoiceModal() {
		choiceModal = new JDialog(smartDrawDialog, "Keep?", true);
		choiceModal.setLayout(new GridBagLayout());
		choiceModal.setVisible(false);
		choiceModal.setLocationRelativeTo(smartDrawDialog);
		choiceModal.setResizable(false);
		choiceModal.setSize(300, 100);

		
		JLabel choiceLabel = new JLabel("<html><div style='text-align: center;'>Would you like to keep the new layout,<br/> revert or try again?</div></html>", SwingConstants.CENTER);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.gridwidth = 3;
		gbc.anchor = GridBagConstraints.NORTH;
		
		choiceModal.add(choiceLabel, gbc);
		
		JButton keepButton = new JButton("Keep layout");
		keepButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				smartDrawDialog.setVisible(false);
				choiceModal.setVisible(false);				
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		choiceModal.add(keepButton, gbc);
		
		JButton revertButton = new JButton("Revert");
		revertButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				CreateGui.getDrawingSurface().getUndoManager().undo();
				smartDrawDialog.setVisible(false);
				choiceModal.setVisible(false);
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		choiceModal.add(revertButton, gbc);
		
		JButton retryButton = new JButton("Try Again");
		retryButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				choiceModal.setVisible(false);
				smartDrawDialog.setVisible(true);
			}
		});
		gbc = new GridBagConstraints();
		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.fill = GridBagConstraints.NONE;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTH;
		choiceModal.add(retryButton, gbc);
		
	}*/
	
	private Object getMessageComponent(){
		JTextPane pane = new JTextPane();
		pane.setContentType("text/html");
		pane.setText(getHelpMessage());
		pane.setEditable(false);
		pane.setCaretPosition(0);
		for(MouseListener listener : pane.getMouseListeners()){
			pane.removeMouseListener(listener);
		}
		Dimension dim = new Dimension(500,400);
		pane.setPreferredSize(dim);  
		pane.setMargin(new Insets(5,5,5,5));  
		JScrollPane scrollPane = new JScrollPane(pane);  
		scrollPane.setPreferredSize(dim);  
		return scrollPane;  
	}

	
	private void cancelWorker() {
		if (worker != null && !worker.isDone()) {
			boolean cancelled = false;
			do {
				cancelled = worker.cancel(true);
			} while (!cancelled);
		}
	}
	
	private void enableButtons() {
		if(CreateGui.getDrawingSurface().getGuiModel().getPlaceTransitionObjects().size() > 0) {
			drawButton.setEnabled(true);
			drawButton.setToolTipText("Smart draw with the current options");
			if(!(randomStartObjectCheckBox.isSelected()))
				objectDropdown.setEnabled(true);
			
			randomStartObjectCheckBox.setEnabled(true);
		} else {
			drawButton.setEnabled(false);
			drawButton.setToolTipText("You must have at least one object to smart draw");
			objectDropdown.setEnabled(false);
			randomStartObjectCheckBox.setEnabled(false);
		}
	}
}