KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > views > BytecodeReferenceView


1 /*
2  * Copyright area
3  */

4
5 package de.loskutov.bco.views;
6
7 import java.net.URL JavaDoc;
8
9 import org.eclipse.help.internal.appserver.WebappManager;
10 import org.eclipse.help.internal.base.BaseHelpSystem;
11 import org.eclipse.jface.text.ITextSelection;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.browser.Browser;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.ui.IPartListener2;
18 import org.eclipse.ui.ISelectionListener;
19 import org.eclipse.ui.ISelectionService;
20 import org.eclipse.ui.IViewReference;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.IWorkbenchPartReference;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.part.ViewPart;
25 import org.objectweb.asm.util.AbstractVisitor;
26
27 import de.loskutov.bco.BytecodeOutlinePlugin;
28
29
30 public class BytecodeReferenceView extends ViewPart implements IPartListener2, ISelectionListener {
31
32     private static final String JavaDoc NLS_PREFIX = "BytecodeReferenceView.";
33     private Browser browser;
34
35     public void createPartControl(Composite parent) {
36         browser = new Browser(parent, SWT.BORDER);
37         browser.setText(BytecodeOutlinePlugin.getResourceString(NLS_PREFIX
38             + "empty.selection.text"));
39         getSite().getWorkbenchWindow().getPartService().addPartListener(this);
40
41         // TODO run this in background!
42
BaseHelpSystem.ensureWebappRunning();
43     }
44
45     public void dispose() {
46         getSite().getWorkbenchWindow().getPartService().removePartListener(this);
47         super.dispose();
48     }
49
50     public void setFocus() {
51         /* nothing to do */
52     }
53
54     public void partActivated(IWorkbenchPartReference partRef) {
55         //
56
}
57
58     public void partBroughtToTop(IWorkbenchPartReference partRef) {
59         //
60
}
61
62     public void partClosed(IWorkbenchPartReference partRef) {
63         //
64
}
65
66     public void partDeactivated(IWorkbenchPartReference partRef) {
67         //
68
}
69
70     public void partOpened(IWorkbenchPartReference partRef) {
71         // WORKAROUND - sometimes Eclipse does not invoke partVisible(),
72
// but only partOpened()...
73
partVisible(partRef);
74     }
75
76     public void partHidden(IWorkbenchPartReference partRef) {
77         if (partRef.getId().equals(getSite().getId())) {
78             getSite().getWorkbenchWindow().getSelectionService()
79                 .removePostSelectionListener(BytecodeOutlineView.class.getName(), this);
80         }
81     }
82
83     public void partVisible(IWorkbenchPartReference partRef) {
84         if (partRef.getId().equals(getSite().getId())) {
85             IWorkbenchWindow workbenchWindow = getSite().getWorkbenchWindow();
86             ISelectionService selectionService = workbenchWindow
87                 .getSelectionService();
88             String JavaDoc partId = BytecodeOutlineView.class.getName();
89             selectionService.addPostSelectionListener(partId, this);
90
91             // perform initialization with already existing selection (if any)
92
ISelection selection = selectionService.getSelection(partId);
93             if(selection != null) {
94                 IViewReference viewReference = workbenchWindow.getActivePage()
95                     .findViewReference(partId);
96                 if(viewReference != null) {
97                     selectionChanged(viewReference.getView(false), selection);
98                 }
99             }
100         }
101     }
102
103     public void partInputChanged(IWorkbenchPartReference partRef) {
104         //
105
}
106
107     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
108         if(!(part instanceof BytecodeOutlineView)){
109             return;
110         }
111         int line = -1;
112         String JavaDoc opcodeName = null;
113         if (selection instanceof ITextSelection) {
114             line = ((ITextSelection)selection).getStartLine();
115         } else if(selection instanceof IStructuredSelection){
116             IStructuredSelection sselection = (IStructuredSelection) selection;
117             int size = sselection.size();
118             if(size == 1 && sselection.getFirstElement() instanceof Integer JavaDoc){
119                 line = ((Integer JavaDoc)sselection.getFirstElement()).intValue();
120             }
121         }
122
123         if(line >= 0){
124             int opcode = ((BytecodeOutlineView)part).getBytecodeInstructionAtLine(line);
125             if (opcode != -1) {
126                 opcodeName = AbstractVisitor.OPCODES[opcode];
127             }
128         }
129
130         if (opcodeName != null) {
131             opcodeName = checkOpcodeName(opcodeName);
132
133             URL JavaDoc url = getHelpResource(opcodeName);
134             if (url != null) {
135                 browser.setUrl(url.toString());
136             } else {
137                 browser.setText(BytecodeOutlinePlugin.getResourceString(NLS_PREFIX
138                     + "empty.selection.text"));
139             }
140         } else {
141             browser.setText(BytecodeOutlinePlugin.getResourceString(NLS_PREFIX
142                 + "empty.selection.text"));
143         }
144     }
145
146     private String JavaDoc checkOpcodeName(String JavaDoc opcodeName) {
147         opcodeName = opcodeName.toLowerCase();
148         /*
149          * we need an additional check for DCONST_1...5, FCONST_1...5 etc case
150          * to convert it to DCONST_D etc
151          */

152         int sepIndex = opcodeName.indexOf('_');
153         if(sepIndex > 0 && Character.isDigit(opcodeName.charAt(sepIndex + 1))){
154             opcodeName = opcodeName.substring(0, sepIndex);
155             switch(opcodeName.charAt(0)){
156                 case 'd':
157                     opcodeName += "_d";
158                     break;
159                 case 'f':
160                     opcodeName += "_f";
161                     break;
162                 case 'l':
163                     opcodeName += "_l";
164                     break;
165                 default:
166                     // ICONST uses "n"
167
opcodeName += "_n";
168                     break;
169             }
170         }
171         return opcodeName;
172     }
173
174     private URL JavaDoc getHelpResource(String JavaDoc name) {
175         try {
176             // BaseHelpSystem.resolve() method is not awailable in 3.0
177
String JavaDoc host = WebappManager.getHost();
178             int port = WebappManager.getPort();
179             String JavaDoc href = "/"
180                 + BytecodeOutlinePlugin.getDefault().getBundle()
181                     .getSymbolicName() + "/doc/ref-" + name + ".html";
182             return new URL JavaDoc("http://" + host + ":" + port + "/help/nftopic"
183                 + href);
184             // return BaseHelpSystem.resolve( href, true);
185
// return new File(
186
// BytecodeOutlinePlugin.PLUGIN_PATH+"/doc/ref-"+name.toLowerCase()+".html").toURL();
187

188         } catch (Exception JavaDoc e) {
189             return null;
190         }
191     }
192 }
193
194
Popular Tags