Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/robrohan/treebeard/Find.java


1   /*
2    * Treebeard: an xml xslt transfomer
3    * Copyright (C) 2002 Rob Rohan
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the
6    * Free Software Foundation; either version 2 of the License, or (at your
7    * option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; without even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
12   * General Public License for more details.
13   * 
14   * You should have received a copy of the GNU General Public License along 
15   * with this program; if not, write to the Free Software Foundation, Inc.,
16   * 675 Mass Ave, Cambridge, MA 02139, USA.
17   *
18   * Email: me@robrohan.com
19   * 
20   * Find.java
21   *
22   * Created on July 21, 2002, 4:07 PM
23   */
24  
25  package com.robrohan.treebeard;
26  
27  import org.syntax.jedit.JEditTextArea;
28  
29  /**
30   * The find dialog
31   * @author  rob
32   */
33  public class Find extends javax.swing.JInternalFrame{ // javax.swing.JDialog {
34      
35      /** The xslt Pane to use */    
36      public JEditTextArea xsltPane;
37      /** The xml Pane to use */    
38      public JEditTextArea xmlPane;
39      /** The output pane to use */    
40      public JEditTextArea outputPane;
41      
42      int match_instance = 0;
43      
44      /** Creates new form Find
45       * @param parent to whom does this find belong
46       * @param modal modal?
47       */
48      public Find(java.awt.Frame parent, boolean modal) {
49          //super(title,resizable, closeable, maximizable, iconifiable);
50          super("Find",false,true,false,true);
51          //super(parent, modal);
52          initComponents();
53          this.comboDocument.addItem("XSLT");
54          this.comboDocument.addItem("XML");
55          this.comboDocument.addItem("OUTPUT");
56          
57          this.btnGroupFindin.add(this.radioTag);
58          this.btnGroupFindin.add(this.radioText);
59          this.btnGroupFindin.add(this.radioAttribute);
60          this.btnGroupFindin.add(this.radioAny);
61          this.radioTag.setSelected(true);
62          
63      }
64      
65      /** This method is called from within the constructor to
66       * initialize the form.
67       * WARNING: Do NOT modify this code. The content of this method is
68       * always regenerated by the Form Editor.
69       */
70      private void initComponents() {//GEN-BEGIN:initComponents
71          btnGroupFindin = new javax.swing.ButtonGroup();
72          jPanel1 = new javax.swing.JPanel();
73          panCenter = new javax.swing.JPanel();
74          jPanel2 = new javax.swing.JPanel();
75          jPanel10 = new javax.swing.JPanel();
76          jLabel1 = new javax.swing.JLabel();
77          txtFind = new javax.swing.JTextField();
78          jLabel2 = new javax.swing.JLabel();
79          txtReplace = new javax.swing.JTextField();
80          jLabel3 = new javax.swing.JLabel();
81          comboDocument = new javax.swing.JComboBox();
82          jPanel6 = new javax.swing.JPanel();
83          jPanel8 = new javax.swing.JPanel();
84          radioTag = new javax.swing.JRadioButton();
85          radioText = new javax.swing.JRadioButton();
86          radioAttribute = new javax.swing.JRadioButton();
87          radioAny = new javax.swing.JRadioButton();
88          jPanel7 = new javax.swing.JPanel();
89          cmdOk = new javax.swing.JButton();
90          cmdCancel = new javax.swing.JButton();
91          jPanel3 = new javax.swing.JPanel();
92          jPanel4 = new javax.swing.JPanel();
93          jPanel5 = new javax.swing.JPanel();
94  
95          setClosable(true);
96          setIconifiable(true);
97          setTitle("Find");
98          setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
99          getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
100 
101         panCenter.setLayout(new java.awt.GridLayout(3, 1, 5, 5));
102 
103         jPanel2.setLayout(new java.awt.BorderLayout(5, 5));
104 
105         jPanel10.setLayout(new java.awt.GridLayout(3, 2, 5, 5));
106 
107         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
108         jLabel1.setText("Find:");
109         jPanel10.add(jLabel1);
110 
111         txtFind.setToolTipText("Enter regular expression or nomal text");
112         jPanel10.add(txtFind);
113 
114         jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
115         jLabel2.setText("Replace with:");
116         jPanel10.add(jLabel2);
117 
118         txtReplace.addKeyListener(new java.awt.event.KeyAdapter() {
119             public void keyTyped(java.awt.event.KeyEvent evt) {
120                 txtReplaceKeyTyped(evt);
121             }
122             public void keyPressed(java.awt.event.KeyEvent evt) {
123                 txtReplaceKeyPressed(evt);
124             }
125             public void keyReleased(java.awt.event.KeyEvent evt) {
126                 txtReplaceKeyReleased(evt);
127             }
128         });
129 
130         jPanel10.add(txtReplace);
131 
132         jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
133         jLabel3.setText("In document:");
134         jPanel10.add(jLabel3);
135 
136         jPanel10.add(comboDocument);
137 
138         jPanel2.add(jPanel10, java.awt.BorderLayout.CENTER);
139 
140         panCenter.add(jPanel2);
141 
142         jPanel6.setLayout(new java.awt.BorderLayout(5, 5));
143 
144         jPanel6.setBorder(new javax.swing.border.TitledBorder("Find in"));
145         jPanel8.setLayout(new java.awt.GridLayout(2, 2, 5, 5));
146 
147         radioTag.setText("Tag");
148         jPanel8.add(radioTag);
149 
150         radioText.setText("Text");
151         jPanel8.add(radioText);
152 
153         radioAttribute.setText("Attribute");
154         jPanel8.add(radioAttribute);
155 
156         radioAny.setText("Free Search");
157         jPanel8.add(radioAny);
158 
159         jPanel6.add(jPanel8, java.awt.BorderLayout.CENTER);
160 
161         panCenter.add(jPanel6);
162 
163         jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 5, 25));
164 
165         cmdOk.setText("Search");
166         cmdOk.addActionListener(new java.awt.event.ActionListener() {
167             public void actionPerformed(java.awt.event.ActionEvent evt) {
168                 cmdOkActionPerformed(evt);
169             }
170         });
171 
172         jPanel7.add(cmdOk);
173 
174         cmdCancel.setText("Close");
175         cmdCancel.addActionListener(new java.awt.event.ActionListener() {
176             public void actionPerformed(java.awt.event.ActionEvent evt) {
177                 cmdCancelActionPerformed(evt);
178             }
179         });
180 
181         jPanel7.add(cmdCancel);
182 
183         panCenter.add(jPanel7);
184 
185         getContentPane().add(panCenter, java.awt.BorderLayout.CENTER);
186 
187         getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
188 
189         getContentPane().add(jPanel4, java.awt.BorderLayout.EAST);
190 
191         getContentPane().add(jPanel5, java.awt.BorderLayout.WEST);
192 
193         pack();
194     }//GEN-END:initComponents
195 
196     private void txtReplaceKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtReplaceKeyReleased
197         if(this.txtReplace.getText().length() > 0){
198             setToReplace();
199         }else{
200             setToFind();
201         }
202     }//GEN-LAST:event_txtReplaceKeyReleased
203 
204     /** set the find to do a replace instead */    
205     public void setToReplace(){
206         this.radioAny.setSelected(true);
207         this.radioAttribute.setEnabled(false);
208         this.radioTag.setEnabled(false);
209         this.radioText.setEnabled(false);
210     }
211     
212     /** set the find to do a find (instead of replace) */    
213     public void setToFind(){
214         this.radioAttribute.setEnabled(true);
215         this.radioTag.setEnabled(true);
216         this.radioText.setEnabled(true);
217     }
218     
219     private void txtReplaceKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtReplaceKeyPressed
220        //move again
221     }//GEN-LAST:event_txtReplaceKeyPressed
222 
223     private void txtReplaceKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtReplaceKeyTyped
224         //moved
225     }//GEN-LAST:event_txtReplaceKeyTyped
226 
227     private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOkActionPerformed
228         if(this.txtReplace.getText().length() > 0){
229             doReplace();
230         }else{
231             doSearch();
232         }
233     }//GEN-LAST:event_cmdOkActionPerformed
234 
235     private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
236         killWindow();
237     }//GEN-LAST:event_cmdCancelActionPerformed
238     
239     private void killWindow(){
240         setVisible(false);
241         //dispose();   
242     }
243     
244     /** Closes the dialog */
245     private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
246         killWindow();
247     }//GEN-LAST:event_closeDialog
248     
249     /**
250      * for testing
251      */
252     /* public static void main(String args[]) {
253         new Find(new javax.swing.JFrame(), true).show();
254     } */
255     
256     private void doReplace(){
257         JEditTextArea tempPane=null;
258         
259         //pick which document to search
260         switch (comboDocument.getSelectedIndex()){
261             case 0:
262                 tempPane = this.xsltPane;
263                 break;
264             case 1:
265                 tempPane = this.xmlPane;
266                 break;
267             case 2:
268                 tempPane = this.outputPane;
269                 break;
270         }
271         
272         String pattern = this.txtFind.getText();
273         
274         try{
275             org.apache.regexp.RE r = new org.apache.regexp.RE(pattern);
276             tempPane.setText(r.subst(tempPane.getText(),this.txtReplace.getText()));
277         }catch(Exception e){
278             System.err.println("Find->doReplace: " + e);
279             e.printStackTrace(System.err);
280         }
281     }
282     
283     private void doSearch(){
284         JEditTextArea tempPane=null;
285         String pattStart="";
286         String pattEnd="";
287         
288         //pick which document to search
289         //System.out.println(comboDocument.getSelectedIndex());
290         switch (comboDocument.getSelectedIndex()){
291             case 0:
292                 tempPane = this.xsltPane;
293                 break;
294             case 1:
295                 tempPane = this.xmlPane;
296                 break;
297             case 2:
298                 tempPane = this.outputPane;
299                 break;
300         }
301         
302         //pick which type of search to do
303         if( this.radioTag.isSelected() ){
304             pattStart = "</*";
305             pattEnd = "/*>";    
306         }
307         if( this.radioText.isSelected() ){
308             pattStart = ">.*";
309             pattEnd = ".*<";
310         }
311         
312         if( this.radioAttribute.isSelected() ){
313             pattStart="= *[\"\'].*";
314             pattEnd=".*[\"\']";
315         }
316         //free text search
317         if( this.radioAny.isSelected() ){;}
318         
319         
320         String pattern = pattStart + this.txtFind.getText() + pattEnd;
321         
322         try{
323             org.apache.regexp.RE r = new org.apache.regexp.RE(pattern);
324                      
325             //using the last position (match_instance) look for the next
326             // pattern using the Regular Expression
327             if(r.match(tempPane.getText(), match_instance)){
328                 
329                 //we found something, tell the pane to highlight it
330                  //r.getParenEnd(0) - r.getParen(0).length());
331                 tempPane.setSelectionEnd(r.getParenEnd(0));
332                 tempPane.setSelectionStart(r.getParenStart(0));
333 
334                 tempPane.requestFocus();
335                 
336                 //set to the next instance, or 0 if we hit the end
337                 if(r.getParenEnd(0) < tempPane.getText().length()){
338                     match_instance = r.getParenEnd(0);
339                 }else{
340                     tempPane.setSelectionStart(0);
341                     tempPane.setSelectionEnd(0);
342                     tempPane.requestFocus();
343                     match_instance = 0;
344                 }
345             //just set the instance to 0 if nothing is found
346             }else{
347                 tempPane.setSelectionStart(0);
348                 tempPane.setSelectionEnd(0);
349                 tempPane.requestFocus();
350                 match_instance = 0;
351             }
352             
353         }catch(Exception e){
354             System.err.println("Find->doSearch: " + e);
355             e.printStackTrace(System.err);
356         }
357     }
358     
359     // Variables declaration - do not modify//GEN-BEGIN:variables
360     private javax.swing.JRadioButton radioTag;
361     private javax.swing.JTextField txtFind;
362     private javax.swing.JRadioButton radioAny;
363     private javax.swing.JPanel jPanel4;
364     private javax.swing.JButton cmdOk;
365     private javax.swing.JLabel jLabel1;
366     private javax.swing.JLabel jLabel3;
367     private javax.swing.JRadioButton radioAttribute;
368     private javax.swing.JPanel jPanel3;
369     private javax.swing.JRadioButton radioText;
370     private javax.swing.JPanel jPanel10;
371     private javax.swing.JLabel jLabel2;
372     private javax.swing.JComboBox comboDocument;
373     private javax.swing.JPanel jPanel2;
374     private javax.swing.JPanel jPanel5;
375     private javax.swing.JPanel jPanel8;
376     private javax.swing.ButtonGroup btnGroupFindin;
377     private javax.swing.JButton cmdCancel;
378     private javax.swing.JPanel jPanel7;
379     private javax.swing.JPanel jPanel1;
380     private javax.swing.JPanel jPanel6;
381     private javax.swing.JPanel panCenter;
382     private javax.swing.JTextField txtReplace;
383     // End of variables declaration//GEN-END:variables
384     
385 }