[919] | 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 |
|
---|
[4] | 19 | package org.expeditee.agents;
|
---|
| 20 |
|
---|
| 21 | import java.awt.Point;
|
---|
| 22 | import java.util.ArrayList;
|
---|
[185] | 23 | import java.util.Collection;
|
---|
[4] | 24 | import java.util.Collections;
|
---|
| 25 | import java.util.Comparator;
|
---|
| 26 |
|
---|
| 27 | import org.expeditee.gui.Frame;
|
---|
| 28 | import org.expeditee.gui.FrameGraphics;
|
---|
[185] | 29 | import org.expeditee.gui.FrameUtils;
|
---|
[4] | 30 | import org.expeditee.items.Text;
|
---|
| 31 |
|
---|
| 32 | public 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) {
|
---|
[185] | 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 |
|
---|
[4] | 46 | ArrayList<Text> textItems = new ArrayList<Text>();
|
---|
[185] | 47 | textItems.addAll(itemsToSort);
|
---|
[4] | 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) {
|
---|
[185] | 58 | return String.CASE_INSENSITIVE_ORDER.compare(a.getText(), b
|
---|
| 59 | .getText());
|
---|
[4] | 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 | } |
---|