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

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

Added calculate action

File size: 4.6 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 }
33
34 public Format(String param) {
35 try {
36 if (param.startsWith("+"))
37 param = param.substring(1);
38 _adjust = Integer.parseInt(param);
39 } catch (Exception e) {
40 e.printStackTrace();
41 }
42 }
43
44 @Override
45 public Frame process(Frame start) {
46 //Check the position of the cursor and only format stuff inside the same box as the cursor
47 Collection<Text> itemsToFormat = FrameUtils.getCurrentTextItems();
48 if(itemsToFormat.size() < 1){
49 itemsToFormat = start.getBodyTextItems(true);
50 }
51
52 ArrayList<Item> columnHeads = new ArrayList<Item>();
53
54 ArrayList<ArrayList<Text>> columns = new ArrayList<ArrayList<Text>>();
55
56 for (Text t : itemsToFormat) {
57 int col = findColumn(columnHeads, t);
58 // if this is the head of a new column
59 if (col < 0) {
60 columnHeads.add(t);
61 columns.add(new ArrayList<Text>());
62 // otherwise the column for this item has already been
63 // found set the column to be the one we just added...
64 col = columns.size() - 1;
65 } else
66 columns.get(col).add(t);
67 }
68
69 // check for empty columns
70 int[] clear = new int[columnHeads.size()];
71 for (int i = 0; i < columns.size(); i++)
72 clear[i] = columns.get(i).size();
73
74 // remove empty columns
75 for (int i = (clear.length - 1); i >= 0; i--)
76 if (clear[i] == 0) {
77 columns.remove(i);
78 columnHeads.remove(i);
79 }
80
81 // if there are no columns to align, we are done
82 if (columns.size() == 0)
83 return null;
84
85 // sort the column heads by X position
86 Collections.sort(columnHeads, new Comparator<Item>() {
87 public int compare(Item o1, Item o2) {
88 return o1.getX() - o2.getX();
89 }
90
91 });
92
93 // sort lists by their X axis
94 Collections.sort(columns, new Comparator<ArrayList<Text>>() {
95 public int compare(ArrayList<Text> o1, ArrayList<Text> o2) {
96 if (o2.size() == 0)
97 return -10;
98
99 if (o1.size() == 0)
100 return 10;
101
102 Item i1 = o1.get(0);
103 Item i2 = o2.get(0);
104
105 return (i1.getX() - i2.getX());
106 }
107
108 });
109
110 int res = FrameUtils.Align(columns.get(0), true, _adjust);
111 _success = _success && (res >= 0);
112
113 for (int i = 0; i < columns.size() - 1; i++) {
114 List<Text> list = columns.get(i);
115
116 int maxX = 0;
117 int maxY = 0;
118 for (Item it : list) {
119 maxX = Math.max(maxX, it.getX() + it.getBoundsWidth());
120 maxY = Math.max(maxY, it.getY() + it.getBoundsHeight());
121 }
122
123 int xCheck = maxX;
124 for (Item it : columns.get(i + 1))
125 xCheck = Math.max(xCheck, maxX
126 + /* item.getX() + */it.getBoundsWidth());
127
128 if (xCheck < FrameGraphics.getMaxSize().width) {
129 if (columnHeads.get(i + 1).getX() < maxX
130 && columnHeads.get(i + 1).getY() < maxY)
131 columnHeads.get(i + 1).setX(maxX);
132
133 for (Item it : columns.get(i + 1))
134 if (it.getX() < maxX && it.getY() < maxY)
135 it.setX(maxX);
136 }
137
138 res = FrameUtils.Align(columns.get(i + 1), true, _adjust);
139 _success = _success && (res >= 0);
140 }
141
142 // align all the separate columns
143 /*
144 * for(ArrayList<Item> l : columns){ int res = FrameUtils.Align(l,
145 * true); _success = _success && (res >= 0); }
146 */
147 start.setChanged(true);
148 FrameGraphics.Repaint();
149 return null;
150 }
151
152 /**
153 * Finds which column contains the given Item, and returns the index to the
154 * column in the start & end lists. Returns -1 if no column is found that
155 * contains the given Item.
156 *
157 * @param toFind
158 * The Item to determine the correct column for
159 * @return The index of the column in the lists, or -1 if no column is found
160 */
161 private int findColumn(ArrayList<Item> columnHeads, Item toFind) {
162 for (Item top : columnHeads)
163 if (FrameUtils.inSameColumn(top, toFind))
164 return columnHeads.indexOf(top);
165
166 return -1;
167 }
168
169 @Override
170 protected void finalise(Frame start) {
171 if (_success)
172 overwriteMessage("Formatting complete.");
173 }
174
175 @Override
176 protected void message(String message) {
177 }
178
179 @Override
180 protected void overwriteMessage(String message) {
181 }
182
183}
Note: See TracBrowser for help on using the repository browser.