import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MainGUI extends SimpleFrame{
private JTextField field1= new JTextField(20);
private JTextField field2= new JTextField(20);
private JLabel similarity1 = new JLabel("Similarity1");
private JLabel time1 = new JLabel("Time1");
public MainGUI(){
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setBackground(Color.LIGHT_GRAY);
//Buttons, fields, labels START
c.anchor = GridBagConstraints.FIRST_LINE_START; //bottom of space
c.gridx = 0; //Moves via x in colums
c.gridwidth = 0; //2 columns wide
c.gridy = 0; //moves via y in rows
JButton text1Button = new JButton("Text 1: ");
c.insets = new Insets(20,35,0,0); //moves the starting point from (top, left, bottom, right)
this.add(text1Button, c);
JButton text2Button = new JButton("Text 2: ");
c.insets = new Insets(55, 35,0, 0);
this.add(text2Button, c);
field1.setPreferredSize(new Dimension(200,25));
field1.setBackground(Color.LIGHT_GRAY);
c.insets = new Insets(20,120,0,0);
this.add(field1, c);
field2.setPreferredSize(new Dimension(200,25));
field2.setBackground(Color.LIGHT_GRAY);
c.insets = new Insets(55,120,0,0);
this.add(field2, c);
JLabel label1 = new JLabel(" Similarity ");
label1.setFont(new Font("Monospaced",Font.BOLD,25));
c.insets = new Insets(125, 200,0, 0);
this.add(label1, c);
JLabel label2 = new JLabel(" Time ");
label2.setFont(new Font("Monospaced",Font.BOLD,25));
c.insets = new Insets(125, 400,0, 0);
this.add(label2, c);
c.anchor = GridBagConstraints.LINE_START;
JButton algo1Button = new JButton(" Algorithm1 ");
algo1Button.setPreferredSize(new Dimension(150,40));
c.insets = new Insets(25, -100,0, 0);
this.add(algo1Button, c);
c.anchor = GridBagConstraints.FIRST_LINE_START;
similarity1.setFont(new Font("Monospaced",Font.PLAIN,15));
c.insets = new Insets(180, 250,0, 0);
this.add(similarity1, c);
time1.setFont(new Font("Monospaced",Font.PLAIN,15));
c.insets = new Insets(180, 425,0, 0);
this.add(time1, c);
//Buttons, fields, labels END
//Menubar and menus START
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu(" FILE ");
menuBar.add(fileMenu);
JMenuItem newItem = new JMenuItem("New");
fileMenu.add(newItem);
fileMenu.addSeparator();
fileMenu.addSeparator();
JMenuItem closeItem = new JMenuItem("Close");
fileMenu.add(closeItem);
//Menubar and menus END
//Menulisteners
MenuListener MenuLi = new MenuListener(this);
newItem.addActionListener(MenuLi);
closeItem.addActionListener(MenuLi);
//ButtonListeners
ButtonListener ButLi = new ButtonListener(this);
text1Button.addActionListener(ButLi);
text2Button.addActionListener(ButLi);
algo1Button.addActionListener(ButLi);
}
public void initialize(){
field1.setText("");
field2.setText("");
similarity1.setText("0");
time1.setText("0");
butt.text1 = " ";
butt.text2 = " ";
}
//MenuListener internal class
class MenuListener implements ActionListener {
private JFrame frame;
MenuListener(JFrame fr) {
frame = fr;
}
public MenuListener(MainGUI Frame) {
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() instanceof JMenuItem) {
String command = evt.getActionCommand();
if (command.equals("Close")) {
System.exit(0);
}else if (command.equals("New")) {
initialize();
} else{
System.out.println("ERROR: In the menudepartment!");
}
}
}
}
//ButtonListener internal class
class ButtonListener implements ActionListener {
private JFrame frame;
private LevensteinDistance levenshtein;
File selectedFile1 = null;
File selectedFile2 = null;
Scanner test1 = null;
Scanner test2 = null;
String text1 = "";
String text2 = "";
String tex1 = "";
String tex2 = "";
ButtonListener(JFrame fr) {
frame = fr;
}
public ButtonListener(MainGUI Frame) {
}
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() instanceof JButton) {
String command = arg0.getActionCommand();
if (command.equals("Text 1: ")) {
//
String[] text = new String[] { "txt" };
//
JFileChooser FC = new JFileChooser();
//
FC.addChoosableFileFilter(new SimpleFileFilter(text,
"Only textfiles (*.txt)"));
//
int returnVal = FC.showOpenDialog(frame);
if(returnVal == JFileChooser.APPROVE_OPTION){
selectedFile1 = FC.getSelectedFile();
field1.setText(selectedFile1.getName());
}
text1 = ""+"";
try{
test1 = new Scanner(selectedFile1);
} catch(FileNotFoundException e){
System.out.println("Text 1 is not found!");
}
while(test1.hasNextLine()){
text1 += test1.nextLine();
text1 = text1.toLowerCase();
}
}else if (command.equals("Text 2: ")) {
//
String[] text = new String[] { "txt" };
//
JFileChooser FC = new JFileChooser();
//
FC.addChoosableFileFilter(new SimpleFileFilter(text,
"Only textfiles (*.txt)"));
//
int returnVal = FC.showOpenDialog(frame);
if(returnVal == JFileChooser.APPROVE_OPTION){
selectedFile2 = FC.getSelectedFile();
field2.setText(selectedFile2.getName());
}
try{
test2 = new Scanner(selectedFile2);
} catch(FileNotFoundException e){
System.out.println("Text 2 is not found!");
}
text2 = ""+"";
while(test2.hasNextLine()){
text2 += test2.nextLine();
text2 = text2.toLowerCase();
}
} else if (command.equals(" Algorithm1 ")) {
text1 = text1.replaceAll("[.,!?#]", "");
text1 = text1.replaceAll("&","and");
text2 = text2.replaceAll("[.,!?#]", "");
text2 = text2.replaceAll("&","and");
tex1 = text1;
tex2 = text2;
double ratio = levenshtein.calculate(tex1, tex2);
similarity1.setText(Double.toString(ratio)+"%");
} else{
System.out.println("ERROR: In the button department!");
}
}
}
}
public static void main(String[] args0) {
new MainGUI().showIt("SimilarityBtw2");
}
}