1 | /**
|
---|
2 | * HFormat.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 |
|
---|
19 | package org.expeditee.agents;
|
---|
20 |
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import org.expeditee.gui.Frame;
|
---|
26 | import org.expeditee.items.Item;
|
---|
27 | import org.expeditee.items.Text;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A simple formatting agent that aligns the Y values.
|
---|
31 | *
|
---|
32 | * @author Mike
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class HFormat extends Format {
|
---|
36 |
|
---|
37 | public HFormat() {
|
---|
38 | super();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public Frame process(Frame start) {
|
---|
43 | Collection<Text> itemsToFormat = getItemsToFormat(start);
|
---|
44 |
|
---|
45 | if(itemsToFormat.size() == 0)
|
---|
46 | return null;
|
---|
47 |
|
---|
48 | List<Item> changedItems = new ArrayList<Item>();
|
---|
49 |
|
---|
50 | float anchorY = 0F;
|
---|
51 | float yThreshold = 0F;
|
---|
52 |
|
---|
53 | for(Text t: itemsToFormat){
|
---|
54 | if(t.getY() > yThreshold + anchorY){
|
---|
55 | anchorY = t.getY();
|
---|
56 | yThreshold = t.getSize();
|
---|
57 | }else{
|
---|
58 | if(t.getY() != anchorY && !changedItems.contains(t)) {
|
---|
59 | Item copy = t.copy();
|
---|
60 | copy.setID(t.getID());
|
---|
61 | changedItems.add(copy);
|
---|
62 | }
|
---|
63 | t.setY(anchorY);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | System.out.println(changedItems);
|
---|
68 | start.addToUndoMove(changedItems);
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 | }
|
---|