1 package net.sourceforge.pmd.util.viewer.gui; 2 3 import net.sourceforge.pmd.ast.ParseException; 4 import net.sourceforge.pmd.util.viewer.model.ViewerModel; 5 import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent; 6 import net.sourceforge.pmd.util.viewer.model.ViewerModelListener; 7 import net.sourceforge.pmd.util.viewer.util.NLS; 8 9 import javax.swing.*; 10 import java.awt.BorderLayout; 11 import java.awt.FlowLayout; 12 import java.awt.event.ActionEvent; 13 import java.awt.event.ActionListener; 14 15 16 /*** 17 * viewer's main frame 18 * 19 * @author Boris Gruschko ( boris at gruschko.org ) 20 * @version $Id: MainFrame.java,v 1.10 2005/09/02 19:36:22 tomcopeland Exp $ 21 */ 22 23 public class MainFrame 24 extends JFrame 25 implements ActionListener, ActionCommands, ViewerModelListener { 26 private ViewerModel model; 27 private SourceCodePanel sourcePanel; 28 private ASTPanel astPanel; 29 private XPathPanel xPathPanel; 30 private JButton compileBtn; 31 private JButton evalBtn; 32 private JLabel statusLbl; 33 34 /*** 35 * constructs and shows the frame 36 */ 37 public MainFrame() { 38 super(NLS.nls("MAIN.FRAME.TITLE")); 39 init(); 40 } 41 42 private void init() { 43 model = new ViewerModel(); 44 model.addViewerModelListener(this); 45 sourcePanel = new SourceCodePanel(model); 46 astPanel = new ASTPanel(model); 47 xPathPanel = new XPathPanel(model); 48 getContentPane().setLayout(new BorderLayout()); 49 JSplitPane editingPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel); 50 editingPane.setResizeWeight(0.5d); 51 JPanel interactionsPane = new JPanel(new BorderLayout()); 52 interactionsPane.add(xPathPanel, BorderLayout.SOUTH); 53 interactionsPane.add(editingPane, BorderLayout.CENTER); 54 getContentPane().add(interactionsPane, BorderLayout.CENTER); 55 compileBtn = new JButton(NLS.nls("MAIN.FRAME.COMPILE_BUTTON.TITLE")); 56 compileBtn.setActionCommand(COMPILE_ACTION); 57 compileBtn.addActionListener(this); 58 evalBtn = new JButton(NLS.nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE")); 59 evalBtn.setActionCommand(EVALUATE_ACTION); 60 evalBtn.addActionListener(this); 61 evalBtn.setEnabled(false); 62 statusLbl = new JLabel(); 63 statusLbl.setHorizontalAlignment(SwingConstants.RIGHT); 64 JPanel btnPane = new JPanel(new FlowLayout(FlowLayout.LEFT)); 65 btnPane.add(compileBtn); 66 btnPane.add(evalBtn); 67 btnPane.add(statusLbl); 68 getContentPane().add(btnPane, BorderLayout.SOUTH); 69 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 70 pack(); 71 setSize(800, 600); 72 setVisible(true); 73 } 74 75 /*** 76 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) 77 */ 78 public void actionPerformed(ActionEvent e) { 79 String command = e.getActionCommand(); 80 long t0, t1; 81 if (command.equals(COMPILE_ACTION)) { 82 try { 83 t0 = System.currentTimeMillis(); 84 model.commitSource(sourcePanel.getSourceCode()); 85 t1 = System.currentTimeMillis(); 86 setStatus(NLS.nls("MAIN.FRAME.COMPILATION.TOOK") + " " + (t1 - t0) + " ms"); 87 } catch (ParseException exc) { 88 setStatus(NLS.nls("MAIN.FRAME.COMPILATION.PROBLEM") + " " + exc.toString()); 89 new ParseExceptionHandler(this, exc); 90 } 91 } else if (command.equals(EVALUATE_ACTION)) { 92 try { 93 t0 = System.currentTimeMillis(); 94 model.evaluateXPathExpression(xPathPanel.getXPathExpression(), this); 95 t1 = System.currentTimeMillis(); 96 setStatus(NLS.nls("MAIN.FRAME.EVALUATION.TOOK") + " " + (t1 - t0) + " ms"); 97 } catch (Exception exc) { 98 setStatus(NLS.nls("MAIN.FRAME.EVALUATION.PROBLEM") + " " + exc.toString()); 99 new ParseExceptionHandler(this, exc); 100 } 101 } 102 } 103 104 /*** 105 * Sets the status bar message 106 * 107 * @param string the new status, the empty string will be set if the value is <code>null</code> 108 */ 109 private void setStatus(String string) { 110 if (string == null) 111 string = ""; 112 statusLbl.setText(string); 113 } 114 115 /*** 116 * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent) 117 */ 118 public void viewerModelChanged(ViewerModelEvent e) { 119 evalBtn.setEnabled(model.hasCompiledTree()); 120 } 121 }