SwingApplication.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import java.awt.Component;
  2. import java.awt.Container;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.PrintStream;
  13. import java.io.PrintWriter;
  14. import java.util.HashSet;
  15. import java.util.Iterator;
  16. import java.util.Vector;
  17. import javax.swing.BorderFactory;
  18. import javax.swing.ImageIcon;
  19. import javax.swing.JButton;
  20. import javax.swing.JFileChooser;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JPanel;
  24. import javax.swing.JTextField;
  25. import javax.swing.SwingUtilities;
  26. import javax.swing.UIManager;
  27. import javax.swing.UnsupportedLookAndFeelException;
  28. public class SwingApplication extends JPanel
  29. implements ActionListener
  30. {
  31. protected JTextField textField;
  32. protected JTextField inputField;
  33. private static String labelPrefix = "Status: Idle";
  34. final JLabel label = new JLabel(labelPrefix);
  35. final JLabel label1 = new JLabel("Input File: ");
  36. final JLabel label2 = new JLabel("Output File: ");
  37. final JFileChooser fc = new JFileChooser();
  38. JButton button2;
  39. JButton button;
  40. String fileName = "";
  41. JButton savebutton;
  42. HashSet<Node> output;
  43. String outFileName = "";
  44. JButton resetbutton;
  45. JButton viewbutton;
  46. File outFile;
  47. ImageIcon icon = new ImageIcon("sr.JPG");
  48. final JLabel labelicon = new JLabel(this.icon);
  49. static final String LOOKANDFEEL = null;
  50. public Component createComponents() {
  51. this.button = new JButton("Start PACT");
  52. this.button.setMnemonic(73);
  53. this.button.addActionListener(this);
  54. this.label.setLabelFor(this.button);
  55. this.button2 = new JButton("Open Input File...");
  56. this.button2.setMnemonic(73);
  57. this.button2.addActionListener(this);
  58. this.label.setLabelFor(this.button2);
  59. this.savebutton = new JButton("Save Output As...");
  60. this.savebutton.setMnemonic(73);
  61. this.savebutton.addActionListener(this);
  62. this.label.setLabelFor(this.savebutton);
  63. this.resetbutton = new JButton("Reset PACT");
  64. this.resetbutton.setMnemonic(73);
  65. this.resetbutton.addActionListener(this);
  66. this.label.setLabelFor(this.resetbutton);
  67. this.viewbutton = new JButton("View Output");
  68. this.viewbutton.setMnemonic(73);
  69. this.viewbutton.addActionListener(this);
  70. this.label.setLabelFor(this.viewbutton);
  71. this.textField = new JTextField(15);
  72. this.textField.setEnabled(false);
  73. this.inputField = new JTextField(20);
  74. JPanel pane = new JPanel(new GridLayout(6, 2));
  75. pane.add(this.label1);
  76. pane.add(new JLabel(""));
  77. pane.add(this.textField);
  78. pane.add(this.button2);
  79. pane.add(new JLabel(""));
  80. pane.add(this.button);
  81. pane.add(new JLabel(""));
  82. pane.add(this.label);
  83. pane.add(new JLabel(""));
  84. pane.add(new JLabel(""));
  85. pane.add(this.resetbutton);
  86. pane.add(this.savebutton);
  87. pane.setBorder(BorderFactory.createEmptyBorder(
  88. 30,
  89. 30,
  90. 10,
  91. 30));
  92. return pane;
  93. }
  94. public void actionPerformed(ActionEvent e) {
  95. this.resetbutton.setEnabled(true);
  96. if (e.getSource() == this.button2)
  97. {
  98. int returnVal = this.fc.showOpenDialog(this);
  99. if (returnVal == 0) {
  100. File file = this.fc.getSelectedFile();
  101. this.fileName = file.getName();
  102. this.textField.setText(this.fileName);
  103. }
  104. } else if (e.getSource() == this.button)
  105. {
  106. String outName = "output.txt";
  107. try
  108. {
  109. this.output = process(this.fileName, outName);
  110. } catch (FileNotFoundException io) {
  111. this.label.setText("Status: Fail, file error");
  112. } catch (IOException fe) {
  113. this.label.setText("Status: Fail, read error");
  114. } catch (BracketException be) {
  115. this.label.setText("Status: Input error, try again");
  116. this.resetbutton.setEnabled(false);
  117. throw new BracketException();
  118. } catch (Exception ge) {
  119. this.label.setText("Status: Fail, general error");
  120. this.resetbutton.setEnabled(false);
  121. throw new RuntimeException();
  122. }
  123. if (this.fileName.equals(""))
  124. this.label.setText("Status: Fail, file error");
  125. else {
  126. this.label.setText("Status: Success");
  127. }
  128. }
  129. else if (e.getSource() == this.savebutton)
  130. {
  131. int returnVal = this.fc.showSaveDialog(this);
  132. if (returnVal == 0) {
  133. File file = this.fc.getSelectedFile();
  134. this.outFileName = file.getName();
  135. try {
  136. PrintWriter summary = new PrintWriter(new FileWriter(
  137. this.outFileName));
  138. Iterator pIt = this.output.iterator();
  139. while (pIt.hasNext()) {
  140. summary.println(((Node)pIt.next()).getVenn());
  141. }
  142. summary.close();
  143. } catch (IOException io) {
  144. this.label.setText("Status: Fail, write error");
  145. }
  146. }
  147. } else if (e.getSource() == this.resetbutton) {
  148. this.output.clear();
  149. this.fileName = "";
  150. this.outFileName = "";
  151. this.label.setText("Status: Idle");
  152. this.textField.setText("");
  153. }
  154. }
  155. private HashSet<Node> process(String in, String out)
  156. throws IOException, FileNotFoundException, BracketException
  157. {
  158. BufferedReader inB = new BufferedReader(new FileReader(in));
  159. String line = inB.readLine();
  160. Vector inputs = new Vector();
  161. while (line != null) {
  162. inputs.add(line);
  163. line = inB.readLine();
  164. }
  165. inB.close();
  166. Iterator it = inputs.iterator();
  167. HashSet printResult = new HashSet();
  168. HashSet currres = new HashSet();
  169. HashSet subres = new HashSet();
  170. if (!it.hasNext()) {
  171. System.out.println("Input file empty!");
  172. }
  173. Node curr1 = null;
  174. if (it.hasNext()) {
  175. curr1 = new Node(null, (String)it.next());
  176. currres.add(curr1);
  177. }
  178. if (!it.hasNext()) {
  179. printResult.add(curr1);
  180. }
  181. while (it.hasNext()) {
  182. subres.clear();
  183. Node curr2 = new Node(null, (String)it.next());
  184. Iterator cIt = currres.iterator();
  185. while (cIt.hasNext()) {
  186. HashSet com = ((Node)cIt.next()).reducedCombine(curr2);
  187. System.out.println("com: " + com);
  188. Iterator comIt = com.iterator();
  189. while (comIt.hasNext()) {
  190. subres.add((Node)comIt.next());
  191. }
  192. }
  193. Iterator sIt = subres.iterator();
  194. currres.clear();
  195. while (sIt.hasNext()) {
  196. currres.add((Node)sIt.next());
  197. }
  198. }
  199. Iterator itC = currres.iterator();
  200. printResult.clear();
  201. while (itC.hasNext()) {
  202. printResult.add((Node)itC.next());
  203. }
  204. HashSet temp = removeDuplicates(printResult);
  205. PrintWriter summary = new PrintWriter(new FileWriter(
  206. out));
  207. Iterator pIt = temp.iterator();
  208. while (pIt.hasNext()) {
  209. summary.println(((Node)pIt.next()).getVenn());
  210. }
  211. summary.close();
  212. return temp;
  213. }
  214. private HashSet<Node> removeDuplicates(HashSet<Node> h)
  215. {
  216. Iterator it = h.iterator();
  217. HashSet result = new HashSet();
  218. while (it.hasNext()) {
  219. Node curr = (Node)it.next();
  220. if (!contains(curr, result)) {
  221. result.add(curr);
  222. }
  223. }
  224. return result;
  225. }
  226. private boolean contains(Node n, HashSet<Node> h) {
  227. Iterator it = h.iterator();
  228. while (it.hasNext()) {
  229. Node curr = (Node)it.next();
  230. if (curr.equals(n)) {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. private static void initLookAndFeel() {
  237. String lookAndFeel = null;
  238. if (LOOKANDFEEL != null) {
  239. if (LOOKANDFEEL.equals("Metal")) {
  240. lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
  241. } else if (LOOKANDFEEL.equals("System")) {
  242. lookAndFeel = UIManager.getSystemLookAndFeelClassName();
  243. } else if (LOOKANDFEEL.equals("Motif")) {
  244. lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  245. } else if (LOOKANDFEEL.equals("GTK+")) {
  246. lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
  247. } else {
  248. System.err.println("Unexpected value of LOOKANDFEEL specified: " +
  249. LOOKANDFEEL);
  250. lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
  251. }
  252. try
  253. {
  254. UIManager.setLookAndFeel(lookAndFeel);
  255. } catch (ClassNotFoundException e) {
  256. System.err.println("Couldn't find class for specified look and feel:" +
  257. lookAndFeel);
  258. System.err.println("Did you include the L&F library in the class path?");
  259. System.err.println("Using the default look and feel.");
  260. } catch (UnsupportedLookAndFeelException e) {
  261. System.err.println("Can't use the specified look and feel (" +
  262. lookAndFeel +
  263. ") on this platform.");
  264. System.err.println("Using the default look and feel.");
  265. } catch (Exception e) {
  266. System.err.println("Couldn't get specified look and feel (" +
  267. lookAndFeel +
  268. "), for some reason.");
  269. System.err.println("Using the default look and feel.");
  270. e.printStackTrace();
  271. }
  272. }
  273. }
  274. private static void createAndShowGUI()
  275. {
  276. initLookAndFeel();
  277. JFrame.setDefaultLookAndFeelDecorated(true);
  278. JFrame frame = new JFrame("PACT 2.0");
  279. frame.setDefaultCloseOperation(3);
  280. SwingApplication app = new SwingApplication();
  281. Component contents = app.createComponents();
  282. frame.getContentPane().add(contents, "Center");
  283. frame.pack();
  284. frame.setVisible(true);
  285. }
  286. public static void main(String[] args)
  287. {
  288. SwingUtilities.invokeLater(new Runnable() {
  289. public void run() {
  290. SwingApplication.access$0();
  291. }
  292. });
  293. }
  294. }