1 3 20 21 package edu.neu.ccs.jmk.swing; 22 23 import edu.neu.ccs.jmk.*; 24 import edu.neu.ccs.jmk.awt.RunQueue; 25 26 import java.io.*; 27 import java.util.*; 28 import java.text.*; 29 import java.awt.*; 30 import java.awt.event.*; 31 import javax.swing.*; 32 import javax.swing.text.*; 33 34 public final 35 class JMakeWindow 36 extends JEditorPane 37 { 38 private final static String NL = System.getProperty("line.separator"); 39 40 static private Make make; 41 static private String [] targets=null; 42 static private final RunQueue tasks = new RunQueue(); 43 44 private static boolean autoscroll_=true; 45 private static boolean autoclear_=true; 46 private static boolean automake_=false; 47 private static String LnF_=null; 48 49 private Icon TrafficNone = null; 50 private Icon TrafficGreen = null; 51 private Icon TrafficYellow = null; 52 private Icon TrafficRed = null; 53 54 private int trafficStatus_=0; 55 56 57 private JMakeWindow(Make make, String [] targets) 58 { 59 this.make = make; 60 this.targets = targets; 61 setEditable(false); 62 make.setOut(new JmkPrintWriter(new MyWriter(), true)); 64 new Thread (tasks).start(); 65 } 66 67 final class MyWriter 68 extends JmkWriter 69 { 70 private boolean closed_ = false; 71 private MutableAttributeSet blackcolor_=null; 72 private MutableAttributeSet bluecolor_=null; 73 private MutableAttributeSet redcolor_=null; 74 75 public MyWriter() 76 { 77 super(); 78 79 blackcolor_ = new SimpleAttributeSet(); 80 StyleConstants.setForeground(blackcolor_, Color.black); 81 bluecolor_ = new SimpleAttributeSet(); 82 StyleConstants.setForeground(bluecolor_, Color.blue); 83 redcolor_ = new SimpleAttributeSet(); 84 StyleConstants.setForeground(redcolor_, Color.red); 85 StyleConstants.setBold(redcolor_, true); 86 } 87 88 private void appendLine(String _s,AttributeSet _attr) 89 throws BadLocationException 90 { 91 Document document=mw.getDocument(); 92 document.insertString(document.getEndPosition().getOffset()-1,_s+NL,_attr); 93 } 94 95 public void jmk_println(String _s) 96 { 97 try 98 { 99 appendLine(_s, bluecolor_); 100 101 } catch ( BadLocationException e ) 102 { 103 System.err.println(e.toString()); 104 } 105 } 106 107 public void jmkerr_println(String _s) 108 { 109 try 110 { 111 appendLine(_s, redcolor_); 112 113 } catch ( BadLocationException e ) 114 { 115 System.err.println(e.toString()); 116 } 117 setRed(); 118 } 119 120 private StringBuffer buffer_ = new StringBuffer (80); 121 private boolean eol=false; 122 123 public void write (char cbuf[], int off, int len) 124 throws IOException 125 { 126 if ( closed_ ) 127 throw new IOException("Write of a closed writer"); 128 129 try 130 { 131 for ( int i = off; i < len; i++ ) 132 { 133 char ch = cbuf[i]; 134 if ( eol ) 135 { 136 eol = false; 137 if ( ch == '\n' ) 138 continue; 139 } 140 if ( ch == '\n' || ch == '\r' ) 141 { 142 if ( ch == '\r' ) 143 eol = true; 144 appendLine(buffer_.toString(),blackcolor_); 145 buffer_.setLength(0); 146 } else 147 { 148 buffer_.append(ch); 149 } 150 } 151 152 } catch ( BadLocationException e ) 153 { 154 System.err.println(e.toString()); 155 } 156 157 if ( autoscroll_ ) 159 { 160 JScrollBar sb=logSP.getVerticalScrollBar(); 161 sb.setValue(sb.getMaximum()); 162 } 163 164 } 165 166 public void flush() 167 throws IOException 168 { 169 170 if ( closed_ ) 171 throw new IOException("Flush of a closed writer"); 172 173 } 174 175 public void close() 176 throws IOException 177 { 178 if ( !closed_ ) 179 { 180 flush(); 181 closed_ = true; 182 } 183 } 184 185 } 186 187 private static String makefileLabel(Make make) 188 { 189 return(" Makefile: " + make.getFile().getPath()); 190 } 191 192 public static void createMakeWindow(Make make, String [] targets) 193 { 194 195 JmkProperties jp=JmkProperties.getInstance(); 196 jp.load(); 197 198 LnF_=jp.getString("jmk.lookandfeel", UIManager.getSystemLookAndFeelClassName()); 199 autoscroll_=jp.getBoolean("jmk.autoscroll",true); 200 autoclear_=jp.getBoolean("jmk.autoclear",true); 201 automake_=jp.getBoolean("jmk.automake",false); 202 203 try 205 { 206 UIManager.setLookAndFeel(LnF_ ); 207 } catch ( Exception e ) 208 { 209 System.err.println(e.toString()); 210 } 211 212 f = new JFrame("Jmk"); 213 f.setIconImage((new ImageIcon (f.getClass().getResource ("/edu/neu/ccs/jmk/swing/gfx/jmkicon.gif"))).getImage()); 215 216 f.addWindowListener (new java.awt.event.WindowAdapter () 217 { 218 public void windowClosing (java.awt.event.WindowEvent evt) 219 { 220 exitForm (evt); 221 } 222 }); 223 f.getContentPane ().setLayout (new java.awt.BorderLayout ()); 224 225 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 227 Dimension windowSize = new Dimension(screenSize.width/2, screenSize.height/2); 228 f.setBounds( (screenSize.width-windowSize.width)/4, 229 (screenSize.height-windowSize.height)/4, windowSize.width, windowSize.height); 230 231 mainMB = new JMenuBar (); 232 fileMN = new JMenu (); 233 fileMN.setText ("File"); 234 fileMN.setMnemonic('F'); 235 openMI = new JMenuItem (); 236 openMI.setText ("Open"); 237 openMI.addActionListener (new java.awt.event.ActionListener () 238 { 239 public void actionPerformed (java.awt.event.ActionEvent evt) 240 { 241 openMIActionPerformed (evt); 242 } 243 }); 244 openMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK)); 245 246 fileMN.add(openMI); 247 248 reloadMI = new JMenuItem (); 249 reloadMI.setText ("Reload"); 250 reloadMI.addActionListener (new java.awt.event.ActionListener () 251 { 252 public void actionPerformed (java.awt.event.ActionEvent evt) 253 { 254 reloadMIActionPerformed (evt); 255 } 256 }); 257 reloadMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK)); 258 259 fileMN.add(reloadMI); 260 261 fileMN.addSeparator(); 262 263 exitMI = new JMenuItem (); 264 exitMI.setText ("Exit"); 265 exitMI.addActionListener (new java.awt.event.ActionListener () 266 { 267 public void actionPerformed (java.awt.event.ActionEvent evt) 268 { 269 exitMIActionPerformed (evt); 270 } 271 }); 272 exitMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); 273 fileMN.add(exitMI); 274 275 mainMB.add(fileMN); 276 277 commandMN = new JMenu (); 278 commandMN.setText ("Command"); 279 commandMN.setMnemonic('C'); 280 makeMI = new JMenuItem (); 281 makeMI.setText ("Make"); 282 makeMI.addActionListener (new ActionListener () 283 { 284 public void actionPerformed (java.awt.event.ActionEvent evt) 285 { 286 makeMIActionPerformed (evt); 287 } 288 }); 289 makeMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); 290 commandMN.add(makeMI); 291 292 cancelMI = new JMenuItem (); 293 cancelMI.setText ("Cancel"); 294 cancelMI.addActionListener (new ActionListener () 295 { 296 public void actionPerformed (java.awt.event.ActionEvent evt) 297 { 298 cancelMIActionPerformed (evt); 299 } 300 }); 301 cancelMI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); 302 commandMN.add(cancelMI); 303 304 mainMB.add(commandMN); 305 306 optionsMN = new JMenu (); 307 optionsMN.setText ("Options"); 308 optionsMN.setMnemonic('O'); 309 prefMI = new JMenuItem (); 310 prefMI.setText ("Preferences"); 311 prefMI.setMnemonic('P'); 312 prefMI.addActionListener (new ActionListener () 313 { 314 public void actionPerformed (java.awt.event.ActionEvent evt) 315 { 316 prefMIActionPerformed (evt); 317 } 318 }); 319 optionsMN.add(prefMI); 320 321 optionsMN.addSeparator(); 322 323 verboseMI = new JCheckBoxMenuItem (); 324 verboseMI.setState (make.isVerbose()); 325 verboseMI.setText ("Verbose"); 326 verboseMI.setMnemonic('V'); 327 verboseMI.addActionListener (new java.awt.event.ActionListener () 328 { 329 public void actionPerformed (java.awt.event.ActionEvent evt) 330 { 331 verboseMIActionPerformed (evt); 332 } 333 }); 334 optionsMN.add(verboseMI); 335 336 justprintMI = new JCheckBoxMenuItem (); 337 justprintMI.setState (make.isJustPrinting()); 338 justprintMI.setText ("Just Print"); 339 justprintMI.setMnemonic('J'); 340 justprintMI.addActionListener (new java.awt.event.ActionListener () 341 { 342 public void actionPerformed (java.awt.event.ActionEvent evt) 343 { 344 justprintMIActionPerformed (evt); 345 } 346 }); 347 optionsMN.add(justprintMI); 348 349 mainMB.add(optionsMN); 350 351 helpMN = new JMenu (); 352 helpMN.setText ("Help"); 353 helpMN.setMnemonic('H'); 354 aboutMI = new JMenuItem (); 355 aboutMI.setText ("About..."); 356 aboutMI.setMnemonic('A'); 357 aboutMI.addActionListener (new java.awt.event.ActionListener () 358 { 359 public void actionPerformed (java.awt.event.ActionEvent evt) 360 { 361 aboutMIActionPerformed (evt); 362 } 363 }); 364 helpMN.add(aboutMI); 365 366 mainMB.add(helpMN); 367 368 f.setJMenuBar(mainMB); 369 370 logSP = new JScrollPane (); 371 logSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 372 mw = new JMakeWindow(make, targets); 373 mw.setContentType ("text/rtf"); 374 375 mw.addKeyListener (new java.awt.event.KeyAdapter () 376 { 377 public void keyPressed (java.awt.event.KeyEvent evt) 378 { 379 if ( evt.getKeyCode()==java.awt.event.KeyEvent.VK_ENTER ) 380 mw.makeTarget(); 381 } 382 } 383 ); 384 385 logSP.setViewportView (mw); 386 f.getContentPane ().add (logSP, "Center"); 387 388 mainTB = new JToolBar (); 389 mainTB.setFloatable (false); 390 mainTB.setLayout (new java.awt.FlowLayout (0, 5, 5)); 391 392 openBT = new JButton (); 393 openBT.setIcon (new ImageIcon (f.getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/VCROpen.gif"))); 394 openBT.setToolTipText ("Open a new makefile"); 395 openBT.addActionListener (new java.awt.event.ActionListener () 396 { 397 public void actionPerformed (java.awt.event.ActionEvent evt) 398 { 399 openBTActionPerformed (evt); 400 } 401 }); 402 mainTB.add (openBT); 403 404 playBT = new JButton (); 405 playBT.setIcon (new ImageIcon (f.getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/VCRPlay.gif"))); 406 playBT.setToolTipText ("Run makefile"); 407 playBT.addActionListener (new java.awt.event.ActionListener () 408 { 409 public void actionPerformed (java.awt.event.ActionEvent evt) 410 { 411 playBTActionPerformed (evt); 412 } 413 }); 414 mainTB.add (playBT); 415 416 stopBT = new JButton (); 417 stopBT.setIcon (new ImageIcon (f.getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/VCRStop.gif"))); 418 stopBT.setSelectedIcon (new ImageIcon (f.getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/VCRStop.gif"))); 419 stopBT.setToolTipText ("Stop makefile processing"); 420 stopBT.addActionListener (new java.awt.event.ActionListener () 421 { 422 public void actionPerformed (java.awt.event.ActionEvent evt) 423 { 424 stopBTActionPerformed (evt); 425 } 426 }); 427 mainTB.add (stopBT); 428 429 targetCB = new JComboBox (); 430 targetCB.setToolTipText ("Enter your target"); 431 targetCB.setEditable (true); 432 targetCB.addActionListener (new java.awt.event.ActionListener () 433 { 434 public void actionPerformed (java.awt.event.ActionEvent evt) 435 { 436 targetCBActionPerformed (evt); 437 } 438 }); 439 440 targetCB.addItem(getTargetString()); 441 targetCB.setSelectedIndex(0); 442 443 mainTB.add (targetCB); 444 445 f.getContentPane ().add (mainTB, "North"); 446 447 filenameLB = new JLabel (makefileLabel(make)); 448 mw.setNone(); 449 filenameLB.setDisabledIcon ((Icon) null); 450 f.getContentPane ().add (filenameLB, "South"); 451 452 f.show(); 453 454 mw.load(); 456 } 457 458 461 private void makeTask(String [] args, String targetString) 462 { 463 boolean failed=false; 464 465 try 466 { 467 if ( !make.make(args) ) 468 ((JmkPrintWriter)make.getOut()).jmk_println("Nothing to make"); 469 } catch ( CommandFailedException ex ) 470 { 471 failed=true; 472 ((JmkPrintWriter)make.getOut()).jmkerr_println("Command failed: " + ex.getMessage()); 473 } 474 if ( targetString.equals("") ) 475 targetString = "Make"; 476 else 477 targetString = "Made " + targetString; 478 ((JmkPrintWriter)make.getOut()).jmk_println(targetString + " at " + now()); 479 if ( !failed ) setGreen(); 480 } 481 482 485 private void make() 486 { 487 final String targetString = getTargetString(); 488 final String [] args = targets; 489 tasks.add(new Runnable () 490 { 491 public void run() 492 { 493 makeTask(args, targetString); 494 } 495 }); 496 } 497 498 502 private void loadTask() 503 { 504 String fileName = make.getFile().getPath(); 505 try 506 { 507 make.load(); 508 ((JmkPrintWriter)make.getOut()).jmk_println("Loaded " + fileName + " at " + now()); 509 } catch ( FileNotFoundException ex ) 510 { 511 ((JmkPrintWriter)make.getOut()).jmkerr_println("Cannot find makefile " + fileName); 512 } catch ( ParseError pe ) 513 { 514 ((JmkPrintWriter)make.getOut()).jmkerr_println(pe.getFileName() + ":" + pe.getLineNumber() 515 + ": Parse error: " + pe.getMessage()); 516 } 517 } 518 519 522 private void load() 523 { 524 tasks.add(new Runnable () 525 { 526 public void run() 527 { 528 loadTask(); 529 } 530 }); 531 } 532 533 536 private void cancel() 537 { 538 tasks.removeAll(); 539 make.setInterruptEnabled(true); 540 ((JmkPrintWriter)make.getOut()).jmk_println("Cancel requested"); 541 } 542 543 private DateFormat formatter_; 544 545 private String now() 546 { 547 if ( formatter_ == null ) 548 { 549 formatter_ = DateFormat.getTimeInstance(); 550 formatter_.setTimeZone(TimeZone.getDefault()); 551 } 552 return(formatter_.format(new Date())); 553 } 554 555 static private String cachedTargetString; 556 557 561 private static String [] string2String_array(String str) 562 { 563 Vector v = new Vector(); 564 int s = 0; 566 while ( s < str.length() ) 567 { 568 if ( str.charAt(s) == ' ' ) 569 s++; 570 else 571 { 572 int e = str.indexOf(' ', s); if ( e < 0 ) 574 { 575 v.addElement(str.substring(s)); 576 break; 577 } 578 v.addElement(str.substring(s, e)); 579 s = e + 1; 580 } 581 } 582 583 String [] str_array = new String [v.size()]; 584 v.copyInto(str_array); 585 return(str_array); 586 } 587 588 591 static private String getTargetString() 592 { 593 if ( cachedTargetString == null ) 594 { 595 if ( targets.length > 0 ) 596 { 597 cachedTargetString = targets[0]; 598 for ( int i = 1; i < targets.length; i++ ) 599 cachedTargetString += " " + targets[i]; 600 } else 601 cachedTargetString = ""; 602 } 603 return(cachedTargetString); 604 } 605 606 static private void aboutMIActionPerformed (java.awt.event.ActionEvent evt) 607 { 608 new AboutDG (f, true).show (); 609 } 610 611 static private void justprintMIActionPerformed (java.awt.event.ActionEvent evt) 612 { 613 make.setJustPrinting(!make.isJustPrinting()); 614 } 615 616 static private void verboseMIActionPerformed (java.awt.event.ActionEvent evt) 617 { 618 make.setVerbose(!make.isVerbose()); 619 } 620 621 static private void makeMIActionPerformed (java.awt.event.ActionEvent evt) 622 { 623 makeTarget(); 624 } 625 626 static private void makeTarget() 627 { 628 mw.setNone(); 629 if ( autoclear_ ) 630 mw.setText(""); 631 mw.make(); 632 } 633 634 static private void prefMIActionPerformed (java.awt.event.ActionEvent evt) 635 { 636 PreferenceDG pref=new PreferenceDG(f, true); 637 638 pref.setAutoscroll(autoscroll_); 639 pref.setAutoclear(autoclear_); 640 pref.setAutomake(automake_); 641 pref.setLnF(LnF_); 642 643 pref.show (); 644 645 if ( pref.getExitStatus()!=0 ) 646 { 647 autoscroll_=pref.getAutoscroll(); 648 autoclear_=pref.getAutoclear(); 649 automake_=pref.getAutomake(); 650 651 String lf=pref.getSelectedLnF(); 653 if ( LnF_.compareTo(lf)!=0 ) 654 { 655 LnF_=lf; 656 try 657 { 658 UIManager.setLookAndFeel(lf); 659 SwingUtilities.updateComponentTreeUI(f); 660 } catch ( Exception e ) 661 { 662 System.err.println(e.toString()); 663 } 664 } 665 666 } 667 } 668 669 static private void cancelMIActionPerformed (java.awt.event.ActionEvent evt) 670 { 671 mw.setYellow(); 672 mw.cancel(); 673 } 674 675 static private void exitMIActionPerformed (java.awt.event.ActionEvent evt) 676 { 677 System.exit (0); 678 } 679 680 static private void reloadMIActionPerformed (java.awt.event.ActionEvent evt) 681 { 682 mw.load(); 683 } 684 685 private static JFileChooser fc_=null; 686 687 static private void openMIActionPerformed (java.awt.event.ActionEvent evt) 688 { 689 690 if ( fc_==null ) 691 { 692 fc_=new JFileChooser(); 693 694 javax.swing.filechooser.FileFilter fileFilter=new JmkFilter(); 695 fc_.addChoosableFileFilter(fileFilter); 696 fc_.setFileFilter(fileFilter); 697 698 } 699 700 int ret=fc_.showOpenDialog(f); 701 if ( ret==JFileChooser.APPROVE_OPTION ) 702 { 703 mw.setGreen(); 704 File file=fc_.getSelectedFile(); 705 setMakefilename(file); 706 make.setFile(file); 707 mw.load(); 708 } 709 } 710 711 static private void targetCBActionPerformed (java.awt.event.ActionEvent evt) 712 { 713 714 String s=(String )targetCB.getSelectedItem(); 715 716 boolean found=false; 717 for ( int i=0; found==false && i<targetCB.getItemCount();i++ ) 719 { 720 String v=(String )targetCB.getItemAt(i); 721 if ( v.compareTo(s)==0 ) 722 found=true; 723 } 724 if ( found==false ) 725 targetCB.addItem(s); 726 727 if ( !cachedTargetString.equals(s) ) 728 { 729 cachedTargetString = s; 730 targets = string2String_array(s); 731 } 732 733 if ( automake_ ) 734 makeMIActionPerformed(evt); 735 } 736 737 static private void stopBTActionPerformed (java.awt.event.ActionEvent evt) 738 { 739 cancelMIActionPerformed(evt); 740 } 741 742 static private void playBTActionPerformed (java.awt.event.ActionEvent evt) 743 { 744 makeTarget(); 745 } 746 747 static private void openBTActionPerformed (java.awt.event.ActionEvent evt) 748 { 749 openMIActionPerformed(evt); 750 } 751 752 static private void setMakefilename(File _file) 753 { 754 filenameLB.setText("Makefile: "+_file.getPath()); 755 f.setTitle("Jmk - "+_file.getName()); 756 } 757 758 private void setNone() 759 { 760 if ( filenameLB!=null ) 762 { 763 if ( TrafficNone==null ) 764 TrafficNone = new ImageIcon (f.getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/TrafficOff.gif")); 765 766 filenameLB.setIcon (TrafficNone); 767 trafficStatus_=0; 768 } 769 } 770 771 private void setGreen() 772 { 773 if ( trafficStatus_!=1 ) 774 { 775 if ( TrafficGreen==null ) 776 TrafficGreen = new ImageIcon (getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/TrafficGreen.gif")); 777 filenameLB.setIcon (TrafficGreen); 778 trafficStatus_=1; 779 } 780 } 781 782 private void setRed() 783 { 784 if ( trafficStatus_!=2 ) 785 { 786 if ( TrafficRed==null ) 787 TrafficRed = new ImageIcon (getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/TrafficRed.gif")); 788 filenameLB.setIcon (TrafficRed); 789 trafficStatus_=2; 790 } 791 } 792 793 private void setYellow() 794 { 795 if ( trafficStatus_!=3 ) 796 { 797 if ( TrafficYellow==null ) 798 TrafficYellow = new ImageIcon (getClass ().getResource ("/edu/neu/ccs/jmk/swing/gfx/TrafficYellow.gif")); 799 filenameLB.setIcon (TrafficYellow); 800 trafficStatus_=3; 801 } 802 } 803 804 805 private static void exitForm(java.awt.event.WindowEvent evt) 806 { 807 System.exit (0); 808 } 809 810 static private JFrame f=null; 811 static private JMenuBar mainMB; 812 static private JMenu fileMN; 813 static private JMenuItem openMI; 814 static private JScrollPane logSP; 815 static private JMakeWindow mw; 816 static private JToolBar mainTB; 817 static private JButton openBT; 818 static private JButton playBT; 819 static private JButton stopBT; 820 static private JComboBox targetCB; 821 static private JLabel filenameLB=null; 822 static private JMenuItem reloadMI; 823 static private JMenuItem exitMI; 824 static private JMenu commandMN; 825 static private JMenuItem makeMI; 826 static private JMenuItem cancelMI; 827 static private JMenu optionsMN; 828 static private JMenuItem prefMI; 829 static private JCheckBoxMenuItem verboseMI; 830 static private JCheckBoxMenuItem justprintMI; 831 static private JMenu helpMN; 832 static private JMenuItem aboutMI; 833 834 } 835 | Popular Tags |