1 11 package org.eclipse.pde.internal.ui.util; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.util.HashMap ; 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 fDocMap = new HashMap (); 65 66 public static String 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, ""); 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 selectType(IResource resource, int scope, String 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); 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 115 public static String createClass(String 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 newValue = wizard.getQualifiedName(); 147 name = newValue.replace('.', '/') + ".java"; 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 Display.getCurrent().beep(); 162 } catch (CoreException e) { 163 PDEPlugin.logException(e); 164 } 165 return null; 166 } 167 168 public static String getOSGIConstantJavaDoc(String constant, IJavaProject jp) { 169 return getJavaDoc(constant, jp, "org.osgi.framework.Constants"); } 171 172 public static String getJavaDoc(String constant, IJavaProject jp, String className) { 173 HashMap map = (HashMap )fDocMap.get(className); 174 if (map == null) 175 fDocMap.put(className, map = new HashMap ()); 176 String javaDoc = (String )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 (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 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 formatJavaDoc(String text) { 203 StringBuffer buffer = new StringBuffer (); 204 HTMLPrinter.insertPageProlog(buffer, 0, TextUtil.getJavaDocStyleSheerURL()); 205 buffer.append(text); 206 HTMLPrinter.addPageEpilog(buffer); 207 return buffer.toString(); 208 } 209 210 213 private static String getString(Reader reader) { 214 StringBuffer buf= new StringBuffer (); 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 e) { 221 return null; 222 } 223 return buf.toString(); 224 } 225 226 234 public static TypeFieldAssistDisposer addTypeFieldAssistToText( 235 Text text, IProject project, int searchScope) { 236 int bits = SWT.TOP | SWT.LEFT; 239 ControlDecoration controlDecoration = new ControlDecoration(text, bits); 240 controlDecoration.setMarginWidth(0); 243 controlDecoration.setDescriptionText( 245 PDEUIMessages.PDEJavaHelper_msgContentAssistAvailable); 246 controlDecoration.setShowHover(true); 248 controlDecoration.setShowOnlyOnFocus(true); 249 FieldDecoration contentProposalImage = 251 FieldDecorationRegistry.getDefault().getFieldDecoration( 252 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL); 253 controlDecoration.setImage(contentProposalImage.getImage()); 254 255 TypeContentProposalProvider proposalProvider = 257 new TypeContentProposalProvider(project, 258 searchScope); 259 TextContentAdapter textContentAdapter = new TextContentAdapter(); 261 String command = "org.eclipse.ui.edit.text.contentAssist.proposals"; char[] autoActivationChars = new char[]{TypeContentProposalProvider.F_DOT}; 265 ContentAssistCommandAdapter adapter = 267 new ContentAssistCommandAdapter( 268 text, 269 textContentAdapter, 270 proposalProvider, 271 command, 272 autoActivationChars); 273 ILabelProvider labelProvider = new TypeProposalLabelProvider(); 276 adapter.setLabelProvider(labelProvider); 277 adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE); 279 adapter.setFilterStyle(ContentProposalAdapter.FILTER_NONE); 281 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 |