001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.views;
006:
007: import org.eclipse.core.resources.IFile;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.core.resources.IResource;
010: import org.eclipse.core.resources.IResourceChangeEvent;
011: import org.eclipse.core.resources.IResourceChangeListener;
012: import org.eclipse.core.resources.IResourceDelta;
013: import org.eclipse.core.resources.IResourceDeltaVisitor;
014: import org.eclipse.core.resources.ResourcesPlugin;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.jdt.core.IJavaProject;
018: import org.eclipse.jdt.core.IType;
019: import org.eclipse.jdt.core.JavaCore;
020: import org.eclipse.jdt.core.JavaModelException;
021: import org.eclipse.jdt.internal.ui.JavaPluginImages;
022: import org.eclipse.jdt.launching.JavaRuntime;
023: import org.eclipse.jface.viewers.ISelection;
024: import org.eclipse.jface.viewers.ISelectionChangedListener;
025: import org.eclipse.jface.viewers.IStructuredContentProvider;
026: import org.eclipse.jface.viewers.ITableLabelProvider;
027: import org.eclipse.jface.viewers.LabelProvider;
028: import org.eclipse.jface.viewers.SelectionChangedEvent;
029: import org.eclipse.jface.viewers.StructuredSelection;
030: import org.eclipse.jface.viewers.TableViewer;
031: import org.eclipse.jface.viewers.Viewer;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.graphics.Image;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.ui.IPartListener;
036: import org.eclipse.ui.IWorkbenchPart;
037: import org.eclipse.ui.IWorkbenchWindow;
038: import org.eclipse.ui.PlatformUI;
039: import org.eclipse.ui.part.ViewPart;
040: import org.terracotta.dso.BootJarHelper;
041: import org.terracotta.dso.JdtUtils;
042: import org.terracotta.dso.TcPlugin;
043: import org.terracotta.dso.actions.ActionUtil;
044:
045: import java.io.File;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.Enumeration;
050: import java.util.zip.ZipEntry;
051: import java.util.zip.ZipFile;
052:
053: public class BootJarView extends ViewPart implements
054: IResourceChangeListener, IResourceDeltaVisitor,
055: ISelectionChangedListener, IPartListener {
056: public static final String ID_BOOTJAR_VIEW_PART = "org.terracotta.dso.ui.views.bootJarView";
057:
058: private TableViewer viewer;
059: private IFile bootJarFile;
060: private IJavaProject lastJavaProject;
061: private static final String EMPTY_CONTENT_ELEM = "No boot JAR found.";
062: private static final Object[] EMPTY_CONTENT = { EMPTY_CONTENT_ELEM };
063:
064: public BootJarView() {
065: super ();
066: bootJarFile = getBootJarFile();
067: }
068:
069: public void createPartControl(Composite parent) {
070: viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
071: | SWT.V_SCROLL);
072: viewer.setContentProvider(new ViewContentProvider());
073: viewer.setLabelProvider(new ViewLabelProvider());
074: viewer.setInput(getViewSite());
075: viewer.addSelectionChangedListener(this );
076: getSite().getPage().addPartListener(this );
077: ResourcesPlugin.getWorkspace().addResourceChangeListener(this );
078: }
079:
080: class ViewContentProvider implements IStructuredContentProvider {
081: public void inputChanged(Viewer v, Object oldInput,
082: Object newInput) {/**/
083: }
084:
085: public void dispose() {/**/
086: }
087:
088: public Object[] getElements(Object parent) {
089: ArrayList<String> list = new ArrayList<String>();
090:
091: if (bootJarFile != null
092: && !bootJarFile
093: .isSynchronized(IResource.DEPTH_ZERO)) {
094: try {
095: bootJarFile
096: .refreshLocal(IResource.DEPTH_ZERO, null);
097: } catch (CoreException ce) {/**/
098: }
099: }
100:
101: String fileName = null;
102: String tip = "";
103: String desc = "";
104:
105: if (bootJarFile == null
106: || !bootJarFile.exists()
107: || !bootJarFile.getProject().isOpen()
108: || !TcPlugin.getDefault().hasTerracottaNature(
109: bootJarFile.getProject())) {
110: fileName = getDefaultBootJarPath();
111: tip = fileName;
112: desc = "System bootjar: "
113: + new File(fileName).getName();
114: } else {
115: fileName = bootJarFile.getLocation().toOSString();
116: tip = desc = bootJarFile.getProject().getName()
117: + IPath.SEPARATOR + bootJarFile.getName();
118: }
119:
120: File file = new File(fileName);
121: setTitleToolTip(tip);
122: setContentDescription(desc);
123:
124: if (fileName == null || !file.exists()) {
125: return EMPTY_CONTENT;
126: }
127:
128: ZipFile zipFile = null;
129: try {
130: zipFile = new ZipFile(fileName);
131: Enumeration entries = zipFile.entries();
132: while (entries.hasMoreElements()) {
133: ZipEntry e = (ZipEntry) entries.nextElement();
134: String name = e.getName();
135:
136: if (name.endsWith(".class")
137: && !name.startsWith("com/tc")) {
138: name = name.substring(0, name.lastIndexOf('.'))
139: .replace('/', '.').replace('$', '.');
140: list.add(name);
141: }
142: }
143: } catch (IOException ioe) {
144: ioe.printStackTrace();
145: } finally {
146: if (zipFile != null) {
147: try {
148: zipFile.close();
149: } catch (IOException ioe) {/**/
150: }
151: }
152: }
153:
154: Collections.sort(list);
155:
156: return list.toArray();
157: }
158: }
159:
160: static class ViewLabelProvider extends LabelProvider implements
161: ITableLabelProvider {
162: public String getColumnText(Object obj, int index) {
163: return getText(obj);
164: }
165:
166: public Image getColumnImage(Object obj, int index) {
167: return getImage(obj);
168: }
169:
170: public Image getImage(Object obj) {
171: String imgName = (obj != EMPTY_CONTENT_ELEM) ? JavaPluginImages.IMG_OBJS_CLASS
172: : JavaPluginImages.IMG_OBJS_REFACTORING_ERROR;
173: return JavaPluginImages.get(imgName);
174: }
175: }
176:
177: public void setFocus() {
178: viewer.getControl().setFocus();
179: }
180:
181: public IFile getBootJarFile() {
182: IWorkbenchWindow window = PlatformUI.getWorkbench()
183: .getActiveWorkbenchWindow();
184:
185: if (window != null) {
186: ISelection selection = window.getSelectionService()
187: .getSelection();
188:
189: if (selection != null) {
190: IJavaProject javaProject = ActionUtil
191: .locateSelectedJavaProject(selection);
192:
193: if (javaProject != null) {
194: IProject project = javaProject.getProject();
195: IFile localBootJar = project
196: .getFile(safeGetInstallBootJarName(javaProject));
197:
198: if (localBootJar != null) {
199: lastJavaProject = javaProject;
200: return localBootJar;
201: }
202: }
203: }
204: }
205:
206: return null;
207: }
208:
209: private static String getDefaultBootJarPath() {
210: String bootJarName = safeGetInstallBootJarName();
211:
212: if (bootJarName != null) {
213: IPath path = BootJarHelper.getHelper().getBootJarPath(
214: bootJarName);
215: if (path != null) {
216: return path.toOSString();
217: }
218: }
219:
220: return null;
221: }
222:
223: private static String safeGetInstallBootJarName(
224: IJavaProject javaProject) {
225: try {
226: String portablePath = null;
227: IPath jrePath = JavaRuntime.computeJREEntry(javaProject)
228: .getPath();
229: if (jrePath != null) {
230: portablePath = jrePath.toPortableString();
231: }
232: return BootJarHelper.getHelper().getBootJarName(
233: portablePath);
234: } catch (CoreException ce) {
235: ce.printStackTrace();
236: return null;
237: }
238: }
239:
240: private static String safeGetInstallBootJarName() {
241: try {
242: return BootJarHelper.getHelper().getBootJarName();
243: } catch (CoreException ce) {
244: ce.printStackTrace();
245: return null;
246: }
247: }
248:
249: public boolean visit(IResourceDelta delta) {
250: if (viewer == null || viewer.getTable().isDisposed()
251: || PlatformUI.getWorkbench().isClosing()) {
252: return false;
253: }
254:
255: IResource res = delta.getResource();
256: if (res instanceof IProject) {
257: IProject project = (IProject) res;
258:
259: if ((delta.getKind() == IResourceDelta.ADDED && TcPlugin
260: .getDefault().hasTerracottaNature(project))
261: || (delta.getFlags() & IResourceDelta.DESCRIPTION) != 0) {
262: reset();
263: return false;
264: }
265: }
266:
267: if (isAffected(delta)) {
268: reset();
269: return false;
270: }
271:
272: return true;
273: }
274:
275: private boolean isAffected(IResourceDelta delta) {
276: IResource res = delta.getResource();
277:
278: if (res instanceof IFile) {
279: if (res.equals(bootJarFile)) {
280: return true;
281: }
282: }
283:
284: IResourceDelta[] children = delta.getAffectedChildren();
285: for (int i = 0; i < children.length; i++) {
286: if (isAffected(children[i])) {
287: return true;
288: }
289: }
290:
291: return false;
292: }
293:
294: public void resourceChanged(final IResourceChangeEvent event) {
295: int type = event.getType();
296:
297: if (type == IResourceChangeEvent.PRE_DELETE
298: || type == IResourceChangeEvent.PRE_CLOSE) {
299: getSite().getShell().getDisplay().asyncExec(new Runnable() {
300: public void run() {
301: clear();
302: }
303: });
304: } else if (type == IResourceChangeEvent.POST_CHANGE) {
305: getSite().getShell().getDisplay().asyncExec(new Runnable() {
306: public void run() {
307: try {
308: bootJarFile = getBootJarFile();
309: event.getDelta().accept(BootJarView.this );
310: } catch (CoreException ce) {
311: ce.printStackTrace();
312: }
313: }
314: });
315: }
316: }
317:
318: public void selectionChanged(SelectionChangedEvent event) {
319: ISelection sel = event.getSelection();
320:
321: if (!sel.isEmpty() && bootJarFile != null) {
322: if (sel instanceof StructuredSelection) {
323: StructuredSelection ss = (StructuredSelection) sel;
324:
325: if (ss.size() == 1) {
326: Object obj = ss.getFirstElement();
327:
328: if (obj instanceof String) {
329: String typeName = (String) obj;
330: try {
331: IJavaProject javaProject = JavaCore
332: .create(bootJarFile.getProject());
333: IType type = JdtUtils.findType(javaProject,
334: typeName);
335: if (type != null) {
336: ConfigUI.jumpToMember(type);
337: }
338: } catch (JavaModelException jme) {
339: jme.printStackTrace();
340: }
341: }
342: }
343: }
344: }
345: }
346:
347: public void partActivated(IWorkbenchPart part) {
348: if (part != this ) {
349: IWorkbenchWindow window = part.getSite()
350: .getWorkbenchWindow();
351:
352: if (window != null) {
353: ISelection selection = window.getSelectionService()
354: .getSelection();
355:
356: if (selection != null) {
357: IJavaProject javaProject = ActionUtil
358: .locateSelectedJavaProject(selection);
359:
360: if (javaProject != null
361: && !javaProject.equals(lastJavaProject)) {
362: bootJarFile = getBootJarFile();
363: reset();
364: }
365: }
366: }
367: }
368: }
369:
370: public void partBroughtToTop(IWorkbenchPart part) {/**/
371: }
372:
373: public void partClosed(IWorkbenchPart part) {/**/
374: }
375:
376: public void partDeactivated(IWorkbenchPart part) {/**/
377: }
378:
379: public void partOpened(IWorkbenchPart part) {/**/
380: }
381:
382: void reset() {
383: viewer.setContentProvider(new ViewContentProvider());
384: viewer.setInput(getViewSite());
385: }
386:
387: void clear() {
388: bootJarFile = null;
389: reset();
390: }
391:
392: public void dispose() {
393: viewer.removeSelectionChangedListener(this);
394: getSite().getPage().removePartListener(this);
395: ResourcesPlugin.getWorkspace().removeResourceChangeListener(
396: this);
397:
398: super.dispose();
399: }
400: }
|