001: /*
002: * gnu/regexp/util/REApplet.java
003: * Copyright (C) 1998 Wes Biggs
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published
007: * by the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018: */
019:
020: package gnu.regexp.util;
021:
022: import java.applet.*;
023: import java.awt.*;
024: import gnu.regexp.*;
025:
026: /**
027: * This is a simple applet to demonstrate the capabilities of gnu.regexp.
028: * To run it, use appletviewer on the reapplet.html file included in the
029: * documentation directory.
030: *
031: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
032: * @version 1.02
033: */
034: public class REApplet extends Applet {
035: private Label l1, l2, l3, l4;
036: private Button b;
037: private TextField tf;
038: private TextArea input, output;
039: private Checkbox insens;
040: private Choice syntax;
041: private static String[] names = new String[] { "awk", "ed",
042: "egrep", "emacs", "grep", "POSIX awk", "POSIX basic",
043: "POSIX egrep", "POSIX extended", "POSIX minimal basic",
044: "POSIX minimal extended", "sed", "perl 4",
045: "perl 4 (singe line)", "perl 5", "perl 5 (single line)" };
046:
047: private static RESyntax[] values = new RESyntax[] {
048: RESyntax.RE_SYNTAX_AWK, RESyntax.RE_SYNTAX_ED,
049: RESyntax.RE_SYNTAX_EGREP, RESyntax.RE_SYNTAX_EMACS,
050: RESyntax.RE_SYNTAX_GREP, RESyntax.RE_SYNTAX_POSIX_AWK,
051: RESyntax.RE_SYNTAX_POSIX_BASIC,
052: RESyntax.RE_SYNTAX_POSIX_EGREP,
053: RESyntax.RE_SYNTAX_POSIX_EXTENDED,
054: RESyntax.RE_SYNTAX_POSIX_MINIMAL_BASIC,
055: RESyntax.RE_SYNTAX_POSIX_MINIMAL_EXTENDED,
056: RESyntax.RE_SYNTAX_SED, RESyntax.RE_SYNTAX_PERL4,
057: RESyntax.RE_SYNTAX_PERL4_S, RESyntax.RE_SYNTAX_PERL5,
058: RESyntax.RE_SYNTAX_PERL5_S };
059:
060: /** Creates an REApplet. */
061: public REApplet() {
062: super ();
063: }
064:
065: /** Initializes the applet and constructs GUI elements. */
066: public void init() {
067: // test run RE stuff to cache gnu.regexp.* classes.
068: try {
069: RE x = new RE("^.*(w[x])\1$");
070: REMatchEnumeration xx = x.getMatchEnumeration("wxwx");
071: while (xx.hasMoreMatches())
072: xx.nextMatch().toString();
073: } catch (REException arg) {
074: }
075:
076: setBackground(Color.lightGray);
077:
078: /*
079: Layout looks like this:
080:
081: [0,0:[0,0: Regular Expression] [1,0: Textbox]
082: [0,1: Expression Syntax] [1,1: [0,0: Choice] [1,0: Checkbox]]
083: [1,2: Button]]
084: [0,1: Input Text] [1,1: Match]
085: [0,2: Textarea] [1,2: Textarea]
086: */
087:
088: GridBagLayout gbag = new GridBagLayout();
089: setLayout(gbag);
090: GridBagConstraints c = new GridBagConstraints();
091: Panel p = new Panel();
092: GridBagLayout gbag2 = new GridBagLayout();
093: p.setLayout(gbag2);
094:
095: c.anchor = GridBagConstraints.WEST;
096: c.weightx = 1.0;
097:
098: // [0,0: Regular Expression]
099: c.gridx = 0;
100: c.gridy = 0;
101: l1 = new Label("Regular Expression");
102: gbag2.setConstraints(l1, c);
103: p.add(l1);
104:
105: // [1,0: TextField]
106: c.gridx = 1;
107: tf = new TextField(getParameter("regexp"), 30);
108: gbag2.setConstraints(tf, c);
109: p.add(tf);
110:
111: // [0,1: Expression Syntax]
112: c.gridx = 0;
113: c.gridy = 1;
114: l4 = new Label("Expression Syntax");
115: gbag2.setConstraints(l4, c);
116: p.add(l4);
117:
118: // [1,1: subpanel]
119: Panel p2 = new Panel();
120: GridBagLayout gbag3 = new GridBagLayout();
121: p2.setLayout(gbag3);
122: c.gridx = 1;
123: gbag2.setConstraints(p2, c);
124: p.add(p2);
125:
126: // Subpanel [0,0: Choice]
127: c.gridx = 0;
128: c.gridy = 0;
129: syntax = new Choice();
130: for (int i = 0; i < names.length; i++)
131: syntax.addItem(names[i]);
132: String zz = getParameter("syntax");
133: if (zz != null) {
134: try {
135: syntax.select(getParameter("syntax"));
136: } catch (IllegalArgumentException e) {
137: }
138: }
139:
140: gbag3.setConstraints(syntax, c);
141: p2.add(syntax);
142:
143: c.gridx = 1;
144: insens = new Checkbox("Ignore case", false);
145: gbag3.setConstraints(insens, c);
146: p2.add(insens);
147:
148: // Next Row
149: c.gridx = 1;
150: c.gridy = 2;
151: b = new Button("Match");
152: gbag2.setConstraints(b, c);
153: p.add(b);
154:
155: // Add the entire upper panel.
156: c.gridwidth = 2;
157: c.gridheight = 1;
158: c.gridx = 0;
159: c.gridy = 0;
160: c.anchor = GridBagConstraints.CENTER;
161: gbag.setConstraints(p, c);
162: add(p);
163:
164: c.gridwidth = 1;
165: c.gridheight = 1;
166:
167: // Main: [0,1]:
168: l2 = new Label("Input Text");
169: c.gridwidth = 1;
170: c.gridx = 0;
171: c.gridy = 1;
172: gbag.setConstraints(l2, c);
173: add(l2);
174:
175: l3 = new Label("Matches Found");
176: c.gridx = 1;
177: gbag.setConstraints(l3, c);
178: add(l3);
179:
180: input = new TextArea(getParameter("input"), 5, 30);
181: c.gridx = 0;
182: c.gridy = 2;
183: gbag.setConstraints(input, c);
184: add(input);
185:
186: c.gridx = 1;
187: output = new TextArea(5, 30);
188: output.setEditable(false);
189: c.gridwidth = GridBagConstraints.REMAINDER;
190: gbag.setConstraints(output, c);
191: add(output);
192: }
193:
194: /**
195: * Handles events in the applet. Returns true if the indicated event
196: * was handled, false for all other events.
197: */
198: public boolean action(Event e, Object arg) {
199: Object target = e.target;
200:
201: if (target == b) { // match
202: try {
203: String expr = tf.getText();
204: RE reg = null;
205: RESyntax res = values[syntax.getSelectedIndex()];
206: reg = new RE(expr,
207: insens.getState() ? RE.REG_ICASE : 0, res);
208: REMatchEnumeration en = reg.getMatchEnumeration(input
209: .getText());
210: StringBuffer sb = new StringBuffer();
211: int matchNum = 0;
212: while (en.hasMoreMatches()) {
213: sb.append(String.valueOf(++matchNum));
214: sb.append(". ");
215: sb.append(en.nextMatch().toString());
216: sb.append('\n');
217: }
218: output.setText(sb.toString());
219: } catch (REException err) {
220: output.setText(err.getMessage());
221: }
222: return true;
223: } else
224: return false;
225: }
226: }
|