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;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.osgi.util.NLS;
016: import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
017: import org.eclipse.pde.internal.runtime.PDERuntimePlugin;
018: import org.eclipse.pde.internal.runtime.PDERuntimePluginImages;
019: import org.eclipse.pde.internal.runtime.spy.dialogs.SpyDialog;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Display;
023: import org.eclipse.ui.IWorkbenchPart;
024: import org.eclipse.ui.forms.events.HyperlinkAdapter;
025: import org.eclipse.ui.forms.events.HyperlinkEvent;
026: import org.eclipse.ui.forms.widgets.FormText;
027: import org.eclipse.ui.forms.widgets.FormToolkit;
028: import org.osgi.framework.Bundle;
029:
030: public class SpyFormToolkit extends FormToolkit {
031:
032: private class SpyHyperlinkAdapter extends HyperlinkAdapter {
033:
034: private SpyDialog dialog;
035:
036: public SpyHyperlinkAdapter(SpyDialog dialog) {
037: this .dialog = dialog;
038: }
039:
040: public void linkActivated(HyperlinkEvent e) {
041: String clazz = (String) e.getHref();
042: Bundle bundle = (Bundle) bundleClassByName.get(clazz);
043: SpyIDEUtil.openClass(bundle.getSymbolicName(), clazz);
044: dialog.close();
045: }
046: }
047:
048: private Map bundleClassByName = new HashMap();
049: private SpyDialog dialog;
050:
051: public SpyFormToolkit(SpyDialog dialog) {
052: super (Display.getDefault());
053: this .dialog = dialog;
054: }
055:
056: public FormText createFormText(Composite parent, boolean trackFocus) {
057: FormText text = super .createFormText(parent, trackFocus);
058: if (PDERuntimePlugin.HAS_IDE_BUNDLES) {
059: text.addHyperlinkListener(new SpyHyperlinkAdapter(dialog));
060: }
061: return text;
062: }
063:
064: public String createInterfaceSection(FormText text, String title,
065: Class[] clazzes) {
066: StringBuffer buffer = new StringBuffer();
067: if (clazzes.length > 0) {
068: buffer.append("<p>"); //$NON-NLS-1$
069: buffer.append(title);
070: buffer.append("</p>"); //$NON-NLS-1$
071: for (int i = 0; i < clazzes.length; i++) {
072: buffer
073: .append("<li bindent=\"20\" style=\"image\" value=\"interface\">"); //$NON-NLS-1$
074: createClassReference(buffer, clazzes[i]);
075: buffer.append("</li>"); //$NON-NLS-1$
076: }
077: Image image = PDERuntimePluginImages
078: .get(PDERuntimePluginImages.IMG_INTERFACE_OBJ);
079: text.setImage("interface", image); //$NON-NLS-1$
080: }
081: return buffer.toString();
082: }
083:
084: public String createClassSection(FormText text, String title,
085: Class[] clazzes) {
086: StringBuffer buffer = new StringBuffer();
087: if (clazzes.length > 0) {
088: buffer.append("<p>"); //$NON-NLS-1$
089: buffer.append(title);
090: buffer.append("</p>"); //$NON-NLS-1$
091: for (int i = 0; i < clazzes.length; i++) {
092: buffer
093: .append("<li bindent=\"20\" style=\"image\" value=\"class\">"); //$NON-NLS-1$
094: createClassReference(buffer, clazzes[i]);
095: buffer.append("</li>"); //$NON-NLS-1$
096: }
097: Image image = PDERuntimePluginImages
098: .get(PDERuntimePluginImages.IMG_CLASS_OBJ);
099: text.setImage("class", image); //$NON-NLS-1$
100: }
101: return buffer.toString();
102: }
103:
104: public String createIdentifierSection(FormText text, String title,
105: String[] ids) {
106: StringBuffer buffer = new StringBuffer();
107: if (ids.length > 0) {
108: buffer.append("<p>"); //$NON-NLS-1$
109: buffer.append(title);
110: buffer.append("</p>"); //$NON-NLS-1$
111: for (int i = 0; i < ids.length; i++) {
112: buffer
113: .append("<li bindent=\"20\" style=\"image\" value=\"id\">"); //$NON-NLS-1$
114: buffer.append(ids[i]);
115: buffer.append("</li>"); //$NON-NLS-1$
116: }
117: Image image = PDERuntimePluginImages
118: .get(PDERuntimePluginImages.IMG_ID_OBJ);
119: text.setImage("id", image); //$NON-NLS-1$
120: }
121: return buffer.toString();
122: }
123:
124: // TODO create this convenience method
125: public String createHelpIdentifierSection(FormText text,
126: String title, IWorkbenchPart part) {
127: StringBuffer buffer = new StringBuffer();
128: return buffer.toString();
129: }
130:
131: private void createClassReference(StringBuffer buffer, Class clazz) {
132: Bundle bundle = PDERuntimePlugin.HAS_IDE_BUNDLES ? PDERuntimePlugin
133: .getDefault().getPackageAdmin().getBundle(clazz)
134: : null;
135: if (bundle != null) {
136: bundleClassByName.put(clazz.getName(), bundle);
137: buffer.append("<a href=\"").append( //$NON-NLS-1$
138: clazz.getName()).append("\">") //$NON-NLS-1$
139: .append(getSimpleName(clazz)).append("</a>"); //$NON-NLS-1$
140: } else {
141: buffer.append(clazz.getName());
142: }
143: }
144:
145: // TODO refactor me, I'm ugly
146: public void generatePluginDetailsText(Bundle bundle,
147: String objectId, String objectType, StringBuffer buffer,
148: FormText text) {
149: if (bundle != null) {
150: String version = (String) (bundle.getHeaders()
151: .get(org.osgi.framework.Constants.BUNDLE_VERSION));
152:
153: buffer.append("<p>"); //$NON-NLS-1$
154: buffer
155: .append(PDERuntimeMessages.SpyDialog_contributingPluginId_title);
156: buffer.append("</p>"); //$NON-NLS-1$
157: buffer
158: .append("<li bindent=\"20\" style=\"image\" value=\"plugin\">"); //$NON-NLS-1$
159: buffer.append(bundle.getSymbolicName());
160: buffer.append(" ("); //$NON-NLS-1$
161: buffer.append(version);
162: buffer.append(")"); //$NON-NLS-1$
163: buffer.append("</li>"); //$NON-NLS-1$
164:
165: Image pluginImage = PDERuntimePluginImages
166: .get(PDERuntimePluginImages.IMG_PLUGIN_OBJ);
167: text.setImage("plugin", pluginImage); //$NON-NLS-1$
168:
169: if (objectId != null) {
170: buffer.append("<p>"); //$NON-NLS-1$
171: buffer
172: .append(NLS
173: .bind(
174: PDERuntimeMessages.SpyDialog_contributingPluginId_desc,
175: objectType));
176: buffer.append("</p>"); //$NON-NLS-1$
177: buffer
178: .append("<li bindent=\"20\" style=\"image\" value=\"id\">"); //$NON-NLS-1$
179: buffer.append(objectId);
180: buffer.append("</li>"); //$NON-NLS-1$
181: }
182: }
183: }
184:
185: private String getSimpleName(Class clazz) {
186: String fullName = clazz.getName();
187: int index = fullName.lastIndexOf('.');
188: String name = fullName.substring(index + 1, fullName.length());
189: if (name != null)
190: return name;
191: return fullName;
192: }
193:
194: }
|