View Javadoc
1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.ui;
20  
21  import java.awt.BorderLayout;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import javax.swing.ButtonGroup;
28  import javax.swing.JButton;
29  import javax.swing.JFrame;
30  import javax.swing.JPanel;
31  import javax.swing.JRadioButton;
32  import javax.swing.JScrollPane;
33  import javax.swing.JSplitPane;
34  import javax.swing.JTextArea;
35  import javax.swing.JTree;
36  import javax.swing.tree.DefaultMutableTreeNode;
37  
38  /***
39   * @author christoph
40   */
41  public class RegRex extends JFrame {
42    private JTextArea inputArea;
43    private JTextArea patternArea;
44    private JTextArea outputArea;
45    private JRadioButton numberConvert;
46    private JRadioButton stringConvert;
47  
48    private JTree regexTree;
49    private DefaultMutableTreeNode root;
50  
51    public RegRex() {
52      super("RegRex - Simple RegularExpression Studio");
53  
54      inputArea = new JTextArea(6, 20);
55      getContentPane().add(new JScrollPane(inputArea), BorderLayout.WEST);
56  
57      JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
58  
59      patternArea = new JTextArea(6, 40);
60      pane.add(new JScrollPane(patternArea), JSplitPane.TOP);
61  
62      outputArea = new JTextArea(6, 40);
63      pane.add(new JScrollPane(outputArea), JSplitPane.BOTTOM);
64  
65      this.getContentPane().add(pane, BorderLayout.CENTER);
66  
67      root = new DefaultMutableTreeNode();
68      regexTree = new JTree(root);
69  
70      this.getContentPane().add(new JScrollPane(regexTree), BorderLayout.EAST);
71  
72      JPanel controlsPanel = new JPanel();
73  
74      ButtonGroup group = new ButtonGroup();
75  
76      stringConvert = new JRadioButton("String");
77      stringConvert.setSelected(true);
78      group.add(stringConvert);
79      controlsPanel.add(stringConvert);
80  
81      numberConvert = new JRadioButton("Number");
82      group.add(numberConvert);
83      controlsPanel.add(numberConvert);
84  
85      JButton convertButton = new JButton("Match");
86      controlsPanel.add(convertButton);
87  
88      getContentPane().add(controlsPanel, BorderLayout.SOUTH);
89  
90      convertButton.addActionListener(new ActionListener() {
91        public void actionPerformed(ActionEvent evt) {
92          applyPattern();
93        }
94      });
95  
96      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
97    }
98  
99    /***
100    *  
101    */
102   protected void applyPattern() {
103     StringBuffer result = new StringBuffer();
104     try {
105 
106       Pattern pattern = Pattern.compile(patternArea.getText());
107       Matcher matcher = pattern.matcher(inputArea.getText());
108 
109       root.removeAllChildren();
110 
111       while (matcher.find()) {
112         DefaultMutableTreeNode matchNode =
113           new DefaultMutableTreeNode(
114             matcher.group()
115               + " from: "
116               + matcher.start()
117               + " to: "
118               + matcher.end());
119 
120         for (int i = 0; i <= matcher.groupCount(); i++) {
121 
122           DefaultMutableTreeNode groupNode =
123             new DefaultMutableTreeNode(
124               matcher.group(i)
125                 + " from: "
126                 + matcher.start(i)
127                 + " to: "
128                 + matcher.end(i));
129 
130           matchNode.add(groupNode);
131         }
132 
133         root.add(matchNode);
134       }
135 
136     } catch (Exception ex) {
137       ex.printStackTrace();
138       result.append('\n');
139       result.append(ex.getLocalizedMessage());
140     }
141     outputArea.setText(result.toString());
142   }
143 
144   public static void main(String[] args) {
145     RegRex regRex = new RegRex();
146     regRex.pack();
147     regRex.show();
148   }
149 }