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

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.Comparator;
25
26import org.expeditee.core.Point;
27import org.expeditee.gui.DisplayController;
28import org.expeditee.gui.Frame;
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 DisplayController.requestRefresh(true);
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.