Package views

Source Code of views.RegExTextPanel

package views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;

import model.Model;
import application.Application;

public class RegExTextPanel extends JPanel implements PropertyChangeListener {

  // Reference to the model
  private Model model;
 
  private JButton jButtonLoad = null;
  private JButton jButtonSave = null;
  private JButton jButtonApplyPartial = null;
  private JButton jButtonApply = null;
  private JPanel jPanelButtons = null;
  private JLabel jLabelFile = null;
  private JLabel jLabelRegEx = null;
  private JTextArea jRegExTextArea = null;
  private JScrollPane jScrollPaneText = null;
  private JPanel jPanelValidity = null;
  private JLabel jLabelErrorText = null;
  private JLabel jLabelValidIcon = null;
  private ImageIcon unValidIcon = null;
  private DefaultHighlighter highlighter = null;
  private DefaultHighlightPainter highlightPainter = null;
 
  public RegExTextPanel(Model model) {
    super();
    this.model = model;
    model.addPropertyChangeListener(this);
    initialize();
  }

  private void initialize() {
        this.setLayout(new BorderLayout());
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        this.setSize(new java.awt.Dimension(422,218));
        this.setBorder(BorderFactory.createTitledBorder(Application.messages.getString("TITLE_REGULAR_EXPRESSION")));
        this.add(getJPanelButtons(), java.awt.BorderLayout.NORTH);
        this.add(getJScrollPaneText(), java.awt.BorderLayout.CENTER);
        this.add(getJPanelValidity(), java.awt.BorderLayout.SOUTH);
  }

