1 11 package org.eclipse.pde.internal.ui.util; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.Path; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.IType; 21 import org.eclipse.jdt.core.JavaCore; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.core.search.SearchEngine; 24 import org.eclipse.jdt.ui.IJavaElementSearchConstants; 25 import org.eclipse.jdt.ui.JavaUI; 26 import org.eclipse.jface.window.Window; 27 import org.eclipse.jface.wizard.WizardDialog; 28 import org.eclipse.pde.internal.ui.PDEPlugin; 29 import org.eclipse.pde.internal.ui.PDEUIMessages; 30 import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeValue; 31 import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeWizard; 32 import org.eclipse.swt.widgets.Display; 33 import org.eclipse.ui.IWorkbenchPage; 34 import org.eclipse.ui.PartInitException; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.dialogs.SelectionDialog; 37 import org.eclipse.ui.ide.IDE; 38 39 public class PDEJavaHelper { 40 41 public static String selectType() { 42 try { 43 SelectionDialog dialog = JavaUI.createTypeDialog( 44 PDEPlugin.getActiveWorkbenchShell(), 45 PlatformUI.getWorkbench().getProgressService(), 46 SearchEngine.createWorkspaceScope(), 47 IJavaElementSearchConstants.CONSIDER_ALL_TYPES, 48 false, ""); dialog.setTitle(PDEUIMessages.ClassAttributeRow_dialogTitle); 50 if (dialog.open() == Window.OK) { 51 IType type = (IType) dialog.getResult()[0]; 52 return type.getFullyQualifiedName('$'); 53 } 54 } catch (JavaModelException e) { 55 } 56 return null; 57 } 58 59 68 public static String createClass(String name, IProject project, JavaAttributeValue value, boolean createIfNoNature) { 69 name = trimNonAlphaChars(name).replace('$', '.'); 70 try { 71 if (project.hasNature(JavaCore.NATURE_ID)) { 72 IJavaProject javaProject = JavaCore.create(project); 73 IJavaElement result = null; 74 if (name.length() > 0) 75 result = javaProject.findType(name); 76 if (result != null) 77 JavaUI.openInEditor(result); 78 else { 79 JavaAttributeWizard wizard = new JavaAttributeWizard(value); 80 WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); 81 dialog.create(); 82 SWTUtil.setDialogSize(dialog, 400, 500); 83 int dResult = dialog.open(); 84 if (dResult == Window.OK) 85 return wizard.getQualifiedNameWithArgs(); 86 } 87 } else if (createIfNoNature) { 88 IResource resource = project.findMember(new Path(name)); 89 if (resource != null && resource instanceof IFile) { 90 IWorkbenchPage page = PDEPlugin.getActivePage(); 91 IDE.openEditor(page, (IFile) resource, true); 92 } else { 93 JavaAttributeWizard wizard = new JavaAttributeWizard(value); 94 WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); 95 dialog.create(); 96 SWTUtil.setDialogSize(dialog, 400, 500); 97 int dResult = dialog.open(); 98 if (dResult == Window.OK) { 99 String newValue = wizard.getQualifiedName(); 100 name = newValue.replace('.', '/') + ".java"; resource = project.findMember(new Path(name)); 102 if (resource != null && resource instanceof IFile) { 103 IWorkbenchPage page = PDEPlugin.getActivePage(); 104 IDE.openEditor(page, (IFile) resource, true); 105 } 106 return newValue; 107 } 108 } 109 } 110 } catch (PartInitException e) { 111 PDEPlugin.logException(e); 112 } catch (JavaModelException e) { 113 Display.getCurrent().beep(); 115 } catch (CoreException e) { 116 PDEPlugin.logException(e); 117 } 118 return null; 119 } 120 121 public static String trimNonAlphaChars(String value) { 122 value = value.trim(); 123 while (value.length() > 0 && !Character.isLetter(value.charAt(0))) 124 value = value.substring(1, value.length()); 125 int loc = value.indexOf(":"); if (loc != -1 && loc > 0) 127 value = value.substring(0, loc); 128 else if (loc == 0) 129 value = ""; return value; 131 } 132 } 133 | Popular Tags |