KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 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 JavaDoc 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, ""); //$NON-NLS-1$
49
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     /**
60      * Open/Create a java class
61      *
62      * @param name fully qualified java classname
63      * @param project
64      * @param value for creation of the class
65      * @param createIfNoNature will create the class even if the project has no java nature
66      * @return null if the class exists or the name of the newly created class
67      */

68     public static String JavaDoc createClass(String JavaDoc 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 JavaDoc newValue = wizard.getQualifiedName();
100                         name = newValue.replace('.', '/') + ".java"; //$NON-NLS-1$
101
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             // nothing
114
Display.getCurrent().beep();
115         } catch (CoreException e) {
116             PDEPlugin.logException(e);
117         }
118         return null;
119     }
120     
121     public static String JavaDoc trimNonAlphaChars(String JavaDoc 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(":"); //$NON-NLS-1$
126
if (loc != -1 && loc > 0)
127             value = value.substring(0, loc);
128         else if (loc == 0)
129             value = ""; //$NON-NLS-1$
130
return value;
131     }
132 }
133
Popular Tags