All pastes #967945 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-02 19:10 UTC
rendered paste body
import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import javax.swing.text.*;

public class RegexKing implements ActionListener, KeyListener{

public static void main(String[] args) {
	new RegexKing();
}

public RegexKing(){

	initGUI();
	setupGUI();
	displayFrame();

}

public void initGUI(){

	// init
	frame = new JFrame("Regex King");
	container = new JPanel();

	inputArea = new JTextArea();
	regexField =new JTextField();
	outputArea = new JTextArea();

	quickMatch = new JCheckBox("Attempt Match on Change");
	matchButton = new JButton("Match");

	inputScroll = new JScrollPane(inputArea);
	outputScroll = new JScrollPane(outputArea);

	// setup
	outputArea.setEditable(false);
	matchButton.addActionListener(this);
	regexField.addKeyListener(this);

	// key binding
	KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, Event.SHIFT_MASK, false);
	Action keyact = new AbstractAction(){
		public void actionPerformed(ActionEvent e){
		doMatch();
		}
	};

	container.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "DO_MATCH");
	container.getActionMap().put("DO_MATCH", keyact);

}

public void setupGUI(){

	gbl = new GridBagLayout();
	gbc = new GridBagConstraints();

  	container.setLayout(gbl);

	setInsets(2, 2, 2, 2);
 	gbcf = GridBagConstraints.BOTH;

  	setComp(inputScroll, 10, 10, 20, 1, 1, 1);
  	setComp(quickMatch, 10, 20, 1, 1, 1, .1);
	setComp(matchButton, 20, 20, 1, 1, 1, .1);
	setComp(regexField, 10, 30, 20, 1, 1, .1);
	setComp(outputScroll, 10, 40, 20, 1, 1, 1);

}

public void setComp(JComponent comp, int gx, int gy, int gw, int gh, double wx, double wy){

	gbc.gridx = gx;
	gbc.gridy = gy;
	gbc.gridwidth = gw;
	gbc.gridheight = gh;
	gbc.weightx = wx;
	gbc.weighty = wy;

	gbc.fill = gbcf;

	gbl.setConstraints(comp, gbc);
	container.add(comp);

}

public void setInsets(int top, int bottom, int left, int right){

  	gbc.insets.top = top;
  	gbc.insets.bottom = bottom;
  	gbc.insets.left = left;
  	gbc.insets.right = right;

}

public void displayFrame(){

	frame.setContentPane(container);
	frame.setSize(500, 500);
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);

}

public void keyPressed(KeyEvent e){
	if(quickMatch.isSelected() == true){
	doMatch();
	}
}

public void keyReleased(KeyEvent e){

}

public void keyTyped(KeyEvent e){

}

public void actionPerformed(ActionEvent evt) {

	if(evt.getSource() == matchButton){
	doMatch();
	}

}

public void doMatch(){

	outputArea.setText("\nAttempting Match...");

	inputArea.getHighlighter().removeAllHighlights();

	try{

	String inputText = inputArea.getText();
	Pattern pattern = Pattern.compile(regexField.getText());
	Matcher matcher = pattern.matcher(inputText);

	int matchCount = 0;

	while(matcher.find()){

	matchCount++;
	outputArea.append("\n\nMatch #" + matchCount);

	for(int i = 0; i < matcher.groupCount() + 1; i++){
	outputArea.append("\nGroup #" + i + ": " + matcher.group(i));
	outputArea.append(" , " + matcher.end(i));
	inputArea.getHighlighter().addHighlight(
		matcher.start(i), matcher.end(i), DefaultHighlighter.DefaultPainter);
	}

	}

	} catch(Exception exc){
	outputArea.append("\nEXCEPTION THROWN");
	//exc.printStackTrace();
	}

	outputArea.append("\n\nFinished.\n");

}

	GridBagLayout gbl;
	GridBagConstraints gbc;
	int gbcf;

	JFrame frame;
	JPanel container;

	JTextArea inputArea;
	JTextField regexField;
	JTextArea outputArea;

	JScrollPane inputScroll;
	JScrollPane outputScroll;

	JCheckBox quickMatch;
	JButton matchButton;

}