/* Copyright (c) 2006 L. Searchwell. All Rights Reserved. */ import java.awt.*; //import java.awt.Insets; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; //import javax.swing.text.*; import java.awt.BorderLayout; import java.awt.GridLayout; public class PalindromeGUI extends JFrame { private String tempStr = null, result_msg[]; private Palindrome palind; private JLabel message1, message2, message3; private JButton clear, check, quit; private JCheckBox punctuation_onoff; private JTextArea input_textspace, result_textspace; //private BorderLayout border_format; // private static final int BTN_ROWS = 1, BTN_COLS = /*5*/4, TXT_ROWS = 2, TXT_COLS = 1; private GridLayout grid_format; private JPanel message_panel, button_panel_South, text_panel; PalindromeGUI() { this(";-)"); } PalindromeGUI(String windowlabel) { super("Lymon's Palindromes " + (windowlabel != null ? windowlabel : ":-)")); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); result_msg = new String[3]; result_msg[0] = ""; result_msg[1] = "Yes, that is a palindrome!"; result_msg[2] = "That is a not palindrome."; palind = new Palindrome(); //Visible Elements: message1 = new JLabel("\tEnter your text below:"); message2 = new JLabel("Length: " + palind.getInputString().length()); message3 = new JLabel(result_msg[0]); input_textspace = new JTextArea(); input_textspace.getDocument().addDocumentListener( //new DocumentListener() {... new myDocListener() ); (result_textspace = new JTextArea()).setEditable(false); result_textspace.setBackground(new Color(204, 204, 255)); (clear = new JButton("Clear")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { input_textspace.setText(""); result_textspace.setText(""); message3.setText(result_msg[0]); } }); clear.setEnabled(false); clear.setForeground(Color.GREEN.darker()); (check = new JButton("Check!")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { String text = null; try { if((text = input_textspace.getText()) != null) { if(palind.check(text)) { result_textspace.setText(/*"That is a palindrome:\n--\n" +*/ palind.getDepuntuatedString()); message3.setText(result_msg[1]); return; } } result_textspace.setText(/*"That is not a palindrome:\n--\n" + palind.getInputString() + "\n\n" +*/ palind.getDepuntuatedString()); message3.setText(result_msg[2]); } catch(NullPointerException npe) { return; } start(text); } }); check.setEnabled(false); check.setForeground(Color.RED); (quit = new JButton("Quit.")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); quit.setMargin(new Insets(0, 40, 0, 0)); (punctuation_onoff = new JCheckBox("Ignore Punctuation", true)).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { palind.IGNORE_PUNCTUATION = punctuation_onoff.isSelected(); } }); //Formatting Elements: //Layout Elements: getContentPane().setLayout((/*border_format =*/ new BorderLayout())); getContentPane().add((message_panel = new JPanel()), BorderLayout.NORTH); getContentPane().add((button_panel_South = new JPanel()), BorderLayout.SOUTH); getContentPane().add((text_panel = new JPanel()), BorderLayout.CENTER); grid_format = new GridLayout(BTN_ROWS, BTN_COLS); button_panel_South.setLayout((grid_format /*= new GridLayout(BTN_ROWS, BTN_COLS)*/)); button_panel_South.add(punctuation_onoff); button_panel_South.add(clear); button_panel_South.add(check); button_panel_South.add(quit); message_panel.setLayout(grid_format); message_panel.add(message1); message3.setBackground(Color.GREEN); message3.setForeground(Color.RED); message_panel.add(message3); message_panel.add(message2); input_textspace.setMargin(new Insets(8, 8, 8, 8)); input_textspace.setLineWrap(true); result_textspace.setMargin(new Insets(8, 8, 8, 8)); result_textspace.setLineWrap(true); // getContentPane().add((text_panel = new JPanel()), BorderLayout.CENTER); text_panel.setLayout((new GridLayout(TXT_ROWS, TXT_COLS))); text_panel.add(new JScrollPane(input_textspace)); text_panel.add(new JScrollPane(result_textspace)); setSize(640, 320); setVisible(true); } public void setTestString() { ; } public void getTestString() { ; } public void start(String s) { if(palind.check(s)) result_textspace.setText(palind.getDepuntuatedString()); else ; } private class myDocListener implements DocumentListener { //new DocumentListener() { public void changedUpdate(DocumentEvent de) { message3.setText(result_msg[0]); if((tempStr = input_textspace.getText()) != null) { //System.out.println("changedUpdate!: " + tempStr.length() + " : " + input_textspace.getText()); if(tempStr.length() <= 0) { clear.setEnabled(false); check.setEnabled(false); } else { clear.setEnabled(true); check.setEnabled(true); } } } public void insertUpdate(DocumentEvent de) { message3.setText(result_msg[0]); if((tempStr = input_textspace.getText()) != null) { message2.setText("Length: " + input_textspace.getText().length()); //System.out.println("insertUpdate!: " + tempStr.length() + " : " + input_textspace.getText()); if(tempStr.length() <= 0) { clear.setEnabled(false); check.setEnabled(false); } else { clear.setEnabled(true); check.setEnabled(true); } } } public void removeUpdate(DocumentEvent de) { message3.setText(result_msg[0]); if((tempStr = input_textspace.getText()) != null) { message2.setText("Length: " + input_textspace.getText().length()); //System.out.println("removeUpdate!: " + tempStr.length() + " : " + input_textspace.getText()); if(tempStr.length() <= 0) { clear.setEnabled(false); check.setEnabled(false); } else { clear.setEnabled(true); check.setEnabled(true); } } } //} } // private class myButtonEventListener implements ActionListener // { // public void actionPerformed(ActionEvent event) { // String text = null; // if(event.getSource() == clear) { // input_textspace.setText(""); // result_textspace.setText(""); // } else if(event.getSource() == check) { // try { // if((text = input_textspace.getText()) != null) { // if(palind.check(text)) { // result_textspace.setText("That is a palindrome:\n--\n" + // palind.getDepuntuatedString()); // return; // } // } // result_textspace.setText("That is not a palindrome:\n--\n" + // palind.getInputString() + "\n\n" + // palind.getDepuntuatedString()); // } catch(NullPointerException npe) { // return; // } // start(text); // } else if(event.getSource() == quit) { // System.exit(0); // } // } // } }