001: package net.sourceforge.pmd.util.viewer.gui;
002:
003: import net.sourceforge.pmd.PMD;
004: import net.sourceforge.pmd.TargetJDK1_3;
005: import net.sourceforge.pmd.TargetJDK1_4;
006: import net.sourceforge.pmd.TargetJDK1_5;
007: import net.sourceforge.pmd.TargetJDK1_6;
008: import net.sourceforge.pmd.TargetJDK1_7;
009: import net.sourceforge.pmd.TargetJDKVersion;
010: import net.sourceforge.pmd.ast.ParseException;
011: import net.sourceforge.pmd.util.viewer.model.ViewerModel;
012: import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
013: import net.sourceforge.pmd.util.viewer.model.ViewerModelListener;
014: import net.sourceforge.pmd.util.viewer.util.NLS;
015:
016: import javax.swing.*;
017: import java.awt.BorderLayout;
018: import java.awt.FlowLayout;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021:
022: /**
023: * viewer's main frame
024: *
025: * @author Boris Gruschko ( boris at gruschko.org )
026: * @version $Id: MainFrame.java 5874 2008-03-07 01:59:30Z xlv $
027: */
028:
029: public class MainFrame extends JFrame implements ActionListener,
030: ActionCommands, ViewerModelListener {
031: private ViewerModel model;
032: private SourceCodePanel sourcePanel;
033: private XPathPanel xPathPanel;
034: private JButton evalBtn;
035: private JLabel statusLbl;
036: private JRadioButtonMenuItem jdk13MenuItem;
037: private JRadioButtonMenuItem jdk14MenuItem;
038: private JRadioButtonMenuItem jdk15MenuItem; //NOPMD
039: private JRadioButtonMenuItem jdk16MenuItem;
040: private JRadioButtonMenuItem jdk17MenuItem;
041:
042: /**
043: * constructs and shows the frame
044: */
045: public MainFrame() {
046: super (NLS.nls("MAIN.FRAME.TITLE") + " (v " + PMD.VERSION + ')');
047: init();
048: }
049:
050: private void init() {
051: model = new ViewerModel();
052: model.addViewerModelListener(this );
053: sourcePanel = new SourceCodePanel(model);
054: ASTPanel astPanel = new ASTPanel(model);
055: xPathPanel = new XPathPanel(model);
056: getContentPane().setLayout(new BorderLayout());
057: JSplitPane editingPane = new JSplitPane(
058: JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel);
059: editingPane.setResizeWeight(0.5d);
060: JPanel interactionsPane = new JPanel(new BorderLayout());
061: interactionsPane.add(xPathPanel, BorderLayout.SOUTH);
062: interactionsPane.add(editingPane, BorderLayout.CENTER);
063: getContentPane().add(interactionsPane, BorderLayout.CENTER);
064: JButton compileBtn = new JButton(NLS
065: .nls("MAIN.FRAME.COMPILE_BUTTON.TITLE"));
066: compileBtn.setActionCommand(COMPILE_ACTION);
067: compileBtn.addActionListener(this );
068: evalBtn = new JButton(NLS
069: .nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE"));
070: evalBtn.setActionCommand(EVALUATE_ACTION);
071: evalBtn.addActionListener(this );
072: evalBtn.setEnabled(false);
073: statusLbl = new JLabel();
074: statusLbl.setHorizontalAlignment(SwingConstants.RIGHT);
075: JPanel btnPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
076: btnPane.add(compileBtn);
077: btnPane.add(evalBtn);
078: btnPane.add(statusLbl);
079: getContentPane().add(btnPane, BorderLayout.SOUTH);
080:
081: JMenuBar menuBar = new JMenuBar();
082: JMenu menu = new JMenu("JDK");
083: ButtonGroup group = new ButtonGroup();
084: jdk13MenuItem = new JRadioButtonMenuItem("JDK 1.3");
085: jdk13MenuItem.setSelected(false);
086: group.add(jdk13MenuItem);
087: menu.add(jdk13MenuItem);
088: jdk14MenuItem = new JRadioButtonMenuItem("JDK 1.4");
089: jdk14MenuItem.setSelected(true);
090: group.add(jdk14MenuItem);
091: menu.add(jdk14MenuItem);
092: jdk15MenuItem = new JRadioButtonMenuItem("JDK 1.5");
093: jdk15MenuItem.setSelected(false);
094: group.add(jdk15MenuItem);
095: menu.add(jdk15MenuItem);
096: jdk16MenuItem = new JRadioButtonMenuItem("JDK 1.6");
097: jdk16MenuItem.setSelected(false);
098: group.add(jdk16MenuItem);
099: menu.add(jdk16MenuItem);
100: jdk17MenuItem = new JRadioButtonMenuItem("JDK 1.7");
101: jdk17MenuItem.setSelected(false);
102: group.add(jdk17MenuItem);
103: menu.add(jdk17MenuItem);
104: menuBar.add(menu);
105: setJMenuBar(menuBar);
106:
107: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
108: pack();
109: setSize(800, 600);
110: setVisible(true);
111: }
112:
113: private TargetJDKVersion createJDKVersion() {
114: if (jdk14MenuItem.isSelected()) {
115: return new TargetJDK1_4();
116: } else if (jdk13MenuItem.isSelected()) {
117: return new TargetJDK1_3();
118: } else if (jdk16MenuItem.isSelected()) {
119: return new TargetJDK1_6();
120: } else if (jdk17MenuItem.isSelected()) {
121: return new TargetJDK1_7();
122: }
123: return new TargetJDK1_5();
124: }
125:
126: /**
127: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
128: */
129: public void actionPerformed(ActionEvent e) {
130: String command = e.getActionCommand();
131: long t0, t1;
132: if (command.equals(COMPILE_ACTION)) {
133: try {
134: t0 = System.currentTimeMillis();
135: model.commitSource(sourcePanel.getSourceCode(),
136: createJDKVersion());
137: t1 = System.currentTimeMillis();
138: setStatus(NLS.nls("MAIN.FRAME.COMPILATION.TOOK") + " "
139: + (t1 - t0) + " ms");
140: } catch (ParseException exc) {
141: setStatus(NLS.nls("MAIN.FRAME.COMPILATION.PROBLEM")
142: + " " + exc.toString());
143: new ParseExceptionHandler(this , exc);
144: }
145: } else if (command.equals(EVALUATE_ACTION)) {
146: try {
147: t0 = System.currentTimeMillis();
148: model.evaluateXPathExpression(xPathPanel
149: .getXPathExpression(), this );
150: t1 = System.currentTimeMillis();
151: setStatus(NLS.nls("MAIN.FRAME.EVALUATION.TOOK") + " "
152: + (t1 - t0) + " ms");
153: } catch (Exception exc) {
154: setStatus(NLS.nls("MAIN.FRAME.EVALUATION.PROBLEM")
155: + " " + exc.toString());
156: new ParseExceptionHandler(this , exc);
157: }
158: }
159: }
160:
161: /**
162: * Sets the status bar message
163: *
164: * @param string the new status, the empty string will be set if the value is <code>null</code>
165: */
166: private void setStatus(String string) {
167: statusLbl.setText(string == null ? "" : string);
168: }
169:
170: /**
171: * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
172: */
173: public void viewerModelChanged(ViewerModelEvent e) {
174: evalBtn.setEnabled(model.hasCompiledTree());
175: }
176: }
|