001: /*******************************************************************************
002: * Copyright (c) 2005, 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: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import java.io.IOException;
013: import java.io.Reader;
014: import java.util.HashMap;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.jdt.core.IBuffer;
022: import org.eclipse.jdt.core.IField;
023: import org.eclipse.jdt.core.IJavaElement;
024: import org.eclipse.jdt.core.IJavaProject;
025: import org.eclipse.jdt.core.ISourceRange;
026: import org.eclipse.jdt.core.IType;
027: import org.eclipse.jdt.core.JavaCore;
028: import org.eclipse.jdt.core.JavaModelException;
029: import org.eclipse.jdt.ui.JavaUI;
030: import org.eclipse.jface.fieldassist.ContentProposalAdapter;
031: import org.eclipse.jface.fieldassist.ControlDecoration;
032: import org.eclipse.jface.fieldassist.FieldDecoration;
033: import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
034: import org.eclipse.jface.fieldassist.IContentProposalListener;
035: import org.eclipse.jface.fieldassist.IContentProposalListener2;
036: import org.eclipse.jface.fieldassist.TextContentAdapter;
037: import org.eclipse.jface.viewers.ILabelProvider;
038: import org.eclipse.jface.window.Window;
039: import org.eclipse.jface.wizard.WizardDialog;
040: import org.eclipse.pde.internal.core.util.PDEJavaHelper;
041: import org.eclipse.pde.internal.ui.PDEPlugin;
042: import org.eclipse.pde.internal.ui.PDEUIMessages;
043: import org.eclipse.pde.internal.ui.editor.contentassist.TypeContentProposalListener;
044: import org.eclipse.pde.internal.ui.editor.contentassist.TypeContentProposalProvider;
045: import org.eclipse.pde.internal.ui.editor.contentassist.TypeFieldAssistDisposer;
046: import org.eclipse.pde.internal.ui.editor.contentassist.TypeProposalLabelProvider;
047: import org.eclipse.pde.internal.ui.editor.contentassist.display.JavaDocCommentReader;
048: import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeValue;
049: import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeWizard;
050: import org.eclipse.pde.internal.ui.editor.text.HTMLPrinter;
051: import org.eclipse.swt.SWT;
052: import org.eclipse.swt.widgets.Display;
053: import org.eclipse.swt.widgets.Text;
054: import org.eclipse.ui.IWorkbenchPage;
055: import org.eclipse.ui.PartInitException;
056: import org.eclipse.ui.PlatformUI;
057: import org.eclipse.ui.dialogs.SelectionDialog;
058: import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
059: import org.eclipse.ui.ide.IDE;
060:
061: public class PDEJavaHelperUI {
062:
063: private static HashMap fDocMap = new HashMap();
064:
065: public static String selectType(IResource resource, int scope) {
066: if (resource == null)
067: return null;
068: IProject project = resource.getProject();
069: try {
070: SelectionDialog dialog = JavaUI.createTypeDialog(PDEPlugin
071: .getActiveWorkbenchShell(), PlatformUI
072: .getWorkbench().getProgressService(), PDEJavaHelper
073: .getSearchScope(project), scope, false, ""); //$NON-NLS-1$
074: dialog
075: .setTitle(PDEUIMessages.ClassAttributeRow_dialogTitle);
076: if (dialog.open() == Window.OK) {
077: IType type = (IType) dialog.getResult()[0];
078: return type.getFullyQualifiedName('$');
079: }
080: } catch (JavaModelException e) {
081: }
082: return null;
083: }
084:
085: public static String selectType(IResource resource, int scope,
086: String filter) {
087: if (resource == null)
088: return null;
089: IProject project = resource.getProject();
090: try {
091: SelectionDialog dialog = JavaUI.createTypeDialog(PDEPlugin
092: .getActiveWorkbenchShell(), PlatformUI
093: .getWorkbench().getProgressService(), PDEJavaHelper
094: .getSearchScope(project), scope, false, filter); //$NON-NLS-1$
095: dialog
096: .setTitle(PDEUIMessages.ClassAttributeRow_dialogTitle);
097: if (dialog.open() == Window.OK) {
098: IType type = (IType) dialog.getResult()[0];
099: return type.getFullyQualifiedName('$');
100: }
101: } catch (JavaModelException e) {
102: }
103: return null;
104: }
105:
106: /**
107: * Open/Create a java class
108: *
109: * @param name fully qualified java classname
110: * @param project
111: * @param value for creation of the class
112: * @param createIfNoNature will create the class even if the project has no java nature
113: * @return null if the class exists or the name of the newly created class
114: */
115: public static String createClass(String name, IProject project,
116: JavaAttributeValue value, boolean createIfNoNature) {
117: name = TextUtil.trimNonAlphaChars(name).replace('$', '.');
118: try {
119: if (project.hasNature(JavaCore.NATURE_ID)) {
120: IJavaProject javaProject = JavaCore.create(project);
121: IJavaElement result = null;
122: if (name.length() > 0)
123: result = javaProject.findType(name);
124: if (result != null)
125: JavaUI.openInEditor(result);
126: else {
127: JavaAttributeWizard wizard = new JavaAttributeWizard(
128: value);
129: WizardDialog dialog = new WizardDialog(PDEPlugin
130: .getActiveWorkbenchShell(), wizard);
131: dialog.create();
132: SWTUtil.setDialogSize(dialog, 400, 500);
133: int dResult = dialog.open();
134: if (dResult == Window.OK)
135: return wizard.getQualifiedNameWithArgs();
136: }
137: } else if (createIfNoNature) {
138: IResource resource = project.findMember(new Path(name));
139: if (resource != null && resource instanceof IFile) {
140: IWorkbenchPage page = PDEPlugin.getActivePage();
141: IDE.openEditor(page, (IFile) resource, true);
142: } else {
143: JavaAttributeWizard wizard = new JavaAttributeWizard(
144: value);
145: WizardDialog dialog = new WizardDialog(PDEPlugin
146: .getActiveWorkbenchShell(), wizard);
147: dialog.create();
148: SWTUtil.setDialogSize(dialog, 400, 500);
149: int dResult = dialog.open();
150: if (dResult == Window.OK) {
151: String newValue = wizard.getQualifiedName();
152: name = newValue.replace('.', '/') + ".java"; //$NON-NLS-1$
153: resource = project.findMember(new Path(name));
154: if (resource != null
155: && resource instanceof IFile) {
156: IWorkbenchPage page = PDEPlugin
157: .getActivePage();
158: IDE
159: .openEditor(page, (IFile) resource,
160: true);
161: }
162: return newValue;
163: }
164: }
165: }
166: } catch (PartInitException e) {
167: PDEPlugin.logException(e);
168: } catch (JavaModelException e) {
169: // nothing
170: Display.getCurrent().beep();
171: } catch (CoreException e) {
172: PDEPlugin.logException(e);
173: }
174: return null;
175: }
176:
177: public static String getOSGIConstantJavaDoc(String constant,
178: IJavaProject jp) {
179: return getJavaDoc(constant, jp, "org.osgi.framework.Constants"); //$NON-NLS-1$
180: }
181:
182: public static String getJavaDoc(String constant, IJavaProject jp,
183: String className) {
184: HashMap map = (HashMap) fDocMap.get(className);
185: if (map == null)
186: fDocMap.put(className, map = new HashMap());
187: String javaDoc = (String) map.get(constant);
188:
189: if (javaDoc == null) {
190: try {
191: IType type = jp.findType(className);
192: if (type != null) {
193: char[] chars = constant.toCharArray();
194: for (int i = 0; i < chars.length; i++)
195: chars[i] = chars[i] == '-' ? '_' : Character
196: .toUpperCase(chars[i]);
197: IField field = type.getField(new String(chars));
198: ISourceRange range = field.getJavadocRange();
199: if (range == null)
200: return null;
201: IBuffer buff = type.getOpenable().getBuffer();
202: JavaDocCommentReader reader = new JavaDocCommentReader(
203: buff, range.getOffset(), range.getOffset()
204: + range.getLength() - 1);
205: String text = getString(reader);
206: javaDoc = formatJavaDoc(text);
207: map.put(constant, javaDoc);
208: }
209: } catch (JavaModelException e) {
210: }
211: }
212: return javaDoc;
213: }
214:
215: private static String formatJavaDoc(String text) {
216: StringBuffer buffer = new StringBuffer();
217: HTMLPrinter.insertPageProlog(buffer, 0, TextUtil
218: .getJavaDocStyleSheerURL());
219: buffer.append(text);
220: HTMLPrinter.addPageEpilog(buffer);
221: return buffer.toString();
222: }
223:
224: /**
225: * Gets the reader content as a String
226: */
227: private static String getString(Reader reader) {
228: StringBuffer buf = new StringBuffer();
229: char[] buffer = new char[1024];
230: int count;
231: try {
232: while ((count = reader.read(buffer)) != -1)
233: buf.append(buffer, 0, count);
234: } catch (IOException e) {
235: return null;
236: }
237: return buf.toString();
238: }
239:
240: /**
241: * Disposer returned used to dispose of label provider and remove listeners
242: * Callers responsibility to call dispose method when underlying text
243: * widget is being disposed
244: * @param text
245: * @param project
246: * @return
247: */
248: public static TypeFieldAssistDisposer addTypeFieldAssistToText(
249: Text text, IProject project, int searchScope) {
250: // Decorate the text widget with the light-bulb image denoting content
251: // assist
252: int bits = SWT.TOP | SWT.LEFT;
253: ControlDecoration controlDecoration = new ControlDecoration(
254: text, bits);
255: // Configure text widget decoration
256: // No margin
257: controlDecoration.setMarginWidth(0);
258: // Custom hover tip text
259: controlDecoration
260: .setDescriptionText(PDEUIMessages.PDEJavaHelper_msgContentAssistAvailable);
261: // Custom hover properties
262: controlDecoration.setShowHover(true);
263: controlDecoration.setShowOnlyOnFocus(true);
264: // Hover image to use
265: FieldDecoration contentProposalImage = FieldDecorationRegistry
266: .getDefault().getFieldDecoration(
267: FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
268: controlDecoration.setImage(contentProposalImage.getImage());
269:
270: // Create the proposal provider
271: TypeContentProposalProvider proposalProvider = new TypeContentProposalProvider(
272: project, searchScope);
273: // Default text widget adapter for field assist
274: TextContentAdapter textContentAdapter = new TextContentAdapter();
275: // Content assist command
276: String command = "org.eclipse.ui.edit.text.contentAssist.proposals"; //$NON-NLS-1$
277: // Set auto activation character to be a '.'
278: char[] autoActivationChars = new char[] { TypeContentProposalProvider.F_DOT };
279: // Create the adapter
280: ContentAssistCommandAdapter adapter = new ContentAssistCommandAdapter(
281: text, textContentAdapter, proposalProvider, command,
282: autoActivationChars);
283: // Configure the adapter
284: // Add label provider
285: ILabelProvider labelProvider = new TypeProposalLabelProvider();
286: adapter.setLabelProvider(labelProvider);
287: // Replace text field contents with accepted proposals
288: adapter
289: .setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
290: // Disable default filtering - custom filtering done
291: adapter.setFilterStyle(ContentProposalAdapter.FILTER_NONE);
292: // Add listeners required to reset state for custom filtering
293: TypeContentProposalListener proposalListener = new TypeContentProposalListener();
294: adapter
295: .addContentProposalListener((IContentProposalListener) proposalListener);
296: adapter
297: .addContentProposalListener((IContentProposalListener2) proposalListener);
298:
299: return new TypeFieldAssistDisposer(adapter, proposalListener);
300: }
301:
302: }
|