source: trunk/org/expeditee/agents/Sort.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 1.5 KB
Line 
1package org.expeditee.agents;
2
3import java.awt.Point;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
7
8import org.expeditee.gui.Frame;
9import org.expeditee.gui.FrameGraphics;
10import org.expeditee.items.Item;
11import org.expeditee.items.Text;
12
13public 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}
Note: See TracBrowser for help on using the repository browser.