Revision: 6913
Author: victormote
Date: 2006-03-08 15:31:27 -0800 (Wed, 08 Mar 2006)
ViewCVS: http://svn.sourceforge.net/foray/?rev=6913&view=rev
Log Message:
-----------
Add logic for writing multiple break opportunities.
Modified Paths:
--------------
trunk/foray/foray-pretty/src/java/org/foray/pretty/FOrayPretty.java
Modified: trunk/foray/foray-pretty/src/java/org/foray/pretty/FOrayPretty.java
===================================================================
--- trunk/foray/foray-pretty/src/java/org/foray/pretty/FOrayPretty.java 2006-03-08 22:25:15 UTC (rev 6912)
+++ trunk/foray/foray-pretty/src/java/org/foray/pretty/FOrayPretty.java 2006-03-08 23:31:27 UTC (rev 6913)
@@ -596,7 +596,24 @@
* in that queue item. */
private int[] breakOpportunityItems;
private int[] breakOpportunityIndexes;
+ /* chunkSizes[n] = the size in chars of the chunk ending at
+ * breakOpportunityItems[n], breakOpportunityIndex[n]. */
+ private int[] chunkSizes;
+ /* If there are n break opportunities, there are n + 1 chunks. This
+ * stores the size of the last chunk. */
+ private int lastChunkSize;
+ /* The following items are similar to breakOpportunityItems and
+ * breakOpportunityIndexes, except they represent breaks actually
+ * chosen. */
+ private int[] breakChosenItems;
+ private int[] breakChosenIndexes;
+
+ /* The last index used in the "breakChosen" variables. Those items are
+ * sized to generally be larger than they need to be, so this is needed
+ * to keep track of the amount actually used. */
+ private int breakChosenIndex = -1;
+
Queue() {
}
@@ -604,11 +621,35 @@
if (attemptWriteAll()) {
return;
}
- breakAtLastOpportunity();
- /* Otherwise, search backwards through the items in the queue looking
- * for the last break opportunity*/
+ this.listBreakOpportunities();
+ this.chooseBreaks();
+ this.writeChosenChunks();
+ this.reset();
}
+ private void chooseBreaks() {
+ /* Make these arrays as big as the opportunity arrays to avoid
+ * resizing. */
+ int arraySize = this.breakOpportunityIndexes.length;
+ this.breakChosenItems = new int[arraySize];
+ this.breakChosenIndexes = new int[arraySize];
+
+ if (arraySize < 1) {
+ return;
+ }
+
+ /* For now, just choose the last opportunity. */
+ this.breakChosenIndex ++;
+ this.breakChosenItems[this.breakChosenIndex]
+ = this.breakOpportunityItems[arraySize - 1];
+ this.breakChosenIndexes[this.breakChosenIndex]
+ = this.breakOpportunityIndexes[arraySize - 1];
+
+ /* The main thing to watch out for here is that the last line is not
+ * too long. */
+// if (this.lastChunkSize >)
+ }
+
/**
* If everything can be written on the current line, do it. Otherwise,
* do nothing.
@@ -629,8 +670,20 @@
writeRawPCDATA(returnItem);
this.toWriteQueue.remove(0);
}
+ reset();
}
+ private void reset() {
+ this.breakChosenIndex = -1;
+ this.breakChosenItems = null;
+ this.breakChosenIndexes = null;
+ this.breakOpportunityItems = null;
+ this.breakOpportunityIndexes = null;
+ this.chunkSizes = null;
+ this.lastChunkSize = -1;
+ this.toWriteQueue.clear();
+ }
+
protected int size() {
return toWriteQueue.size();
}
@@ -655,33 +708,50 @@
return charCount;
}
- private void breakAtLastOpportunity() {
- this.listBreakOpportunities();
- /* If there is no break, there is nothing to do but write the whole
- * thing.*/
- if (this.breakOpportunityItems.length < 1) {
+ private void writeChosenChunks() {
+ /* If there are no breaks, there is nothing to do but write the
+ * whole thing.*/
+ if (this.breakChosenIndex < 1) {
this.writeAll();
return;
}
- /* Otherwise, split into two chunks. */
- /* Write the first chunk. */
- int lastIndex = this.breakOpportunityItems.length - 1;
- int lastBreakItem = this.breakOpportunityItems[lastIndex];
- int lastBreakIndex = this.breakOpportunityIndexes[lastIndex];
- writeChunk(0, 0, lastBreakItem, lastBreakIndex);
- write(newLine());
- /* Write the second chunk. */
- writeChunk(lastBreakItem, lastBreakIndex + 1,
- this.toWriteQueue.size() - 1,
- this.getLastItem().length() - 1);
- /* Clear the queue contents. */
- this.toWriteQueue.clear();
+ /* Notice that if there are n breaks, we loop through n + 1
+ * times, so that the last chunk gets written. */
+ for (int i = 0; i <= this.breakChosenIndex; i++) {
+ int startItem = 0;
+ int startIndex = 0;
+ int endItem = 0;
+ int endIndex = 0;
+
+ /* Adjust starting items for the items after the first. */
+ if (i != 0) {
+ startItem = this.breakChosenItems[i - 1];
+ startIndex = this.breakChosenIndexes[i - 1];
+ }
+
+ /* Set the ending items. */
+ if (i == this.breakChosenIndex) {
+ endItem = this.toWriteQueue.size() - 1;
+ endIndex = this.getLastItem().length() - 1;
+ } else {
+ endItem = this.breakChosenItems[i];
+ endItem = this.breakChosenIndexes[i];
+ }
+
+ /* Write the chunk. */
+ writeChunk(startItem, startIndex, endItem, endIndex);
+ write(newLine());
+ }
}
private void listBreakOpportunities() {
int count = countBreakOpportunities();
this.breakOpportunityItems = new int[count];
this.breakOpportunityIndexes = new int[count];
+ this.chunkSizes = new int[count];
+ if (count == 0) {
+ return;
+ }
count = 0;
for (int i = 0; i < this.toWriteQueue.size(); i++) {
String string = this.get(i);
@@ -690,12 +760,34 @@
if (c == ' ') {
this.breakOpportunityItems[count] = i;
this.breakOpportunityIndexes[count] = j;
+ int startItem = 0;
+ int startIndex = 0;
+ if (count > 0) {
+ startItem = this.breakOpportunityItems[count - 1];
+ startIndex = this.breakOpportunityIndexes[count - 1];
+ }
+ int endItem = i;
+ int endIndex = j;
+ int chunkSize = chunkSize(startItem, startIndex,
+ endItem, endIndex);
+ this.chunkSizes[count] = chunkSize;
count ++;
}
}
}
+ count = this.breakOpportunityItems.length;
+ this.lastChunkSize = chunkSize(
+ this.breakOpportunityItems[count - 1],
+ this.breakOpportunityIndexes[count - 1],
+ this.toWriteQueue.size() - 1,
+ this.getLastItem().length() - 1);
}
+ private int chunkSize(int startItem, int startItemIndex, int endItem,
+ int endItemIndex) {
+ return 0;
+ }
+
private int countBreakOpportunities() {
int count = 0;
for (int i = 0; i < this.toWriteQueue.size(); i++) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|