  /**
   * This method initializes jButtonLoad 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getjButtonLoad() {
    if (jButtonLoad == null) {
      jButtonLoad = new JButton();
      jButtonLoad.setText(Application.messages.getString("BUTTON_LOAD"));
      jButtonLoad.setBounds(new java.awt.Rectangle(10,20,90,25));
      jButtonLoad.setName("jButtonLoad");
      jButtonLoad.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          openRegExFile();
        }
      });
    }
    return jButtonLoad;
  }

  /**
   * This method initializes jButtonSave
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonSave() {
    if (jButtonSave == null) {
      jButtonSave = new JButton();
      jButtonSave.setText(Application.messages.getString("BUTTON_SAVE"));
      jButtonSave.setBounds(new java.awt.Rectangle(105,20,90,25));
      jButtonSave.setName("jButtonSave");
      jButtonSave.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          saveRegExFile();
        }
      });
    }
    return jButtonSave;
  }

  /**
   * This method initializes jButtonApplyPartial 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonApplyPartial() {
    if (jButtonApplyPartial == null) {
      jButtonApplyPartial = new JButton();
      jButtonApplyPartial.setText(Application.messages.getString("BUTTON_APPLY_PARTIAL"));
      jButtonApplyPartial.setBounds(new java.awt.Rectangle(225,20,100,25));
      jButtonApplyPartial.setName("jButtonApplyPartial");
      jButtonApplyPartial.setEnabled(false);
      jButtonApplyPartial.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          applyPartialRegEx();
        }
      });
    }
    return jButtonApplyPartial;
  }
 
  /**
   * This method initializes jButtonApply 
   *  
   * @return javax.swing.JButton 
   */
  private JButton getJButtonApply() {
    if (jButtonApply == null) {
      jButtonApply = new JButton();
      jButtonApply.setText(Application.messages.getString("BUTTON_APPLY"));
      jButtonApply.setBounds(new java.awt.Rectangle(330,20,100,25));
      jButtonApply.setName("jButtonApply");
      jButtonApply.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          applyRegEx();
        }
      });
    }
    return jButtonApply;
  }

  /**
   * This method initializes jPanelButtons 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelButtons() {
    if (jPanelButtons == null) {
      jLabelFile = new JLabel();
      jLabelFile.setBounds(new java.awt.Rectangle(10,0,175,15));
      jLabelFile.setText(Application.messages.getString("LABEL_FILE"));
      jLabelRegEx = new JLabel();
      jLabelRegEx.setBounds(new java.awt.Rectangle(225,0,205,15));
      jLabelRegEx.setText(Application.messages.getString("LABEL_APPLY_REGEX"));
      jPanelButtons = new JPanel();
      jPanelButtons.setLayout(null);
      jPanelButtons.setPreferredSize(new java.awt.Dimension(450,50));
      jPanelButtons.add(getjButtonLoad(), null);
      jPanelButtons.add(getJButtonSave(), null);
      jPanelButtons.add(getJButtonApplyPartial(), null);
      jPanelButtons.add(getJButtonApply(), null);
      jPanelButtons.add(jLabelFile, null);
      jPanelButtons.add(jLabelRegEx, null);
    }
    return jPanelButtons;
  }

  /**
   * This method initializes jRegExTextArea 
   *  
   * @return javax.swing.JTextArea 
   */
  private JTextArea getJRegExTextArea() {
    if (jRegExTextArea == null) {
      jRegExTextArea = new JTextArea();
      jRegExTextArea.setLineWrap(true);
      jRegExTextArea.setRows(3);
      jRegExTextArea.setBackground(new Color(199, 255, 191));
      highlightPainter = new DefaultHighlightPainter(new Color(255, 100, 100));
      highlighter = new DefaultHighlighter();
      jRegExTextArea.setHighlighter(highlighter);
      jRegExTextArea.addKeyListener(new java.awt.event.KeyAdapter() {  
        public void keyReleased(java.awt.event.KeyEvent e) {
          if (!e.isControlDown())
            // Warn the object when a key is being typed
            onRegExChange();
        }
      });
      jRegExTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseReleased(java.awt.event.MouseEvent e) {
          toogleApplyPartialButton();
        }
      });
      jRegExTextArea.addCaretListener(new javax.swing.event.CaretListener() {
        public void caretUpdate(javax.swing.event.CaretEvent e) {
          model.setCaretPosition(e.getMark());
        }
      });
    }
    return jRegExTextArea;
  }

  /**
   * This method initializes jPanelValidity 
   *  
   * @return javax.swing.JPanel 
   */
  private JPanel getJPanelValidity() {
    if (jPanelValidity == null) {
      unValidIcon = new ImageIcon(this.getClass().getClassLoader().getResource("images/unvalid.png"));
      jLabelValidIcon = new JLabel();
      jLabelValidIcon.setBounds(new java.awt.Rectangle(3,4,16,16));
      jLabelValidIcon.setPreferredSize(new java.awt.Dimension(16,16));
      jLabelValidIcon.setText("");
     
      jLabelErrorText = new JLabel();
      jLabelErrorText.setText("");
      jLabelErrorText.setBounds(new java.awt.Rectangle(23,4,396,16));
     
      jPanelValidity = new JPanel();
      jPanelValidity.setLayout(null);
      jPanelValidity.setSize(new java.awt.Dimension(396,113));
      jPanelValidity.setPreferredSize(new java.awt.Dimension(300,20));
      jPanelValidity.add(jLabelValidIcon, null);
      jPanelValidity.add(jLabelErrorText, null);
    }
    return jPanelValidity;
  }

  /**
   * This method initializes jScrollPaneText 
   *  
   * @return javax.swing.JScrollPane 
   */
  private JScrollPane getJScrollPaneText() {
    if (jScrollPaneText == null) {
      jScrollPaneText = new JScrollPane();
      jScrollPaneText.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      jScrollPaneText.setViewportView(getJRegExTextArea());
    }
    return jScrollPaneText;
  }

  /**
   * openRegExFile loads a new file
   */
  private void openRegExFile() {
    model.openRegExFile();
  }
 
  /**
   * openRegExFile saves a file
   */
  private void saveRegExFile() {
    model.saveRegExFile();
  }
 
  /**
   * onRegExChange set the new value of the regex from the one in the textfield
   */
  private void onRegExChange() {
    model.setRegEx(jRegExTextArea.getText());
  }
 
  /**
   * applyPartialRegEx 
   */
  private void applyPartialRegEx() {
    model.applypartialRegEx();
  }
 
  /**
   * applyRegEx
   */
  private void applyRegEx() {
    model.applyRegEx();
  }
 
  /**
   * toogleApplyPartialButton
   */
  private void toogleApplyPartialButton() {
    // here might be the applyPartialRegEx method to dynamically apply the selection
    //model.applypartialRegEx(jRegExTextArea.getSelectionStart(), jRegExTextArea.getSelectionEnd());
    model.isValid(jRegExTextArea.getSelectionStart(), jRegExTextArea.getSelectionEnd());
  }
 
  /**
   * propertyChange verifies whehther the change occured on the "valid" property
   * and applies the changes if yes
   * It does receive and update the new regular expression in case a file's loaded
   * @param java.beans.PropertyChangeEvent
   */
  public void propertyChange(PropertyChangeEvent arg0) {
    if (arg0.getPropertyName() == "valid") {
      PatternSyntaxException newValue = (PatternSyntaxException)arg0.getNewValue();
      if(newValue == null//regex is valid
      {
        jButtonApply.setEnabled(true);
        jRegExTextArea.setBackground(new Color(199, 255, 191));
        jRegExTextArea.setToolTipText(null);
        jLabelValidIcon.setIcon(null);
        jLabelErrorText.setText("");
        highlighter.removeAllHighlights();
      }
      else      //regex isn't valid
      {
        jButtonApply.setEnabled(false);
        jRegExTextArea.setBackground(new Color(255, 180, 190));
        jLabelValidIcon.setIcon(unValidIcon);
        jRegExTextArea.setToolTipText(convertStringToHtml(newValue.getMessage()));
        jLabelErrorText.setText(newValue.getDescription());
        highlighter.removeAllHighlights();
        try
        {
          highlighter.addHighlight(newValue.getIndex(),newValue.getIndex()+1, highlightPainter);
        }
        catch(Exception exception)
        {
       
        }
      }
    }
   
    if (arg0.getPropertyName() == "subValid") {
      if (arg0.getNewValue().equals(true))
        jButtonApplyPartial.setEnabled(true);
      else
        jButtonApplyPartial.setEnabled(false);
    }
   
    if (arg0.getPropertyName() == "regEx") {
      if(((String)arg0.getNewValue()).compareTo(jRegExTextArea.getText()) != 0)
        jRegExTextArea.setText((String)arg0.getNewValue());
    }
    if (arg0.getPropertyName() == "caretPosition") {
      jRegExTextArea.setCaretPosition((Integer)arg0.getNewValue());
    }
  }
 
  public static String convertStringToHtml(String toConvert)
  {
    String result;
    Pattern p = Pattern.compile("\n");
    Matcher m = p.matcher(toConvert);
    result = m.replaceAll("<br>");
    result = "<html>" + result +"</html>";
    return result;
  }
}
TOP

Related Classes of views.RegExTextPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.