-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBlock.java
More file actions
1963 lines (1712 loc) · 62.1 KB
/
Copy pathBlock.java
File metadata and controls
1963 lines (1712 loc) · 62.1 KB
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblocks;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import openblocks.renderable.BlockImageIcon;
import openblocks.renderable.RenderableBlock;
import openblocks.renderable.BlockImageIcon.ImageLocation;
import openblocks.workspace.ISupportMemento;
import openblocks.yacodeblocks.Escapers;
import openblocks.yacodeblocks.ProcedureBlockManager;
import openblocks.codeblocks.BlockConnector.PositionType;
/**
* Block holds the mutable prop (data) of a particular block.
* These mutable prop include socket, before, after and blocks,
* "bad"-ness. In addition, Block maintains information to
* describe a particular block's relationship with other blocks.
*
*/
public class Block implements ISupportMemento {
/** The ID that is to be assigned to the next new block */
private static long NEXT_ID = 1;
//Defines a NULL id for a Block
public static final Long NULL = Long.valueOf(-1);
/** A universal hashmap of all the Block instances*/
private final static HashMap<Long, Block> ALL_BLOCKS= new HashMap<Long,Block>();
public static final String BAD_BLOCK_FORMAT_MESSAGE =
"Block may have an old version. Please replace it";
//block identifying information
private final Long blockID;
private String label;
private String pageLabel = null;
private String genusName;
//block connection information
private List<BlockConnector> sockets;
private BlockConnector plug;
private BlockConnector before;
private BlockConnector after;
/* If shouldReceiveReport is true, BlockParser will generate code to
* report its value every time it is executed. Variable Declarations are
* a special case. If a variable declaration shouldReceiveReport,
* every set or definition of it sends a report to the def block.
*/
private boolean shouldReceiveReport = false;
/**
* The expand-groups. A list is used instead of a map, because we don't
* expect a lot of groups in one block.
*/
private List<List<BlockConnector>> expandGroups;
//this flag determines if this block will create stubs if its
//genus species that it does. if false, then this block even though
//it may have stubs will not create stubs
protected boolean linkToStubs = true;
//block state information
private boolean isBad = false;
// For compiler errors
private ComplaintDepartment complaintDepartment;
// private boolean isPermanentlyBad = false;
private String badMsg;
//focus information
private boolean hasFocus = false;
//additional properties of a block
//can not contain keys that are within genus
protected HashMap<String, String> properties = new HashMap<String, String>();
//argument descriptions
private ArrayList<String> argumentDescriptions;
//optional block description that overrides genus description
private String description = null;
public HashMap<String, String> getProperties() {
return properties;
}
/**
* Constructs a new Block from the specified information. This class constructor is
* protected as block loading from XML content or the (careful!) creation of its subclasses
* should override BlockID assignment.
* @param id the Block ID of this
* @param genusName the String name of this block's BlockGenus
* @param label the String label of this Block
*/
protected Block(Long id, String genusName, String label, boolean linkToStubs){
if (ALL_BLOCKS.containsKey(id)) {
Block dup = ALL_BLOCKS.get(id);
System.out.println("pre-existing block is: "+dup+" with genus "+dup.getGenusName()+" and label "+dup.getBlockLabel());
assert !ALL_BLOCKS.containsKey(id) : "Block id: "+id+" already exists! BlockGenus "+genusName+" label: "+label;
}
this.blockID = id;
//if this assigned id value equals the next id to automatically assign
// a new block, increment the next id value by 1
if (id.longValue() == NEXT_ID)
NEXT_ID++;
sockets = new ArrayList<BlockConnector>();
argumentDescriptions = new ArrayList<String>();
//copy connectors from BlockGenus
try {
BlockGenus genus = BlockGenus.getGenusWithName(genusName);
if (genus == null)
throw new Exception("genusName: "+genusName+" does not exist.");
//copy the block connectors from block genus
Iterable<BlockConnector> iter = genus.getInitSockets();
for (BlockConnector con : iter){
sockets.add(new BlockConnector(con));
}
if (genus.getInitPlug() != null)
plug = new BlockConnector(genus.getInitPlug());
if (genus.getInitBefore() != null)
before = new BlockConnector(genus.getInitBefore());
if (genus.getInitAfter() != null)
after = new BlockConnector(genus.getInitAfter());
this.genusName = genusName;
this.label = label;
complaintDepartment = new ComplaintDepartment(blockID);
Iterable<String> argumentIter = genus.getInitialArgumentDescriptions();
for (String arg : argumentIter){
argumentDescriptions.add(arg.trim());
}
this.expandGroups = new ArrayList<List<BlockConnector>>(genus.getExpandGroups());
//add to ALL_BLOCKS
//warning: publishing this block before constructor finishes has the
//potential to cause some problems such as data races
//other threads could access this block from getBlock()
ALL_BLOCKS.put(this.blockID, this);
//add itself to stubs hashmap
//however factory blocks will have entries in hashmap...
if (linkToStubs && this.hasStubs()){
BlockStub.putNewParentInStubMap(this.blockID);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Constructs a new <code>Block</code> instance. Using the genusName specified
* of this Block's corresponding BlockGenus, this constructor populates this Block
* with its genus information.
* @param genusName the name of its associated <code>BlockGenus</code>
* @param label the label of this Block.
* @param linkToStubs if true, this block can have stubs and be linked to them;
* if false, then this block even though the genus specifies it will not be
* linked to stubs
*/
public Block(String genusName, String label, boolean linkToStubs){
//more will go into constructor;
this(NEXT_ID, genusName, label, linkToStubs);
NEXT_ID++;
while(ALL_BLOCKS.containsKey(NEXT_ID))
NEXT_ID++;
}
/**
* Constructs a new <code>Block</code> instance. Using the genusName specified
* of this Block's corresponding BlockGenus, this constructor populates this Block
* with its genus information.
* @param genusName the name of its associated <code>BlockGenus</code>
* @param label the label of this Block.
*/
public Block(String genusName, String label){
//more will go into constructor;
this(NEXT_ID, genusName, label, true);
NEXT_ID++;
while(ALL_BLOCKS.containsKey(NEXT_ID))
NEXT_ID++;
}
/**
* Constructs a new <code>Block</code> instance. Using the genusName
* specified of this Block's corresponding BlockGenus, this constructor
* populates this Block with its genus information. It also initializes
* the properties of the block
* @param genusName the name of its associated <code>BlockGenus</code>
* @param label the label of this Block.
* @param properties the properties to use to initialize this block's
* properties
*/
public Block(String genusName, String label, HashMap<String, String> properties) {
this(genusName, label);
this.properties.putAll(properties);
}
/**
* Constructs a new <code>Block</code> instance. Using the genusName specified
* of this Block's corresponding BlockGenus, this constructor populates this Block
* with its genus information.
* @param genusName the name of its associated <code>BlockGenus</code>
*/
public Block(String genusName){
this(genusName, BlockGenus.getGenusWithName(genusName).getInitialLabel());
}
/**
* Constructs a new <code>Block</code> instance. Using the genusName specified
* of this Block's corresponding BlockGenus, this constructor populates this Block
* with its genus information.
* @param genusName the name of its associated <code>BlockGenus</code>
* @param linkToStubs if true, this block can have stubs and be linked to them;
* if false, then this block even though the genus specifies it will not be
* linked to stubs
*/
public Block(String genusName, boolean linkToStubs){
this(genusName, BlockGenus.getGenusWithName(genusName).getInitialLabel(), linkToStubs);
}
/**
* Returns the Block instance with the specified blockID
* @param blockID
* @return the Block instance with the specified blockID
*/
public static Block getBlock(Long blockID){
return ALL_BLOCKS.get(blockID);
}
/**
* Convenience method to return renderable block
* @return renderable block corresponding to block
*/
public RenderableBlock getRenderableBlock(){
return RenderableBlock.getRenderableBlock(blockID);
}
/**
* Clears all block instances and resets id assignment.
*/
public static void reset(){
//System.out.println("reseting all blocks");
ALL_BLOCKS.clear();
NEXT_ID = 1;
}
/**
* Remove block from ALL_BLOCKS for benefit of RenderableBlock who created
* it just to get a collapsed shape.
*/
public void forget() {
ALL_BLOCKS.remove(blockID);
}
///////////////////
//BLOCK prop
///////////////////
/**
* Returns the block ID of this
* @return the block ID of this
*/
public Long getBlockID(){
return blockID;
}
/**
* Sets the block property with the specified property and value. If this block's
* genus already contains a value with the same property, then the specified property
* will not be added to this block's property collection.
* @param property the property key to set
* @param value the value associated with this property
* @return true if this property was set successfully
*/
public boolean setProperty(String property, String value){
if (getGenus().getProperty(property) != null)
return false;
else{
properties.put(property, value);
return true;
}
}
/**
* Returns the block label of this
* @return the block label of this
*/
public String getBlockLabel(){
return getGenus().getLabelPrefix()+label+getGenus().getLabelSuffix();
}
/**
* Returns true iff this block has a page label and it is non-empty
* @return true iff this block has a page label and it is non-empty
*/
public boolean hasPageLabel(){
return pageLabel != null && !pageLabel.equals("");
}
/**
* Returns true iff this declaration block has arguments
* It must be a procdecl or event handler to have them.
* @return true iff this declaration block has arguments
*/
public boolean hasArguments() {
if(isProcedureDeclBlock() || isEventHandlerBlock())
for (BlockConnector con : getSockets()) {
final Block block = Block.getBlock(con.getBlockID());
if (block != null && block.isArgument()) {
return true;
}
}
return false;
}
/**
* Returns true iff this block is an argument
* @return true iff this block is an argument
*/
public boolean isArgument() {
return getProperty("ya-kind").equals("argument");
}
/**
* Returns true iff this block is in a drawer
* @return true iff this block is in a drawer
*/
public boolean isInDrawer() {
if (getRenderableBlock().getParent() == null) return false;
return (getRenderableBlock().getParent() instanceof openblocks.workspace.FactoryCanvas);
}
/**
* Returns the page label string of this
* @return the page label string of this
*/
public String getPageLabel() {
return pageLabel;
}
/**
* Returns the decorator label string of this
* @return the decorator label string of this
*/
public String getDecoratorLabel() {
return getGenus().getDecorator();
}
/**
* Sets the block label of this iff this block label is editable
* @param newLabel the desired label
*/
public void setBlockLabel(String newLabel){
if (this.linkToStubs && this.hasStubs())
BlockStub.parentNameChanged(this.label, newLabel, this.blockID);
label = newLabel;
}
/**
* Sets the page label of this
* @param newPageLabel the desired page label
*/
public void setPageLabel(String newPageLabel) {
//update stubs
if (this.linkToStubs && this.hasStubs())
BlockStub.parentPageLabelChanged(newPageLabel, this.blockID);
pageLabel = newPageLabel;
}
/**
* Returns the BlockGenus of this
* @return the BlockGenus of this
*/
protected BlockGenus getGenus(){
return BlockGenus.getGenusWithName(genusName);
}
/**
* Changes the genus of this block, while maintaining this block's
* relationships with other blocks it's connected to.
* @param genusName the String name of the BlockGenus to change this Block to
* @param preserveLabel if the label should be preserved, or reset to the genus's default
*/
public void changeGenusTo(String genusName, boolean preserveLabel){
this.genusName = genusName;
BlockGenus newGenus = BlockGenus.getGenusWithName(genusName);
if (!preserveLabel) {
label = newGenus.getInitialLabel();
}
RenderableBlock.getRenderableBlock(blockID).setColor(newGenus.getColor());
// Caution: Here we set the block's tooltip from the new genus. In general, a block
// might have a description that overrides the default coming from its genus.
// We ignore that default here. There might be cases where this is the wrong thing.
// An example could be a properties block (see ComponentBlockManager.addNewChild)
// So this needs to be rethought if we ever permit changing the genus of properties blocks.
String newDescription = newGenus.getBlockDescription();
if (newDescription != null) {
RenderableBlock.getRenderableBlock(blockID).setBlockToolTip(newDescription.trim());
} else {
// this catches the case where the new genus does not have a description
RenderableBlock.getRenderableBlock(blockID).setBlockToolTip(null);
}
// We also trash any old (overriding) description. As above, this might not be the right
// thing for some blocks.
setBlockDescription(null);
}
/**
* Called to signaling whether a report code should be
* generated.
* @param should true iff a report is needed
*/
public void setShouldReceiveReport(boolean should) {
shouldReceiveReport = should;
if (!should) {
getRenderableBlock().removeReport();
}
}
/**
* @return true iff a report is needed
* reflects last boolean passed to setShouldReceiveReport
*/
public boolean shouldReceiveReport() {
return shouldReceiveReport;
}
////////////////////////////////
//BLOCK CONNECTION METHODS
////////////////////////////////
/**
* Returns the Block ID connected to the before connector of this; Block.Null
* if this does not have a before block
* @return the Block ID connected to the before connector of this; Block.Null
* if this does not have a before block
*/
public Long getBeforeBlockID(){
if (before == null)
return Block.NULL;
return before.getBlockID();
}
/**
* Returns the Block ID connected to the after connector of this;
* Block.Null if this does not have an after block
* @return the Block ID connected to the after connector of this;
* Block.Null if this does not have an after block
*/
public Long getAfterBlockID(){
if (after == null)
return Block.NULL;
return after.getBlockID();
}
/**
* Returns the BlockConnector representing the connection to the block after this
* @return the BlockConnector of the after connector
*/
public BlockConnector getAfterConnector() {
return after;
}
/**
* Returns the BlockConnector representing the connection to the block before this
* @return the BlockConnector of the before connector
*/
public BlockConnector getBeforeConnector() {
return before;
}
/**
* Resets the before and after connectors to their initial kinds. Only
* privileged classes (ie. BlockStub) should call this method.
*/
void resetBeforeAndAfter() {
before = new BlockConnector(getGenus().getInitBefore());
after = new BlockConnector(getGenus().getInitAfter());
}
/**
* Removes the before and after connectors. Only privileged classes
* (ie. BlockStub) should call this method.
*/
void removeBeforeAndAfter() {
before = null;
after = null;
}
/**
* Return the expand-group for the given group. Can be null if group
* doesn't exist.
*/
private static List<BlockConnector> getExpandGroup(List<List<BlockConnector>> groups, String group) {
for (List<BlockConnector> list : groups) {
// Always at least one element in the group.
if (list.get(0).getExpandGroup().equals(group)) {
return list;
}
}
return null;
}
/**
* Expand a socket group in this block. For now, all new sockets will
* be added after the last socket in the group.
*/
private void expandSocketGroup(String group) {
List<BlockConnector> expandSockets = getExpandGroup(expandGroups, group);
assert expandSockets != null;
// Search for the socket to insert after.
int index = sockets.size() - 1;
String label = expandSockets.get(expandSockets.size() - 1).getLabel();
for (; index >= 0; index--) {
BlockConnector conn = sockets.get(index);
if (conn.getLabel().equals(label) && conn.getExpandGroup().equals(group))
break;
}
assert index >= 0;
// Insert all the new sockets
for (BlockConnector conn : expandSockets) {
index++;
BlockConnector newConn = new BlockConnector(conn);
sockets.add(index, newConn);
}
}
/**
* Shrink a socket group (un-expand it).
*/
private void shrinkSocketGroup(BlockConnector socket) {
String group = socket.getExpandGroup();
List<BlockConnector> expandSockets = getExpandGroup(expandGroups, group);
assert expandSockets != null;
// Search for the first socket in the group, if not the expandable
// one.
String label = expandSockets.get(0).getLabel();
int index = getSocketIndex(socket);
for (; index >= 0; index--) {
BlockConnector con = sockets.get(index);
if (con.getLabel().equals(label) && con.getExpandGroup().equals(group))
break;
}
assert index >= 0;
// Remove all the sockets.
removeSocket(index);
int total = expandSockets.size();
for (int i = 1; i < total;) {
BlockConnector con = sockets.get(index);
if (con.getLabel().equals(expandSockets.get(i).getLabel()) && con.getExpandGroup().equals(group)) {
removeSocket(index);
i++;
}
else {
index++;
}
}
}
/**
* Returns true if the given expandable socket can be removed.
*/
private boolean canRemoveSocket(BlockConnector socket) {
int total = sockets.size();
int first = -1;
for (int i = 0; i < total; i++) {
BlockConnector conn = sockets.get(i);
if (conn == socket) {
if (first == -1) first = i;
else return true;
}
else if (conn.getPositionType().equals(socket.getPositionType()) &&
conn.isExpandable() == socket.isExpandable() &&
conn.initKind().equals(socket.initKind()) &&
conn.getExpandGroup().equals(socket.getExpandGroup())) {
if (first == -1) first = i;
else return true;
}
}
// If the socket is the first and last of its kind, then we can NOT
// remove it. (We also can't remove it if they're both -1, obviously.)
return false;
}
/**
* Informs this Block that a block with id connectedBlockID has connected to the specified
* connectedSocket
*/
public void blockConnected(BlockConnector connectedSocket, Long connectedBlockID){
if (connectedSocket.isExpandable()){
if (connectedSocket.getExpandGroup().length() > 0) {
// Part of an expand group
expandSocketGroup(connectedSocket.getExpandGroup());
}
else {
//expand into another one
int index = getSocketIndex(connectedSocket);
addSocket(index+1, connectedSocket.initKind(), connectedSocket.getPositionType(),
connectedSocket.getLabel(), connectedSocket.isLabelEditable(),
connectedSocket.isIndented(), connectedSocket.isExpandable(), Block.NULL);
}
}
//NOTE: must update the sockets of this before updating its stubs as stubs use this as a reference to update its own sockets
//if block has stubs, update its stubs as well
if (hasStubs() && ProcedureBlockManager.isArgSocket(connectedSocket)) {
BlockStub.parentConnectorsChanged(blockID);
}
}
/**
* Informs this Block that a block has disconnected from the specified disconnectedSocket
* @param disconnectedSocket
*/
public void blockDisconnected(BlockConnector disconnectedSocket){
if (disconnectedSocket.isExpandable() && canRemoveSocket(disconnectedSocket)){
if (disconnectedSocket.getExpandGroup().length() > 0)
shrinkSocketGroup(disconnectedSocket);
else
removeSocket(disconnectedSocket);
}
//NOTE: must update the sockets of this before updating its stubs as stubs use this as a reference to update its own sockets
//if block has stubs, update its stubs as well
if (hasStubs() && ProcedureBlockManager.isArgSocket(disconnectedSocket)) {
BlockStub.parentConnectorsChanged(blockID);
}
}
////////////////////////////////
//BLOCK SOCKET AND PLUG METHODS
////////////////////////////////
/**
* Returns an unmodifiable iterable over a safe copy of the Sockets of this
* @return an unmodifiable iterable over a safe copy of the Sockets of this
*/
public Iterable<BlockConnector> getSockets(){
return Collections.unmodifiableList(new ArrayList<BlockConnector>(sockets));
}
/**
* Returns the number of sockets of this
* @return the number of sockets of this
*/
public int getNumSockets(){
return sockets.size();
}
/**
* Returns the socket (BlockConnector instance) at the specified index
* @param index the index of the desired socket. 0 <= index < getNumSockets()
* @return the socket (BlockConnector instance) at the specified index
*/
public BlockConnector getSocketAt(int index){
assert index < sockets.size() : "Index "+index+" is greater than the num of sockets: "+sockets.size()+" of "+this;
return sockets.get(index);
}
/**
* Replaces the socket at the specified index with the new specified parameters
* @param index of the BlockConnector to replace
* @param isLabelEditable is true iff this BlockConnector can have its labels edited.
* @param isIndented is true iff this BlockConnected should be indented far to the left
* @return true if socket successfully replaced
*/
public boolean setSocketAt(int index, String kind, PositionType pos, String label, boolean isLabelEditable,
boolean isIndented, boolean isExpandable, Long blockID){
if (sockets.set(index, new BlockConnector(kind, pos, label, isLabelEditable, isIndented, isExpandable, blockID)) != null)
return true;
else return false;
}
/**
* Returns the index number of a given socket
* @param socket a socket of this block
* @return the index number of a given socket or -1 if socket doesn't exists on the block
*/
public int getSocketIndex(BlockConnector socket) {
for (int i=0; i<sockets.size() ; i++) {
BlockConnector currentSocket = sockets.get(i);
//check for reference equality
if (currentSocket == socket) {
return i;
}
}
//then it was not found
return -1;
}
/**
* Adds another socket to this. Socket is added to the "end" of socket list.
* @param kind the socket kind of new socket
* @param positionType the BlockConnector.PositionType of the new connector
* @param label the label of the new socket
* @param isLabelEditable is true iff this BlockConnector can have its labels edited.
* @param isIndented is true iff this BlockConnected should be indented far to the left
* @param isExpandable true iff this connector can expand to another connector
* @param blockID the block id of the block attached to new socket
*/
public void addSocket(String kind, PositionType positionType, String label, boolean isLabelEditable,
boolean isIndented, boolean isExpandable, Long blockID){
BlockConnector newSocket = new BlockConnector(kind, positionType, label, isLabelEditable, isIndented, isExpandable, blockID);
sockets.add(newSocket);
}
/**
* Adds another socket to this. Socket is inserted at the specified index of socket list, where 0 is the first socket.
* if index is equal numOfSockets(), then socket is added to the end of the socket list. If index > numOfSockets(), an
* exception is thrown.
* @param index the index to insert new socket to
* @param kind the socket kind of new socket
* @param label the label of the new socket
* @param positionType the BlockConnector.PositionType of the new connector
* @param isLabelEditable is true iff this BlockConnector can have its labels edited.
* @param isIndented is true iff this BlockConnected should be indented far to the left
* @param isExpandable true iff this connector can expand to another connector
* @param blockID the block id of the block attached to new socket
*/
public BlockConnector addSocket(int index, String kind, PositionType positionType, String label,
boolean isLabelEditable, boolean isIndented, boolean isExpandable, Long blockID){
BlockConnector newSocket = new BlockConnector(kind, positionType, label,
isLabelEditable, isIndented, isExpandable, blockID);
sockets.add(index, newSocket);
return newSocket;
}
/**
* Removes the socket at the specified index
* @param index the index of the socket to remove
*/
public void removeSocket(int index){
removeSocket(sockets.get(index));
}
/**
* Removes specified socket
* @param socket BlockConnector to remove from this
*/
public void removeSocket(BlockConnector socket){
//disconnect any blocks connected to socket
if (socket.getBlockID() != Block.NULL){
Block connectedBlock = Block.getBlock(socket.getBlockID());
connectedBlock.getConnectorTo(this.blockID).setConnectorBlockID(Block.NULL);
socket.setConnectorBlockID(Block.NULL);
RenderableBlock.getRenderableBlock(blockID).blockDisconnected(socket);
}
sockets.remove(socket);
}
//plug information
/**
* Returns if BlockConnector plug exists
* @return if BlockConnector plug exists
*/
public boolean hasPlug(){
return !(plug == null);
}
/**
* Returns the BlockConnector plug of this
* @return the BlockConnector plug of this
*/
public BlockConnector getPlug(){
return plug;
}
/**
* Sets the plug of this.
* @param kind the socket kind of plug
* @param label the label of the plug
* @param positionType the BlockConnector.PositionType of this plug
* @param isLabelEditable is true iff this BlockConnector can have its labels edited.
* @param blockID the block id of the block attached to plug
*/
public void setPlug(String kind, PositionType positionType, String label, boolean isLabelEditable, Long blockID) {
plug = new BlockConnector(kind, positionType, label, isLabelEditable, false, false, blockID);
}
/**
* Sets the plug kind of this
* @param kind the desired plug kind
*/
public void setPlugKind(String kind){
assert plug != null : "Plug is null! Can not set this information.";
if (hasPlug())
plug.setKind(kind);
}
/**
* Sets the plug label of this
* @param label the desired plug label
*/
public void setPlugLabel(String label){
assert plug != null : "Plug is null! Can not set this information.";
if (hasPlug())
plug.setLabel(label);
}
/**
* Sets the block attached to this plug
* @param id the block id to attach to this plug
*/
public void setPlugBlockID(Long id){
assert plug != null : "Plug is null! Can not set this information.";
if (hasPlug())
plug.setConnectorBlockID(id);
}
/**
* Return plug kind; null if plug does not exist
* @return plug kind; null if plug does not exist
*/
public String getPlugKind(){
if (hasPlug())
return plug.getKind();
return null;
}
/**
* Return plug label; null if plug does not exist
* @return plug label; null if plug does not exist
*/
public String getPlugLabel(){
if (hasPlug())
return plug.getLabel();
return null;
}
/**
* Return plug block id; null if plug does not exist
* @return plug block id; null if plug does not exist
*/
public Long getPlugBlockID(){
if (plug == null)
return Block.NULL;
return plug.getBlockID();
}
/**
* Removes the plug.
*/
void removePlug() {
plug = null;
}
/**
* Searches for the BlockConnector linking this block to another block
* @param otherBlockID the Block ID if the other block
* @return the BlockConnector linking this block to the other block
*/
public BlockConnector getConnectorTo(Long otherBlockID) {
if (otherBlockID == null || otherBlockID == Block.NULL)
return null;
if (getPlugBlockID().equals(otherBlockID))
return plug;
if (getBeforeBlockID().equals(otherBlockID))
return before;
if (getAfterBlockID().equals(otherBlockID))
return after;
for (BlockConnector socket : getSockets())
if (socket.getBlockID().equals(otherBlockID))
return socket;
return null;
}
////////////////
//"BAD" METHODS
////////////////
/**
* Returns true iff this block is "bad." Bad means that this block has an associated compile error.
*/
public boolean isBad(){
return isBad;
}
/**
* Sets the "bad"-ness of this block. Bad means that this block has an associated compile error.
* @param isBad
*/
public void setBad(boolean isBad){
this.isBad = isBad;
}
/**
* Returns the "bad" message of this block.
*/
public String getBadMsg(){
return badMsg;
}
/**
* Sets the message describing this block's badness. In other words, the message describes the compile
* error associated with this block.
* @param badMsg
*/
public void setBadMsg(String badMsg){
this.badMsg = badMsg;
}
////////////////
//FOCUS METHODS
////////////////
/**
* Returns true iff this block has focus. Focus means it is currently selected in the workspace.
* Multiple blocks can have focus simultaniously.
*/
public boolean hasFocus(){
return hasFocus;
}
/**
* Sets the focus state of the block. Should only be used by FocusManager.
* @param hasFocus
*/
public void setFocus(boolean hasFocus){
this.hasFocus = hasFocus;
}
////////////////////
//DEFAULT ARGUMENTS
////////////////////
/**
* Links all the default arguments specified in the <code>BlockGenus</code> of this to the
* specified sockets of this block. By default a new <code>Block</code> does not have
* default arguments attached. Each index in the Long list corresponds to the index of the socket
* the default argument is attached to. If an element in this list is Block.NULL, then no default argument
* exists for that socket or there already is a block attached at that socket.
*
* Default arguments are linked whenever a block is dragged to the workspace for the first time
*
* @return Returns a Long list of the newly created default argument block IDs; null if this block has none.
*/
public Iterable<Long> linkAllDefaultArgs(){
if (getGenus().hasDefaultArgs()){
ArrayList<Long> defargIDs = new ArrayList<Long>();
for (BlockConnector con : sockets){
Long id = con.linkDefArgument();
defargIDs.add(id);
//if id not null, then connect def arg's plug to this block
if (id != Block.NULL)
Block.getBlock(id).getPlug().setConnectorBlockID(this.blockID);
}
return defargIDs;
}
return null;
}