KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > detail > constants > ClassElementOpener


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.browser.detail.constants;
9
10 import org.gjt.jclasslib.browser.BrowserTreeNode;
11 import org.gjt.jclasslib.browser.config.window.*;
12 import org.gjt.jclasslib.structures.CPInfo;
13 import org.gjt.jclasslib.structures.InvalidByteCodeException;
14 import org.gjt.jclasslib.structures.constants.*;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.awt.event.ActionEvent JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20
21 /**
22     Component that opens named references to methods and fields.
23
24     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
25     @version $Revision: 1.1 $ $Date: 2003/08/18 08:14:43 $
26 */

27 public class ClassElementOpener implements ActionListener JavaDoc {
28
29     private JButton btnShow;
30     private CPInfo cpInfo;
31     private AbstractConstantInfoDetailPane detailPane;
32
33     /**
34      * Constructor.
35      * @param detailPane the parent detail pane.
36      */

37     public ClassElementOpener(AbstractConstantInfoDetailPane detailPane) {
38         this.detailPane = detailPane;
39
40         btnShow = new JButton("Show");
41         btnShow.addActionListener(this);
42     }
43
44     public void actionPerformed(ActionEvent JavaDoc event) {
45
46         try {
47             ConstantClassInfo classInfo = null;
48             BrowserPath browserPath = null;
49             if (cpInfo instanceof ConstantClassInfo) {
50                 classInfo = (ConstantClassInfo)cpInfo;
51             } else if (cpInfo instanceof ConstantReference) {
52                 ConstantReference reference = (ConstantReference)cpInfo;
53                 ConstantNameAndTypeInfo nameAndType = reference.getNameAndTypeInfo();
54                 classInfo = reference.getClassInfo();
55                 String JavaDoc category = null;
56                 if (cpInfo instanceof ConstantFieldrefInfo) {
57                     category = BrowserTreeNode.NODE_FIELD;
58                 } else if (cpInfo instanceof ConstantMethodrefInfo || cpInfo instanceof ConstantInterfaceMethodrefInfo){
59                     category = BrowserTreeNode.NODE_METHOD;
60                 }
61                 if (category != null) {
62                     browserPath = new BrowserPath();
63                     browserPath.addPathComponent(new CategoryHolder(category));
64                     browserPath.addPathComponent(new ReferenceHolder(nameAndType.getName(), nameAndType.getDescriptor()));
65                 }
66             }
67             if (classInfo == null) {
68                 return;
69             }
70             String JavaDoc className = classInfo.getName().replace('/', '.');
71             detailPane.getBrowserServices().openClassFile(className, browserPath);
72         } catch (InvalidByteCodeException ex) {
73             ex.printStackTrace();
74         }
75     }
76
77     /**
78      * Add an opening button to the supplied detail pane.
79      * @param detailPane the detail pane.
80      * @param gridy the current <tt>gridy</tt> of the <tt>GridBagLayout</tt> of the detail pane.
81      * @return the number of added rows.
82      */

83     public int addSpecial(AbstractConstantInfoDetailPane detailPane, int gridy) {
84
85         GridBagConstraints gc = new GridBagConstraints();
86         gc.weightx = 1;
87         gc.anchor = GridBagConstraints.WEST;
88         gc.insets = new Insets(5, 10, 0, 10);
89         gc.gridy = gridy;
90         gc.gridx = 0;
91         gc.gridwidth = 3;
92
93         detailPane.add(btnShow, gc);
94
95         return 1;
96     }
97
98     /**
99      * Set the constant pool info that is to be the source of the link.
100      * @param cpInfo the contant pool info.
101      */

102     public void setCPInfo(CPInfo cpInfo) {
103
104         this.cpInfo = cpInfo;
105
106         String JavaDoc buttonText = null;
107         if (cpInfo instanceof ConstantClassInfo) {
108             buttonText = "Show class";
109             try {
110                 if (((ConstantClassInfo)cpInfo).getName().equals(detailPane.getBrowserServices().getClassFile().getThisClassName())) {
111                     buttonText = null;
112                 }
113             } catch (InvalidByteCodeException e) {
114             }
115         } else if (cpInfo instanceof ConstantFieldrefInfo) {
116             buttonText = "Show field";
117         } else if (cpInfo instanceof ConstantMethodrefInfo) {
118             buttonText = "Show method";
119         } else if (cpInfo instanceof ConstantInterfaceMethodrefInfo) {
120             buttonText = "Show interface method";
121         }
122
123         if (buttonText != null) {
124             btnShow.setVisible(true);
125             btnShow.setText(buttonText);
126         } else {
127             btnShow.setVisible(false);
128         }
129     }
130
131 }
132
Popular Tags