11package actions ;
22
3+ import java .awt .Component ;
34import java .util .ArrayList ;
45
56import paintcomponents .LineSegment ;
@@ -20,12 +21,32 @@ public boolean canPerformAction() {
2021 //only two points can be selected
2122 if (items .size () != 2 ) return false ;
2223 //selected component must be of type point
24+
2325 for (PaintComponent paintComponent : items ) {
2426 if (!(paintComponent instanceof SimplePoint )){
2527 return false ;
2628 }
2729 }
28- //TODO If line segment already exists, do not add again!!!
30+
31+ //check if line segment already exist
32+ // if exists, then it will not form a new line segment
33+ ArrayList <PaintComponent > components = panel .getPaintComponents ();
34+ LineSegment line = null ;
35+
36+ //get all paintComponents
37+ for (PaintComponent paintComponent : components ) {
38+ if ( paintComponent instanceof LineSegment ) {
39+ line = (LineSegment ) paintComponent ;
40+ //check front point and to point similarities
41+ if (items .get (0 ) == line .getFromPoint () &&
42+ items .get (1 ) == line .getToPoint ())
43+ return false ;
44+ if (items .get (1 ) == line .getFromPoint () &&
45+ items .get (0 ) == line .getToPoint ())
46+ return false ;
47+ }
48+ }
49+
2950 //TODO Do not allow adding two line segments connecting the same point
3051 return true ;
3152
0 commit comments