1 package csdl.jblanket.app; 2 3 import csdl.jblanket.JBlanketException; 4 import csdl.jblanket.app.tree.NodeContentHandler; 5 import csdl.jblanket.app.tree.NodeErrorHandler; 6 import csdl.jblanket.app.tree.MethodNode; 7 import csdl.jblanket.app.tree.Node; 8 import csdl.jblanket.app.tree.TreeWriter; 9 import csdl.jblanket.util.MethodCategories; 10 11 import java.awt.BorderLayout ; 12 import java.awt.Color ; 13 import java.awt.Container ; 14 import java.awt.Dimension ; 15 import java.awt.FlowLayout ; 16 import java.awt.GridBagConstraints ; 17 import java.awt.GridBagLayout ; 18 import java.awt.GridLayout ; 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 import java.awt.event.WindowAdapter ; 22 import java.awt.event.WindowEvent ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.swing.JButton ; 33 import javax.swing.JOptionPane ; 34 import javax.swing.JPanel ; 35 import javax.swing.JScrollPane ; 36 import javax.swing.JFrame ; 37 import javax.swing.JTextPane ; 38 import javax.swing.JTree ; 39 import javax.swing.UIManager ; 40 import javax.swing.text.BadLocationException ; 41 import javax.swing.text.Document ; 42 import javax.swing.text.Style ; 43 import javax.swing.text.StyleConstants ; 44 import javax.swing.text.StyleContext ; 45 import javax.swing.tree.DefaultMutableTreeNode ; 46 import javax.swing.tree.DefaultTreeModel ; 47 import javax.swing.tree.TreePath ; 48 import javax.swing.tree.TreeSelectionModel ; 49 51 import org.apache.xerces.parsers.SAXParser; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.XMLReader ; 54 import org.xml.sax.SAXException ; 55 56 121 public class ExcludeIndividualMethodApp extends JPanel { 122 123 125 private boolean playWithLineStyle = false; 126 private String lineStyle = "Angled"; 127 128 129 private DefaultTreeModel treeModel; 130 131 private final JTree tree; 132 133 private JFrame frame; 134 135 136 private MethodCategories categories; 137 138 protected boolean excludeOneLineMethods; 139 140 protected boolean excludeConstructors; 141 142 151 public ExcludeIndividualMethodApp(boolean verbose, boolean excludeOneLineMethods, 152 boolean excludeConstructors, JFrame frame) 153 throws JBlanketException { 154 155 this.categories = MethodCategories.getInstance(); 156 157 this.excludeOneLineMethods = excludeOneLineMethods; 158 this.excludeConstructors = excludeConstructors; 159 160 DefaultMutableTreeNode top = new DefaultMutableTreeNode (new Node("System")); 162 treeModel = new DefaultTreeModel (top); 163 createTree(); 164 165 this.tree = new JTree (treeModel); 167 this.tree.getSelectionModel() 168 .setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); 169 this.tree.setCellRenderer(new AppRenderer()); 170 171 setLayout(new BorderLayout ()); 172 setPreferredSize(new Dimension (600, 500)); 173 174 JScrollPane treeView = new JScrollPane (tree); 176 add(treeView, BorderLayout.CENTER); 177 178 JPanel bottomPanel = new JPanel (new GridLayout (0, 1)); 179 bottomPanel.add(createButtonPanel()); 180 add(bottomPanel, BorderLayout.EAST); 181 182 this.frame = frame; 183 } 184 185 190 private void createTree() throws JBlanketException { 191 if (this.excludeOneLineMethods) { 193 this.categories.getFileName("oneLineFile"); 194 } 195 if (this.excludeConstructors) { 196 this.categories.getFileName("constructorFile"); 197 } 198 this.categories.getFileName("excludedIndividualFile"); 200 201 Iterator i = this.categories.getCategories().iterator(); 202 while (i.hasNext()) { 203 String key = (String ) i.next(); 204 File nextFile = new File ((String ) this.categories.getFileName(key)); 205 if (!(key.equals("excludedIndividualFile") && !nextFile.exists())) { 206 parseXmlFile(nextFile, key); 207 } 208 } 209 210 } 211 212 219 private void parseXmlFile(File file, String methodCategory) throws JBlanketException { 220 XMLReader reader = new SAXParser(); 222 232 reader.setContentHandler(new NodeContentHandler(this.treeModel, methodCategory)); 234 reader.setErrorHandler(new NodeErrorHandler()); 236 InputSource inputSource = null; 238 239 try { 240 inputSource = new InputSource (new FileInputStream (file)); 241 } 242 catch (IOException e) { 243 throw new JBlanketException("Unable to open file " + file); 244 } 245 246 inputSource.setSystemId(file.getAbsolutePath()); 247 248 try { 249 reader.parse(inputSource); 250 } 251 catch (SAXException e) { 252 throw new JBlanketException("Unable to parse file " + file, e); 253 } 254 catch (IOException e) { 255 throw new JBlanketException("Unable to read file " + file, e); 256 } 257 } 258 259 264 private JPanel createLegend() { 265 266 List legendList = new ArrayList (); 268 legendList.add("plain -- tested\n"); 269 legendList.add("BOLD"); 270 legendList.add(" -- untested\n"); 271 legendList.add("black -- included\n"); 272 273 legendList.add("blue"); 275 legendList.add(" -- constructor\n"); 276 278 legendList.add("purple"); 280 legendList.add(" -- one-line\n"); 281 283 legendList.add("red"); 284 legendList.add(" -- user excluded\n"); 285 286 List stylesList = new ArrayList (); 288 stylesList.add("regular"); 289 stylesList.add("bold"); 290 stylesList.add("regular"); 291 stylesList.add("black"); 292 293 stylesList.add("blue"); 295 stylesList.add("regular"); 296 298 stylesList.add("purple"); 300 stylesList.add("regular"); 301 stylesList.add("red"); 303 stylesList.add("regular"); 304 305 JTextPane legendPane = new JTextPane (); 306 legendPane.setEditable(false); 307 legendPane.setBackground(new Color (175, 180, 199)); 309 312 initStylesForTextPane(legendPane); 313 Document doc = legendPane.getDocument(); 314 try { 315 316 String [] legend = (String []) legendList.toArray(new String [legendList.size()]); 317 String [] styles = (String []) legendList.toArray(new String [legendList.size()]); 318 for (int i = 0; i < styles.length; i++) { 319 doc.insertString(doc.getLength(), legend[i], legendPane.getStyle(styles[i])); 320 } 321 } 322 catch (BadLocationException e) { 323 System.out.println("Unable to display legend properly"); 324 } 325 326 JPanel legendPanel = new JPanel (new FlowLayout ()); 327 legendPanel.add(legendPane); 328 return legendPanel; 329 } 330 331 336 protected void initStylesForTextPane(JTextPane textPane) { 337 338 Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 339 340 Style regular = textPane.addStyle("regular", def); 341 StyleConstants.setFontFamily(def, "SansSerif"); 342 343 Style s = textPane.addStyle("bold", regular); 344 StyleConstants.setBold(s, true); 345 346 s = textPane.addStyle("black", regular); 347 StyleConstants.setForeground(s, Color.black); 348 349 s = textPane.addStyle("green", regular); 350 StyleConstants.setForeground(s, Color.green); 351 352 s = textPane.addStyle("red", regular); 353 StyleConstants.setForeground(s, Color.red); 354 355 s = textPane.addStyle("yellow", regular); 356 StyleConstants.setForeground(s, Color.yellow); 357 358 s = textPane.addStyle("orange", regular); 359 StyleConstants.setForeground(s, Color.orange); 360 361 s = textPane.addStyle("blue", regular); 362 StyleConstants.setForeground(s, Color.blue); 363 364 s = textPane.addStyle("purple", regular); 365 StyleConstants.setForeground(s, new Color (122, 22, 160)); 366 } 367 368 373 private JPanel createButtonPanel() { 374 375 JPanel buttonPanel = new JPanel (); 376 377 JButton excludeButton = new JButton ("Exclude"); 378 excludeButton.addActionListener(new ActionListener () { 379 public void actionPerformed(ActionEvent e) { 380 TreePath [] selectedPaths = tree.getSelectionPaths(); 381 if (selectedPaths == null) { 382 return; 384 } 385 for (int i = 0; i < selectedPaths.length; i++) { 386 DefaultMutableTreeNode selectedNode = 388 (DefaultMutableTreeNode ) selectedPaths[i].getLastPathComponent(); 389 processExcludeButton(selectedNode); 390 paint(getGraphics()); 391 } 392 } 393 }); 394 395 JButton includeButton = new JButton ("Include"); 396 includeButton.addActionListener(new ActionListener () { 397 public void actionPerformed(ActionEvent e) { 398 TreePath [] selectedPaths = tree.getSelectionPaths(); 399 if (selectedPaths == null) { 400 return; 402 } 403 for (int i = 0; i < selectedPaths.length; i++) { 404 DefaultMutableTreeNode selectedNode = 406 (DefaultMutableTreeNode ) selectedPaths[i].getLastPathComponent(); 407 processIncludeButton(selectedNode); 408 paint(getGraphics()); 409 } 410 } 411 }); 412 413 JButton saveButton = new JButton ("Save"); 414 saveButton.addActionListener(new ActionListener () { 415 public void actionPerformed(ActionEvent e) { 416 processSaveButton(); 417 } 418 }); 419 420 GridBagLayout gridbag = new GridBagLayout (); 421 GridBagConstraints c = new GridBagConstraints (); 422 423 c.fill = GridBagConstraints.BOTH; 424 c.weightx = 1.0; 425 426 buttonPanel.setLayout(new BorderLayout ()); 427 428 c.gridwidth = GridBagConstraints.REMAINDER; 429 430 JPanel topPanel = new JPanel (gridbag); 431 gridbag.setConstraints(excludeButton, c); 432 topPanel.add(excludeButton); 433 gridbag.setConstraints(includeButton, c); 434 topPanel.add(includeButton); 435 buttonPanel.add(topPanel, BorderLayout.NORTH); 436 437 JPanel legendPanel = createLegend(); 438 gridbag.setConstraints(legendPanel, c); 439 buttonPanel.add(legendPanel, BorderLayout.CENTER); 440 441 JPanel bottomPanel = new JPanel (gridbag); 442 gridbag.setConstraints(saveButton, c); 443 bottomPanel.add(saveButton); 444 buttonPanel.add(bottomPanel, BorderLayout.SOUTH); 445 446 return buttonPanel; 447 } 448 449 454 private void processExcludeButton(DefaultMutableTreeNode treeNode) { 455 456 if (!treeNode.isLeaf()) { 457 Enumeration children = treeNode.children(); 458 while (children.hasMoreElements()) { 459 DefaultMutableTreeNode nextChild = (DefaultMutableTreeNode ) children.nextElement(); 460 processExcludeButton(nextChild); 461 } 462 } 463 else { 464 MethodNode nextMethod = (MethodNode) treeNode.getUserObject(); 465 nextMethod.setNewColor(MethodCategoryColor.EXCLUDED_INDIVIDUAL.getColor()); 466 } 467 } 468 469 474 private void processIncludeButton(DefaultMutableTreeNode treeNode) { 475 476 if (!treeNode.isLeaf()) { 477 Enumeration children = treeNode.children(); 478 while (children.hasMoreElements()) { 479 DefaultMutableTreeNode nextChild = (DefaultMutableTreeNode ) children.nextElement(); 480 processIncludeButton(nextChild); 481 } 482 } 483 else { 484 MethodNode nextMethod = (MethodNode) treeNode.getUserObject(); 485 if ((nextMethod.getOrigColor().equals(MethodCategoryColor.EXCLUDED_INDIVIDUAL.getColor())) && 488 (nextMethod.getNewColor().equals(MethodCategoryColor.EXCLUDED_INDIVIDUAL.getColor()))) { 489 nextMethod.setNewColor(MethodCategoryColor.UNTESTED.getColor()); 490 } 491 else { 492 nextMethod.setNewColor(nextMethod.getOrigColor()); 494 } 495 } 496 } 497 498 501 private void processSaveButton() { 502 String fileName = categories.getFileName("excludedIndividualFile"); 504 505 try { 506 (new TreeWriter(treeModel)).store(new FileOutputStream (new File (fileName))); 507 } 508 catch (IOException e) { 509 System.out.println("Unable to store individually excluded methods to " + fileName); 510 } 511 512 JOptionPane.showMessageDialog(frame, "Excluded methods saved to\n" + fileName, 513 "JBlanket Notification", JOptionPane.PLAIN_MESSAGE); 514 } 515 516 522 public static void main(String args[]) throws JBlanketException { 523 main(java.util.Arrays.asList(args)); 524 } 525 526 532 public static void main(List args) throws JBlanketException { 533 534 boolean verbose = false; 536 String reportFormat = "frames"; 538 539 boolean excludeOneLineMethods = false; 541 boolean excludeConstructors = false; 543 544 MethodCategories categories = MethodCategories.getInstance(); 545 546 int i; 548 for (i = 0; i < args.size(); ++i) { 550 551 String argument = (String ) args.get(i); 552 553 if (argument.equals("-verbose")) { 554 verbose = (new Boolean ((String ) args.get(++i))).booleanValue(); 555 } 556 else if (argument.equals("-excludeOneLineMethods")) { 557 excludeOneLineMethods = (new Boolean ((String ) args.get(++i))).booleanValue(); 558 } 559 else if (argument.equals("-excludeConstructors")) { 560 excludeConstructors = (new Boolean ((String ) args.get(++i))).booleanValue(); 561 } 562 else if (argument.equals("-oneLineFile")) { 563 categories.addCategory("oneLineFile", (String ) args.get(++i)); 564 } 565 else if (argument.equals("-constructorFile")) { 566 categories.addCategory("constructorFile", (String ) args.get(++i)); 567 } 568 else if (argument.equals("-excludedIndividualFile")) { 569 categories.addCategory("excludedIndividualFile", (String ) args.get(++i)); 570 } 571 else if (argument.equals("-total.testedFile")) { 572 categories.addCategory("total.testedFile"); 573 } 574 else if (argument.equals("-total.untestedFile")) { 575 categories.addCategory("total.untestedFile"); 576 } 577 else { 578 System.out.println("Incorrect usage: " + argument); 579 System.exit(1); 580 } 581 } 582 583 try { 584 UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 588 } 589 catch (Exception e) { 590 } 592 593 JFrame frame = new JFrame ("JBlanket - Exclude Individual Methods"); 594 595 Container contentPane = frame.getContentPane(); 596 contentPane.setLayout(new GridLayout (0, 1)); 597 contentPane.add(new ExcludeIndividualMethodApp(verbose, excludeOneLineMethods, 598 excludeConstructors, frame)); 599 600 frame.addWindowListener(new WindowAdapter () { 601 public void windowClosing(WindowEvent e) { 602 System.exit(0); 603 } 604 }); 605 frame.pack(); 606 frame.setVisible(true); 607 } 608 } | Popular Tags |