source: trunk/src/org/expeditee/agents/Format.java@ 1415

Last change on this file since 1415 was 1415, checked in by bln4, 5 years ago

Renamed Frame.getItems() to Frame.getSortedItems() to better represent its functionality.

-> org.apollo.ApolloGestureActions
-> org.apollo.ApolloSystem
-> org.expeditee.actions.Actions
-> org.expeditee.actions.Debug
-> org.expeditee.actions.ExploratorySearchActions
-> org.expeditee.actions.JfxBrowserActions
-> org.expeditee.actions.Misc
-> org.expeditee.actions.Navigation
-> org.expeditee.actions.ScriptBase
-> org.expeditee.actions.Simple
-> org.expeditee.agents.ComputeTree
-> org.expeditee.agents.CopyTree
-> org.expeditee.agents.DisplayComet
-> org.expeditee.agents.DisplayTree
-> org.expeditee.agents.DisplayTreeLeaves
-> org.expeditee.agents.GraphFramesetLinks
-> org.expeditee.agents.TreeProcessor
-> org.expeditee.gio.gesture.StandardGestureActions
-> org.expeditee.gui.DisplayController
-> org.expeditee.gui.FrameCreator
-> org.expeditee.gui.FrameIO
-> org.expeditee.io.DefaultTreeWriter
-> org.expeditee.io.JavaWriter
-> org.expeditee.io.PDF2Writer
-> org.expeditee.io.TXTWriter
-> org.expeditee.io.WebParser
-> org.expeditee.io.flowlayout.XGroupItem
-> org.expeditee.items.Dot
-> org.expeditee.items.Item
-> org.expeditee.items.ItemUtils
-> org.expeditee.network.FrameShare
-> org.expeditee.stats.TreeStats


Created ItemsList class to wrap ArrayList<Item>. Frames now use this new class to store its body list (used for display) as well as its primaryBody and surrogateBody.

-> org.expeditee.agents.Format
-> org.expeditee.agents.HFormat
-> org.expeditee.gio.gesture.StandardGestureActions
-> org.expeditee.gui.Frame
-> org.expeditee.gui.FrameUtils


Refactorted Frame.setResort(bool) to Frame.invalidateSorted() to better function how it is intended to with a more accurate name.

-> org.expeditee.agents.Sort


When writing out .exp files and getting attributes to respond to LEFT + RIGHT click, boolean items are by default true. This has always been the case. An ammendment to this is that defaults can now be established.
Also added 'EnterClick' functionality. If cursored over a item with this property and you press enter, it acts as if you have clicked on it instead.

-> org.expeditee.assets.resources-public.framesets.authentication.1.exp to 6.exp
-> org.expeditee.gio.gesture.StandardGestureActions
-> org.expeditee.gio.input.KBMInputEvent
-> org.expeditee.gio.javafx.JavaFXConversions
-> org.expeditee.gio.swing.SwingConversions
-> org.expeditee.gui.AttributeUtils
-> org.expeditee.io.Conversion
-> org.expeditee.io.DefaultFrameWriter
-> org.expeditee.items.Item


Fixed a bug caused by calling Math.abs on Integer.MIN_VALUE returning unexpected result. Due to zero being a thing, you cannot represent Math.abs(Integer.MIN_VALUE) in a Integer object. The solution is to use Integer.MIN_VALUE + 1 instead of Integer.MIN_VALUE.

-> org.expeditee.core.bounds.CombinationBounds
-> org.expeditee.io.flowlayout.DimensionExtent


Recoded the contains function in EllipticalBounds so that intersection tests containing circles work correctly.

-> org.expeditee.core.bounds.EllipticalBounds


Added toString() to PolygonBounds to allow for useful printing during debugging.

-> org.expeditee.core.bounds.PolygonBounds

Implemented Surrogate Mode!

-> org.expeditee.encryption.io.EncryptedExpReader
-> org.expeditee.encryption.io.EncryptedExpWriter
-> org.expeditee.encryption.items.surrogates.EncryptionDetail
-> org.expeditee.encryption.items.surrogates.Label
-> org.expeditee.gui.FrameUtils
-> org.expeditee.gui.ItemsList
-> org.expeditee.items.Item
-> org.expeditee.items.Text


???? Use Integer.MAX_VALUE cast to a float instead of Float.MAX_VALUE. This fixed some bug which I cannot remember.

-> org.expeditee.gio.TextLayoutManager
-> org.expeditee.gio.swing.SwingTextLayoutManager


Improved solution for dealing with the F10 key taking focus away from Expeditee due to it being a assessibility key.

-> org.expeditee.gio.swing.SwingInputManager


Renamed variable visibleItems in FrameGraphics.paintFrame to itemsToPaintCanditates to better represent functional intent.

-> org.expeditee.gui.FrameGraphics


Improved checking for if personal resources exist before recreating them

-> org.expeditee.gui.FrameIO


Repeated messages to message bay now have a visual feedback instead of just a beep. This visual feedback is in the form of a count of the amount of times it has repeated.

-> org.expeditee.gui.MessageBay


Updated comment on the Vector class to explain what vectors are.

-> org.expeditee.gui.Vector


Added constants to represent all of the property keys in DefaultFrameReader and DefaultFrameWriter.

-> org.expeditee.io.DefaultFrameReader
-> org.expeditee.io.DefaultFrameWriter


Updated the KeyList setting to be more heirarcial with how users will store their Secrets.

-> org.expeditee.settings.identity.secrets.KeyList

File size: 6.9 KB
Line 
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
19package org.expeditee.agents;
20
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashSet;
26import java.util.List;
27
28import org.expeditee.gui.DisplayController;
29import org.expeditee.gui.Frame;
30import org.expeditee.gui.FrameUtils;
31import org.expeditee.gui.ItemsList;
32import org.expeditee.items.Item;
33import 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 */
44public 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}
Note: See TracBrowser for help on using the repository browser.