1 5 package org.terracotta.dso.views; 6 7 import org.eclipse.core.resources.IFile; 8 import org.eclipse.core.resources.IProject; 9 import org.eclipse.core.resources.IResource; 10 import org.eclipse.core.resources.IResourceChangeEvent; 11 import org.eclipse.core.resources.IResourceChangeListener; 12 import org.eclipse.core.resources.IResourceDelta; 13 import org.eclipse.core.resources.IResourceDeltaVisitor; 14 import org.eclipse.core.resources.ResourcesPlugin; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.jdt.core.IJavaProject; 18 import org.eclipse.jdt.core.IType; 19 import org.eclipse.jdt.core.JavaCore; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.internal.ui.JavaPluginImages; 22 import org.eclipse.jdt.launching.JavaRuntime; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.IStructuredContentProvider; 26 import org.eclipse.jface.viewers.ITableLabelProvider; 27 import org.eclipse.jface.viewers.LabelProvider; 28 import org.eclipse.jface.viewers.SelectionChangedEvent; 29 import org.eclipse.jface.viewers.StructuredSelection; 30 import org.eclipse.jface.viewers.TableViewer; 31 import org.eclipse.jface.viewers.Viewer; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.graphics.Image; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.ui.IPartListener; 36 import org.eclipse.ui.IWorkbenchPart; 37 import org.eclipse.ui.IWorkbenchWindow; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.part.ViewPart; 40 import org.terracotta.dso.BootJarHelper; 41 import org.terracotta.dso.JdtUtils; 42 import org.terracotta.dso.TcPlugin; 43 import org.terracotta.dso.actions.ActionUtil; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 import java.util.ArrayList ; 48 import java.util.Collections ; 49 import java.util.Enumeration ; 50 import java.util.zip.ZipEntry ; 51 import java.util.zip.ZipFile ; 52 53 public class BootJarView extends ViewPart 54 implements IResourceChangeListener, 55 IResourceDeltaVisitor, 56 ISelectionChangedListener, 57 IPartListener 58 { 59 public static final String ID_BOOTJAR_VIEW_PART = "org.terracotta.dso.ui.views.bootJarView"; 60 61 private TableViewer viewer; 62 private IFile bootJarFile; 63 private IJavaProject lastJavaProject; 64 private static final String EMPTY_CONTENT_ELEM = "No boot JAR found."; 65 private static final Object [] EMPTY_CONTENT = {EMPTY_CONTENT_ELEM}; 66 67 public BootJarView() { 68 super(); 69 bootJarFile = getBootJarFile(); 70 } 71 72 public void createPartControl(Composite parent) { 73 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 74 viewer.setContentProvider(new ViewContentProvider()); 75 viewer.setLabelProvider(new ViewLabelProvider()); 76 viewer.setInput(getViewSite()); 77 viewer.addSelectionChangedListener(this); 78 getSite().getPage().addPartListener(this); 79 ResourcesPlugin.getWorkspace().addResourceChangeListener(this); 80 } 81 82 class ViewContentProvider implements IStructuredContentProvider { 83 public void inputChanged(Viewer v, Object oldInput, Object newInput) {} 84 85 public void dispose() {} 86 87 public Object [] getElements(Object parent) { 88 ArrayList <String > list = new ArrayList <String >(); 89 90 if(bootJarFile != null && !bootJarFile.isSynchronized(IResource.DEPTH_ZERO)) { 91 try { 92 bootJarFile.refreshLocal(IResource.DEPTH_ZERO, null); 93 } catch(CoreException ce) {} 94 } 95 96 String fileName = null; 97 String tip = ""; 98 String desc = ""; 99 100 if(bootJarFile == null || 101 !bootJarFile.exists() || 102 !bootJarFile.getProject().isOpen() || 103 !TcPlugin.getDefault().hasTerracottaNature(bootJarFile.getProject())) 104 { 105 fileName = getDefaultBootJarPath(); 106 tip = fileName; 107 desc = "System bootjar: " + new File(fileName).getName(); 108 } else { 109 fileName = bootJarFile.getLocation().toOSString(); 110 tip = desc = bootJarFile.getProject().getName() + IPath.SEPARATOR + bootJarFile.getName(); 111 } 112 113 File file = new File(fileName); 114 setTitleToolTip(tip); 115 setContentDescription(desc); 116 117 if(fileName == null || !file.exists()) { 118 return EMPTY_CONTENT; 119 } 120 121 ZipFile zipFile = null; 122 try { 123 zipFile = new ZipFile (fileName); 124 Enumeration entries = zipFile.entries(); 125 while(entries.hasMoreElements()) { 126 ZipEntry e = (ZipEntry )entries.nextElement(); 127 String name = e.getName(); 128 129 if(name.endsWith(".class") && !name.startsWith("com/tc")) { 130 name = name.substring(0, name.lastIndexOf('.')).replace('/', '.').replace('$', '.'); 131 list.add(name); 132 } 133 } 134 } catch(IOException ioe) { 135 ioe.printStackTrace(); 136 } finally { 137 if(zipFile != null) { 138 try { 139 zipFile.close(); 140 } catch(IOException ioe) {} 141 } 142 } 143 144 Collections.sort(list); 145 146 return list.toArray(); 147 } 148 } 149 150 class ViewLabelProvider extends LabelProvider implements 151 ITableLabelProvider { 152 public String getColumnText(Object obj, int index) { 153 return getText(obj); 154 } 155 156 public Image getColumnImage(Object obj, int index) { 157 return getImage(obj); 158 } 159 160 public Image getImage(Object obj) { 161 String imgName = (obj != EMPTY_CONTENT_ELEM) ? JavaPluginImages.IMG_OBJS_CLASS : JavaPluginImages.IMG_OBJS_REFACTORING_ERROR; 162 return JavaPluginImages.get(imgName); 163 } 164 } 165 166 public void setFocus() { 167 viewer.getControl().setFocus(); 168 } 169 170 public IFile getBootJarFile() { 171 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 172 173 if(window != null) { 174 ISelection selection = window.getSelectionService().getSelection(); 175 176 if(selection != null) { 177 IJavaProject javaProject = ActionUtil.locateSelectedJavaProject(selection); 178 179 if(javaProject != null) { 180 IProject project = javaProject.getProject(); 181 IFile localBootJar = project.getFile(safeGetInstallBootJarName(javaProject)); 182 183 if(localBootJar != null) { 184 lastJavaProject = javaProject; 185 return localBootJar; 186 } 187 } 188 } 189 } 190 191 return null; 192 } 193 194 private static String getDefaultBootJarPath() { 195 String bootJarName = safeGetInstallBootJarName(); 196 197 if(bootJarName != null) { 198 IPath path = BootJarHelper.getHelper().getBootJarPath(bootJarName); 199 if(path != null) { 200 return path.toOSString(); 201 } 202 } 203 204 return null; 205 } 206 207 private static String safeGetInstallBootJarName(IJavaProject javaProject) { 208 try { 209 String portablePath = null; 210 IPath jrePath = JavaRuntime.computeJREEntry(javaProject).getPath(); 211 if(jrePath != null) { 212 portablePath = jrePath.toPortableString(); 213 } 214 return BootJarHelper.getHelper().getBootJarName(portablePath); 215 } catch(CoreException ce) { 216 ce.printStackTrace(); 217 return null; 218 } 219 } 220 221 private static String safeGetInstallBootJarName() { 222 try { 223 return BootJarHelper.getHelper().getBootJarName(); 224 } catch(CoreException ce) { 225 ce.printStackTrace(); 226 return null; 227 } 228 } 229 230 public boolean visit(IResourceDelta delta) { 231 if(viewer == null || 232 viewer.getTable().isDisposed() || 233 PlatformUI.getWorkbench().isClosing()) { 234 return false; 235 } 236 237 IResource res = delta.getResource(); 238 if(res instanceof IProject) { 239 IProject project = (IProject)res; 240 241 if((delta.getKind() == IResourceDelta.ADDED && 242 TcPlugin.getDefault().hasTerracottaNature(project)) || 243 (delta.getFlags() & IResourceDelta.DESCRIPTION) != 0) 244 { 245 reset(); 246 return false; 247 } 248 } 249 250 if(isAffected(delta)) { 251 reset(); 252 return false; 253 } 254 255 return true; 256 } 257 258 private boolean isAffected(IResourceDelta delta) { 259 IResource res = delta.getResource(); 260 261 if(res instanceof IFile) { 262 if(res.equals(bootJarFile)) { 263 return true; 264 } 265 } 266 267 IResourceDelta[] children = delta.getAffectedChildren(); 268 for(int i = 0; i < children.length; i++) { 269 if(isAffected(children[i])) { 270 return true; 271 } 272 } 273 274 return false; 275 } 276 277 public void resourceChanged(final IResourceChangeEvent event){ 278 int type = event.getType(); 279 280 if(type == IResourceChangeEvent.PRE_DELETE || 281 type == IResourceChangeEvent.PRE_CLOSE) 282 { 283 getSite().getShell().getDisplay().asyncExec(new Runnable () { 284 public void run(){ 285 clear(); 286 } 287 }); 288 } else if(type == IResourceChangeEvent.POST_CHANGE) { 289 getSite().getShell().getDisplay().asyncExec(new Runnable () { 290 public void run(){ 291 try { 292 bootJarFile = getBootJarFile(); 293 event.getDelta().accept(BootJarView.this); 294 } catch(CoreException ce) { 295 ce.printStackTrace(); 296 } 297 } 298 }); 299 } 300 } 301 302 public void selectionChanged(SelectionChangedEvent event) { 303 ISelection sel = event.getSelection(); 304 305 if(!sel.isEmpty() && bootJarFile != null) { 306 if(sel instanceof StructuredSelection) { 307 StructuredSelection ss = (StructuredSelection)sel; 308 309 if(ss.size() == 1) { 310 Object obj = ss.getFirstElement(); 311 312 if(obj instanceof String ) { 313 String typeName = (String )obj; 314 try { 315 IJavaProject javaProject = JavaCore.create(bootJarFile.getProject()); 316 IType type = JdtUtils.findType(javaProject, typeName); 317 if(type != null) { 318 ConfigUI.jumpToMember(type); 319 } 320 } catch(JavaModelException jme) { 321 jme.printStackTrace(); 322 } 323 } 324 } 325 } 326 } 327 } 328 329 public void partActivated(IWorkbenchPart part) { 330 if(part != this) { 331 IWorkbenchWindow window = part.getSite().getWorkbenchWindow(); 332 333 if(window != null) { 334 ISelection selection = window.getSelectionService().getSelection(); 335 336 if(selection != null) { 337 IJavaProject javaProject = ActionUtil.locateSelectedJavaProject(selection); 338 339 if(javaProject != null && !javaProject.equals(lastJavaProject)) { 340 bootJarFile = getBootJarFile(); 341 reset(); 342 } 343 } 344 } 345 } 346 } 347 348 public void partBroughtToTop(IWorkbenchPart part) {} 349 public void partClosed(IWorkbenchPart part) {} 350 public void partDeactivated(IWorkbenchPart part) {} 351 public void partOpened(IWorkbenchPart part) {} 352 353 void reset() { 354 viewer.setContentProvider(new ViewContentProvider()); 355 viewer.setInput(getViewSite()); 356 } 357 358 void clear() { 359 bootJarFile = null; 360 reset(); 361 } 362 363 public void dispose() { 364 viewer.removeSelectionChangedListener(this); 365 getSite().getPage().removePartListener(this); 366 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 367 368 super.dispose(); 369 } 370 } 371
| Popular Tags
|