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