KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > QueryBuilderSqlTextArea


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  *
22  * @author Sanjay Dhamankar
23  */

24
25 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
26
27 import java.awt.*;
28 import java.awt.event.*;
29
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import javax.swing.text.*;
33
34 import javax.swing.JTextPane JavaDoc;
35
36 import javax.swing.JPopupMenu JavaDoc;
37 import javax.swing.JMenu JavaDoc;
38 import javax.swing.JMenuItem JavaDoc;
39
40 import org.openide.util.NbBundle;
41
42 public class QueryBuilderSqlTextArea extends JTextPane JavaDoc
43         implements ActionListener, KeyListener {
44
45     private boolean DEBUG = false;
46     private boolean stringIsParsed = false;
47     private QueryBuilder _queryBuilder;
48     private JPopupMenu JavaDoc _sqlTextPopup;
49     String JavaDoc _lastGoodQuery = null;
50     private JMenuItem JavaDoc runQueryMenuItem;
51     private JMenuItem JavaDoc parseQueryMenuItem;
52     private boolean _queryChanged = false;
53     private String JavaDoc keysTyped;
54     // This flag is used to decide whether the focusLost is caused
55
// when the popup (Parse Query, Run Query) is displayed.
56
// In the case where the focusLost is generated by the popup menu
57
// we do not need to parse the query.
58
// This is added to fix the problem of it is no longer possible to
59
// execute an arbitrary query. That is, if the user types in a query
60
// and then tries to run it, the query editor will always parse/generate
61
// it, which may transform the query formulation. That makes it harder
62
// to test out minor re-formulations in syntax.
63
//
64

65     // After removing the focus listener this may not be required
66
// please remove later.
67
private boolean _maybeShowPopup = false;
68
69     // private JPopupMenu _tableColumnPopup;
70

71     // code for syntax highlighting
72

73     private String JavaDoc keywordString = "";
74     private int currentPosition = 0;
75     private SimpleAttributeSet keyword = new SimpleAttributeSet();
76     private SimpleAttributeSet schema = new SimpleAttributeSet();
77     private SimpleAttributeSet table = new SimpleAttributeSet();
78     private SimpleAttributeSet column = new SimpleAttributeSet();
79     private SimpleAttributeSet normal = new SimpleAttributeSet();
80
81     private DefaultStyledDocument dsDocument = (DefaultStyledDocument)
82     getStyledDocument();
83     private static ArrayList JavaDoc keywords = null;
84     // SQL 92 keywords
85
// http://sqlzoo.net/sql92.html
86
private static final String JavaDoc[] sqlReservedWords = new String JavaDoc[] {
87         "ABSOLUTE", "ACTION", "ADD", "ALL", "ALLOCATE", "ALTER", // NOI18N
88
"AND", "ANY", "ARE", "AS", "ASC", "ASSERTION", "AT", // NOI18N
89
"AUTHORIZATION", "AVG", "BEGIN", "BETWEEN", "BIT", // NOI18N
90
"BIT_LENGTH", "BOTH", "BY", "CASCADE", "CASCADED", "CASE", // NOI18N
91
"CAST", "CATALOG", "CHAR", "CHARACTER", "CHAR_LENGTH", // NOI18N
92
"CHARACTER_LENGTH", "CHECK", "CLOSE", "COALESCE", "COLLATE", // NOI18N
93
"COLLATION", "COLUMN", "COMMIT", "CONNECT", "CONNECTION", // NOI18N
94
"CONSTRAINT", "CONSTRAINTS", "CONTINUE", "CONVERT", // NOI18N
95
"CORRESPONDING", "COUNT", "CREATE", "CROSS", "CURRENT", // NOI18N
96
"CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", // NOI18N
97
"CURRENT_USER", "CURSOR", "DATE", "DAY", "DEALLOCATE", "DEC", // NOI18N
98
"DECIMAL", "DECLARE", "DEFAULT", "DEFERRABLE", "DEFERRED", // NOI18N
99
"DELETE", "DESC", "DESCRIBE", "DESCRIPTOR", "DIAGNOSTICS", // NOI18N
100
"DISCONNECT", "DISTINCT", "DOMAIN", "DOUBLE", "DROP", "ELSE", // NOI18N
101
"END", "END-EXEC", "ESCAPE", "EXCEPT", "EXCEPTION", "EXEC", // NOI18N
102
"EXECUTE", "EXISTS", "EXTERNAL", "EXTRACT", "FALSE", "FETCH", // NOI18N
103
"FIRST", "FLOAT", "FOR", "FOREIGN", "FOUND", "FROM", "FULL", // NOI18N
104
"GET", "GLOBAL", "GO", "GOTO", "GRANT", "GROUP", "HAVING", // NOI18N
105
"HOUR", "IDENTITY", "IMMEDIATE", "IN", "INDICATOR", // NOI18N
106
"INITIALLY", "INNER", "INPUT", "INSENSITIVE", "INSERT", "INT", // NOI18N
107
"INTEGER", "INTERSECT", "INTERVAL", "INTO", "IS", "ISOLATION", // NOI18N
108
"JOIN", "KEY", "LANGUAGE", "LAST", "LEADING", "LEFT", "LEVEL", // NOI18N
109
"LIKE", "LOCAL", "LOWER", "MATCH", "MAX", "MIN", "MINUTE", // NOI18N
110
"MODULE", "MONTH", "NAMES", "NATIONAL", "NATURAL", "NCHAR", // NOI18N
111
"NEXT", "NO", "NOT", "NULL", "NULLIF", "NUMERIC", // NOI18N
112
"OCTET_LENGTH", "OF", "ON", "ONLY", "OPEN", "OPTION", "OR", // NOI18N
113
"ORDER", "OUTER", "OUTPUT", "OVERLAPS", "PAD", "PARTIAL", // NOI18N
114
"POSITION", "PRECISION", "PREPARE", "PRESERVE", "PRIMARY", // NOI18N
115
"PRIOR", "PRIVILEGES", "PROCEDURE", "PUBLIC", "READ", "REAL", // NOI18N
116
"REFERENCES", "RELATIVE", "RESTRICT", "REVOKE", "RIGHT", // NOI18N
117
"ROLLBACK", "ROWS", "SCHEMA", "SCROLL", "SECOND", "SECTION", // NOI18N
118
"SELECT", "SESSION", "SESSION_USER", "SET", "SIZE", "SMALLINT", // NOI18N
119
"SOME", "SPACE", "SQL", "SQLCODE", "SQLERROR", "SQLSTATE", // NOI18N
120
"SUBSTRING", "SUM", "SYSTEM_USER", "TABLE", "TEMPORARY", // NOI18N
121
"THEN", "TIME", "TIMESTAMP", "TIMEZONE_HOUR", // NOI18N
122
"TIMEZONE_MINUTE", "TO", "TRAILING", "TRANSACTION", // NOI18N
123
"TRANSLATE", "TRANSLATION", "TRIM", "TRUE", "UNION", "UNIQUE", // NOI18N
124
"UNKNOWN", "UPDATE", "UPPER", "USAGE", "USER", "USING", "VALUE", // NOI18N
125
"VALUES", "VARCHAR", "VARYING", "VIEW", "WHEN", "WHENEVER", // NOI18N
126
"WHERE", "WITH", "WORK", "WRITE", "YEAR", "ZONE" // NOI18N
127
};
128     static {
129         keywords = new ArrayList JavaDoc();
130         for (int i=0; i<sqlReservedWords.length; i++) {
131             keywords.add(sqlReservedWords[i]);
132         }
133     }
134     
135     // TODO JFB: sntax checking. turned off, turn on when fixed.
136
public static final boolean SYNTAX_HIGHLIGHT = true ;
137     // Constructor
138

139     public QueryBuilderSqlTextArea(QueryBuilder queryBuilder) {
140         super();
141         _queryBuilder = queryBuilder;
142         _sqlTextPopup = createSqlTextPopup();
143         
144         
145         
146         if ( SYNTAX_HIGHLIGHT ) {
147             addKeyListener(this);
148         }
149         
150         // set the bold attribute
151
// colors chosen from :
152
// http://ui.netbeans.org/docs/hi/annotations/index2.html
153
// StyleConstants.setBold(keyword, true);
154
StyleConstants.setForeground(keyword,new Color(0,0,153));
155         
156 // StyleConstants.setBold(schema, true);
157
StyleConstants.setForeground(schema, new Color(0,111,0));
158         
159 // StyleConstants.setBold(table, true);
160

161 // StyleConstants.setBold(column, true);
162
StyleConstants.setForeground(column,new Color(120,0,0));
163           
164         // Add support for code completion (comment out, breaks syntax highlighting)
165
// QueryBuilderSqlCompletion doc = new QueryBuilderSqlCompletion( this, sqlReservedWords);
166
// this.setDocument(doc);
167
}
168     
169     
170     
171     public void keyTyped(KeyEvent e) {
172         if ( SYNTAX_HIGHLIGHT
173                 // we don't recognize the syntax, no highlighting.
174
&& (_queryBuilder.getParseErrorMessage() == null ) ) {
175             this.currentPosition = this.getCaretPosition();
176             processChar(e.getKeyChar());
177         }
178         
179         
180     }
181     
182     /** ignore */
183     public void keyReleased(KeyEvent e) {
184         
185     }
186     
187     /** Handle the key pressed event and change the focus if a particular
188      * key combination is pressed. */

189     public void keyPressed(KeyEvent e) {
190         if( e.isShiftDown() ) {
191             int code = e.getKeyCode();
192             switch(code) {
193                 // diagram pane
194
case KeyEvent.VK_F10:
195                     // this check is added to fix a bug where the popup menu
196
// remains disabled when the user has removed the last table
197
// from the graph and then added (typed/pasted) text query.
198
// First check if the text is not just white spaces.
199
if ( ((JTextPane JavaDoc)(e.getComponent())).getText().trim().length() != 0 ) {
200                         parseQueryMenuItem.setEnabled(true);
201                         runQueryMenuItem.setEnabled(true);
202                     } else {
203                         // user may have just typed white spaces or may have typed
204
// a wrong query which got restored after he 'cancel'ed
205
// the warning dialog about non-standard query, which
206
// restored the previous 'blank' query.
207
parseQueryMenuItem.setEnabled(false);
208                         runQueryMenuItem.setEnabled(false);
209                     }
210                     _maybeShowPopup = true;
211                     _sqlTextPopup.show(e.getComponent(), 0, 0);
212                     break;
213             }
214         }
215         _queryBuilder.handleKeyPress(e);
216     }
217     
218     // code related to syntax highlighting
219

220     // Replace the document string at the given position ( pos )
221
// with the string ( str ) and with the given attributes ( attr )
222
private void replaceString( String JavaDoc str, int pos,
223             SimpleAttributeSet attr ) {
224         try {
225             dsDocument.remove( pos - str.length(), str.length() );
226             dsDocument.insertString(pos - str.length(), str, attr);
227         } catch (Exception JavaDoc ex){
228             // should never happen !!!
229
// ex.printStackTrace();
230
}
231     }
232     
233     // function which checks if the current paragraph element is
234
// either a SQL keyword, a schema name , a table name or a column name.
235
private void checkKeyword() {
236         int offset = this.currentPosition;
237         Element element = dsDocument.getParagraphElement( offset );
238         String JavaDoc elementText = "";
239         try {
240             elementText = dsDocument.getText( element.getStartOffset(),
241                     element.getEndOffset() - element.getStartOffset() );
242         } catch ( Exception JavaDoc ex ) {
243             // should never happen !!!
244
// ex.printStackTrace();
245
}
246         int elementTextLength = elementText.length();
247         if ( elementTextLength == 0 ) return;
248         
249         int i = 0;
250         
251         if ( element.getStartOffset() > 0 ) {
252             offset = offset - element.getStartOffset();
253         }
254         if ( ( offset >= 0 ) && ( offset <= elementTextLength-1 ) ) {
255             i = offset;
256             while ( i > 0 ){
257                 // traverse back until a delimiter is found
258
i--;
259                 char charAt = elementText.charAt( i );
260                 if ( (charAt == ' ') || (i == 0) || // NOI18N
261
(charAt == '.') || (charAt == '"') || // NOI18N
262
(charAt == '\'') || // NOI18N
263
(charAt == '\t') || // NOI18N
264
(charAt == ',') ) { // NOI18N
265
if (i != 0) {
266                         i++;
267                     }
268                     keywordString =
269                             elementText.substring(i, offset);//skip the period
270

271                     String JavaDoc s = keywordString.trim().toUpperCase();
272                     String JavaDoc db_element = keywordString.trim();
273                     // check if it is a keyword
274
if (keywords.contains(s)){
275                         replaceString(s, currentPosition, keyword);
276                     }
277                     // check if it is schema name
278
else if ( _queryBuilder.isSchemaName( db_element ) ) {
279                         replaceString(db_element, currentPosition, schema);
280                     }
281                     // check if it is table name
282
else if ( _queryBuilder.isTableName( db_element ) ) {
283                         replaceString(db_element, currentPosition, table);
284                     }
285                     // check if it is column name
286
else if ( _queryBuilder.isColumnName( db_element ) ) {
287                         replaceString(db_element, currentPosition, column);
288                     }
289                     // if none of the above is true, insert the text string
290
// with normal attributes.
291
else {
292                         replaceString( keywordString, currentPosition, normal);
293                     }
294                     break;
295                 }
296             }
297         }
298     }
299     
300     
301     // function which checks if the current paragraph element is
302
// either a SQL keyword, a schema name , a table name or a column name.
303
private void checkString() {
304         int offset = this.currentPosition;
305         Element element = dsDocument.getParagraphElement( offset );
306         String JavaDoc elementText = "";
307         try {
308             elementText = dsDocument.getText( element.getStartOffset(),
309                     element.getEndOffset() - element.getStartOffset() );
310         } catch ( Exception JavaDoc ex ) {
311             // should never happen !!!
312
// ex.printStackTrace();
313
}
314         int elementTextLength = elementText.length();
315         if ( elementTextLength == 0 ) return;
316         
317         int i = 0;
318         
319         if ( element.getStartOffset() > 0 ) {
320             offset = offset - element.getStartOffset();
321         }
322         if ( ( offset >= 0 ) && ( offset <= elementTextLength ) ) {
323             i = offset;
324             while ( i > 0 ){
325                 // traverse back until a delimiter is found
326
i--;
327                 char charAt = elementText.charAt( i );
328                 if ( (charAt == '"') || (charAt == '\'' ) ) { // NOI18N
329
keywordString =
330                             elementText.substring(i, offset);//skip the period
331

332                     String JavaDoc s = keywordString.toUpperCase();
333                     String JavaDoc db_element = keywordString;
334                     String JavaDoc db_element_wo_quotes;
335                     if ( keywordString.length() > 2 &&
336                             ( (keywordString.startsWith("\"") && keywordString.endsWith("\"") ) ||
337                             (keywordString.startsWith("\'") && keywordString.endsWith("\'") ) ) ) {
338                         db_element_wo_quotes =
339                                 keywordString.substring(1, keywordString.length()-1);
340                     } else if (keywordString.length() > 2 && ( keywordString.startsWith("\"") || keywordString.startsWith("\'") ) ) {
341                         db_element_wo_quotes =
342                                 keywordString.substring(1, keywordString.length());
343                     } else
344                         db_element_wo_quotes = keywordString;
345                     // check if it is schema name
346
if ( _queryBuilder.isSchemaName( db_element_wo_quotes ) ) {
347                         replaceString(db_element, currentPosition, schema);
348                     }
349                     // check if it is table name
350
else if ( _queryBuilder.isTableName( db_element_wo_quotes ) ) {
351                         replaceString(db_element, currentPosition, table);
352                     }
353                     // check if it is column name
354
else if ( _queryBuilder.isColumnName( db_element_wo_quotes ) ) {
355                         replaceString(db_element, currentPosition, column);
356                     }
357                     // if none of the above is true, insert the text string
358
// with normal attributes.
359
else {
360                         replaceString( keywordString, currentPosition, normal);
361                     }
362                     break;
363                 }
364             }
365         }
366     }
367     
368     
369     private void processString( String JavaDoc str ) {
370         char strChar = str.charAt(0);
371         // if '"' is encontered keep processing till the next one is found
372
if ( strChar == '"' || strChar == '\'') { // NOI18N
373
if ( stringIsParsed ) {
374                 checkString();
375                 stringIsParsed = false;
376             } else
377                 stringIsParsed = true;
378         }
379         if ( ! stringIsParsed ) {
380             // if a white-space character or a '.' or ',' is encountered
381
// check if it is a keyword
382
if ( strChar == ' ' || strChar == '\n' || // NOI18N
383
strChar == '\t' || strChar == '.' || // NOI18N
384
strChar == ',' ) { // NOI18N
385
checkKeyword();
386             }
387         } else {
388             checkString();
389         }
390     }
391     
392     private void processChar(char strChar) {
393         char[] chrstr = new char[1];
394         chrstr[0] = strChar;
395         String JavaDoc str = new String JavaDoc(chrstr);
396         //this.keysTyped = str;
397
processString(str);
398     }
399     
400     private void processWords(String JavaDoc str){
401         StringBuffer JavaDoc wordBuffer = new StringBuffer JavaDoc();;
402         stringIsParsed = false;
403         for ( int i =0; i < str.length(); i++ ) {
404             char strChar = str.charAt(i);
405             if ( strChar == '"' || strChar == '\'') { // NOI18N
406
if ( stringIsParsed ) {
407                     stringIsParsed = false;
408                     wordBuffer.append(strChar);
409                     processWord( i, wordBuffer.toString());
410                     wordBuffer = null;
411                     wordBuffer = new StringBuffer JavaDoc();
412                 } else {
413                     stringIsParsed = true;
414                 }
415             }
416             if (!stringIsParsed) {
417                 if ( strChar == ' ' || strChar == '\n' || // NOI18N
418
strChar == '\t' || strChar == '.' || // NOI18N
419
strChar == ',' ) { // NOI18N
420
processWord( i, wordBuffer.toString());
421                     wordBuffer = null;
422                     wordBuffer = new StringBuffer JavaDoc();
423                 } else
424                     wordBuffer.append(strChar);
425             } else {
426                 wordBuffer.append(strChar);
427             }
428         }
429     }
430     
431     private void processWord(int position, String JavaDoc str) {
432         String JavaDoc s = str.toUpperCase();
433         String JavaDoc db_element = str;
434         String JavaDoc db_element_wo_quotes;
435         if (str.length() > 2 &&
436                 ( (str.startsWith("\"") && str.endsWith("\"") ) ||
437                 (str.startsWith("\'") && str.endsWith("\'") ) ) ) {
438             db_element_wo_quotes = str.substring(1, str.length()-1);
439             position = position+1;
440         } else if ( str.length() > 2 &&
441                 ( str.startsWith("\"") ||
442                 str.startsWith("\'") ) ) {
443             db_element_wo_quotes = str.substring(1, str.length());
444             position = position+1;
445         } else {
446             db_element_wo_quotes = str;
447         }
448         
449         boolean checkMore = true ;
450         // check if it is a keyword
451
if (keywords.contains(s)){
452             replaceString(s, position, keyword);
453             checkMore = false ;
454         }
455         // check if it is schema name
456

457         while ( checkMore ) {
458             if ( _queryBuilder.isSchemaName( db_element_wo_quotes ) ) {
459                 replaceString(db_element, position, schema);
460                 break ;
461             }
462             if ( _queryBuilder.isTableName( db_element_wo_quotes ) ) {
463                 replaceString(db_element, position, table);
464                 break ;
465             }
466             
467             // check if it is column name
468
if ( _queryBuilder.isColumnName( db_element_wo_quotes ) ) {
469                 replaceString(db_element, position, column);
470                 break ;
471             }
472             
473             
474             replaceString( str, position, normal);
475             
476             break ;
477         }
478     }
479     
480     // Create a background menu, with entries for parsing or executing the query.
481

482     JPopupMenu JavaDoc createSqlTextPopup() {
483         JPopupMenu JavaDoc sqlTextPopup;
484         JMenu JavaDoc menu;
485         JMenuItem JavaDoc menuItem;
486         
487         //Create the popup menu.
488
sqlTextPopup = new JPopupMenu JavaDoc();
489         
490         parseQueryMenuItem = new JMenuItem JavaDoc(NbBundle.getMessage(QueryBuilderSqlTextArea.class, "PARSE_QUERY")); // NOI18N
491
parseQueryMenuItem.addActionListener(this);
492         sqlTextPopup.add(parseQueryMenuItem);
493         
494         runQueryMenuItem = new JMenuItem JavaDoc(NbBundle.getMessage(QueryBuilderSqlTextArea.class, "RUN_QUERY")); // NOI18N
495
runQueryMenuItem.addActionListener(this);
496         sqlTextPopup.add(runQueryMenuItem);
497         
498         // Add listener to the text area so the popup menu can come up.
499
MouseListener popupListener = new sqlTextListener(sqlTextPopup);
500         super.addMouseListener(popupListener);
501         
502         return ( sqlTextPopup );
503     }
504     
505     public void setParseQueryMenuEnabled( boolean onOff ) {
506         parseQueryMenuItem.setEnabled(onOff);
507     }
508     
509     public void setRunQueryMenuEnabled( boolean onOff ) {
510         runQueryMenuItem.setEnabled(onOff);
511     }
512     
513     
514     // Inner classes, for menus
515

516     class sqlTextListener extends MouseAdapter {
517         
518         JPopupMenu JavaDoc popup;
519         
520         sqlTextListener(JPopupMenu JavaDoc popupMenu) {
521             popup = popupMenu;
522         }
523         
524         public void mousePressed(MouseEvent e) {
525             maybeShowPopup(e);
526         }
527         
528         public void mouseReleased(MouseEvent e) {
529             mousePressed(e);
530         }
531         
532         private void maybeShowPopup(MouseEvent e) {
533             if (e.isPopupTrigger()) {
534                 // this check is added to fix a bug where the popup menu
535
// remains disabled when the user has removed the last table
536
// from the graph and then added (typed/pasted) text query.
537
// First check if the text is not just white spaces.
538
if ( ((JTextPane JavaDoc)(e.getComponent())).getText().trim().length() != 0 ) {
539                     parseQueryMenuItem.setEnabled(true);
540                     runQueryMenuItem.setEnabled(true);
541                 } else {
542                     // user may have just typed white spaces or may have typed
543
// a wrong query which got restored after he 'cancel'ed
544
// the warning dialog about non-standard query, which
545
// restored the previous 'blank' query.
546
parseQueryMenuItem.setEnabled(false);
547                     runQueryMenuItem.setEnabled(false);
548                 }
549                 _maybeShowPopup = true;
550                 popup.show(e.getComponent(), e.getX(), e.getY());
551             }
552         }
553     }
554     
555     public boolean queryChanged() {
556         return ( ! this.getText().equals( _lastGoodQuery ) );
557     }
558     
559     
560     // Actions -- Menu selections
561

562     public void actionPerformed(ActionEvent JavaDoc e) {
563         
564         JMenuItem JavaDoc source = (JMenuItem JavaDoc)(e.getSource());
565         
566         if (source.getText().equals(NbBundle.getMessage(QueryBuilder.class, "PARSE_QUERY"))) { // NOI18N
567

568             _maybeShowPopup = false;
569             String JavaDoc currentQuery = this.getText();
570             if ( (currentQuery != null) &&
571                     (currentQuery.trim().length() != 0 ) ) {
572                 // ** Always parse if the user asks for it !!!!!
573
/*
574                 if ( currentQuery.equals ( _lastGoodQuery ) && false )
575                 {
576                     _queryChanged = false;
577                     return;
578                 }
579                  */

580                 _queryChanged = true ;
581                 QueryBuilder.showBusyCursor( true );
582                 _queryBuilder.populate(currentQuery, true);
583                 QueryBuilder.showBusyCursor( false );
584             }
585         } else if (source.getText().equals(NbBundle.getMessage(QueryBuilder.class, "RUN_QUERY"))) { // NOI18N
586
_maybeShowPopup = false;
587             
588             String JavaDoc currentQuery = this.getText();
589             //
590
// if query is changed then parse it first
591
if ( (currentQuery != null) &&
592                     (currentQuery.trim().length() != 0 ) ) {
593                 if ( currentQuery.trim().equals( _lastGoodQuery ) ) {
594                     _queryChanged = false;
595                 } else {
596                     _queryChanged = true ;
597                     QueryBuilder.showBusyCursor( true );
598                     // run the query even if there is a parse error
599
// as this may be query not correctly parsed by the parser
600
// but still it may be a valid query which the user
601
// may want to run. See 6254361
602
_queryBuilder.populate(this.getText(), true) ;
603                     QueryBuilder.showBusyCursor( false );
604                 }
605             } else {
606                 return;
607             }
608             
609             // Execute the query
610
_queryBuilder.executeQuery(this.getText());
611             
612         }
613     }
614     
615     /**
616      * Sets the text of this TextComponent to the specified text. Also records it
617      * for possible reset later.
618      */

619     public void setQueryText(String JavaDoc str) {
620         
621         // to make sure the last part of the incoming string
622
// is highlighted.
623

624         String JavaDoc text = new String JavaDoc( str + " " );
625         
626         super.setText(text);
627         
628         if (DEBUG)
629             System.out.println("setQueryText called with " + str + "\n" ); // NOI18N
630

631         
632         /*
633         if (DEBUG) {
634           PerfTimer perfTimer = new PerfTimer();
635           perfTimer.print("setQueryText: Before processWords : "); // NOI18N
636         }
637          */

638         
639         if ( SYNTAX_HIGHLIGHT
640                 // we don't recognize the syntax, no highlighting.
641
&& (_queryBuilder.getParseErrorMessage() == null ) ) {
642             processWords(text);
643         }
644         
645         /*
646         if (DEBUG)
647           perfTimer.print("setQueryText: After processWords : "); // NOI18N
648          */

649         
650         if ( ! text.trim().equals( _lastGoodQuery ) ) {
651             _lastGoodQuery = text;
652             _queryChanged = true;
653         } else {
654             _queryChanged = false;
655         }
656         _maybeShowPopup = false;
657     }
658     
659 /*
660    public class PerfTimer {
661  
662         long _time;
663  
664         public PerfTimer() {
665             resetTimer();
666         }
667  
668         // reset Timer
669         public void resetTimer(){
670             // set current time
671             _time = System.currentTimeMillis();
672         }
673  
674         public long elapsedTime() {
675             // get elapsed Time
676             return (System.currentTimeMillis() - _time);
677         }
678  
679         public void print(String aString) {
680             System.out.println(aString + ": " + this.elapsedTime() + " ms"); // NOI18N
681         }
682     }
683  */

684     
685     /**
686      * Restore the last good query.
687      */

688     
689     public void restoreLastGoodQuery() {
690         super.setText( _lastGoodQuery );
691     }
692     
693     /**
694      * Save the last good query.
695      */

696     
697     public void saveLastGoodQuery(String JavaDoc query) {
698         _lastGoodQuery = query ;
699     }
700     
701     /**
702      * Clears the text area
703      */

704     void clear() {
705         this.setQueryText(""); // NOI18N
706
}
707     
708     /**
709      * Resets the text to the last parsed query
710      */

711     void reset() {
712         this.setQueryText(_lastGoodQuery);
713     }
714 }
715
Popular Tags