View Javadoc

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