001: /*
002: * MacroManager.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.sql;
013:
014: import java.awt.Frame;
015: import java.awt.Window;
016: import java.io.File;
017: import java.io.FileNotFoundException;
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: import javax.swing.SwingUtilities;
026: import workbench.gui.macros.MacroEntry;
027:
028: import workbench.gui.macros.MacroManagerDialog;
029: import workbench.gui.sql.SqlPanel;
030: import workbench.interfaces.MacroChangeListener;
031: import workbench.log.LogMgr;
032: import workbench.resource.Settings;
033: import workbench.util.StringUtil;
034: import workbench.util.WbPersistence;
035:
036: /**
037: * A class to manage, store and apply SQL macros (alias)
038: *
039: * @author support@sql-workbench.net
040: */
041: public class MacroManager {
042: private HashMap<String, String> macros;
043: private boolean modified = false;
044: private List<MacroChangeListener> changeListeners = null;
045: private boolean errorDuringLoad = false;
046: private String selectedTextKey = Settings.getInstance()
047: .getProperty("workbench.macro.key.selectioin",
048: "${selection}$");
049: private String selectedStatementKey = Settings.getInstance()
050: .getProperty("workbench.macro.key.selectedstmt",
051: "${selected_statement}$");
052: private String currentStatementKey = Settings.getInstance()
053: .getProperty("workbench.macro.key.currentstatement",
054: "${current_statement}$");
055: private String editorTextKey = Settings.getInstance().getProperty(
056: "workbench.macro.key.editortext", "${text}$");
057: private static MacroManager instance = new MacroManager();
058:
059: private MacroManager() {
060: }
061:
062: public static MacroManager getInstance() {
063: return instance;
064: }
065:
066: public boolean hadLoadErrors() {
067: return this .errorDuringLoad;
068: }
069:
070: public synchronized String getMacroText(String aKey) {
071: loadIfNecessary();
072: if (aKey == null)
073: return null;
074: String sql = this .macros.get(aKey.toLowerCase());
075: return sql;
076: }
077:
078: public synchronized boolean hasTextKey(String sql) {
079: if (sql == null)
080: return false;
081: return (sql.indexOf(editorTextKey) > -1);
082: }
083:
084: public synchronized boolean hasSelectedKey(String sql) {
085: if (sql == null)
086: return false;
087: return (sql.indexOf(selectedTextKey) > -1)
088: || (sql.indexOf(selectedStatementKey) > -1);
089: }
090:
091: public synchronized boolean hasCurrentKey(String sql) {
092: if (sql == null)
093: return false;
094: return (sql.indexOf(currentStatementKey) > -1);
095: }
096:
097: public synchronized String replaceCurrent(String sql,
098: String statementAtCursor) {
099: if (statementAtCursor == null || sql == null)
100: return sql;
101: return StringUtil.replace(sql, currentStatementKey,
102: statementAtCursor);
103: }
104:
105: public synchronized String replaceEditorText(String sql, String text) {
106: if (text == null || sql == null)
107: return sql;
108: return StringUtil.replace(sql, currentStatementKey, text);
109: }
110:
111: public synchronized String replaceSelected(String sql,
112: String selectedText) {
113: if (selectedText == null || sql == null)
114: return sql;
115:
116: if (sql.indexOf(selectedTextKey) > -1) {
117: return StringUtil.replace(sql, selectedTextKey,
118: selectedText);
119: } else if (sql.indexOf(selectedStatementKey) > -1) {
120: String stmt = selectedText.trim();
121: if (stmt.endsWith(";")) {
122: stmt = stmt.substring(0, stmt.length() - 1);
123: }
124: return StringUtil.replace(sql, selectedStatementKey, stmt);
125: }
126: return sql;
127: }
128:
129: public synchronized void removeMacro(String aKey) {
130: loadIfNecessary();
131: if (aKey == null)
132: return;
133: this .macros.remove(aKey);
134: this .modified = true;
135: this .fireMacroListChange();
136: }
137:
138: public synchronized List<String> getMacroList() {
139: loadIfNecessary();
140: Set<String> keys = this .macros.keySet();
141: ArrayList<String> result = new ArrayList<String>(keys);
142: return result;
143: }
144:
145: public synchronized String[] getMacroNames() {
146: loadIfNecessary();
147: Set keys = this .macros.keySet();
148: String[] result = new String[keys.size()];
149: Iterator itr = keys.iterator();
150: for (int i = 0; itr.hasNext(); i++) {
151: result[i] = (String) itr.next();
152: }
153: return result;
154: }
155:
156: public synchronized void setMacro(String aKey, String aText) {
157: loadIfNecessary();
158: if (aKey == null || aKey.trim().length() == 0)
159: return;
160: this .macros.put(aKey.toLowerCase(), aText);
161: this .modified = true;
162: this .fireMacroListChange();
163: }
164:
165: public synchronized void setMacros(Collection<MacroEntry> newMacros) {
166: if (newMacros == null)
167: return;
168: this .macros = new HashMap<String, String>(); // clear out the old entries
169:
170: for (MacroEntry entry : newMacros) {
171: this .macros.put(entry.getName().toLowerCase(), entry
172: .getText());
173: }
174: this .modified = true;
175: this .fireMacroListChange();
176: }
177:
178: public boolean isModified() {
179: return this .modified;
180: }
181:
182: public synchronized void clearAll() {
183: if (this .macros != null) {
184: this .macros.clear();
185: } else {
186: this .macros = new HashMap<String, String>();
187: }
188: this .modified = true;
189: this .fireMacroListChange();
190: }
191:
192: public synchronized void addChangeListener(
193: MacroChangeListener aListener) {
194: if (this .changeListeners == null) {
195: this .changeListeners = new ArrayList<MacroChangeListener>();
196: }
197: this .changeListeners.add(aListener);
198: }
199:
200: public void selectAndRun(SqlPanel aPanel) {
201: loadIfNecessary();
202: Window w = SwingUtilities.getWindowAncestor(aPanel);
203: Frame parent = null;
204: if (w instanceof Frame) {
205: parent = (Frame) w;
206: }
207: MacroManagerDialog d = new MacroManagerDialog(parent, aPanel);
208: d.setVisible(true);
209: }
210:
211: public synchronized void saveMacros() {
212: if (this .macros != null && this .modified) {
213: WbPersistence writer = new WbPersistence(this
214: .getMacroFile().getAbsolutePath());
215: try {
216: writer.writeObject(this .macros);
217: } catch (Exception th) {
218: LogMgr.logError("MacroManager.saveMacros()",
219: "Error saving macros", th);
220: }
221: this .modified = false;
222: this .errorDuringLoad = false;
223: }
224: }
225:
226: private void loadIfNecessary() {
227: if (this .macros == null)
228: this .loadMacros();
229: }
230:
231: private File getMacroFile() {
232: File f = new File(Settings.getInstance().getConfigDir(),
233: "WbMacros.xml");
234: return f;
235: }
236:
237: @SuppressWarnings("unchecked")
238: private void loadMacros() {
239: try {
240: WbPersistence reader = new WbPersistence(this
241: .getMacroFile().getAbsolutePath());
242: Object o = reader.readObject();
243: if (o instanceof HashMap) {
244: this .macros = (HashMap) o;
245: } else {
246: this .macros = new HashMap<String, String>(20);
247: }
248: } catch (FileNotFoundException fne) {
249: this .errorDuringLoad = false;
250: this .macros = new HashMap<String, String>(20);
251: } catch (Exception e) {
252: LogMgr.logError("MacroManager.loadMacros()",
253: "Error loading macro file", e);
254: this .macros = new HashMap<String, String>(20);
255: this .errorDuringLoad = true;
256: }
257: this .modified = false;
258: }
259:
260: private void fireMacroListChange() {
261: if (this .changeListeners == null)
262: return;
263: for (MacroChangeListener listener : this.changeListeners) {
264: if (listener != null) {
265: listener.macroListChanged();
266: }
267: }
268: }
269:
270: }
|