1 | package org.expeditee.items.MagneticConstraint.Actions;
|
---|
2 |
|
---|
3 | import org.expeditee.gui.DisplayController;
|
---|
4 | import org.expeditee.items.Item;
|
---|
5 | import org.expeditee.items.Text;
|
---|
6 | import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;
|
---|
7 | import org.expeditee.items.MagneticConstraint.Utilities.Line;
|
---|
8 |
|
---|
9 | public class UpALineAction extends MagneticConstraintAction {
|
---|
10 |
|
---|
11 | @Override
|
---|
12 | public boolean exec(final Item item) {
|
---|
13 | if(item.getParent() == null || !this.isSpIDERCodePage(item.getParent())) return false;
|
---|
14 | final Line thisLine = Line.getLineContainingToken(item);
|
---|
15 | final Item startOfThisLine = thisLine.get(0);
|
---|
16 | final Item startOfLastLine = thisLine.last().get(0);
|
---|
17 | if(startOfLastLine == null) return false;
|
---|
18 | final Line lastLine = Line.getLineFromToken(startOfLastLine);
|
---|
19 | final int lastLineLength =
|
---|
20 | lastLine.get(lastLine.size() - 1).getX() + lastLine.get(lastLine.size() - 1).getBoundsWidth() - lastLine.get(0).getX();
|
---|
21 | final int distanceThroughCurrentLine = DisplayController.getMouseX() - startOfThisLine.getX();
|
---|
22 | if(distanceThroughCurrentLine <= lastLineLength) {
|
---|
23 | Item above = lastLine.get(0);
|
---|
24 | for(final Item token : lastLine) {
|
---|
25 | if(token.getX() <= item.getX()) above = token;
|
---|
26 | else break;
|
---|
27 | }
|
---|
28 | moveCursor(above);
|
---|
29 | } else {
|
---|
30 | moveCursor(lastLine.get(lastLine.size() - 1));
|
---|
31 | }
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 |
|
---|
35 | private boolean moveCursor(final Item toMoveTo) {
|
---|
36 | if(toMoveTo == null) return false;
|
---|
37 | if(toMoveTo instanceof Text) {
|
---|
38 | final Text asText = (Text) toMoveTo;
|
---|
39 | DisplayController.setTextCursor(asText, Text.END);
|
---|
40 | } else DisplayController.setCursorPosition(toMoveTo.getPosition(), false);
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public boolean invert(final Item item) {
|
---|
46 | System.err.println("Up a line action does not have a inverted implementation.");
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 | }
|
---|