001: /*
002: * CreateSnippetAction.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.actions;
013:
014: import java.awt.Toolkit;
015: import java.awt.datatransfer.Clipboard;
016: import java.awt.datatransfer.StringSelection;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.InputEvent;
019: import java.awt.event.KeyEvent;
020:
021: import java.io.BufferedReader;
022: import java.io.StringReader;
023: import javax.swing.KeyStroke;
024:
025: import workbench.interfaces.TextContainer;
026: import workbench.log.LogMgr;
027: import workbench.resource.ResourceMgr;
028: import workbench.resource.Settings;
029: import workbench.util.FileUtil;
030: import workbench.util.StringUtil;
031:
032: /**
033: * Action to create a piece of Java code that declares the currently
034: * selected SQL statement as a variable.
035: * @see workbench.util.StringUtil#makeJavaString(String, String, boolean)
036: * @author support@sql-workbench.net
037: */
038: public class CreateSnippetAction extends WbAction {
039: private TextContainer client;
040:
041: public CreateSnippetAction(TextContainer aClient) {
042: super ();
043: this .client = aClient;
044: this .initMenuDefinition("MnuTxtCreateSnippet", KeyStroke
045: .getKeyStroke(KeyEvent.VK_O, InputEvent.ALT_MASK));
046: this .setMenuItemName(ResourceMgr.MNU_TXT_SQL);
047: }
048:
049: public void executeAction(ActionEvent e) {
050: String sql = this .client.getSelectedText();
051: if (sql == null) {
052: sql = this .client.getText();
053: }
054: String code = makeJavaString(sql);
055: Clipboard clp = Toolkit.getDefaultToolkit()
056: .getSystemClipboard();
057: StringSelection sel = new StringSelection(code);
058: clp.setContents(sel, sel);
059: }
060:
061: public String makeJavaString(String sql) {
062: if (sql == null)
063: return "";
064:
065: String prefix = Settings.getInstance().getProperty(
066: "workbench.clipcreate.codeprefix", "String sql = ");
067: String concat = Settings.getInstance().getProperty(
068: "workbench.clipcreate.concat", "+");
069: boolean includeNewLine = Settings.getInstance()
070: .getBoolProperty("workbench.clipcreate.includenewline",
071: true);
072:
073: StringBuilder result = new StringBuilder(sql.length()
074: + prefix.length() + 10);
075: result.append(prefix);
076: if (prefix.endsWith("="))
077: result.append(" ");
078: int k = result.length();
079: StringBuilder indent = new StringBuilder(k);
080: for (int i = 0; i < k; i++)
081: indent.append(' ');
082: BufferedReader reader = new BufferedReader(
083: new StringReader(sql));
084: boolean first = true;
085: try {
086: String line = reader.readLine();
087: while (line != null) {
088: line = StringUtil.replace(line, "\"", "\\\"");
089: if (first)
090: first = false;
091: else
092: result.append(indent);
093: result.append('"');
094: if (line.endsWith(";")) {
095: line = line.substring(0, line.length() - 1);
096: }
097: result.append(line);
098:
099: line = reader.readLine();
100: if (line != null) {
101: if (includeNewLine) {
102: result.append(" \\n\"");
103: } else {
104: result.append(" \"");
105: }
106: result.append(" " + concat + "\n");
107: } else {
108: result.append("\"");
109: }
110: }
111: result.append(';');
112: } catch (Exception e) {
113: result
114: .append("(Error when creating Java code, see logfile for details)");
115: LogMgr.logError("CreateSnippetActions.makeJavaString()",
116: "Error creating Java String", e);
117: } finally {
118: FileUtil.closeQuitely(reader);
119: }
120: return result.toString();
121: }
122:
123: }
|