source: trunk/src/org/expeditee/agents/Sort.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 2.4 KB
Line 
1/**
2 * Sort.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.agents;
20
21import java.awt.Point;
22import java.util.ArrayList;
23import java.util.Collection;
24import java.util.Collections;
25import java.util.Comparator;
26
27import org.expeditee.gui.Frame;
28import org.expeditee.gui.FrameGraphics;
29import org.expeditee.gui.FrameUtils;
30import org.expeditee.items.Text;
31
32public class Sort extends DefaultAgent {
33
34 /**
35 * Sorts all the text Items on the given Frame alphabetically. The Items are
36 * then rearranged to reflect this ordering.
37 */
38 public Frame process(Frame start) {
39 // Check the position of the cursor and only format stuff inside the
40 // same box as the cursor
41 Collection<Text> itemsToSort = FrameUtils.getCurrentTextItems();
42 if (itemsToSort.size() < 1) {
43 itemsToSort = start.getBodyTextItems(false);
44 }
45
46 ArrayList<Text> textItems = new ArrayList<Text>();
47 textItems.addAll(itemsToSort);
48
49 // copy current positions of items
50 ArrayList<Point> positions = new ArrayList<Point>(textItems.size());
51
52 for (int i = 0; i < textItems.size(); i++)
53 positions.add(i, textItems.get(i).getPosition());
54
55 // Sort text items by their strings
56 Collections.sort(textItems, new Comparator<Text>() {
57 public int compare(Text a, Text b) {
58 return String.CASE_INSENSITIVE_ORDER.compare(a.getText(), b
59 .getText());
60 }
61 });
62
63 // update positions based on new order
64 for (int i = 0; i < positions.size(); i++)
65 textItems.get(i)
66 .setPosition(positions.get(i).x, positions.get(i).y);
67
68 // items will need to be resorted after this
69 start.setResort(true);
70 FrameGraphics.Repaint();
71
72 return null;
73 }
74
75 @Override
76 protected void finalise(Frame start) {
77 message("Sorting Complete.");
78 }
79}
Note: See TracBrowser for help on using the repository browser.