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 org.eclipse.core.commands.ExecutionEvent;
013: import org.eclipse.jface.dialogs.Dialog;
014: import org.eclipse.jface.preference.IPreferencePage;
015: import org.eclipse.jface.preference.PreferenceDialog;
016: import org.eclipse.pde.internal.runtime.spy.SpyFormToolkit;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.IEditorPart;
021: import org.eclipse.ui.IEditorReference;
022: import org.eclipse.ui.IViewReference;
023: import org.eclipse.ui.IWorkbenchPart;
024: import org.eclipse.ui.IWorkbenchWindow;
025: import org.eclipse.ui.forms.widgets.ExpandableComposite;
026: import org.eclipse.ui.forms.widgets.FormText;
027: import org.eclipse.ui.forms.widgets.ScrolledForm;
028: import org.eclipse.ui.forms.widgets.Section;
029: import org.eclipse.ui.forms.widgets.TableWrapData;
030: import org.eclipse.ui.handlers.HandlerUtil;
031: import org.eclipse.ui.internal.WorkbenchPartReference;
032: import org.eclipse.ui.part.ViewPart;
033:
034: public class ActiveHelpSection implements ISpySection {
035:
036: private static String HELP_KEY = "org.eclipse.ui.help"; //$NON-NLS-1$
037:
038: public void build(ScrolledForm form, SpyFormToolkit toolkit,
039: ExecutionEvent event) {
040: final Shell shell = HandlerUtil.getActiveShell(event);
041: Object object = shell.getData();
042: if (object == null)
043: return;
044:
045: StringBuffer helpBuffer = new StringBuffer();
046: // process help
047: if (object instanceof PreferenceDialog) {
048: PreferenceDialog dialog = (PreferenceDialog) object;
049: IPreferencePage page = (IPreferencePage) dialog
050: .getSelectedPage();
051: processChildren(page.getControl().getShell(), toolkit,
052: helpBuffer);
053: } else if (object instanceof Dialog) {
054: Dialog dialog = (Dialog) object;
055: processChildren(dialog.getShell(), toolkit, helpBuffer);
056: } else {
057: helpBuffer.append(processControlHelp(event, toolkit));
058: }
059:
060: // ensure we actually have help
061: // TODO we need to make this cleaner... help processing is complicated atm
062: if (helpBuffer != null && helpBuffer.length() > 0) {
063: Section section = toolkit.createSection(form.getBody(),
064: ExpandableComposite.TITLE_BAR);
065: section.setText("Active Help"); //$NON-NLS-1$
066:
067: FormText text = toolkit.createFormText(section, true);
068: section.setClient(text);
069: TableWrapData td = new TableWrapData();
070: td.align = TableWrapData.FILL;
071: td.grabHorizontal = true;
072: section.setLayoutData(td);
073:
074: StringBuffer buffer = new StringBuffer();
075: buffer.append("<form>"); //$NON-NLS-1$
076: buffer.append(helpBuffer.toString());
077: buffer.append("</form>"); //$NON-NLS-1$
078: text.setText(buffer.toString(), true, false);
079: }
080:
081: }
082:
083: private void processChildren(Control control,
084: SpyFormToolkit toolkit, StringBuffer buffer) {
085: if (control.getData(HELP_KEY) != null) {
086: buffer
087: .append("<li bindent=\"20\">" + control.toString() + " - " + control.getData(HELP_KEY) + "</li>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
088: }
089: if (control instanceof Composite) {
090: Composite composite = (Composite) control;
091: Control[] controls = composite.getChildren();
092: for (int i = 0; i < controls.length; i++) {
093: processChildren(controls[i], toolkit, buffer);
094: }
095: }
096: }
097:
098: private String processControlHelp(ExecutionEvent event,
099: SpyFormToolkit toolkit) {
100: IWorkbenchPart part = HandlerUtil.getActivePart(event);
101: if (part == null)
102: return null;
103:
104: IWorkbenchWindow window = part.getSite().getWorkbenchWindow();
105: if (window == null)
106: return null;
107:
108: StringBuffer buffer = new StringBuffer();
109:
110: Shell shell = null;
111: Control control = null;
112:
113: if (part instanceof IEditorPart) {
114: IEditorPart editorPart = (IEditorPart) part;
115: shell = editorPart.getSite().getShell();
116:
117: for (int j = 0; j < window.getActivePage()
118: .getEditorReferences().length; j++) {
119: IEditorReference er = window.getActivePage()
120: .getEditorReferences()[j];
121: if (er.getId().equals(
122: editorPart.getEditorSite().getId()))
123: if (er instanceof WorkbenchPartReference) {
124: WorkbenchPartReference wpr = (WorkbenchPartReference) er;
125: control = wpr.getPane().getControl();
126: shell = null;
127: break;
128: }
129: }
130: } else if (part instanceof ViewPart) {
131: ViewPart viewPart = (ViewPart) part;
132: shell = viewPart.getSite().getShell();
133: for (int j = 0; j < window.getActivePage()
134: .getViewReferences().length; j++) {
135: IViewReference vr = window.getActivePage()
136: .getViewReferences()[j];
137: if (vr.getId().equals(viewPart.getViewSite().getId()))
138: if (vr instanceof WorkbenchPartReference) {
139: WorkbenchPartReference wpr = (WorkbenchPartReference) vr;
140: control = wpr.getPane().getControl();
141: shell = null;
142: break;
143: }
144: }
145:
146: }
147: if (shell != null) {
148: if (shell.getData(HELP_KEY) != null) {
149: buffer
150: .append("<li bindent=\"20\">" + shell.toString() + " - " + shell.getData(HELP_KEY) + "</li>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
151: }
152: for (int i = 0; i < shell.getChildren().length; i++) {
153: processChildren(shell.getChildren()[i], toolkit, buffer);
154: }
155: } else if (control != null) {
156: if (control.getData(HELP_KEY) != null) {
157: buffer
158: .append("<li bindent=\"20\">" + control.toString() + " - " + control.getData(HELP_KEY) + "</li>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
159: }
160: if (control instanceof Composite) {
161: Composite parent = (Composite) control;
162: for (int i = 0; i < parent.getChildren().length; i++) {
163: processChildren(parent.getChildren()[i], toolkit,
164: buffer);
165: }
166: }
167: }
168: return buffer.toString();
169: }
170:
171: }
|