KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > viewer > gui > MainFrame


1 package net.sourceforge.pmd.util.viewer.gui;
2
3 import net.sourceforge.pmd.TargetJDK1_3;
4 import net.sourceforge.pmd.TargetJDK1_4;
5 import net.sourceforge.pmd.TargetJDK1_5;
6 import net.sourceforge.pmd.TargetJDK1_6;
7 import net.sourceforge.pmd.TargetJDKVersion;
8 import net.sourceforge.pmd.ast.ParseException;
9 import net.sourceforge.pmd.util.viewer.model.ViewerModel;
10 import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
11 import net.sourceforge.pmd.util.viewer.model.ViewerModelListener;
12 import net.sourceforge.pmd.util.viewer.util.NLS;
13
14 import javax.swing.*;
15 import java.awt.BorderLayout JavaDoc;
16 import java.awt.FlowLayout JavaDoc;
17 import java.awt.event.ActionEvent JavaDoc;
18 import java.awt.event.ActionListener JavaDoc;
19
20
21 /**
22  * viewer's main frame
23  *
24  * @author Boris Gruschko ( boris at gruschko.org )
25  * @version $Id: MainFrame.java,v 1.14 2006/10/21 01:30:38 xlv Exp $
26  */

27
28 public class MainFrame
29         extends JFrame
30         implements ActionListener JavaDoc, ActionCommands, ViewerModelListener {
31     private ViewerModel model;
32     private SourceCodePanel sourcePanel;
33     private ASTPanel astPanel;
34     private XPathPanel xPathPanel;
35     private JButton compileBtn;
36     private JButton evalBtn;
37     private JLabel statusLbl;
38     private JRadioButtonMenuItem jdk13MenuItem;
39     private JRadioButtonMenuItem jdk14MenuItem;
40     private JRadioButtonMenuItem jdk15MenuItem;
41     private JRadioButtonMenuItem jdk16MenuItem;
42
43     /**
44      * constructs and shows the frame
45      */

46     public MainFrame() {
47         super(NLS.nls("MAIN.FRAME.TITLE"));
48         init();
49     }
50
51     private void init() {
52         model = new ViewerModel();
53         model.addViewerModelListener(this);
54         sourcePanel = new SourceCodePanel(model);
55         astPanel = new ASTPanel(model);
56         xPathPanel = new XPathPanel(model);
57         getContentPane().setLayout(new BorderLayout JavaDoc());
58         JSplitPane editingPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel);
59         editingPane.setResizeWeight(0.5d);
60         JPanel interactionsPane = new JPanel(new BorderLayout JavaDoc());
61         interactionsPane.add(xPathPanel, BorderLayout.SOUTH);
62         interactionsPane.add(editingPane, BorderLayout.CENTER);
63         getContentPane().add(interactionsPane, BorderLayout.CENTER);
64         compileBtn = new JButton(NLS.nls("MAIN.FRAME.COMPILE_BUTTON.TITLE"));
65         compileBtn.setActionCommand(COMPILE_ACTION);
66         compileBtn.addActionListener(this);
67         evalBtn = new JButton(NLS.nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE"));
68         evalBtn.setActionCommand(EVALUATE_ACTION);
69         evalBtn.addActionListener(this);
70         evalBtn.setEnabled(false);
71         statusLbl = new JLabel();
72         statusLbl.setHorizontalAlignment(SwingConstants.RIGHT);
73         JPanel btnPane = new JPanel(new FlowLayout JavaDoc(FlowLayout.LEFT));
74         btnPane.add(compileBtn);
75         btnPane.add(evalBtn);
76         btnPane.add(statusLbl);
77         getContentPane().add(btnPane, BorderLayout.SOUTH);
78
79         JMenuBar menuBar = new JMenuBar();
80         JMenu menu = new JMenu("JDK");
81         ButtonGroup group = new ButtonGroup();
82         jdk13MenuItem = new JRadioButtonMenuItem("JDK 1.3");
83         jdk13MenuItem.setSelected(false);
84         group.add(jdk13MenuItem);
85         menu.add(jdk13MenuItem);
86         jdk14MenuItem = new JRadioButtonMenuItem("JDK 1.4");
87         jdk14MenuItem.setSelected(true);
88         group.add(jdk14MenuItem);
89         menu.add(jdk14MenuItem);
90         jdk15MenuItem = new JRadioButtonMenuItem("JDK 1.5");
91         jdk15MenuItem.setSelected(false);
92         group.add(jdk15MenuItem);
93         menu.add(jdk15MenuItem);
94         jdk16MenuItem = new JRadioButtonMenuItem("JDK 1.6");
95         jdk16MenuItem.setSelected(false);
96         group.add(jdk16MenuItem);
97         menu.add(jdk16MenuItem);
98         menuBar.add(menu);
99         setJMenuBar(menuBar);
100
101         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
102         pack();
103         setSize(800, 600);
104         setVisible(true);
105     }
106
107     private TargetJDKVersion createJDKVersion() {
108         if (jdk14MenuItem.isSelected()) {
109             return new TargetJDK1_4();
110         } else if (jdk13MenuItem.isSelected()) {
111             return new TargetJDK1_3();
112         } else if (jdk16MenuItem.isSelected()) {
113             return new TargetJDK1_6();
114         }
115         return new TargetJDK1_5();
116     }
117
118     /**
119      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
120      */

121     public void actionPerformed(ActionEvent JavaDoc e) {
122         String JavaDoc command = e.getActionCommand();
123         long t0, t1;
124         if (command.equals(COMPILE_ACTION)) {
125             try {
126                 t0 = System.currentTimeMillis();
127                 model.commitSource(sourcePanel.getSourceCode(), createJDKVersion());
128                 t1 = System.currentTimeMillis();
129                 setStatus(NLS.nls("MAIN.FRAME.COMPILATION.TOOK") + " " + (t1 - t0) + " ms");
130             } catch (ParseException exc) {
131                 setStatus(NLS.nls("MAIN.FRAME.COMPILATION.PROBLEM") + " " + exc.toString());
132                 new ParseExceptionHandler(this, exc);
133             }
134         } else if (command.equals(EVALUATE_ACTION)) {
135             try {
136                 t0 = System.currentTimeMillis();
137                 model.evaluateXPathExpression(xPathPanel.getXPathExpression(), this);
138                 t1 = System.currentTimeMillis();
139                 setStatus(NLS.nls("MAIN.FRAME.EVALUATION.TOOK") + " " + (t1 - t0) + " ms");
140             } catch (Exception JavaDoc exc) {
141                 setStatus(NLS.nls("MAIN.FRAME.EVALUATION.PROBLEM") + " " + exc.toString());
142                 new ParseExceptionHandler(this, exc);
143             }
144         }
145     }
146
147     /**
148      * Sets the status bar message
149      *
150      * @param string the new status, the empty string will be set if the value is <code>null</code>
151      */

152     private void setStatus(String JavaDoc string) {
153         statusLbl.setText(string == null ? "" : string);
154     }
155
156     /**
157      * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
158      */

159     public void viewerModelChanged(ViewerModelEvent e) {
160         evalBtn.setEnabled(model.hasCompiledTree());
161     }
162 }
163
Popular Tags