1 | package org.expeditee.items.MagneticConstraint.Actions;
|
---|
2 |
|
---|
3 | import java.awt.Point;
|
---|
4 |
|
---|
5 | import org.expeditee.gui.DisplayIO;
|
---|
6 | import org.expeditee.gui.FrameIO;
|
---|
7 | import org.expeditee.gui.FrameUtils;
|
---|
8 | import org.expeditee.items.Item;
|
---|
9 | import org.expeditee.items.Text;
|
---|
10 | import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;
|
---|
11 | import org.expeditee.items.MagneticConstraint.Utilities.Line;
|
---|
12 | import org.expeditee.items.MagneticConstraint.Utilities.Paragraph;
|
---|
13 | import org.expeditee.items.MagneticConstraint.Utilities.TextLogic;
|
---|
14 |
|
---|
15 | public class RepelTextDownAction extends MagneticConstraintAction {
|
---|
16 |
|
---|
17 | // private void f(final Item item) {
|
---|
18 | // final List<Item> items = item.getParent().getItems();
|
---|
19 | // for (final Item item2 : items) {
|
---|
20 | // if(item2.getText().compareTo("main") == 0) System.err.println("#Found main");
|
---|
21 | // }
|
---|
22 | // }
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public boolean exec(final Item item) {
|
---|
26 | if(item == null || item.getParent() == null) return false;
|
---|
27 | callback(item);
|
---|
28 | //Calculate content that needs to be moved to next line.
|
---|
29 | //1. Get remainder of line
|
---|
30 | final Line remainderOfLine = Line.getLineFromToken(item);
|
---|
31 | //2. Check if the current item should be included; remove it if not.
|
---|
32 | if(remainderOfLine.getFirst().getText().isEmpty() ||
|
---|
33 | (FrameUtils.getCurrentItem().getPosition().equals(item.getPosition())
|
---|
34 | && remainderOfLine.size() > 0
|
---|
35 | && TextLogic.XIsTowardsRight(DisplayIO.getMouseX(), (Text) remainderOfLine.getFirst()))) {
|
---|
36 | remainderOfLine.removeFirst();
|
---|
37 | }
|
---|
38 |
|
---|
39 | //Move content below this line down
|
---|
40 | //1. Get current line & current paragraph.
|
---|
41 | final Line currentLine = Line.getLineContainingToken(item);
|
---|
42 | final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine);
|
---|
43 | //2. Split current paragraph
|
---|
44 | final Point newLineAt = currentParagraph.split(currentLine);
|
---|
45 | //Move content contained in 'remainderOfLine'
|
---|
46 | //1. Is remainderOfLine not Empty?
|
---|
47 | if(!remainderOfLine.isEmpty()) {
|
---|
48 | //2a. Yes. Move the content
|
---|
49 | final Point first = remainderOfLine.getBoundingBox().getLocation();
|
---|
50 | for(final Item token : remainderOfLine) {
|
---|
51 | double newX = token.getX() - first.getX() + newLineAt.getX();
|
---|
52 | token.setPosition((float) newX, (float) newLineAt.getY());
|
---|
53 | }
|
---|
54 | //2b. Move the cursor; refresh the screen and use callback to update the magnets
|
---|
55 | DisplayIO.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME);
|
---|
56 | callback(null);
|
---|
57 | FrameIO.SaveFrame(remainderOfLine.getLast().getParent());
|
---|
58 | } else {
|
---|
59 | //3a No. Move the cursor; refresh the screen and use callback to update the magnets
|
---|
60 | DisplayIO.setCursorPosition(newLineAt);
|
---|
61 | }
|
---|
62 |
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public boolean invert(Item item) {
|
---|
68 | System.err.println("Repel text down action does not have a inverted implementation.");
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|