import java.awt.Component; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Iterator; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class SwingApplication extends JPanel implements ActionListener { protected JTextField textField; protected JTextField inputField; private static String labelPrefix = "Status: Idle"; final JLabel label = new JLabel(labelPrefix); final JLabel label1 = new JLabel("Input File: "); final JLabel label2 = new JLabel("Output File: "); final JFileChooser fc = new JFileChooser(); JButton button2; JButton button; String fileName = ""; JButton savebutton; HashSet output; String outFileName = ""; JButton resetbutton; JButton viewbutton; File outFile; ImageIcon icon = new ImageIcon("sr.JPG"); final JLabel labelicon = new JLabel(this.icon); static final String LOOKANDFEEL = null; public Component createComponents() { this.button = new JButton("Start PACT"); this.button.setMnemonic(73); this.button.addActionListener(this); this.label.setLabelFor(this.button); this.button2 = new JButton("Open Input File..."); this.button2.setMnemonic(73); this.button2.addActionListener(this); this.label.setLabelFor(this.button2); this.savebutton = new JButton("Save Output As..."); this.savebutton.setMnemonic(73); this.savebutton.addActionListener(this); this.label.setLabelFor(this.savebutton); this.resetbutton = new JButton("Reset PACT"); this.resetbutton.setMnemonic(73); this.resetbutton.addActionListener(this); this.label.setLabelFor(this.resetbutton); this.viewbutton = new JButton("View Output"); this.viewbutton.setMnemonic(73); this.viewbutton.addActionListener(this); this.label.setLabelFor(this.viewbutton); this.textField = new JTextField(15); this.textField.setEnabled(false); this.inputField = new JTextField(20); JPanel pane = new JPanel(new GridLayout(6, 2)); pane.add(this.label1); pane.add(new JLabel("")); pane.add(this.textField); pane.add(this.button2); pane.add(new JLabel("")); pane.add(this.button); pane.add(new JLabel("")); pane.add(this.label); pane.add(new JLabel("")); pane.add(new JLabel("")); pane.add(this.resetbutton); pane.add(this.savebutton); pane.setBorder(BorderFactory.createEmptyBorder( 30, 30, 10, 30)); return pane; } public void actionPerformed(ActionEvent e) { this.resetbutton.setEnabled(true); if (e.getSource() == this.button2) { int returnVal = this.fc.showOpenDialog(this); if (returnVal == 0) { File file = this.fc.getSelectedFile(); this.fileName = file.getName(); this.textField.setText(this.fileName); } } else if (e.getSource() == this.button) { String outName = "output.txt"; try { this.output = process(this.fileName, outName); } catch (FileNotFoundException io) { this.label.setText("Status: Fail, file error"); } catch (IOException fe) { this.label.setText("Status: Fail, read error"); } catch (BracketException be) { this.label.setText("Status: Input error, try again"); this.resetbutton.setEnabled(false); throw new BracketException(); } catch (Exception ge) { this.label.setText("Status: Fail, general error"); this.resetbutton.setEnabled(false); throw new RuntimeException(); } if (this.fileName.equals("")) this.label.setText("Status: Fail, file error"); else { this.label.setText("Status: Success"); } } else if (e.getSource() == this.savebutton) { int returnVal = this.fc.showSaveDialog(this); if (returnVal == 0) { File file = this.fc.getSelectedFile(); this.outFileName = file.getName(); try { PrintWriter summary = new PrintWriter(new FileWriter( this.outFileName)); Iterator pIt = this.output.iterator(); while (pIt.hasNext()) { summary.println(((Node)pIt.next()).getVenn()); } summary.close(); } catch (IOException io) { this.label.setText("Status: Fail, write error"); } } } else if (e.getSource() == this.resetbutton) { this.output.clear(); this.fileName = ""; this.outFileName = ""; this.label.setText("Status: Idle"); this.textField.setText(""); } } private HashSet process(String in, String out) throws IOException, FileNotFoundException, BracketException { BufferedReader inB = new BufferedReader(new FileReader(in)); String line = inB.readLine(); Vector inputs = new Vector(); while (line != null) { inputs.add(line); line = inB.readLine(); } inB.close(); Iterator it = inputs.iterator(); HashSet printResult = new HashSet(); HashSet currres = new HashSet(); HashSet subres = new HashSet(); if (!it.hasNext()) { System.out.println("Input file empty!"); } Node curr1 = null; if (it.hasNext()) { curr1 = new Node(null, (String)it.next()); currres.add(curr1); } if (!it.hasNext()) { printResult.add(curr1); } while (it.hasNext()) { subres.clear(); Node curr2 = new Node(null, (String)it.next()); Iterator cIt = currres.iterator(); while (cIt.hasNext()) { HashSet com = ((Node)cIt.next()).reducedCombine(curr2); System.out.println("com: " + com); Iterator comIt = com.iterator(); while (comIt.hasNext()) { subres.add((Node)comIt.next()); } } Iterator sIt = subres.iterator(); currres.clear(); while (sIt.hasNext()) { currres.add((Node)sIt.next()); } } Iterator itC = currres.iterator(); printResult.clear(); while (itC.hasNext()) { printResult.add((Node)itC.next()); } HashSet temp = removeDuplicates(printResult); PrintWriter summary = new PrintWriter(new FileWriter( out)); Iterator pIt = temp.iterator(); while (pIt.hasNext()) { summary.println(((Node)pIt.next()).getVenn()); } summary.close(); return temp; } private HashSet removeDuplicates(HashSet h) { Iterator it = h.iterator(); HashSet result = new HashSet(); while (it.hasNext()) { Node curr = (Node)it.next(); if (!contains(curr, result)) { result.add(curr); } } return result; } private boolean contains(Node n, HashSet h) { Iterator it = h.iterator(); while (it.hasNext()) { Node curr = (Node)it.next(); if (curr.equals(n)) { return true; } } return false; } private static void initLookAndFeel() { String lookAndFeel = null; if (LOOKANDFEEL != null) { if (LOOKANDFEEL.equals("Metal")) { lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); } else if (LOOKANDFEEL.equals("System")) { lookAndFeel = UIManager.getSystemLookAndFeelClassName(); } else if (LOOKANDFEEL.equals("Motif")) { lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; } else if (LOOKANDFEEL.equals("GTK+")) { lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; } else { System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL); lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); } try { UIManager.setLookAndFeel(lookAndFeel); } catch (ClassNotFoundException e) { System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel); System.err.println("Did you include the L&F library in the class path?"); System.err.println("Using the default look and feel."); } catch (UnsupportedLookAndFeelException e) { System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform."); System.err.println("Using the default look and feel."); } catch (Exception e) { System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason."); System.err.println("Using the default look and feel."); e.printStackTrace(); } } } private static void createAndShowGUI() { initLookAndFeel(); JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("PACT 2.0"); frame.setDefaultCloseOperation(3); SwingApplication app = new SwingApplication(); Component contents = app.createComponents(); frame.getContentPane().add(contents, "Center"); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { SwingApplication.access$0(); } }); } }