1 |
| package net.sourceforge.pmd.util.viewer.gui; |
2 |
| |
3 |
| import net.sourceforge.pmd.ast.ParseException; |
4 |
| import net.sourceforge.pmd.util.viewer.model.ViewerModel; |
5 |
| import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent; |
6 |
| import net.sourceforge.pmd.util.viewer.model.ViewerModelListener; |
7 |
| import net.sourceforge.pmd.util.viewer.util.NLS; |
8 |
| |
9 |
| import javax.swing.*; |
10 |
| import java.awt.BorderLayout; |
11 |
| import java.awt.FlowLayout; |
12 |
| import java.awt.event.ActionEvent; |
13 |
| import java.awt.event.ActionListener; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class MainFrame |
24 |
| extends JFrame |
25 |
| implements ActionListener, ActionCommands, ViewerModelListener { |
26 |
| private ViewerModel model; |
27 |
| private SourceCodePanel sourcePanel; |
28 |
| private ASTPanel astPanel; |
29 |
| private XPathPanel xPathPanel; |
30 |
| private JButton compileBtn; |
31 |
| private JButton evalBtn; |
32 |
| private JLabel statusLbl; |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
0
| public MainFrame() {
|
38 |
0
| super(NLS.nls("MAIN.FRAME.TITLE"));
|
39 |
0
| init();
|
40 |
| } |
41 |
| |
42 |
0
| private void init() {
|
43 |
0
| model = new ViewerModel();
|
44 |
0
| model.addViewerModelListener(this);
|
45 |
0
| sourcePanel = new SourceCodePanel(model);
|
46 |
0
| astPanel = new ASTPanel(model);
|
47 |
0
| xPathPanel = new XPathPanel(model);
|
48 |
0
| getContentPane().setLayout(new BorderLayout());
|
49 |
0
| JSplitPane editingPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel);
|
50 |
0
| editingPane.setResizeWeight(0.5d);
|
51 |
0
| JPanel interactionsPane = new JPanel(new BorderLayout());
|
52 |
0
| interactionsPane.add(xPathPanel, BorderLayout.SOUTH);
|
53 |
0
| interactionsPane.add(editingPane, BorderLayout.CENTER);
|
54 |
0
| getContentPane().add(interactionsPane, BorderLayout.CENTER);
|
55 |
0
| compileBtn = new JButton(NLS.nls("MAIN.FRAME.COMPILE_BUTTON.TITLE"));
|
56 |
0
| compileBtn.setActionCommand(COMPILE_ACTION);
|
57 |
0
| compileBtn.addActionListener(this);
|
58 |
0
| evalBtn = new JButton(NLS.nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE"));
|
59 |
0
| evalBtn.setActionCommand(EVALUATE_ACTION);
|
60 |
0
| evalBtn.addActionListener(this);
|
61 |
0
| evalBtn.setEnabled(false);
|
62 |
0
| statusLbl = new JLabel();
|
63 |
0
| statusLbl.setHorizontalAlignment(SwingConstants.RIGHT);
|
64 |
0
| JPanel btnPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
65 |
0
| btnPane.add(compileBtn);
|
66 |
0
| btnPane.add(evalBtn);
|
67 |
0
| btnPane.add(statusLbl);
|
68 |
0
| getContentPane().add(btnPane, BorderLayout.SOUTH);
|
69 |
0
| setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
70 |
0
| pack();
|
71 |
0
| setSize(800, 600);
|
72 |
0
| setVisible(true);
|
73 |
| } |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
0
| public void actionPerformed(ActionEvent e) {
|
79 |
0
| String command = e.getActionCommand();
|
80 |
0
| long t0, t1;
|
81 |
0
| if (command.equals(COMPILE_ACTION)) {
|
82 |
0
| try {
|
83 |
0
| t0 = System.currentTimeMillis();
|
84 |
0
| model.commitSource(sourcePanel.getSourceCode());
|
85 |
0
| t1 = System.currentTimeMillis();
|
86 |
0
| setStatus(NLS.nls("MAIN.FRAME.COMPILATION.TOOK") + " " + (t1 - t0) + " ms");
|
87 |
| } catch (ParseException exc) { |
88 |
0
| setStatus(NLS.nls("MAIN.FRAME.COMPILATION.PROBLEM") + " " + exc.toString());
|
89 |
0
| new ParseExceptionHandler(this, exc);
|
90 |
| } |
91 |
0
| } else if (command.equals(EVALUATE_ACTION)) {
|
92 |
0
| try {
|
93 |
0
| t0 = System.currentTimeMillis();
|
94 |
0
| model.evaluateXPathExpression(xPathPanel.getXPathExpression(), this);
|
95 |
0
| t1 = System.currentTimeMillis();
|
96 |
0
| setStatus(NLS.nls("MAIN.FRAME.EVALUATION.TOOK") + " " + (t1 - t0) + " ms");
|
97 |
| } catch (Exception exc) { |
98 |
0
| setStatus(NLS.nls("MAIN.FRAME.EVALUATION.PROBLEM") + " " + exc.toString());
|
99 |
0
| new ParseExceptionHandler(this, exc);
|
100 |
| } |
101 |
| } |
102 |
| } |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
0
| private void setStatus(String string) {
|
110 |
0
| if (string == null)
|
111 |
0
| string = "";
|
112 |
0
| statusLbl.setText(string);
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
0
| public void viewerModelChanged(ViewerModelEvent e) {
|
119 |
0
| evalBtn.setEnabled(model.hasCompiledTree());
|
120 |
| } |
121 |
| } |