source: trunk/src/org/expeditee/items/MagneticConstraint/Actions/RepelTextDownAction.java@ 967

Last change on this file since 967 was 967, checked in by bln4, 9 years ago
File size: 2.8 KB
Line 
1package org.expeditee.items.MagneticConstraint.Actions;
2
3import java.awt.Point;
4
5import org.expeditee.gui.DisplayIO;
6import org.expeditee.gui.FrameIO;
7import org.expeditee.gui.FrameUtils;
8import org.expeditee.items.Item;
9import org.expeditee.items.Text;
10import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;
11import org.expeditee.items.MagneticConstraint.Utilities.Line;
12import org.expeditee.items.MagneticConstraint.Utilities.Paragraph;
13import org.expeditee.items.MagneticConstraint.Utilities.TextLogic;
14
15public class RepelTextDownAction extends MagneticConstraintAction {
16
17 public static boolean MergeSameLineParagraphCollisions = false;
18
19 @Override
20 public boolean exec(final Item item) {
21 if(item == null || item.getParent() == null || !this.isSpIDERCodePage(item.getParent())) return false;
22 callback(item);
23 //Calculate content that needs to be moved to next line.
24 //1. Get remainder of line
25 final Line remainderOfLine = Line.getLineFromToken(item);
26 //2. Check if the current item should be included; remove it if not.
27 if(remainderOfLine.getFirst().getText().isEmpty() ||
28 (FrameUtils.getCurrentItem().getPosition().equals(item.getPosition())
29 && remainderOfLine.size() > 0
30 && TextLogic.XIsTowardsRight(DisplayIO.getMouseX(), (Text) remainderOfLine.getFirst()))) {
31 remainderOfLine.removeFirst();
32 }
33
34 final Line currentLine = Line.getLineContainingToken(item);
35 final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine);
36 final Line nextLine = currentLine.next();
37
38 final Point newLineAt;
39 //Move content contained in 'remainderOfLine'
40
41
42 if(!remainderOfLine.isEmpty() && currentParagraph.contains(nextLine) && MergeSameLineParagraphCollisions) {
43 nextLine.prependLine(remainderOfLine);
44 DisplayIO.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME);
45 callback(remainderOfLine.getFirst());
46 FrameIO.SaveFrame(remainderOfLine.getLast().getParent());
47 } else if(!remainderOfLine.isEmpty()) {
48 newLineAt = currentParagraph.split(currentLine);
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 DisplayIO.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME);
55 callback(remainderOfLine.getFirst());
56 FrameIO.SaveFrame(remainderOfLine.getLast().getParent());
57 }else {
58 //3a No. Move the cursor
59 newLineAt = currentParagraph.split(currentLine);
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}
Note: See TracBrowser for help on using the repository browser.