KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > PDEJavaHelperUI


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.util.HashMap JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jdt.core.IBuffer;
23 import org.eclipse.jdt.core.IField;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.core.ISourceRange;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.core.JavaModelException;
30 import org.eclipse.jdt.ui.JavaUI;
31 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
32 import org.eclipse.jface.fieldassist.ControlDecoration;
33 import org.eclipse.jface.fieldassist.FieldDecoration;
34 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
35 import org.eclipse.jface.fieldassist.IContentProposalListener;
36 import org.eclipse.jface.fieldassist.IContentProposalListener2;
37 import org.eclipse.jface.fieldassist.TextContentAdapter;
38 import org.eclipse.jface.viewers.ILabelProvider;
39 import org.eclipse.jface.window.Window;
40 import org.eclipse.jface.wizard.WizardDialog;
41 import org.eclipse.pde.internal.core.util.PDEJavaHelper;
42 import org.eclipse.pde.internal.ui.PDEPlugin;
43 import org.eclipse.pde.internal.ui.PDEUIMessages;
44 import org.eclipse.pde.internal.ui.editor.contentassist.TypeContentProposalListener;
45 import org.eclipse.pde.internal.ui.editor.contentassist.TypeContentProposalProvider;
46 import org.eclipse.pde.internal.ui.editor.contentassist.TypeFieldAssistDisposer;
47 import org.eclipse.pde.internal.ui.editor.contentassist.TypeProposalLabelProvider;
48 import org.eclipse.pde.internal.ui.editor.contentassist.display.JavaDocCommentReader;
49 import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeValue;
50 import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeWizard;
51 import org.eclipse.pde.internal.ui.editor.text.HTMLPrinter;
52 import org.eclipse.swt.SWT;
53 import org.eclipse.swt.widgets.Display;
54 import org.eclipse.swt.widgets.Text;
55 import org.eclipse.ui.IWorkbenchPage;
56 import org.eclipse.ui.PartInitException;
57 import org.eclipse.ui.PlatformUI;
58 import org.eclipse.ui.dialogs.SelectionDialog;
59 import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
60 import org.eclipse.ui.ide.IDE;
61
62 public class PDEJavaHelperUI {
63     
64     private static HashMap JavaDoc fDocMap = new HashMap JavaDoc();
65     
66     public static String JavaDoc selectType(IResource resource, int scope) {
67         if (resource == null) return null;
68         IProject project = resource.getProject();
69         try {
70             SelectionDialog dialog = JavaUI.createTypeDialog(
71                     PDEPlugin.getActiveWorkbenchShell(),
72                     PlatformUI.getWorkbench().getProgressService(),
73                     PDEJavaHelper.getSearchScope(project),
74                     scope,
75                     false, ""); //$NON-NLS-1$
76
dialog.setTitle(PDEUIMessages.ClassAttributeRow_dialogTitle);
77             if (dialog.open() == Window.OK) {
78                 IType type = (IType) dialog.getResult()[0];
79                 return type.getFullyQualifiedName('$');
80             }
81         } catch (JavaModelException e) {
82         }
83         return null;
84     }
85     
86     public static String JavaDoc selectType(IResource resource, int scope, String JavaDoc filter) {
87         if (resource == null) return null;
88         IProject project = resource.getProject();
89         try {
90             SelectionDialog dialog = JavaUI.createTypeDialog(
91                     PDEPlugin.getActiveWorkbenchShell(),
92                     PlatformUI.getWorkbench().getProgressService(),
93                     PDEJavaHelper.getSearchScope(project),
94                     scope,
95                     false, filter); //$NON-NLS-1$
96
dialog.setTitle(PDEUIMessages.ClassAttributeRow_dialogTitle);
97             if (dialog.open() == Window.OK) {
98                 IType type = (IType) dialog.getResult()[0];
99                 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 JavaDoc createClass(String JavaDoc name, IProject project, JavaAttributeValue value, boolean createIfNoNature) {
116         name = TextUtil.trimNonAlphaChars(name).replace('$', '.');
117         try {
118             if (project.hasNature(JavaCore.NATURE_ID)) {
119                 IJavaProject javaProject = JavaCore.create(project);
120                 IJavaElement result = null;
121                 if (name.length() > 0)
122                     result = javaProject.findType(name);
123                 if (result != null)
124                     JavaUI.openInEditor(result);
125                 else {
126                     JavaAttributeWizard wizard = new JavaAttributeWizard(value);
127                     WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
128                     dialog.create();
129                     SWTUtil.setDialogSize(dialog, 400, 500);
130                     int dResult = dialog.open();
131                     if (dResult == Window.OK)
132                         return wizard.getQualifiedNameWithArgs();
133                 }
134             } else if (createIfNoNature) {
135                 IResource resource = project.findMember(new Path(name));
136                 if (resource != null && resource instanceof IFile) {
137                     IWorkbenchPage page = PDEPlugin.getActivePage();
138                     IDE.openEditor(page, (IFile) resource, true);
139                 } else {
140                     JavaAttributeWizard wizard = new JavaAttributeWizard(value);
141                     WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard);
142                     dialog.create();
143                     SWTUtil.setDialogSize(dialog, 400, 500);
144                     int dResult = dialog.open();
145                     if (dResult == Window.OK) {
146                         String JavaDoc newValue = wizard.getQualifiedName();
147                         name = newValue.replace('.', '/') + ".java"; //$NON-NLS-1$
148
resource = project.findMember(new Path(name));
149                         if (resource != null && resource instanceof IFile) {
150                             IWorkbenchPage page = PDEPlugin.getActivePage();
151                             IDE.openEditor(page, (IFile) resource, true);
152                         }
153                         return newValue;
154                     }
155                 }
156             }
157         } catch (PartInitException e) {
158             PDEPlugin.logException(e);
159         } catch (JavaModelException e) {
160             // nothing
161
Display.getCurrent().beep();
162         } catch (CoreException e) {
163             PDEPlugin.logException(e);
164         }
165         return null;
166     }
167     
168     public static String JavaDoc getOSGIConstantJavaDoc(String JavaDoc constant, IJavaProject jp) {
169         return getJavaDoc(constant, jp, "org.osgi.framework.Constants"); //$NON-NLS-1$
170
}
171     
172     public static String JavaDoc getJavaDoc(String JavaDoc constant, IJavaProject jp, String JavaDoc className) {
173         HashMap JavaDoc map = (HashMap JavaDoc)fDocMap.get(className);
174         if (map == null)
175             fDocMap.put(className, map = new HashMap JavaDoc());
176         String JavaDoc javaDoc = (String JavaDoc)map.get(constant);
177         
178         if (javaDoc == null) {
179             try {
180                 IType type = jp.findType(className);
181                 if (type != null) {
182                     char[] chars = constant.toCharArray();
183                     for (int i = 0; i < chars.length; i++)
184                         chars[i] = chars[i] == '-' ? '_' : Character.toUpperCase(chars[i]);
185                     IField field = type.getField(new String JavaDoc(chars));
186                     ISourceRange range = field.getJavadocRange();
187                     if (range == null)
188                         return null;
189                     IBuffer buff = type.getOpenable().getBuffer();
190                     JavaDocCommentReader reader = new JavaDocCommentReader(buff, range.getOffset(),
191                             range.getOffset() + range.getLength() - 1);
192                     String JavaDoc text = getString(reader);
193                     javaDoc = formatJavaDoc(text);
194                     map.put(constant, javaDoc);
195                 }
196             } catch (JavaModelException e) {
197             }
198         }
199         return javaDoc;
200     }
201
202     private static String JavaDoc formatJavaDoc(String JavaDoc text) {
203         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
204         HTMLPrinter.insertPageProlog(buffer, 0, TextUtil.getJavaDocStyleSheerURL());
205         buffer.append(text);
206         HTMLPrinter.addPageEpilog(buffer);
207         return buffer.toString();
208     }
209
210     /**
211      * Gets the reader content as a String
212      */

213     private static String JavaDoc getString(Reader JavaDoc reader) {
214         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
215         char[] buffer= new char[1024];
216         int count;
217         try {
218             while ((count= reader.read(buffer)) != -1)
219                 buf.append(buffer, 0, count);
220         } catch (IOException JavaDoc e) {
221             return null;
222         }
223         return buf.toString();
224     }
225     
226     /**
227      * Disposer returned used to dispose of label provider and remove listeners
228      * Callers responsibility to call dispose method when underlying text
229      * widget is being disposed
230      * @param text
231      * @param project
232      * @return
233      */

234     public static TypeFieldAssistDisposer addTypeFieldAssistToText(
235             Text text, IProject project, int searchScope) {
236         // Decorate the text widget with the light-bulb image denoting content
237
// assist
238
int bits = SWT.TOP | SWT.LEFT;
239         ControlDecoration controlDecoration = new ControlDecoration(text, bits);
240         // Configure text widget decoration
241
// No margin
242
controlDecoration.setMarginWidth(0);
243         // Custom hover tip text
244
controlDecoration.setDescriptionText(
245                 PDEUIMessages.PDEJavaHelper_msgContentAssistAvailable);
246         // Custom hover properties
247
controlDecoration.setShowHover(true);
248         controlDecoration.setShowOnlyOnFocus(true);
249         // Hover image to use
250
FieldDecoration contentProposalImage =
251             FieldDecorationRegistry.getDefault().getFieldDecoration(
252                 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
253         controlDecoration.setImage(contentProposalImage.getImage());
254         
255         // Create the proposal provider
256
TypeContentProposalProvider proposalProvider =
257             new TypeContentProposalProvider(project,
258                     searchScope);
259         // Default text widget adapter for field assist
260
TextContentAdapter textContentAdapter = new TextContentAdapter();
261         // Content assist command
262
String JavaDoc command = "org.eclipse.ui.edit.text.contentAssist.proposals"; //$NON-NLS-1$
263
// Set auto activation character to be a '.'
264
char[] autoActivationChars = new char[]{TypeContentProposalProvider.F_DOT};
265         // Create the adapter
266
ContentAssistCommandAdapter adapter =
267             new ContentAssistCommandAdapter(
268                     text,
269                     textContentAdapter,
270                     proposalProvider,
271                     command,
272                     autoActivationChars);
273         // Configure the adapter
274
// Add label provider
275
ILabelProvider labelProvider = new TypeProposalLabelProvider();
276         adapter.setLabelProvider(labelProvider);
277         // Replace text field contents with accepted proposals
278
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
279         // Disable default filtering - custom filtering done
280
adapter.setFilterStyle(ContentProposalAdapter.FILTER_NONE);
281         // Add listeners required to reset state for custom filtering
282
TypeContentProposalListener proposalListener =
283             new TypeContentProposalListener();
284         adapter.addContentProposalListener((IContentProposalListener)proposalListener);
285         adapter.addContentProposalListener((IContentProposalListener2)proposalListener);
286         
287         return new TypeFieldAssistDisposer(adapter, proposalListener);
288     }
289     
290 }
291
Popular Tags