📄 queryeditortextpanel.java
字号:
/* * QueryEditorTextPanel.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.AWTException;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Graphics;import java.awt.Insets;import java.awt.Robot;import java.awt.event.MouseListener;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.MouseEvent;import java.awt.event.KeyEvent;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextPane;import javax.swing.JMenuItem;import javax.swing.KeyStroke;import javax.swing.JPopupMenu;import java.sql.SQLException;import java.sql.ResultSet;import java.util.Vector;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.SwingUtilities;import javax.swing.border.Border;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.Element;import org.executequery.GUIUtilities;import org.underworldlabs.util.SystemProperties;import org.executequery.SystemUtilities;import org.executequery.gui.text.TextUtilities;import org.underworldlabs.swing.actions.ActionBuilder;import org.executequery.databasemediators.DatabaseConnection;import org.executequery.gui.SQLExecutor;import org.underworldlabs.swing.GUIUtils;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** <p>This object is the primary mediator between the parent * <code>QueryEditor</code> object and the actual Query Editor's * text pane - the <code>QueryEditorTextPane</code>. All commands for the * text pane are propagated through here. This includes all requests * to execute queries, maintainance of the executed history list, * the editor's popup menu to the more simple cut/copy/paste commands. * * @author Takis Diakoumis * @version $Revision: 1.8 $ * @date $Date: 2006/08/24 09:38:11 $ */public class QueryEditorTextPanel extends JPanel implements SQLExecutor, MouseListener { /** The SQL text pane */ private QueryEditorTextPane queryPane; /** The editor's controller */ private QueryEditor queryEditor; /** Analyses and executes queries */ private QueryAnalyser analyser; /** The text pane's popup menu */ private PopMenu popup; /** The current history index */ private int historyNum; /** Constructs a new instance. */ public QueryEditorTextPanel(QueryEditor queryEditor) { super(new BorderLayout()); this.queryEditor = queryEditor; try { jbInit(); } catch (Exception e) { e.printStackTrace(); } historyNum = -1; //historyList = new Vector(); } /** Initializes the state of this instance. */ private void jbInit() throws Exception { // setup the query text panel and associated scroller queryPane = new QueryEditorTextPane(this); analyser = new QueryAnalyser(this); JScrollPane queryScroller = new JScrollPane(); queryScroller.getViewport().add(queryPane, BorderLayout.CENTER); queryScroller.setRowHeaderView(queryPane.getLineBorder()); queryScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); queryScroller.setBorder(new EditorScrollerBorder()); //setPreferredSize(QueryEditorConstants.PANEL_SIZE); add(queryScroller, BorderLayout.CENTER); popup = new PopMenu(); queryPane.addMouseListener(this); boolean commitMode = SystemProperties.getBooleanProperty("user", "editor.connection.commit"); popup.enableCommits(!commitMode); } public void setActivityStatusText(String s) { queryEditor.setActivityStatusText(s); } public void destroyConnection() { analyser.destroyConnection(); } public void setTextPaneBackground(Color c) { queryPane.setBackground(c); } public void setSQLKeywords(boolean reset) { queryPane.setSQLKeywords(reset); } public void setExecuting(boolean executing) { queryEditor.setExecuting(executing); } public void setOutputMessage(int type, String text) { queryEditor.setOutputMessage(type, text); } /* public void setExecutingMessage(String s) { queryEditor.setExecutingMessage(s); } */ /** * Indicates that the editor is closing and performs some cleanup. */ protected void closingEditor() { closeConnection(); /* ------------------------------------------------ * profiling found the popup keeps the * editor from being garbage collected at all!! * a call to removeAll() is a work around for now. * ------------------------------------------------ */ popup.removeAll(); queryEditor = null; } public void showLineNumbers(boolean show) { queryPane.showLineNumbers(show); } protected void setTextFocus() { GUIUtils.requestFocusInWindow(queryPane); } /** * Resets the executing line within the line * number border panel. */ public void resetExecutingLine() { queryPane.resetExecutingLine(); } /** * Resets the text pane's caret position to zero. */ public void resetCaretPosition() { queryPane.setCaretPosition(0); } /** * Interrupts any executing statements and propagates the * call to close the connection to the QueryAnalyser object. */ public void closeConnection() { interruptStatement(); analyser.closeConnection(); } /** * Returns whether a statement execution is in progress. * * @return true | false */ public boolean isExecuting() { return analyser.isExecuting(); } /** * Sets the editor's auto-commit mode to that specified. */ public void setCommitMode(boolean mode) { analyser.setCommitMode(mode); popup.enableCommits(!mode); } /** * Returns the editor's current auto-commit mode. */ public boolean getCommitMode() { return analyser.getCommitMode(); } // ------------------------------------- // Executed query history methods // ------------------------------------- /** * Selects the next query from the history list and places the * query text into the editor. */ public void selectNextQuery() { Vector history = SystemUtilities.getSqlCommandHistory(); queryPane.setText((String)history.elementAt(decrementHistoryNum())); } /** * Selects the previous query from the history list and places the * query text into the editor. */ public void selectPrevQuery() { Vector history = SystemUtilities.getSqlCommandHistory(); queryPane.setText((String)history.elementAt(incrementHistoryNum())); } /** * Increments the history index value. */ private int incrementHistoryNum() { // for previous Vector history = SystemUtilities.getSqlCommandHistory(); if (historyNum < history.size() - 1) { historyNum++; } queryEditor.setNextButtonEnabled(true); if (historyNum == history.size() - 1) {// || historyNum == 0) { queryEditor.setPreviousButtonEnabled(false); } return historyNum; } /** * Decrements the history index value. */ private int decrementHistoryNum() { if (historyNum != 0) { historyNum--; } queryEditor.setPreviousButtonEnabled(true); if (historyNum == 0) { queryEditor.setNextButtonEnabled(false); } return historyNum; } /** ignored statements for the history list */ private final String[] HISTORY_IGNORE = {"COMMIT", "ROLLBACK"}; /** * Adds the secified query to the executed statement history list. */ public void addToHistory(String query) { String _query = query.toUpperCase(); for (int i = 0; i < HISTORY_IGNORE.length; i++) { if (HISTORY_IGNORE[i].compareTo(_query) == 0) { return; } } SystemUtilities.addSqlCommand(query); } /** * Returns whether a call to previous history would be successful. */ public boolean hasPreviousHistory() { Vector history = SystemUtilities.getSqlCommandHistory(); return historyNum < history.size() - 1; } /** * Returns whether a call to next history would be successful. */ public boolean hasNextHistory() { return historyNum > 0; } /** * Returns the executed query history list. */ public Vector getHistoryList() { return SystemUtilities.getSqlCommandHistory(); } /** * Enters the specified text at the editor's current * insertion point. * * @param text - the text to insert */ public void insertTextAtCaret(String text) { int caretIndex = queryPane.getCaretPosition(); queryPane.replaceSelection(text); setTextFocus(); } public JTextPane getQueryArea() { return queryPane; } public void selectAll() { TextUtilities.selectAll(queryPane); } public void selectNone() { TextUtilities.selectNone(queryPane); } public void focusGained() { queryEditor.focusGained(); } public void focusLost() { if (queryEditor != null) { queryEditor.focusLost(); } } public void setRightStatusText(String s) { queryEditor.setRightStatusText(s); } /** * Sets the editor's text content that specified. * * @param s - the text to be set */ public void setQueryAreaText(String s) { try { // uninstall listeners on the text pane queryPane.uninstallListeners(); // clear the current held edits queryPane.clearEdits(); // set the text queryPane.setText(s); } finally { // reinstall listeners on the text pane queryPane.reinstallListeners(); } } /** * Loads the specified text into a blank 'offscreen' document * before switching to the SQL document. */ public void loadText(String text) { queryPane.loadText(text); } /** * Clears the output pane. */ public void clearOutputPane() { if (analyser.isExecuting()) { return; } queryEditor.clearOutputPane(); } public QueryEditorStatusBar getStatusBar() { return queryEditor.getStatusBar(); } public void disableUpdates(boolean disable) { queryPane.disableUpdates(disable); } public void disableCaretUpdate(boolean disable) { queryPane.disableCaretUpdate(disable); } public void preferencesChanged() { queryPane.resetAttributeSets(); analyser.preferencesChanged(); } /** * Indicates a connection has been closed. * * @param the connection thats been closed */ public void disconnected(DatabaseConnection dc) { analyser.disconnected(dc); } // ----------------------------------- // regex replacement arrays // ----------------------------------- private static final String[] REGEX_CHARS = { "\\*", "\\^", "\\.", "\\[", "\\]", "\\(", "\\)", "\\?", "\\&", "\\{", "\\}", "\\+"}; private static final String[] REGEX_SUBS = { "\\\\*", "\\\\^", "\\\\.", "\\\\[", "\\\\]", "\\\\(", "\\\\)", "\\\\?", "\\\\&", "\\\\{", "\\\\}", "\\\\+"}; /** * Moves the caret to the beginning of the specified query. * * @param query - the query to move the cursor to */ public void caretToQuery(String query) { /* String num = query.substring(query.indexOf("Z") + 1, query.indexOf("Z") + 3); int number = Integer.parseInt(num); Log.debug("number: " + number); */ // replace any regex control chars for (int i = 0; i < REGEX_CHARS.length; i++) { //if (i == number) i++; query = query.replaceAll(REGEX_CHARS[i], REGEX_SUBS[i]); //Log.debug("index : " + i + " " + query); } /* Log.debug("---------------------"); Log.debug(query); Log.debug("---------------------"); */ // replace the block comment marker //query = query.replaceAll( // QueryEditorConstants.BLOCK_COMMENT_REGEX, // ".*/\\\\*.*\\\\*/.*"); Matcher matcher = Pattern.compile(query, Pattern.DOTALL). matcher(queryPane.getText()); if (matcher.find()) { int index = matcher.start(); //Log.debug("index: " + index); if (index != -1) { queryPane.setCaretPosition(index); } } matcher = null; GUIUtils.requestFocusInWindow(queryPane); } /** * Returns the currently selected text, or null if not text * is currently selected. * * @return selected text */ public String getSelectedText() { String selection = queryPane.getSelectedText(); if (selection != null && selection.trim().length() > 0) { return selection; } return null; } public void executeSQLAtCursor(DatabaseConnection dc) { if (analyser.isExecuting()) { return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -