001: /*
002: * SqlHistoryEntry.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.sql;
013:
014: import workbench.log.LogMgr;
015: import workbench.util.StringUtil;
016:
017: /**
018: * A single entry for {@link workbench.gui.sql.SqlHistory}
019: *
020: * @author support@sql-workbench.net
021: */
022: public class SqlHistoryEntry {
023: private String text;
024: private int cursorPos;
025: private int selectionStart;
026: private int selectionEnd;
027:
028: public SqlHistoryEntry(String sql, int pos, int selStart, int selEnd) {
029: this .setText(sql);
030: int len = this .text.length();
031: if (pos > len)
032: this .cursorPos = len - 1;
033: else
034: this .cursorPos = pos;
035:
036: if (selStart < 0)
037: this .selectionStart = 0;
038: else
039: this .selectionStart = selStart;
040:
041: if (selEnd > len)
042: this .selectionEnd = len - 1;
043: else
044: this .selectionEnd = selEnd;
045: }
046:
047: public SqlHistoryEntry(String sql) {
048: this .setText(sql);
049: this .cursorPos = -1;
050: this .selectionStart = -1;
051: this .selectionEnd = -1;
052: }
053:
054: public String getText() {
055: return this .text;
056: }
057:
058: public int getCursorPosition() {
059: return this .cursorPos;
060: }
061:
062: public int getSelectionStart() {
063: return this .selectionStart;
064: }
065:
066: public int getSelectionEnd() {
067: return this .selectionEnd;
068: }
069:
070: public void applyTo(EditorPanel editor) {
071: if (editor == null)
072: return;
073: try {
074: editor.setText(this .text);
075: editor.setCaretPosition(0);
076: if (this .cursorPos > -1) {
077: editor.setCaretPosition(this .cursorPos);
078: editor.scrollToCaret();
079: }
080: if (this .selectionStart > -1
081: && this .selectionEnd > this .selectionStart
082: && this .selectionEnd < editor.getDocumentLength()) {
083: editor.select(this .selectionStart, this .selectionEnd);
084: }
085: } catch (Exception e) {
086: LogMgr.logWarning("SqlHistoryEntry.applyTo()",
087: "Error applying " + this .toString(), e);
088: }
089: }
090:
091: public String toString() {
092: return "{" + StringUtil.getMaxSubstring(this .text, 40)
093: + ", Cursor=" + this .cursorPos + ", Selection=["
094: + this .selectionStart + "," + this .selectionEnd + "]}";
095: }
096:
097: public boolean equals(Object o) {
098: if (!(o instanceof SqlHistoryEntry))
099: return false;
100: SqlHistoryEntry other = (SqlHistoryEntry) o;
101: if (this .text.equals(other.text)) {
102: return (this .cursorPos == other.cursorPos
103: && this .selectionEnd == other.selectionEnd && this .selectionStart == other.selectionStart);
104: } else {
105: return false;
106: }
107: }
108:
109: private void setText(String value) {
110: if (value == null) {
111: this .text = "";
112: } else {
113: this.text = value;
114: }
115: }
116: }
|