📄 queryeditor.java
字号:
/* * QueryEditor.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.gui.editor;import java.awt.BorderLayout;import java.awt.Component;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.print.Printable;import java.sql.ResultSet;import java.sql.SQLException;import java.io.File;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JSplitPane;import javax.swing.text.JTextComponent;import org.executequery.Constants;import org.executequery.EventMediator;import org.executequery.GUIUtilities;import org.underworldlabs.util.SystemProperties;import org.executequery.SystemUtilities;import org.executequery.base.DefaultTabView;import org.underworldlabs.swing.DynamicComboBoxModel;import org.executequery.event.ConnectionEvent;import org.executequery.event.ConnectionListener;import org.executequery.databasemediators.DatabaseConnection;import org.executequery.gui.*;import org.executequery.gui.text.TextEditor;import org.executequery.gui.text.TextFileWriter;import org.executequery.databasemediators.DatabaseResourceHolder;import org.executequery.datasource.ConnectionManager;import org.executequery.event.KeywordEvent;import org.executequery.event.KeywordListener;import org.executequery.print.TablePrinter;import org.executequery.print.TextPrinter;import org.underworldlabs.util.MiscUtils;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * The Query Editor. * * @author Takis Diakoumis * @version $Revision: 1.7 $ * @date $Date: 2006/06/07 15:04:19 $ */public class QueryEditor extends DefaultTabView implements ConnectionListener, TextEditor, KeywordListener, DatabaseResourceHolder, FocusablePanel { public static final String TITLE = "Query Editor"; public static final String FRAME_ICON = "Edit16.gif"; /** editor open count for title numbering */ private static int editorCountSequence = 1; /** The editor's status bar */ private QueryEditorStatusBar statusBar; /** The editor's text pan panel */ private QueryEditorTextPanel editor; /** The editor's results panel */ private QueryEditorResultsPanel resultsPanel; /** Whether a file has been opened in the editor */ private boolean openFile = false; /** The open file's path */ private String absolutePath; /** The open file's name */ private String fileName; /** the default file */ private String defaultFileName; /** flags the content as having being changed */ private boolean contentChanged; /** Whether the set printable is text - not the table */ private boolean printingText; /** Whether result set meta data is to * be retained for executed queries */ private boolean holdMetaData; /** the editor's tool bar */ private QueryEditorToolBar toolBar; /** the active connections combo box model */ private DynamicComboBoxModel connectionsModel; /** the active connections combo */ private JComboBox connectionsCombo; /** the result pane base panel */ private JPanel resultsBase; /** the editor split pane */ private JSplitPane splitPane; /** * Constructs a new instance. */ public QueryEditor() { this(null, null); } /** * Creates a new query editor with the specified text content. * * @param the text content to be set */ public QueryEditor(String text) { this(text, null); } /** * Creates a new query editor with the specified text content * and the specified absolute file path. * * @param the text content to be set * @param the absolute file path; */ public QueryEditor(String text, String absolutePath) { super(new GridBagLayout()); printingText = true; // set the open file value setAbsoluteFile(absolutePath); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } defaultFileName = "script" + (editorCountSequence++) + ".sql"; if (text != null) { loadText(text); //setEditorText(text, true); } contentChanged = false; } /** <p>Initializes the state of this instance. */ private void jbInit() throws Exception { // construct the two query text area and results panels statusBar = new QueryEditorStatusBar(); statusBar.setBorder(BorderFactory.createEmptyBorder(2,-1,-2,-2)); editor = new QueryEditorTextPanel(this); resultsPanel = new QueryEditorResultsPanel(this); JPanel baseEditorPanel = new JPanel(new BorderLayout()); baseEditorPanel.add(editor, BorderLayout.CENTER); baseEditorPanel.add(statusBar, BorderLayout.SOUTH); baseEditorPanel.setBorder(BorderFactory.createMatteBorder( 1, 1, 1, 1, GUIUtilities.getDefaultBorderColour())); // add to a base panel - when last tab closed visible set // to false on the tab pane and split collapses - want to avoid this resultsBase = new JPanel(new BorderLayout()); resultsBase.add(resultsPanel, BorderLayout.CENTER); if (GUIUtilities.getLookAndFeel() < Constants.GTK_LAF) { splitPane = new EditorSplitPane(JSplitPane.VERTICAL_SPLIT, baseEditorPanel, resultsBase); } else { splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, baseEditorPanel, resultsBase); } splitPane.setDividerSize(4); splitPane.setResizeWeight(0.5); // --------------------------------------- // the tool bar and conn combo toolBar = new QueryEditorToolBar(); Vector connections = ConnectionManager.getActiveConnections(); connectionsModel = new DynamicComboBoxModel(connections); connectionsCombo = new JComboBox(connectionsModel); if (connectionsModel.getSize() == 0) { connectionsCombo.setEnabled(false); } //connectionsCombo.setFont(new Font("dialog", Font.BOLD, 11)); JPanel toolsPanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(0, 0, 0, 0); gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridy++; gbc.gridx++; gbc.weightx = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; toolsPanel.add(toolBar, gbc); gbc.gridy++; gbc.weightx = 0; gbc.gridwidth = 1; gbc.insets.top = 5; gbc.insets.left = 5; gbc.insets.right = 10; toolsPanel.add(new JLabel("Connection:"), gbc); gbc.gridx++; gbc.weightx = 1.0; gbc.insets.top = 2; gbc.insets.bottom = 2; gbc.insets.left = 0; gbc.insets.right = 0; toolsPanel.add(connectionsCombo, gbc); splitPane.setBorder(BorderFactory.createEmptyBorder(0,3,3,3)); JPanel base = new JPanel(new BorderLayout()); base.add(toolsPanel, BorderLayout.NORTH); base.add(splitPane, BorderLayout.CENTER); gbc.gridy = 1; gbc.gridx = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.insets.top = 0; gbc.insets.bottom = 0; gbc.insets.left = 0; gbc.insets.right = 0; gbc.fill = GridBagConstraints.BOTH; gbc.anchor = GridBagConstraints.SOUTHEAST; add(base, gbc); // register for connection events EventMediator.registerListener(EventMediator.CONNECTION_EVENT, this); // register for keyword events EventMediator.registerListener(EventMediator.KEYWORD_EVENT, this); setEditorPreferences(); statusBar.setCaretPosition(1,1); statusBar.setInsertionMode("INS"); } /** * Notifies this panel of a query execute in progress. * * @param true if executing, false otherwise */ public void setExecuting(boolean executing) { if (executing) { statusBar.startProgressBar(); } else { resultsPanel.finished(); statusBar.stopProgressBar(); } } /** the last divider location before a output hide */ private int lastDividerLocation; /** * Toggles the output pane visible or not. */ public void toggleOutputPaneVisible() { if (resultsBase.isVisible()) { lastDividerLocation = splitPane.getDividerLocation(); resultsBase.setVisible(false); } else { resultsBase.setVisible(true); splitPane.setDividerLocation(lastDividerLocation); } } /** * Enters the specified text at the editor's current * insertion point. * * @param text - the text to insert */ public void insertTextAtCaret(String text) { editor.insertTextAtCaret(text); } /** * Returns the default focus component, the query text * editor component. * * @return the editor component */ public Component getDefaultFocusComponent() { return editor.getQueryArea(); } /** * Sets the editor user preferences. */ public void setEditorPreferences() { setPanelBackgrounds(); statusBar.setVisible( SystemProperties.getBooleanProperty("user", "editor.display.statusbar")); editor.showLineNumbers( SystemProperties.getBooleanProperty("user", "editor.display.linenums")); editor.preferencesChanged(); editor.setCommitMode( SystemProperties.getBooleanProperty("user", "editor.connection.commit")); holdMetaData = SystemProperties.getBooleanProperty("user", "editor.results.metadata"); resultsPanel.setTableProperties(); } /** * Called to inform this component of a change/update * to the user defined key words. */ public void updateSQLKeywords() { editor.setSQLKeywords(true); } /** * Notification of a new keyword added to the list. */ public void keywordsAdded(KeywordEvent e) { editor.setSQLKeywords(true); } /** * Notification of a keyword removed from the list. */ public void keywordsRemoved(KeywordEvent e) { editor.setSQLKeywords(true); } /** * Sets the activity status bar label text to * that specified. * * @param the activity label text */ public void setActivityStatusText(String s) { statusBar.setExecutionTime(s); } /** * Sets the right status bar label text to * that specified. * * @param the right label text */ public void setRightStatusText(String s) { statusBar.setCommitStatus(s); } /** * Sets the text of the left status label. * * @param the text to be set */ public void setLeftStatusText(String s) { statusBar.setStatus(s); } /** * Propagates the call to interrupt an executing process. */ public void interrupt() { resultsPanel.interrupt(); } /** * Sets the result set object. * * @param the executed result set * @param whether to return the result set row count */ public int setResultSet(ResultSet rset, boolean showRowNumber) throws SQLException { return setResultSet(rset, showRowNumber, null); } /** * Sets the result set object. * * @param the executed result set * @param whether to return the result set row count * @param the executed query of the result set */ public int setResultSet(ResultSet rset, boolean showRowNumber, String query) throws SQLException { int rowCount = resultsPanel.setResultSet(rset, showRowNumber); revalidate(); return rowCount; } /** * Sets the result set object. * * @param the executed result set */ public void setResultSet(ResultSet rset) throws SQLException { resultsPanel.setResultSet(rset, true); revalidate(); } /** * Sets the result set object. * * @param the executed result set * @param the executed query of the result set */ public void setResultSet(ResultSet rset, String query) throws SQLException { resultsPanel.setResultSet(rset, true, query); revalidate(); } public void destroyTable() { resultsPanel.destroyTable(); } /** * Sets to display the result set meta data for the * currently selected result set tab. */ public void displayResultSetMetaData() { resultsPanel.displayResultSetMetaData(); } /** * Returns the editor status bar. * * @return the editor's status bar panel */ public QueryEditorStatusBar getStatusBar() { return statusBar; } /** * Disables/enables the listener updates as specified. */ public void disableUpdates(boolean disable) { editor.disableUpdates(disable); } /** * Returns true that a search can be performed on the editor. */ public boolean canSearch() { return true; } /** * Disables/enables the caret update as specified. */ public void disableCaretUpdate(boolean disable) { editor.disableCaretUpdate(disable); } public ResultSetTableModel getResultSetTableModel() { return resultsPanel.getResultSetTableModel(); } public void setResultText(int r, int t) { resultsPanel.setResultText(r, t); } /** * Returns whether a result set panel is selected and that * that panel has a result set row count > 0. * * @return true | false */ public boolean isResultSetSelected() { return resultsPanel.isResultSetSelected(); } /** * Sets the respective panel background colours within * the editor as specified by the user defined properties. */ public void setPanelBackgrounds() { editor.setTextPaneBackground( SystemProperties.getColourProperty("user", "editor.text.background.colour")); resultsPanel.setResultBackground( SystemProperties.getColourProperty("user", "editor.results.background.colour")); } /** * Sets the text of the editor pane to the previous * query available in the history list. Where no previous * query exists, nothing is changed. */ public void selectPreviousQuery() { try { GUIUtilities.showWaitCursor(); editor.selectPrevQuery(); } finally {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -