01: package nl.improved.sqlclient;
02:
03: import java.util.List;
04: import java.util.ArrayList;
05: import java.util.Iterator;
06:
07: public class SQLCommand {
08:
09: private List<StringBuilder> commandLines;
10:
11: /**
12: * Constructor.
13: */
14: public SQLCommand() {
15: commandLines = new ArrayList<StringBuilder>();
16: }
17:
18: public List<StringBuilder> getEditableLines() {
19: return commandLines;
20: }
21:
22: public List<? extends CharSequence> getLines() {
23: return commandLines;
24: }
25:
26: /**
27: * Returns the part of the command that is before the cursor position.
28: * @param cursorPosition the position of the cursor in the command lines
29: * @return the part of the command that is before the cursor position.
30: */
31: public CharSequence getSubCommand(Point cursorPosition) {
32: StringBuilder commandBuffer = new StringBuilder();
33: for (int i = 0; i <= cursorPosition.y; i++) {
34: if (i == cursorPosition.y) {
35: commandBuffer.append(commandLines.get(i).substring(0,
36: cursorPosition.x));
37: } else {
38: commandBuffer.append(commandLines.get(i));
39: if (i < cursorPosition.y) {
40: commandBuffer.append(' ');
41: }
42: }
43: }
44: return commandBuffer;
45: }
46:
47: public boolean endsWith(String s) {
48: return getUntrimmedCommandString().endsWith(s);
49: }
50:
51: public String getUntrimmedCommandString() {
52: StringBuilder returnValue = new StringBuilder();
53: Iterator<? extends CharSequence> parts = getLines().iterator();
54: while (parts.hasNext()) {
55: if (returnValue.length() > 0
56: && returnValue.charAt(returnValue.length() - 1) != '\n') {
57: returnValue.append(' ');
58: }
59: returnValue.append(parts.next());
60: }
61: return returnValue.toString();
62: }
63:
64: public String getCommandString() {
65: String returnString = getUntrimmedCommandString();
66: if (returnString.endsWith(";")) {
67: returnString = returnString.substring(0,
68: returnString.length() - 1).trim();
69: }
70: return returnString;
71: }
72: }
|