View Javadoc

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