001: /*
002: * DbObjectSourcePanel.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.dbobjects;
013:
014: import java.awt.BorderLayout;
015: import java.awt.EventQueue;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import javax.swing.JPanel;
019: import workbench.db.SourceStatementsHelp;
020: import workbench.db.WbConnection;
021: import workbench.gui.MainWindow;
022: import workbench.gui.WbSwingUtilities;
023: import workbench.gui.actions.ReloadAction;
024: import workbench.gui.components.DropDownButton;
025: import workbench.gui.components.FocusIndicator;
026: import workbench.gui.components.WbToolbar;
027: import workbench.gui.sql.EditorPanel;
028: import workbench.gui.sql.PanelContentSender;
029: import workbench.interfaces.Reloadable;
030: import workbench.interfaces.Resettable;
031: import workbench.log.LogMgr;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034: import workbench.util.StringUtil;
035:
036: /**
037: * @author support@sql-workbench.net
038: */
039: public class DbObjectSourcePanel extends JPanel implements
040: ActionListener, Resettable {
041: protected EditorPanel sourceEditor;
042: protected ReloadAction reloadSource;
043: protected DropDownButton editButton;
044: private EditorTabSelectMenu selectTabMenu;
045: private MainWindow parentWindow;
046:
047: public DbObjectSourcePanel(MainWindow parent, Reloadable reloader) {
048: parentWindow = parent;
049: if (reloader != null) {
050: reloadSource = new ReloadAction(reloader);
051: reloadSource.setEnabled(false);
052: }
053:
054: this .sourceEditor = EditorPanel.createSqlEditor();
055: this .sourceEditor.showFindOnPopupMenu();
056: this .sourceEditor.setEditable(false);
057: this .setLayout(new BorderLayout());
058: WbToolbar toolbar = new WbToolbar();
059:
060: if (reloadSource != null) {
061: toolbar.add(reloadSource);
062: reloadSource.addToInputMap(sourceEditor);
063: }
064:
065: this .add(this .sourceEditor, BorderLayout.CENTER);
066: if (reloadSource != null) {
067: this .add(toolbar, BorderLayout.NORTH);
068: }
069: if (parentWindow != null) {
070: editButton = new DropDownButton("Edit in");
071: selectTabMenu = new EditorTabSelectMenu(this , ResourceMgr
072: .getString("LblEditScriptSource"),
073: "LblEditInNewTab", "LblEditInTab", parent);
074: editButton.setDropDownMenu(selectTabMenu.getPopupMenu());
075: toolbar.add(editButton);
076: }
077: if (Settings.getInstance().showFocusInDbExplorer()) {
078: new FocusIndicator(sourceEditor, sourceEditor);
079: }
080: }
081:
082: public void actionPerformed(ActionEvent e) {
083: String command = e.getActionCommand();
084:
085: if (command.startsWith(EditorTabSelectMenu.PANEL_CMD_PREFIX)
086: && this .parentWindow != null) {
087: try {
088: final int panelIndex = Integer.parseInt(command
089: .substring(EditorTabSelectMenu.PANEL_CMD_PREFIX
090: .length()));
091:
092: // Allow the selection change to finish so that
093: // we have the correct table name in the instance variables
094: EventQueue.invokeLater(new Runnable() {
095: public void run() {
096: editText(panelIndex, false);
097: }
098: });
099: } catch (Exception ex) {
100: LogMgr.logError("TableListPanel().actionPerformed()",
101: "Error when accessing editor tab", ex);
102: }
103: }
104: }
105:
106: private void editText(final int panelIndex, final boolean appendText) {
107: if (this .parentWindow != null) {
108: PanelContentSender sender = new PanelContentSender(
109: this .parentWindow);
110: sender.sendContent(getText(), panelIndex, appendText);
111: }
112: }
113:
114: public void setPlainText(final String sql) {
115: setEditorText(sql, false);
116: }
117:
118: /**
119: * Set the SQL source. If the text contains an error message
120: * indicating that the source is not available (as returned by DbMetadata
121: * if the SQL Queries have not been configured for e.g. stored procedures)
122: * syntax highlighting will be disabled.
123: */
124: public void setText(final String sql) {
125: boolean hasText = !StringUtil.isEmptyString(sql);
126: if (reloadSource != null)
127: reloadSource.setEnabled(hasText);
128:
129: if (editButton != null)
130: editButton.setEnabled(hasText);
131: if (hasText
132: && sql
133: .startsWith(SourceStatementsHelp.VIEW_ERROR_START)
134: || sql
135: .startsWith(SourceStatementsHelp.PROC_ERROR_START)
136: || sql.startsWith(ResourceMgr
137: .getString("MsgSynonymSourceNotImplemented"))) {
138: setEditorText(sql, false);
139: } else {
140: setEditorText(sql, true);
141: }
142: }
143:
144: private void setEditorText(final String text,
145: final boolean enableHighlight) {
146: if (enableHighlight) {
147: sourceEditor.enableSqlHighlight();
148: } else {
149: sourceEditor.disableSqlHighlight();
150: }
151:
152: WbSwingUtilities.invoke(new Runnable() {
153: public void run() {
154: sourceEditor.setText(text == null ? "" : text);
155: }
156: });
157:
158: }
159:
160: public String getText() {
161: return sourceEditor.getText();
162: }
163:
164: public void requestFocus() {
165: this .sourceEditor.requestFocus();
166: }
167:
168: public boolean requestFocusInWindow() {
169: return this .sourceEditor.requestFocusInWindow();
170: }
171:
172: public void setCaretPosition(int pos, boolean selectLine) {
173: sourceEditor.setCaretPosition(pos);
174: if (selectLine) {
175: int line = sourceEditor.getCaretLine();
176: int length = sourceEditor.getLineLength(line);
177: int lineStart = sourceEditor.getLineStartOffset(line);
178: if (lineStart > 0 && length > 0) {
179: sourceEditor.select(lineStart, lineStart + length);
180: }
181: }
182: }
183:
184: public void scrollToCaret() {
185: sourceEditor.scrollToCaret();
186: }
187:
188: public void setDatabaseConnection(WbConnection con) {
189: sourceEditor.setDatabaseConnection(con);
190: }
191:
192: public void setEditable(boolean flag) {
193: sourceEditor.setEditable(flag);
194: }
195:
196: public void reset() {
197: this .setText("");
198: }
199:
200: }
|