MainFrame.java
001 package net.sourceforge.pmd.util.viewer.gui;
002 
003 import java.awt.BorderLayout;
004 import java.awt.FlowLayout;
005 import java.awt.event.ActionEvent;
006 import java.awt.event.ActionListener;
007 
008 import javax.swing.ButtonGroup;
009 import javax.swing.JButton;
010 import javax.swing.JFrame;
011 import javax.swing.JLabel;
012 import javax.swing.JMenu;
013 import javax.swing.JMenuBar;
014 import javax.swing.JPanel;
015 import javax.swing.JRadioButtonMenuItem;
016 import javax.swing.JSplitPane;
017 import javax.swing.SwingConstants;
018 
019 import net.sourceforge.pmd.PMD;
020 import net.sourceforge.pmd.lang.LanguageVersion;
021 import net.sourceforge.pmd.lang.java.ast.ParseException;
022 import net.sourceforge.pmd.util.viewer.model.ViewerModel;
023 import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
024 import net.sourceforge.pmd.util.viewer.model.ViewerModelListener;
025 import net.sourceforge.pmd.util.viewer.util.NLS;
026 
027 
028 /**
029  * viewer's main frame
030  *
031  @author Boris Gruschko ( boris at gruschko.org )
032  */
033 
034 public class MainFrame
035         extends JFrame
036         implements ActionListener, ViewerModelListener {
037     private ViewerModel model;
038     private SourceCodePanel sourcePanel;
039     private XPathPanel xPathPanel;
040     private JButton evalBtn;
041     private JLabel statusLbl;
042     private JRadioButtonMenuItem jdk13MenuItem;
043     private JRadioButtonMenuItem jdk14MenuItem;
044     private JRadioButtonMenuItem jdk15MenuItem;  //NOPMD
045     private JRadioButtonMenuItem jdk16MenuItem;
046     private JRadioButtonMenuItem jdk17MenuItem;
047 
048     /**
049      * constructs and shows the frame
050      */
051     public MainFrame() {
052         super(NLS.nls("MAIN.FRAME.TITLE"" (v " + PMD.VERSION + ')');
053         init();
054     }
055 
056     private void init() {
057         model = new ViewerModel();
058         model.addViewerModelListener(this);
059         sourcePanel = new SourceCodePanel(model);
060         ASTPanel astPanel = new ASTPanel(model);
061         xPathPanel = new XPathPanel(model);
062         getContentPane().setLayout(new BorderLayout());
063         JSplitPane editingPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel);
064         editingPane.setResizeWeight(0.5d);
065         JPanel interactionsPane = new JPanel(new BorderLayout());
066         interactionsPane.add(xPathPanel, BorderLayout.SOUTH);
067         interactionsPane.add(editingPane, BorderLayout.CENTER);
068         getContentPane().add(interactionsPane, BorderLayout.CENTER);
069         JButton compileBtn = new JButton(NLS.nls("MAIN.FRAME.COMPILE_BUTTON.TITLE"));
070         compileBtn.setActionCommand(ActionCommands.COMPILE_ACTION);
071         compileBtn.addActionListener(this);
072         evalBtn = new JButton(NLS.nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE"));
073         evalBtn.setActionCommand(ActionCommands.EVALUATE_ACTION);
074         evalBtn.addActionListener(this);
075         evalBtn.setEnabled(false);
076         statusLbl = new JLabel();
077         statusLbl.setHorizontalAlignment(SwingConstants.RIGHT);
078         JPanel btnPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
079         btnPane.add(compileBtn);
080         btnPane.add(evalBtn);
081         btnPane.add(statusLbl);
082         getContentPane().add(btnPane, BorderLayout.SOUTH);
083 
084         JMenuBar menuBar = new JMenuBar();
085         JMenu menu = new JMenu("JDK");
086         ButtonGroup group = new ButtonGroup();
087         jdk13MenuItem = new JRadioButtonMenuItem("JDK 1.3");
088         jdk13MenuItem.setSelected(false);
089         group.add(jdk13MenuItem);
090         menu.add(jdk13MenuItem);
091         jdk14MenuItem = new JRadioButtonMenuItem("JDK 1.4");
092         jdk14MenuItem.setSelected(true);
093         group.add(jdk14MenuItem);
094         menu.add(jdk14MenuItem);
095         jdk15MenuItem = new JRadioButtonMenuItem("JDK 1.5");
096         jdk15MenuItem.setSelected(false);
097         group.add(jdk15MenuItem);
098         menu.add(jdk15MenuItem);
099         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(800600);
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 }