KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > ui > popup > actions > RemoveDerbyNature


1 /*
2
3     Derby - Class org.apache.derby.ui.popup.actions.RemoveDerbyNature
4     
5     Licensed to the Apache Software Foundation (ASF) under one or more
6     contributor license agreements. See the NOTICE file distributed with
7     this work for additional information regarding copyright ownership.
8     The ASF licenses this file to you under the Apache License, Version 2.0
9     (the "License"); you may not use this file except in compliance with
10     the License. You may obtain a copy of the License at
11     
12        http://www.apache.org/licenses/LICENSE-2.0
13     
14     Unless required by applicable law or agreed to in writing, software
15     distributed under the License is distributed on an "AS IS" BASIS,
16     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17     See the License for the specific language governing permissions and
18     limitations under the License.
19
20 */

21 package org.apache.derby.ui.popup.actions;
22
23 import java.util.ArrayList JavaDoc;
24
25 import org.apache.derby.ui.common.CommonNames;
26 import org.apache.derby.ui.common.Messages;
27 import org.apache.derby.ui.util.DerbyServerUtils;
28 import org.apache.derby.ui.util.DerbyUtils;
29 import org.apache.derby.ui.util.Logger;
30 import org.apache.derby.ui.util.SelectionUtil;
31 import org.eclipse.core.resources.IProject;
32 import org.eclipse.core.resources.IProjectDescription;
33 import org.eclipse.core.resources.IResource;
34 import org.eclipse.core.runtime.IStatus;
35 import org.eclipse.jdt.core.IClasspathEntry;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.JavaCore;
38 import org.eclipse.jface.action.IAction;
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.window.ApplicationWindow;
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 RemoveDerbyNature implements IObjectActionDelegate {
49
50     private IJavaProject currentJavaProject;
51     private IProject currentProject;
52     /* (non-Javadoc)
53      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
54      */

55     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
56         }
57
58
59     private static String JavaDoc[] removeDerbyNature(String JavaDoc [] natures){
60         ArrayList JavaDoc arrL=new ArrayList JavaDoc();
61         
62         for (int i=0;i<natures.length;i++){
63             if(!(natures[i].equalsIgnoreCase(CommonNames.DERBY_NATURE))){
64                 arrL.add(natures[i]);
65             }
66         }
67         String JavaDoc [] newNatures= new String JavaDoc [arrL.size()];
68         for(int i=0;i<arrL.size();i++){
69             newNatures[i]=(String JavaDoc)arrL.get(i);
70         }
71         return newNatures;
72     }
73     /* (non-Javadoc)
74      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
75      */

76     public void run(IAction action) {
77         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
78         try {
79             ((ApplicationWindow)window).setStatus(Messages.REMOVING_NATURE);
80             
81             if(currentJavaProject==null){
82                 currentJavaProject=JavaCore.create(currentProject);
83             }
84             //Shutdown server if running for the current project
85
if(DerbyServerUtils.getDefault().getRunning(currentJavaProject.getProject())){
86                 DerbyServerUtils.getDefault().stopDerbyServer(currentJavaProject.getProject());
87             }
88             IClasspathEntry[] rawClasspath = currentJavaProject.getRawClasspath();
89             currentJavaProject.setRawClasspath(DerbyUtils.removeDerbyJars(rawClasspath), null);
90             
91             IProjectDescription description = currentJavaProject.getProject().getDescription();
92             String JavaDoc[] natures = description.getNatureIds();
93
94             description.setNatureIds(removeDerbyNature(natures));
95             currentJavaProject.getProject().setDescription(description, null);
96             // refresh project so user sees changes
97
currentJavaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
98             ((ApplicationWindow)window).setStatus(Messages.DERBY_NATURE_REMOVED);
99         }catch (Exception JavaDoc e) {
100             Logger.log(Messages.ERROR_REMOVING_NATURE+" '"+currentJavaProject.getProject().getName()+"': "+e,IStatus.ERROR);
101
102             Shell shell = new Shell();
103             MessageDialog.openInformation(
104                 shell,
105                 CommonNames.PLUGIN_NAME,
106                 Messages.ERROR_REMOVING_NATURE+":\n" +
107                  SelectionUtil.getStatusMessages(e));
108         }
109
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
114      */

115     public void selectionChanged(IAction action, ISelection selection) {
116         currentJavaProject = SelectionUtil.findSelectedJavaProject(selection);
117         if(currentJavaProject==null){
118             currentProject=org.apache.derby.ui.util.SelectionUtil.findSelectedProject(selection);
119         }
120     }
121
122 }
123
Popular Tags