KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3     Derby - Class org.apache.derby.ui.popup.actions.AddDerbyNature
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
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     /*
55      * (non-Javadoc)
56      *
57      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
58      * org.eclipse.ui.IWorkbenchPart)
59      */

60     public void setActivePart(IAction action, IWorkbenchPart targetPart)
61     {
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
68      */

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             //new way
81
if (currentJavaProject == null)
82             {
83                 // if the java nature is not present
84
// it must be added, along with the Derby nature
85
IProjectDescription description = currentProject
86                         .getDescription();
87                 String JavaDoc[] natureIds = description.getNatureIds();
88                 String JavaDoc[] newNatures = new String JavaDoc[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                 //add the derby nature, the java nature is already present
101
IProjectDescription description = currentJavaProject
102                         .getProject().getDescription();
103                 String JavaDoc[] natures = description.getNatureIds();
104                 String JavaDoc[] newNatures = new String JavaDoc[natures.length + 1];
105                 System.arraycopy(natures, 0, newNatures, 0, natures.length);
106                 // must prefix with plugin id
107
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             // refresh project so user sees new files, libraries, etc
120
currentJavaProject.getProject().refreshLocal(
121                     IResource.DEPTH_INFINITE, null);
122             ((ApplicationWindow) window).setStatus(Messages.DERBY_NATURE_ADDED);
123
124         } catch ( Exception JavaDoc 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     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
144      * org.eclipse.jface.viewers.ISelection)
145      */

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