1 | /**
|
---|
2 | * Format.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.Collections;
|
---|
24 | import java.util.Comparator;
|
---|
25 | import java.util.HashSet;
|
---|
26 | import java.util.List;
|
---|
27 |
|
---|
28 | import org.expeditee.gui.DisplayController;
|
---|
29 | import org.expeditee.gui.Frame;
|
---|
30 | import org.expeditee.gui.FrameUtils;
|
---|
31 | import org.expeditee.gui.ItemsList;
|
---|
32 | import org.expeditee.items.Item;
|
---|
33 | import org.expeditee.items.Text;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * A simple formatting agent that aligns all non-annotation Items on the given
|
---|
37 | * Frame by the X and Y axis of the first Item on the Frame. This agent
|
---|
38 | * separates the items into columns, the actual alignment is done by
|
---|
39 | * FrameUtils.Align()
|
---|
40 | *
|
---|
41 | * @author jdm18
|
---|
42 | *
|
---|
43 | */
|
---|
44 | public class Format extends DefaultAgent {
|
---|
45 | // whether the Items could be formatted successfully
|
---|
46 | private boolean _success = true;
|
---|
47 |
|
---|
48 | // adjustmen from the default format spacing
|
---|
49 | private int _adjust = 0;
|
---|
50 |
|
---|
51 | public Format() {
|
---|
52 | super();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public Format(String param) {
|
---|
56 | try {
|
---|
57 | if (param.startsWith("+"))
|
---|
58 | param = param.substring(1);
|
---|
59 | _adjust = Integer.parseInt(param);
|
---|
60 | } catch (Exception e) {
|
---|
61 | e.printStackTrace();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public Frame process(Frame start) {
|
---|
67 | // TODO What will happen if user runs the SIMPLE form of this...
|
---|
68 | // Does format box need to be disabled?!?!
|
---|
69 | // Check the position of the cursor and only format stuff inside the
|
---|
70 | // same box as the cursor
|
---|
71 | Collection<Text> itemsToFormat = getItemsToFormat(start);
|
---|
72 |
|
---|
73 | List<Item> changedItems = new ArrayList<Item>();
|
---|
74 |
|
---|
75 | ArrayList<Item> columnHeads = new ArrayList<Item>();
|
---|
76 |
|
---|
77 | ArrayList<ArrayList<Text>> columns = new ArrayList<ArrayList<Text>>();
|
---|
78 |
|
---|
79 | for (Text t : itemsToFormat) {
|
---|
80 | int col = findColumn(columnHeads, t);
|
---|
81 | // if this is the head of a new column
|
---|
82 | if (col < 0) {
|
---|
83 | columnHeads.add(t);
|
---|
84 | columns.add(new ArrayList<Text>());
|
---|
85 | // otherwise the column for this item has already been
|
---|
86 | // found set the column to be the one we just added...
|
---|
87 | col = columns.size() - 1;
|
---|
88 | } else
|
---|
89 | columns.get(col).add(t);
|
---|
90 | }
|
---|
91 |
|
---|
92 | // check for empty columns
|
---|
93 | int[] clear = new int[columnHeads.size()];
|
---|
94 | for (int i = 0; i < columns.size(); i++)
|
---|
95 | clear[i] = columns.get(i).size();
|
---|
96 |
|
---|
97 | // remove empty columns
|
---|
98 | for (int i = (clear.length - 1); i >= 0; i--)
|
---|
99 | if (clear[i] == 0) {
|
---|
100 | columns.remove(i);
|
---|
101 | columnHeads.remove(i);
|
---|
102 | }
|
---|
103 |
|
---|
104 | // if there are no columns to align, we are done
|
---|
105 | if (columns.size() == 0)
|
---|
106 | return null;
|
---|
107 |
|
---|
108 | // sort the column heads by X position
|
---|
109 | Collections.sort(columnHeads, new Comparator<Item>() {
|
---|
110 | public int compare(Item o1, Item o2) {
|
---|
111 | return o1.getX() - o2.getX();
|
---|
112 | }
|
---|
113 |
|
---|
114 | });
|
---|
115 |
|
---|
116 | // sort lists by their X axis
|
---|
117 | Collections.sort(columns, new Comparator<ArrayList<Text>>() {
|
---|
118 | public int compare(ArrayList<Text> o1, ArrayList<Text> o2) {
|
---|
119 | if (o2.size() == 0)
|
---|
120 | return -10;
|
---|
121 |
|
---|
122 | if (o1.size() == 0)
|
---|
123 | return 10;
|
---|
124 |
|
---|
125 | Item i1 = o1.get(0);
|
---|
126 | Item i2 = o2.get(0);
|
---|
127 |
|
---|
128 | return (i1.getX() - i2.getX());
|
---|
129 | }
|
---|
130 |
|
---|
131 | });
|
---|
132 |
|
---|
133 | int res = FrameUtils.Align(columns.get(0), true, _adjust);
|
---|
134 | _success = _success && (res >= 0);
|
---|
135 |
|
---|
136 | for (int i = 0; i < columns.size() - 1; i++) {
|
---|
137 | List<Text> list = columns.get(i);
|
---|
138 |
|
---|
139 | int maxX = 0;
|
---|
140 | int maxY = 0;
|
---|
141 | for (Item it : list) {
|
---|
142 | maxX = Math.max(maxX, it.getX() + it.getBoundsWidth());
|
---|
143 | maxY = Math.max(maxY, it.getY() + it.getBoundsHeight());
|
---|
144 | }
|
---|
145 |
|
---|
146 | int xCheck = maxX;
|
---|
147 | for (Item it : columns.get(i + 1))
|
---|
148 | xCheck = Math.max(xCheck, maxX
|
---|
149 | + /* item.getX() + */it.getBoundsWidth());
|
---|
150 |
|
---|
151 | if (xCheck < DisplayController.getFramePaintAreaWidth()) {
|
---|
152 | Item columnHead = columnHeads.get(i + 1);
|
---|
153 | if (columnHead.getX() < maxX && columnHead.getY() < maxY) {
|
---|
154 | if(columnHead.getX() != maxX && !changedItems.contains(columnHead)) {
|
---|
155 | Item copy = columnHead.copy();
|
---|
156 | copy.setID(columnHead.getID());
|
---|
157 | changedItems.add(copy);
|
---|
158 | }
|
---|
159 | columnHead.setX(maxX);
|
---|
160 | }
|
---|
161 |
|
---|
162 | for (Item it : columns.get(i + 1)) {
|
---|
163 | if (it.getX() < maxX && it.getY() < maxY) {
|
---|
164 | if(it.getX() != maxX && !changedItems.contains(it)) {
|
---|
165 | Item copy = it.copy();
|
---|
166 | copy.setID(it.getID());
|
---|
167 | changedItems.add(copy);
|
---|
168 | }
|
---|
169 | it.setX(maxX);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | res = FrameUtils.Align(columns.get(i + 1), true, _adjust, changedItems);
|
---|
175 | _success = _success && (res >= 0);
|
---|
176 | }
|
---|
177 |
|
---|
178 | // align all the separate columns
|
---|
179 | /*
|
---|
180 | * for(ArrayList<Item> l : columns){ int res = FrameUtils.Align(l,
|
---|
181 | * true); _success = _success && (res >= 0); }
|
---|
182 | */
|
---|
183 | start.setChanged(true);
|
---|
184 | start.addToUndoMove(new ItemsList(changedItems));
|
---|
185 | DisplayController.requestRefresh(true);
|
---|
186 | return null;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Finds which column contains the given Item, and returns the index to the
|
---|
191 | * column in the start & end lists. Returns -1 if no column is found that
|
---|
192 | * contains the given Item.
|
---|
193 | *
|
---|
194 | * @param toFind
|
---|
195 | * The Item to determine the correct column for
|
---|
196 | * @return The index of the column in the lists, or -1 if no column is found
|
---|
197 | */
|
---|
198 | private int findColumn(ArrayList<Item> columnHeads, Item toFind) {
|
---|
199 | for (Item top : columnHeads)
|
---|
200 | if (FrameUtils.inSameColumn(top, toFind))
|
---|
201 | return columnHeads.indexOf(top);
|
---|
202 |
|
---|
203 | return -1;
|
---|
204 | }
|
---|
205 |
|
---|
206 | @Override
|
---|
207 | protected void finalise(Frame start) {
|
---|
208 | if (_success)
|
---|
209 | overwriteMessage("Formatting complete.");
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | protected void message(String message) {
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | protected void overwriteMessage(String message) {
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Gets all the items that need to be formatted. If the user clicks in
|
---|
222 | * freeSpace these are all items not enclosed by a rectangle. If the user is
|
---|
223 | * formatting the items in a rectangle this is all the items in the
|
---|
224 | * rectangle.
|
---|
225 | *
|
---|
226 | * @param start
|
---|
227 | */
|
---|
228 | protected Collection<Text> getItemsToFormat(Frame start) {
|
---|
229 | Collection<Text> itemsToFormat = FrameUtils.getCurrentTextItems();
|
---|
230 |
|
---|
231 | // If the cursor is not inside a box...
|
---|
232 | if (itemsToFormat.size() < 1) {
|
---|
233 | // Add all the items that are in free space
|
---|
234 | itemsToFormat = start.getBodyTextItems(true);
|
---|
235 | // Remove all the enclosed items
|
---|
236 | Collection<Item> seen = new HashSet<Item>();
|
---|
237 | for (Item i : start.getVisibleItems()) {
|
---|
238 | if (!seen.contains(i) && i.isEnclosed()) {
|
---|
239 | seen.addAll(i.getEnclosingDots());
|
---|
240 | itemsToFormat.removeAll(i.getEnclosedItems());
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | return itemsToFormat;
|
---|
245 | }
|
---|
246 |
|
---|
247 | }
|
---|