1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import java.awt.Point;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.Comparator;
|
---|
7 |
|
---|
8 | import org.expeditee.gui.Frame;
|
---|
9 | import org.expeditee.gui.FrameGraphics;
|
---|
10 | import org.expeditee.items.Item;
|
---|
11 | import org.expeditee.items.Text;
|
---|
12 |
|
---|
13 | public class Sort extends DefaultAgent {
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Sorts all the text Items on the given Frame alphabetically. The Items are
|
---|
17 | * then rearranged to reflect this ordering.
|
---|
18 | */
|
---|
19 | public Frame process(Frame start) {
|
---|
20 | ArrayList<Text> textItems = new ArrayList<Text>();
|
---|
21 |
|
---|
22 | for (Item i : start.getItems())
|
---|
23 | if (i instanceof Text)
|
---|
24 | // do not sort title and framename
|
---|
25 | if (i.getID() > -1 && i != start.getTitle()
|
---|
26 | && !i.isAnnotation()) {
|
---|
27 | textItems.add((Text) i);
|
---|
28 | }
|
---|
29 |
|
---|
30 | // copy current positions of items
|
---|
31 | ArrayList<Point> positions = new ArrayList<Point>(textItems.size());
|
---|
32 |
|
---|
33 | for (int i = 0; i < textItems.size(); i++)
|
---|
34 | positions.add(i, textItems.get(i).getPosition());
|
---|
35 |
|
---|
36 | // Sort text items by their strings
|
---|
37 | Collections.sort(textItems, new Comparator<Text>() {
|
---|
38 | public int compare(Text a, Text b) {
|
---|
39 | return String.CASE_INSENSITIVE_ORDER.compare(a.getFirstLine(),
|
---|
40 | b.getFirstLine());
|
---|
41 | }
|
---|
42 | });
|
---|
43 |
|
---|
44 | // update positions based on new order
|
---|
45 | for (int i = 0; i < positions.size(); i++)
|
---|
46 | textItems.get(i)
|
---|
47 | .setPosition(positions.get(i).x, positions.get(i).y);
|
---|
48 |
|
---|
49 | // items will need to be resorted after this
|
---|
50 | start.setResort(true);
|
---|
51 | FrameGraphics.Repaint();
|
---|
52 |
|
---|
53 | return null;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | protected void finalise(Frame start) {
|
---|
58 | message("Sorting Complete.");
|
---|
59 | }
|
---|
60 | } |
---|