1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }