<commit_before>/*
 * put your module comment here
 * formatted with JxBeauty (c) <EMAIL>
 */


package  com.swabunga.spell.swing;

import com.swabunga.spell.engine.Word;
import  com.swabunga.spell.event.*;
import  javax.swing.*;
import  javax.swing.text.*;
import  javax.swing.event.*;
import  java.io.File;
import  java.awt.*;
import  java.awt.event.*;
import java.util.*;


/** Implementation of a spell check form.
 *  <p>This needs to layed out correctly but for the most part it works.</p>
 *
 * @author <NAME> (<EMAIL>)
 */
public class JSpellForm extends JPanel
    implements ActionListener, ListSelectionListener {
  /** The Ignore button click action command*/
  public static final String IGNORE_CMD = "IGNORE";
  /** The Ignore All button click action command*/
  public static final String IGNOREALL_CMD = "IGNOREALL";
  /** The Add button click action command*/
  public static final String ADD_CMD = "ADD";
  /** The Replace button click action command*/
  public static final String REPLACE_CMD = "REPLACE";
  /** The Replace All button click action command*/
  public static final String REPLACEALL_CMD = "REPLACEALL";
  /** The Cancel button click action command*/
  public static final String CANCEL_CMD = "CANCEL";
  /** The resource for the Suggestions label*/
  private static final String SUGGESTIONS_RES = "SUGGESTIONS";
  private static final String INVALIDWORD_RES = "INVALIDWORD";

  /* Accessible GUI Components */
  protected JList suggestList;
  protected JTextArea checkText;
  /* The current spell check event */
  protected SpellCheckEvent spellEvent;
  /** The listener list (holds actionlisteners) */
  protected EventListenerList listenerList = new EventListenerList();
  protected ResourceBundle messages;

  /** Panel constructor */
  public JSpellForm () {
    messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
    initialiseGUI();
  }

  /** Helper method to create a JButton with a command, a text label and a listener*/
  private static final JButton createButton (String command, String text, ActionListener listener) {
    JButton btn = new JButton(text);
    btn.setActionCommand(command);
    btn.addActionListener(listener);
    return  btn;
  }

  /** Creates the buttons on the left hand side of the panel*/
  protected JPanel makeEastPanel () {
    JPanel jPanel1 = new JPanel();
    BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);
    jPanel1.setLayout(layout);
    
    JButton ignoreBtn = createButton(IGNORE_CMD, messages.getString(IGNORE_CMD), this);
    ignoreBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE )); 
    jPanel1.add(ignoreBtn);
    
    JButton ignoreAllBtn = createButton(IGNOREALL_CMD, messages.getString(IGNOREALL_CMD), this);
    ignoreAllBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(ignoreAllBtn);
    
    JButton addBtn = createButton(ADD_CMD, messages.getString(ADD_CMD), this);
    addBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(addBtn);
    
    JButton changeBtn = createButton(REPLACE_CMD, messages.getString(REPLACE_CMD), this);
    changeBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(changeBtn);
    
    JButton changeAllBtn = createButton(REPLACEALL_CMD, messages.getString(REPLACEALL_CMD), this);
    changeAllBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(changeAllBtn);
    
    JButton cancelBtn = createButton(CANCEL_CMD, messages.getString(CANCEL_CMD), this);
    cancelBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(cancelBtn);
   
    return  jPanel1;
  }

  protected JPanel makeCentrePanel () {
    JPanel jPanel2 = new JPanel();
    jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
    JLabel lbl1 = new JLabel(messages.getString(INVALIDWORD_RES));
    jPanel2.add(lbl1);
    checkText = new JTextArea();
    jPanel2.add(new JScrollPane(checkText));
    JLabel lbl2 = new JLabel(messages.getString(SUGGESTIONS_RES));
    jPanel2.add(lbl2);
    suggestList = new JList();
    suggestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jPanel2.add(new JScrollPane(suggestList));
    suggestList.addListSelectionListener(this);
    return  jPanel2;
  }

  /** Called by the constructor to initialise the GUI*/
  protected void initialiseGUI () {
    setLayout(new BorderLayout());
    this.add(makeEastPanel(), BorderLayout.EAST);
    this.add(makeCentrePanel(), BorderLayout.CENTER);
  }

  /** Register an action listener */
  public void addActionListener (ActionListener l) {
    listenerList.add(ActionListener.class, l);
  }

  /** Deregister an action listener*/
  public void removeActionListener (ActionListener l) {
    listenerList.remove(ActionListener.class, l);
  }

  protected void fireActionEvent (ActionEvent e) {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length - 2; i >= 0; i -= 2) {
      if (listeners[i] == ActionListener.class) {
        ((ActionListener)listeners[i + 1]).actionPerformed(e);
      }
    }
  }

  /** Sets the current spell check event that is being shown to the user*/
  public void setSpellEvent (SpellCheckEvent event) {
    spellEvent = event;
    DefaultListModel m = new DefaultListModel();
    java.util.List suggestions = event.getSuggestions();
    for (int i = 0; i < suggestions.size(); i++) {
      m.addElement(suggestions.get(i));
    }
    suggestList.setModel(m);
    if (m.size()>0) {
    	suggestList.setSelectedIndex(0);
    	checkText.setText(((Word)m.get(0)).getWord());
    } else {
    	checkText.setText(event.getInvalidWord());
    }
  }

  /** Fired when a value in the list is selected*/
  public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
      Object selectedValue = suggestList.getSelectedValue();
      if (selectedValue != null)
        checkText.setText(selectedValue.toString());<commit_msg>Added a JLabel to show the misspelled word, as not all apps will be able to highlight the word in the text.<commit_after>/*
 * put your module comment here
 * formatted with JxBeauty (c) <EMAIL>
 */


