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

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

Made both types of messages (normal and overwrite mode) output the numbered prefix

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