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

Last change on this file since 962 was 962, 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// 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}
Note: See TracBrowser for help on using the repository browser.