package  com.swabunga.spell.swing;

import com.swabunga.spell.engine.Word;
import  com.swabunga.spell.event.*;
import  javax.swing.*;
import  javax.swing.text.*;
import  javax.swing.event.*;
import  java.io.File;
import  java.awt.*;
import  java.awt.event.*;
import java.util.*;


/** Implementation of a spell check form.
 *  <p>This needs to layed out correctly but for the most part it works.</p>
 *
 * @author <NAME> (<EMAIL>)
 */
public class JSpellForm extends JPanel
    implements ActionListener, ListSelectionListener {
  /** The Ignore button click action command*/
  public static final String IGNORE_CMD = "IGNORE";
  /** The Ignore All button click action command*/
  public static final String IGNOREALL_CMD = "IGNOREALL";
  /** The Add button click action command*/
  public static final String ADD_CMD = "ADD";
  /** The Replace button click action command*/
  public static final String REPLACE_CMD = "REPLACE";
  /** The Replace All button click action command*/
  public static final String REPLACEALL_CMD = "REPLACEALL";
  /** The Cancel button click action command*/
  public static final String CANCEL_CMD = "CANCEL";
  /** The resource for the Suggestions label*/
  private static final String SUGGESTIONS_RES = "SUGGESTIONS";
  private static final String INVALIDWORD_RES = "INVALIDWORD";
  private JLabel wrongWordLabel;

  /* Accessible GUI Components */
  protected JList suggestList;
  protected JTextArea checkText;
  /* The current spell check event */
  protected SpellCheckEvent spellEvent;
  /** The listener list (holds actionlisteners) */
  protected EventListenerList listenerList = new EventListenerList();
  protected ResourceBundle messages;

  /** Panel constructor */
  public JSpellForm () {
    messages = ResourceBundle.getBundle("com.swabunga.spell.swing.messages", Locale.getDefault());
    initialiseGUI();
  }

  /** Helper method to create a JButton with a command, a text label and a listener*/
  private static final JButton createButton (String command, String text, ActionListener listener) {
    JButton btn = new JButton(text);
    btn.setActionCommand(command);
    btn.addActionListener(listener);
    return  btn;
  }

  /** Creates the buttons on the left hand side of the panel*/
  protected JPanel makeEastPanel () {
    JPanel jPanel1 = new JPanel();
    BoxLayout layout = new BoxLayout(jPanel1, BoxLayout.Y_AXIS);
    jPanel1.setLayout(layout);
    
    JButton ignoreBtn = createButton(IGNORE_CMD, messages.getString(IGNORE_CMD), this);
    ignoreBtn.setMaximumSize( new Dimension(Short.MAX_VALUE, Short.MAX_VALUE )); 
    jPanel1.add(ignoreBtn);
    
    JButton ignoreAllBtn = createButton(IGNOREALL_CMD, messages.getString(IGNOREALL_CMD), this);
    ignoreAllBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(ignoreAllBtn);
    
    JButton addBtn = createButton(ADD_CMD, messages.getString(ADD_CMD), this);
    addBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(addBtn);
    
    JButton changeBtn = createButton(REPLACE_CMD, messages.getString(REPLACE_CMD), this);
    changeBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(changeBtn);
    
    JButton changeAllBtn = createButton(REPLACEALL_CMD, messages.getString(REPLACEALL_CMD), this);
    changeAllBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(changeAllBtn);
    
    JButton cancelBtn = createButton(CANCEL_CMD, messages.getString(CANCEL_CMD), this);
    cancelBtn.setMaximumSize(  new Dimension(Short.MAX_VALUE, Short.MAX_VALUE ));
    jPanel1.add(cancelBtn);
   
    return  jPanel1;
  }

  protected JPanel makeCentrePanel () {
    JPanel jPanel2 = new JPanel();
    jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.Y_AXIS));
    JPanel jPanel3 = new JPanel();
    JLabel lbl1 = new JLabel(messages.getString(INVALIDWORD_RES));
    wrongWordLabel = new JLabel("");
    wrongWordLabel.setForeground(Color.RED);
    jPanel3.add(lbl1);
    jPanel3.add(wrongWordLabel);
    jPanel2.add(jPanel3);
    checkText = new JTextArea();
    jPanel2.add(new JScrollPane(checkText));
    JLabel lbl2 = new JLabel(messages.getString(SUGGESTIONS_RES));
    jPanel2.add(lbl2);
    suggestList = new JList();
    suggestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jPanel2.add(new JScrollPane(suggestList));
    suggestList.addListSelectionListener(this);
    return  jPanel2;
  }

  /** Called by the constructor to initialise the GUI*/
  protected void initialiseGUI () {
    setLayout(new BorderLayout());
    this.add(makeEastPanel(), BorderLayout.EAST);
    this.add(makeCentrePanel(), BorderLayout.CENTER);
  }

  /** Register an action listener */
  public void addActionListener (ActionListener l) {
    listenerList.add(ActionListener.class, l);
  }

  /** Deregister an action listener*/
  public void removeActionListener (ActionListener l) {
    listenerList.remove(ActionListener.class, l);
  }

  protected void fireActionEvent (ActionEvent e) {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length - 2; i >= 0; i -= 2) {
      if (listeners[i] == ActionListener.class) {
        ((ActionListener)listeners[i + 1]).actionPerformed(e);
      }
    }
  }

  /** Sets the current spell check event that is being shown to the user*/
  public void setSpellEvent (SpellCheckEvent event) {
    spellEvent = event;
    DefaultListModel m = new DefaultListModel();
    java.util.List suggestions = event.getSuggestions();
    for (int i = 0; i < suggestions.size(); i++) {
      m.addElement(suggestions.get(i));
    }
    suggestList.setModel(m);
    wrongWordLabel.setText(event.getInvalidWord());
   if (m.size()>0) {
    	suggestList.setSelectedIndex(0);
    	checkText.setText(((Word)m.get(0)).getWord());
    } else {
    	checkText.setText(event.getInvalidWord());
    }
  }

  /** Fired when a value in the list is selected*/
  public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
      Object selectedValue = suggestList.getSelectedValue();
      if (selectedValue != null)
        checkText.setText(selectedValue.toString());
{'fraction_non_alphanumeric': 0.10806780725160885, 'fraction_numerical': 0.005022759378433527, 'mean_word_length': 3.954510108864697, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404151', 'n_tokens_mistral': 3922, 'n_tokens_neox': 3839, 'n_words': 1014}