View Javadoc

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