1 21 22 package org.apache.derby.ui.popup.actions; 23 24 import org.apache.derby.ui.common.CommonNames; 25 import org.apache.derby.ui.common.Messages; 26 import org.apache.derby.ui.util.DerbyUtils; 27 import org.apache.derby.ui.util.Logger; 28 import org.apache.derby.ui.util.SelectionUtil; 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.core.resources.IProjectDescription; 31 import org.eclipse.core.resources.IResource; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.jdt.core.IClasspathEntry; 34 import org.eclipse.jdt.core.IJavaProject; 35 import org.eclipse.jdt.core.JavaCore; 36 import org.eclipse.jface.action.IAction; 37 import org.eclipse.jface.dialogs.MessageDialog; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.window.ApplicationWindow; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.graphics.Cursor; 42 import org.eclipse.swt.widgets.Shell; 43 import org.eclipse.ui.IObjectActionDelegate; 44 import org.eclipse.ui.IWorkbenchPart; 45 import org.eclipse.ui.IWorkbenchWindow; 46 import org.eclipse.ui.PlatformUI; 47 48 public class AddDerbyNature implements IObjectActionDelegate 49 { 50 51 private IJavaProject currentJavaProject; 52 private IProject currentProject; 53 54 60 public void setActivePart(IAction action, IWorkbenchPart targetPart) 61 { 62 } 63 64 69 public void run(IAction action) 70 { 71 IWorkbenchWindow window = PlatformUI.getWorkbench() 72 .getActiveWorkbenchWindow(); 73 Cursor waitCursor = new Cursor(window.getShell().getDisplay(), 74 SWT.CURSOR_WAIT); 75 try 76 { 77 window.getShell().setCursor(waitCursor); 78 ((ApplicationWindow) window).setStatus(Messages.ADDING_NATURE); 79 80 if (currentJavaProject == null) 82 { 83 IProjectDescription description = currentProject 86 .getDescription(); 87 String [] natureIds = description.getNatureIds(); 88 String [] newNatures = new String [natureIds.length + 2]; 89 System.arraycopy(natureIds, 0, newNatures, 0, natureIds.length); 90 newNatures[newNatures.length - 2] = JavaCore.NATURE_ID; 91 newNatures[newNatures.length - 1] = CommonNames.DERBY_NATURE; 92 description.setNatureIds(newNatures); 93 currentProject.setDescription(description, null); 94 95 currentJavaProject = (IJavaProject) JavaCore 96 .create((IProject) currentProject); 97 } 98 else 99 { 100 IProjectDescription description = currentJavaProject 102 .getProject().getDescription(); 103 String [] natures = description.getNatureIds(); 104 String [] newNatures = new String [natures.length + 1]; 105 System.arraycopy(natures, 0, newNatures, 0, natures.length); 106 newNatures[natures.length] = CommonNames.DERBY_NATURE; 108 description.setNatureIds(newNatures); 109 currentJavaProject.getProject().setDescription(description, 110 null); 111 } 112 113 IClasspathEntry[] rawClasspath = currentJavaProject 114 .getRawClasspath(); 115 116 currentJavaProject.setRawClasspath(DerbyUtils 117 .addDerbyJars(rawClasspath), null); 118 119 currentJavaProject.getProject().refreshLocal( 121 IResource.DEPTH_INFINITE, null); 122 ((ApplicationWindow) window).setStatus(Messages.DERBY_NATURE_ADDED); 123 124 } catch ( Exception e) 125 { 126 Logger.log(Messages.ERROR_ADDING_NATURE + " '" 127 + currentJavaProject.getProject().getName() + "' : " + e, 128 IStatus.ERROR); 129 Shell shell = new Shell(); 130 MessageDialog.openInformation(shell, CommonNames.PLUGIN_NAME, 131 Messages.ERROR_ADDING_NATURE + ":\n" 132 + SelectionUtil.getStatusMessages(e)); 133 } finally 134 { 135 window.getShell().setCursor(null); 136 waitCursor.dispose(); 137 } 138 } 139 140 146 public void selectionChanged(IAction action, ISelection selection) 147 { 148 currentJavaProject = SelectionUtil.findSelectedJavaProject(selection); 149 150 if (currentJavaProject == null) 151 { 152 currentProject = org.apache.derby.ui.util.SelectionUtil 153 .findSelectedProject(selection); 154 } 155 156 } 157 158 } 159 | Popular Tags |