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

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