View Javadoc

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