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

Last change on this file since 133 was 133, checked in by ra33, 16 years ago

Turned search actions into search agents so they are run on a different thread

File size: 4.3 KB
Line 
1package org.expeditee.agents;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.List;
7
8import org.expeditee.gui.Frame;
9import org.expeditee.gui.FrameGraphics;
10import org.expeditee.gui.FrameUtils;
11import org.expeditee.items.Item;
12
13/**
14 * A simple formatting agent that aligns all non-annotation Items on the given
15 * Frame by the X and Y axis of the first Item on the Frame. This agent
16 * separates the items into columns, the actual alignment is done by
17 * FrameUtils.Align()
18 *
19 * @author jdm18
20 *
21 */
22public class Format extends DefaultAgent {
23 // whether the Items could be formatted successfully
24 private boolean _success = true;
25
26 // adjustmen from the default format spacing
27 private int _adjust = 0;
28
29 public Format() {
30 }
31
32 public Format(String param) {
33 try {
34 if (param.startsWith("+"))
35 param = param.substring(1);
36 _adjust = Integer.parseInt(param);
37 } catch (Exception e) {
38 e.printStackTrace();
39 }
40 }
41
42 @Override
43 public Frame process(Frame start) {
44 ArrayList<Item> columnHeads = new ArrayList<Item>();
45
46 ArrayList<ArrayList<Item>> columns = new ArrayList<ArrayList<Item>>();
47
48 for (Item t : start.getBodyTextItems(true)) {
49 int col = findColumn(columnHeads, t);
50 // if this is the head of a new column
51 if (col < 0) {
52 columnHeads.add(t);
53 columns.add(new ArrayList<Item>());
54 // otherwise the column for this item has already been
55 // found set the column to be the one we just added...
56 col = columns.size() - 1;
57 } else
58 columns.get(col).add(t);
59 }
60
61 // check for empty columns
62 int[] clear = new int[columnHeads.size()];
63 for (int i = 0; i < columns.size(); i++)
64 clear[i] = columns.get(i).size();
65
66 // remove empty columns
67 for (int i = (clear.length - 1); i >= 0; i--)
68 if (clear[i] == 0) {
69 columns.remove(i);
70 columnHeads.remove(i);
71 }
72
73 // if there are no columns to align, we are done
74 if (columns.size() == 0)
75 return null;
76
77 // sort the column heads by X position
78 Collections.sort(columnHeads, new Comparator<Item>() {
79 public int compare(Item o1, Item o2) {
80 return o1.getX() - o2.getX();
81 }
82
83 });
84
85 // sort lists by their X axis
86 Collections.sort(columns, new Comparator<ArrayList<Item>>() {
87 public int compare(ArrayList<Item> o1, ArrayList<Item> o2) {
88 if (o2.size() == 0)
89 return -10;
90
91 if (o1.size() == 0)
92 return 10;
93
94 Item i1 = o1.get(0);
95 Item i2 = o2.get(0);
96
97 return (i1.getX() - i2.getX());
98 }
99
100 });
101
102 int res = FrameUtils.Align(columns.get(0), true, _adjust);
103 _success = _success && (res >= 0);
104
105 for (int i = 0; i < columns.size() - 1; i++) {
106 List<Item> list = columns.get(i);
107
108 int maxX = 0;
109 int maxY = 0;
110 for (Item it : list) {
111 maxX = Math.max(maxX, it.getX() + it.getBoundsWidth());
112 maxY = Math.max(maxY, it.getY() + it.getBoundsHeight());
113 }
114
115 int xCheck = maxX;
116 for (Item it : columns.get(i + 1))
117 xCheck = Math.max(xCheck, maxX
118 + /* item.getX() + */it.getBoundsWidth());
119
120 if (xCheck < FrameGraphics.getMaxSize().width) {
121 if (columnHeads.get(i + 1).getX() < maxX
122 && columnHeads.get(i + 1).getY() < maxY)
123 columnHeads.get(i + 1).setX(maxX);
124
125 for (Item it : columns.get(i + 1))
126 if (it.getX() < maxX && it.getY() < maxY)
127 it.setX(maxX);
128 }
129
130 res = FrameUtils.Align(columns.get(i + 1), true, _adjust);
131 _success = _success && (res >= 0);
132 }
133
134 // align all the separate columns
135 /*
136 * for(ArrayList<Item> l : columns){ int res = FrameUtils.Align(l,
137 * true); _success = _success && (res >= 0); }
138 */
139 start.setChanged(true);
140 FrameGraphics.Repaint();
141 return null;
142 }
143
144 /**
145 * Finds which column contains the given Item, and returns the index to the
146 * column in the start & end lists. Returns -1 if no column is found that
147 * contains the given Item.
148 *
149 * @param toFind
150 * The Item to determine the correct column for
151 * @return The index of the column in the lists, or -1 if no column is found
152 */
153 private int findColumn(ArrayList<Item> columnHeads, Item toFind) {
154 for (Item top : columnHeads)
155 if (FrameUtils.inSameColumn(top, toFind))
156 return columnHeads.indexOf(top);
157
158 return -1;
159 }
160
161 @Override
162 protected void finalise(Frame start) {
163 if (_success)
164 overwriteMessage("Formatting complete.");
165 }
166
167 @Override
168 protected void message(String message) {
169 }
170
171 @Override
172 protected void overwriteMessage(String message) {
173 }
174
175}
Note: See TracBrowser for help on using the repository browser.