001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Chris Aniszczyk <zx@us.ibm.com> - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.runtime.spy.sections;
011:
012: import java.lang.reflect.Field;
013: import java.util.Iterator;
014: import java.util.LinkedHashSet;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.eclipse.core.commands.ExecutionEvent;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.osgi.util.NLS;
021: import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
022: import org.eclipse.pde.internal.runtime.PDERuntimePluginImages;
023: import org.eclipse.pde.internal.runtime.spy.SpyFormToolkit;
024: import org.eclipse.swt.graphics.Image;
025: import org.eclipse.ui.IEditorPart;
026: import org.eclipse.ui.IWorkbenchPart;
027: import org.eclipse.ui.IWorkbenchWindow;
028: import org.eclipse.ui.forms.widgets.ExpandableComposite;
029: import org.eclipse.ui.forms.widgets.FormText;
030: import org.eclipse.ui.forms.widgets.ScrolledForm;
031: import org.eclipse.ui.forms.widgets.Section;
032: import org.eclipse.ui.forms.widgets.TableWrapData;
033: import org.eclipse.ui.handlers.HandlerUtil;
034: import org.eclipse.ui.internal.PartSite;
035: import org.eclipse.ui.internal.PopupMenuExtender;
036: import org.osgi.framework.Bundle;
037:
038: public class ActivePartSection implements ISpySection {
039:
040: public void build(ScrolledForm form, SpyFormToolkit toolkit,
041: ExecutionEvent event) {
042: IWorkbenchWindow window = HandlerUtil
043: .getActiveWorkbenchWindow(event);
044: if (window == null) // if we don't have an active workbench, we don't have a valid selection to analyze
045: return;
046:
047: final IWorkbenchPart part = HandlerUtil.getActivePart(event);
048: String partType = part instanceof IEditorPart ? "editor" : "view"; //$NON-NLS-1$ //$NON-NLS-2$
049: Section section = toolkit.createSection(form.getBody(),
050: ExpandableComposite.TITLE_BAR);
051:
052: section.setText(NLS.bind(
053: PDERuntimeMessages.SpyDialog_activePart_title, part
054: .getSite().getRegisteredName()));
055:
056: FormText text = toolkit.createFormText(section, true);
057: section.setClient(text);
058: TableWrapData td = new TableWrapData();
059: td.align = TableWrapData.FILL;
060: td.grabHorizontal = true;
061: section.setLayoutData(td);
062:
063: StringBuffer buffer = new StringBuffer();
064: buffer.append("<form>"); //$NON-NLS-1$
065:
066: // time to analyze the active part
067: buffer.append(toolkit.createClassSection(text,
068: NLS.bind(PDERuntimeMessages.SpyDialog_activePart_desc,
069: partType), new Class[] { part.getClass() }));
070:
071: // time to analyze the contributing plug-in
072: final Bundle bundle = Platform.getBundle(part.getSite()
073: .getPluginId());
074:
075: toolkit.generatePluginDetailsText(bundle, part.getSite()
076: .getId(), partType, buffer, text);
077:
078: // get menu information using reflection
079: try {
080: PartSite site = (PartSite) part.getSite();
081: Class clazz = site.getClass().getSuperclass();
082: Field field = clazz.getDeclaredField("menuExtenders"); //$NON-NLS-1$
083: field.setAccessible(true);
084: List list = (List) field.get(site);
085: if (list != null && list.size() > 0) {
086: Set menuIds = new LinkedHashSet();
087: for (int i = 0; i < list.size(); i++) {
088: PopupMenuExtender extender = (PopupMenuExtender) list
089: .get(i);
090: menuIds.addAll(extender.getMenuIds());
091: }
092: buffer.append("<p>"); //$NON-NLS-1$
093: buffer
094: .append(PDERuntimeMessages.SpyDialog_activeMenuIds);
095: buffer.append("</p>"); //$NON-NLS-1$
096: for (Iterator it = menuIds.iterator(); it.hasNext();) {
097: buffer
098: .append("<li bindent=\"20\" style=\"image\" value=\"menu\">"); //$NON-NLS-1$
099: buffer.append(it.next().toString());
100: buffer.append("</li>"); //$NON-NLS-1$
101: }
102: Image menuImage = PDERuntimePluginImages
103: .get(PDERuntimePluginImages.IMG_MENU_OBJ);
104: text.setImage("menu", menuImage); //$NON-NLS-1$
105: }
106: } catch (SecurityException e) {
107: e.printStackTrace();
108: } catch (NoSuchFieldException e) {
109: e.printStackTrace();
110: } catch (IllegalArgumentException e) {
111: e.printStackTrace();
112: } catch (IllegalAccessException e) {
113: e.printStackTrace();
114: }
115:
116: buffer.append("</form>"); //$NON-NLS-1$
117:
118: Image idImage = PDERuntimePluginImages
119: .get(PDERuntimePluginImages.IMG_ID_OBJ);
120: text.setImage("id", idImage); //$NON-NLS-1$
121:
122: text.setText(buffer.toString(), true, false);
123: }
124:
125: }
|