KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > verifier > VerifierAppFrame


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.verifier;
18
19 import java.awt.AWTEvent JavaDoc;
20 import java.awt.CardLayout JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.GridLayout JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.WindowEvent JavaDoc;
26 import javax.swing.BorderFactory JavaDoc;
27 import javax.swing.JFrame JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.JMenu JavaDoc;
30 import javax.swing.JMenuBar JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
32 import javax.swing.JOptionPane JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JScrollPane JavaDoc;
35 import javax.swing.JSplitPane JavaDoc;
36 import javax.swing.JTextPane JavaDoc;
37 import javax.swing.ListSelectionModel JavaDoc;
38 import javax.swing.event.ListSelectionEvent JavaDoc;
39 import org.apache.bcel.Repository;
40 import org.apache.bcel.classfile.JavaClass;
41
42 /**
43  * This class implements a machine-generated frame for use with
44  * the GraphicalVerfifier.
45  *
46  * @version $Id: VerifierAppFrame.java 386056 2006-03-15 11:31:56Z tcurdt $
47  * @author Enver Haase
48  * @see GraphicalVerifier
49  */

50 public class VerifierAppFrame extends JFrame JavaDoc {
51
52     JPanel JavaDoc contentPane;
53     JSplitPane JavaDoc jSplitPane1 = new JSplitPane JavaDoc();
54     JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
55     JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
56     JSplitPane JavaDoc jSplitPane2 = new JSplitPane JavaDoc();
57     JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
58     JList JavaDoc classNamesJList = new JList JavaDoc();
59     GridLayout JavaDoc gridLayout1 = new GridLayout JavaDoc();
60     JPanel JavaDoc messagesPanel = new JPanel JavaDoc();
61     GridLayout JavaDoc gridLayout2 = new GridLayout JavaDoc();
62     JMenuBar JavaDoc jMenuBar1 = new JMenuBar JavaDoc();
63     JMenu JavaDoc jMenu1 = new JMenu JavaDoc();
64     JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
65     JScrollPane JavaDoc messagesScrollPane = new JScrollPane JavaDoc();
66     JScrollPane JavaDoc jScrollPane3 = new JScrollPane JavaDoc();
67     GridLayout JavaDoc gridLayout4 = new GridLayout JavaDoc();
68     JScrollPane JavaDoc jScrollPane4 = new JScrollPane JavaDoc();
69     CardLayout JavaDoc cardLayout1 = new CardLayout JavaDoc();
70     private String JavaDoc JUSTICE_VERSION = "JustIce by Enver Haase";
71     private String JavaDoc current_class;
72     GridLayout JavaDoc gridLayout3 = new GridLayout JavaDoc();
73     JTextPane JavaDoc pass1TextPane = new JTextPane JavaDoc();
74     JTextPane JavaDoc pass2TextPane = new JTextPane JavaDoc();
75     JTextPane JavaDoc messagesTextPane = new JTextPane JavaDoc();
76     JMenuItem JavaDoc newFileMenuItem = new JMenuItem JavaDoc();
77     JSplitPane JavaDoc jSplitPane3 = new JSplitPane JavaDoc();
78     JSplitPane JavaDoc jSplitPane4 = new JSplitPane JavaDoc();
79     JScrollPane JavaDoc jScrollPane2 = new JScrollPane JavaDoc();
80     JScrollPane JavaDoc jScrollPane5 = new JScrollPane JavaDoc();
81     JScrollPane JavaDoc jScrollPane6 = new JScrollPane JavaDoc();
82     JScrollPane JavaDoc jScrollPane7 = new JScrollPane JavaDoc();
83     JList JavaDoc pass3aJList = new JList JavaDoc();
84     JList JavaDoc pass3bJList = new JList JavaDoc();
85     JTextPane JavaDoc pass3aTextPane = new JTextPane JavaDoc();
86     JTextPane JavaDoc pass3bTextPane = new JTextPane JavaDoc();
87     JMenu JavaDoc jMenu2 = new JMenu JavaDoc();
88     JMenuItem JavaDoc whatisMenuItem = new JMenuItem JavaDoc();
89     JMenuItem JavaDoc aboutMenuItem = new JMenuItem JavaDoc();
90
91
92     /** Constructor. */
93     public VerifierAppFrame() {
94         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
95         try {
96             jbInit();
97         } catch (Exception JavaDoc e) {
98             e.printStackTrace();
99         }
100     }
101
102
103     /** Initizalization of the components. */
104     private void jbInit() throws Exception JavaDoc {
105         //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Ihr Symbol]")));
106
contentPane = (JPanel JavaDoc) this.getContentPane();
107         contentPane.setLayout(cardLayout1);
108         this.setJMenuBar(jMenuBar1);
109         this.setSize(new Dimension JavaDoc(708, 451));
110         this.setTitle("JustIce");
111         jPanel1.setMinimumSize(new Dimension JavaDoc(100, 100));
112         jPanel1.setPreferredSize(new Dimension JavaDoc(100, 100));
113         jPanel1.setLayout(gridLayout1);
114         jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
115         jPanel2.setLayout(gridLayout2);
116         jPanel3.setMinimumSize(new Dimension JavaDoc(200, 100));
117         jPanel3.setPreferredSize(new Dimension JavaDoc(400, 400));
118         jPanel3.setLayout(gridLayout4);
119         messagesPanel.setMinimumSize(new Dimension JavaDoc(100, 100));
120         messagesPanel.setLayout(gridLayout3);
121         jPanel2.setMinimumSize(new Dimension JavaDoc(200, 100));
122         jMenu1.setText("File");
123         jScrollPane1.getViewport().setBackground(Color.red);
124         messagesScrollPane.getViewport().setBackground(Color.red);
125         messagesScrollPane.setPreferredSize(new Dimension JavaDoc(10, 10));
126         classNamesJList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
127
128             public void valueChanged( ListSelectionEvent JavaDoc e ) {
129                 classNamesJList_valueChanged(e);
130             }
131         });
132         classNamesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
133         jScrollPane3.setBorder(BorderFactory.createLineBorder(Color.black));
134         jScrollPane3.setPreferredSize(new Dimension JavaDoc(100, 100));
135         gridLayout4.setRows(4);
136         gridLayout4.setColumns(1);
137         gridLayout4.setHgap(1);
138         jScrollPane4.setBorder(BorderFactory.createLineBorder(Color.black));
139         jScrollPane4.setPreferredSize(new Dimension JavaDoc(100, 100));
140         pass1TextPane.setBorder(BorderFactory.createRaisedBevelBorder());
141         pass1TextPane.setToolTipText("");
142         pass1TextPane.setEditable(false);
143         pass2TextPane.setBorder(BorderFactory.createRaisedBevelBorder());
144         pass2TextPane.setEditable(false);
145         messagesTextPane.setBorder(BorderFactory.createRaisedBevelBorder());
146         messagesTextPane.setEditable(false);
147         newFileMenuItem.setText("New...");
148         newFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(78,
149                 java.awt.event.KeyEvent.CTRL_MASK, true));
150         newFileMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
151
152             public void actionPerformed( ActionEvent JavaDoc e ) {
153                 newFileMenuItem_actionPerformed(e);
154             }
155         });
156         pass3aTextPane.setEditable(false);
157         pass3bTextPane.setEditable(false);
158         pass3aJList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
159
160             public void valueChanged( ListSelectionEvent JavaDoc e ) {
161                 pass3aJList_valueChanged(e);
162             }
163         });
164         pass3bJList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
165
166             public void valueChanged( ListSelectionEvent JavaDoc e ) {
167                 pass3bJList_valueChanged(e);
168             }
169         });
170         jMenu2.setText("Help");
171         whatisMenuItem.setText("What is...");
172         whatisMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
173
174             public void actionPerformed( ActionEvent JavaDoc e ) {
175                 whatisMenuItem_actionPerformed(e);
176             }
177         });
178         aboutMenuItem.setText("About");
179         aboutMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
180
181             public void actionPerformed( ActionEvent JavaDoc e ) {
182                 aboutMenuItem_actionPerformed(e);
183             }
184         });
185         jSplitPane2.add(messagesPanel, JSplitPane.BOTTOM);
186         messagesPanel.add(messagesScrollPane, null);
187         messagesScrollPane.getViewport().add(messagesTextPane, null);
188         jSplitPane2.add(jPanel3, JSplitPane.TOP);
189         jPanel3.add(jScrollPane3, null);
190         jScrollPane3.getViewport().add(pass1TextPane, null);
191         jPanel3.add(jScrollPane4, null);
192         jPanel3.add(jSplitPane3, null);
193         jSplitPane3.add(jScrollPane2, JSplitPane.LEFT);
194         jScrollPane2.getViewport().add(pass3aJList, null);
195         jSplitPane3.add(jScrollPane5, JSplitPane.RIGHT);
196         jScrollPane5.getViewport().add(pass3aTextPane, null);
197         jPanel3.add(jSplitPane4, null);
198         jSplitPane4.add(jScrollPane6, JSplitPane.LEFT);
199         jScrollPane6.getViewport().add(pass3bJList, null);
200         jSplitPane4.add(jScrollPane7, JSplitPane.RIGHT);
201         jScrollPane7.getViewport().add(pass3bTextPane, null);
202         jScrollPane4.getViewport().add(pass2TextPane, null);
203         jSplitPane1.add(jPanel2, JSplitPane.TOP);
204         jPanel2.add(jScrollPane1, null);
205         jSplitPane1.add(jPanel1, JSplitPane.BOTTOM);
206         jPanel1.add(jSplitPane2, null);
207         jScrollPane1.getViewport().add(classNamesJList, null);
208         jMenuBar1.add(jMenu1);
209         jMenuBar1.add(jMenu2);
210         contentPane.add(jSplitPane1, "jSplitPane1");
211         jMenu1.add(newFileMenuItem);
212         jMenu2.add(whatisMenuItem);
213         jMenu2.add(aboutMenuItem);
214         jSplitPane2.setDividerLocation(300);
215         jSplitPane3.setDividerLocation(150);
216         jSplitPane4.setDividerLocation(150);
217     }
218
219
220     /** Overridden to stop the application on a closing window. */
221     protected void processWindowEvent( WindowEvent JavaDoc e ) {
222         super.processWindowEvent(e);
223         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
224             System.exit(0);
225         }
226     }
227
228
229     synchronized void classNamesJList_valueChanged( ListSelectionEvent JavaDoc e ) {
230         if (e.getValueIsAdjusting()) {
231             return;
232         }
233         current_class = classNamesJList.getSelectedValue().toString();
234         try {
235             verify();
236         } catch (ClassNotFoundException JavaDoc ex) {
237             // FIXME: report the error using the GUI
238
ex.printStackTrace();
239         }
240         classNamesJList.setSelectedValue(current_class, true);
241     }
242
243
244     private void verify() throws ClassNotFoundException JavaDoc {
245         setTitle("PLEASE WAIT");
246         Verifier v = VerifierFactory.getVerifier(current_class);
247         v.flush(); // Don't cache the verification result for this class.
248
VerificationResult vr;
249         vr = v.doPass1();
250         if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
251             pass1TextPane.setText(vr.getMessage());
252             pass1TextPane.setBackground(Color.red);
253             pass2TextPane.setText("");
254             pass2TextPane.setBackground(Color.yellow);
255             pass3aTextPane.setText("");
256             pass3aJList.setListData(new Object JavaDoc[0]);
257             pass3aTextPane.setBackground(Color.yellow);
258             pass3bTextPane.setText("");
259             pass3bJList.setListData(new Object JavaDoc[0]);
260             pass3bTextPane.setBackground(Color.yellow);
261         } else { // Must be VERIFIED_OK, Pass 1 does not know VERIFIED_NOTYET
262
pass1TextPane.setBackground(Color.green);
263             pass1TextPane.setText(vr.getMessage());
264             vr = v.doPass2();
265             if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
266                 pass2TextPane.setText(vr.getMessage());
267                 pass2TextPane.setBackground(Color.red);
268                 pass3aTextPane.setText("");
269                 pass3aTextPane.setBackground(Color.yellow);
270                 pass3aJList.setListData(new Object JavaDoc[0]);
271                 pass3bTextPane.setText("");
272                 pass3bTextPane.setBackground(Color.yellow);
273                 pass3bJList.setListData(new Object JavaDoc[0]);
274             } else { // must be Verified_OK, because Pass1 was OK (cannot be Verified_NOTYET).
275
pass2TextPane.setText(vr.getMessage());
276                 pass2TextPane.setBackground(Color.green);
277                 JavaClass jc = Repository.lookupClass(current_class);
278                 /*
279                  boolean all3aok = true;
280                  boolean all3bok = true;
281                  String all3amsg = "";
282                  String all3bmsg = "";
283                  */

284                 String JavaDoc[] methodnames = new String JavaDoc[jc.getMethods().length];
285                 for (int i = 0; i < jc.getMethods().length; i++) {
286                     methodnames[i] = jc.getMethods()[i].toString().replace('\n', ' ').replace('\t',
287                             ' ');
288                 }
289                 pass3aJList.setListData(methodnames);
290                 pass3aJList.setSelectionInterval(0, jc.getMethods().length - 1);
291                 pass3bJList.setListData(methodnames);
292                 pass3bJList.setSelectionInterval(0, jc.getMethods().length - 1);
293             }
294         }
295         String JavaDoc[] msgs = v.getMessages();
296         messagesTextPane.setBackground(msgs.length == 0 ? Color.green : Color.yellow);
297         String JavaDoc allmsgs = "";
298         for (int i = 0; i < msgs.length; i++) {
299             msgs[i] = msgs[i].replace('\n', ' ');
300             allmsgs += msgs[i] + "\n\n";
301         }
302         messagesTextPane.setText(allmsgs);
303         setTitle(current_class + " - " + JUSTICE_VERSION);
304     }
305
306
307     void newFileMenuItem_actionPerformed( ActionEvent JavaDoc e ) {
308         String JavaDoc classname = JOptionPane
309                 .showInputDialog("Please enter the fully qualified name of a class or interface to verify:");
310         if ((classname == null) || (classname.equals(""))) {
311             return;
312         }
313         VerifierFactory.getVerifier(classname); // let observers do the rest.
314
classNamesJList.setSelectedValue(classname, true);
315     }
316
317
318     synchronized void pass3aJList_valueChanged( ListSelectionEvent JavaDoc e ) {
319         if (e.getValueIsAdjusting()) {
320             return;
321         }
322         Verifier v = VerifierFactory.getVerifier(current_class);
323         String JavaDoc all3amsg = "";
324         boolean all3aok = true;
325         boolean rejected = false;
326         for (int i = 0; i < pass3aJList.getModel().getSize(); i++) {
327             if (pass3aJList.isSelectedIndex(i)) {
328                 VerificationResult vr = v.doPass3a(i);
329                 if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
330                     all3aok = false;
331                     rejected = true;
332                 }
333                 JavaClass jc = null;
334                 try {
335                     jc = Repository.lookupClass(v.getClassName());
336                     all3amsg += "Method '" + jc.getMethods()[i] + "': "
337                             + vr.getMessage().replace('\n', ' ') + "\n\n";
338                 } catch (ClassNotFoundException JavaDoc ex) {
339                     // FIXME: handle the error
340
ex.printStackTrace();
341                 }
342             }
343         }
344         pass3aTextPane.setText(all3amsg);
345         pass3aTextPane.setBackground(all3aok ? Color.green : (rejected ? Color.red : Color.yellow));
346     }
347
348
349     synchronized void pass3bJList_valueChanged( ListSelectionEvent JavaDoc e ) {
350         if (e.getValueIsAdjusting()) {
351             return;
352         }
353         Verifier v = VerifierFactory.getVerifier(current_class);
354         String JavaDoc all3bmsg = "";
355         boolean all3bok = true;
356         boolean rejected = false;
357         for (int i = 0; i < pass3bJList.getModel().getSize(); i++) {
358             if (pass3bJList.isSelectedIndex(i)) {
359                 VerificationResult vr = v.doPass3b(i);
360                 if (vr.getStatus() == VerificationResult.VERIFIED_REJECTED) {
361                     all3bok = false;
362                     rejected = true;
363                 }
364                 JavaClass jc = null;
365                 try {
366                     jc = Repository.lookupClass(v.getClassName());
367                     all3bmsg += "Method '" + jc.getMethods()[i] + "': "
368                             + vr.getMessage().replace('\n', ' ') + "\n\n";
369                 } catch (ClassNotFoundException JavaDoc ex) {
370                     // FIXME: handle the error
371
ex.printStackTrace();
372                 }
373             }
374         }
375         pass3bTextPane.setText(all3bmsg);
376         pass3bTextPane.setBackground(all3bok ? Color.green : (rejected ? Color.red : Color.yellow));
377     }
378
379
380     void aboutMenuItem_actionPerformed( ActionEvent JavaDoc e ) {
381         JOptionPane
382                 .showMessageDialog(
383                         this,
384                         "JustIce is a Java class file verifier.\nIt was implemented by Enver Haase in 2001, 2002.\n<http://jakarta.apache.org/bcel/index.html>",
385                         JUSTICE_VERSION, JOptionPane.INFORMATION_MESSAGE);
386     }
387
388
389     void whatisMenuItem_actionPerformed( ActionEvent JavaDoc e ) {
390         JOptionPane
391                 .showMessageDialog(
392                         this,
393                         "The upper four boxes to the right reflect verification passes according to The Java Virtual Machine Specification.\nThese are (in that order): Pass one, Pass two, Pass three (before data flow analysis), Pass three (data flow analysis).\nThe bottom box to the right shows (warning) messages; warnings do not cause a class to be rejected.",
394                         JUSTICE_VERSION, JOptionPane.INFORMATION_MESSAGE);
395     }
396 }
397
Popular Tags