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