1 4 5 package de.loskutov.bco.views; 6 7 import java.net.URL ; 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 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 BaseHelpSystem.ensureWebappRunning(); 43 } 44 45 public void dispose() { 46 getSite().getWorkbenchWindow().getPartService().removePartListener(this); 47 super.dispose(); 48 } 49 50 public void setFocus() { 51 52 } 53 54 public void partActivated(IWorkbenchPartReference partRef) { 55 } 57 58 public void partBroughtToTop(IWorkbenchPartReference partRef) { 59 } 61 62 public void partClosed(IWorkbenchPartReference partRef) { 63 } 65 66 public void partDeactivated(IWorkbenchPartReference partRef) { 67 } 69 70 public void partOpened(IWorkbenchPartReference partRef) { 71 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 partId = BytecodeOutlineView.class.getName(); 89 selectionService.addPostSelectionListener(partId, this); 90 91 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 } 106 107 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 108 if(!(part instanceof BytecodeOutlineView)){ 109 return; 110 } 111 int line = -1; 112 String 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 ){ 119 line = ((Integer )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 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 checkOpcodeName(String opcodeName) { 147 opcodeName = opcodeName.toLowerCase(); 148 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 opcodeName += "_n"; 168 break; 169 } 170 } 171 return opcodeName; 172 } 173 174 private URL getHelpResource(String name) { 175 try { 176 String host = WebappManager.getHost(); 178 int port = WebappManager.getPort(); 179 String href = "/" 180 + BytecodeOutlinePlugin.getDefault().getBundle() 181 .getSymbolicName() + "/doc/ref-" + name + ".html"; 182 return new URL ("http://" + host + ":" + port + "/help/nftopic" 183 + href); 184 188 } catch (Exception e) { 189 return null; 190 } 191 } 192 } 193 194 | Popular Tags |