001: /*
002: * CodeTools.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.editor;
013:
014: import java.awt.EventQueue;
015: import workbench.resource.Settings;
016: import workbench.util.StringUtil;
017:
018: /**
019: *
020: * @author support@sql-workbench.net
021: */
022: public class CodeTools {
023: private JEditTextArea editor;
024:
025: public CodeTools(JEditTextArea client) {
026: this .editor = client;
027: }
028:
029: /**
030: * Change the currently selected text so that it can be used for a SQL IN statement with
031: * character datatype.
032: * e.g.
033: *<pre>
034: *1234
035: *5678
036: *</pre>
037: *will be converted to
038: *<pre>
039: *('1234',
040: *'4456')
041: *</pre>
042: */
043: public void makeInListForChar() {
044: this .makeInList(true);
045: }
046:
047: public void makeInListForNonChar() {
048: this .makeInList(false);
049: }
050:
051: protected void makeInList(boolean quoteElements) {
052: int startline = editor.getSelectionStartLine();
053: int endline = editor.getSelectionEndLine();
054: int count = (endline - startline + 1);
055: final StringBuilder newText = new StringBuilder(count * 80);
056: String nl = Settings.getInstance()
057: .getInternalEditorLineEnding();
058:
059: try {
060: // make sure at least one character from the last line is selected
061: // if the selection does not extend into the line, then
062: // the line is ignored
063: int selectionLength = editor.getSelectionEnd(endline)
064: - editor.getLineStartOffset(endline);
065: if (selectionLength <= 0)
066: endline--;
067: } catch (Exception e) {
068: // ignore it
069: }
070:
071: int maxElementsPerLine = 5;
072: if (quoteElements) {
073: maxElementsPerLine = Settings.getInstance()
074: .getMaxCharInListElements();
075: } else {
076: maxElementsPerLine = Settings.getInstance()
077: .getMaxNumInListElements();
078: }
079: int elements = 0;
080:
081: boolean newLinePending = false;
082:
083: for (int i = startline; i <= endline; i++) {
084: String line = editor.getLineText(i);
085: if (StringUtil.isEmptyString(line))
086: continue;
087:
088: if (i == startline) {
089: newText.append('(');
090: } else {
091: newText.append(", ");
092: }
093: if (newLinePending) {
094: newText.append(nl);
095: newText.append(' ');
096: newLinePending = false;
097: }
098: if (quoteElements)
099: newText.append('\'');
100: newText.append(line.trim());
101: if (quoteElements)
102: newText.append('\'');
103: elements++;
104: if (i < endline) {
105: if ((elements & maxElementsPerLine) == maxElementsPerLine) {
106: newLinePending = true;
107: elements = 0;
108: }
109: }
110: }
111: newText.append(')');
112: newText.append(nl);
113: EventQueue.invokeLater(new Runnable() {
114: public void run() {
115: editor.setSelectedText(newText.toString());
116: }
117: });
118: }
119:
120: }
|