001: /*
002:
003: Derby - Class org.apache.derby.ui.popup.actions.AddDerbyNature
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.ui.popup.actions;
023:
024: import org.apache.derby.ui.common.CommonNames;
025: import org.apache.derby.ui.common.Messages;
026: import org.apache.derby.ui.util.DerbyUtils;
027: import org.apache.derby.ui.util.Logger;
028: import org.apache.derby.ui.util.SelectionUtil;
029: import org.eclipse.core.resources.IProject;
030: import org.eclipse.core.resources.IProjectDescription;
031: import org.eclipse.core.resources.IResource;
032: import org.eclipse.core.runtime.IStatus;
033: import org.eclipse.jdt.core.IClasspathEntry;
034: import org.eclipse.jdt.core.IJavaProject;
035: import org.eclipse.jdt.core.JavaCore;
036: import org.eclipse.jface.action.IAction;
037: import org.eclipse.jface.dialogs.MessageDialog;
038: import org.eclipse.jface.viewers.ISelection;
039: import org.eclipse.jface.window.ApplicationWindow;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.graphics.Cursor;
042: import org.eclipse.swt.widgets.Shell;
043: import org.eclipse.ui.IObjectActionDelegate;
044: import org.eclipse.ui.IWorkbenchPart;
045: import org.eclipse.ui.IWorkbenchWindow;
046: import org.eclipse.ui.PlatformUI;
047:
048: public class AddDerbyNature implements IObjectActionDelegate {
049:
050: private IJavaProject currentJavaProject;
051: private IProject currentProject;
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
057: * org.eclipse.ui.IWorkbenchPart)
058: */
059: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
066: */
067: public void run(IAction action) {
068: IWorkbenchWindow window = PlatformUI.getWorkbench()
069: .getActiveWorkbenchWindow();
070: Cursor waitCursor = new Cursor(window.getShell().getDisplay(),
071: SWT.CURSOR_WAIT);
072: try {
073: window.getShell().setCursor(waitCursor);
074: ((ApplicationWindow) window)
075: .setStatus(Messages.ADDING_NATURE);
076:
077: //new way
078: if (currentJavaProject == null) {
079: // if the java nature is not present
080: // it must be added, along with the Derby nature
081: IProjectDescription description = currentProject
082: .getDescription();
083: String[] natureIds = description.getNatureIds();
084: String[] newNatures = new String[natureIds.length + 2];
085: System.arraycopy(natureIds, 0, newNatures, 0,
086: natureIds.length);
087: newNatures[newNatures.length - 2] = JavaCore.NATURE_ID;
088: newNatures[newNatures.length - 1] = CommonNames.DERBY_NATURE;
089: description.setNatureIds(newNatures);
090: currentProject.setDescription(description, null);
091:
092: currentJavaProject = (IJavaProject) JavaCore
093: .create((IProject) currentProject);
094: } else {
095: //add the derby nature, the java nature is already present
096: IProjectDescription description = currentJavaProject
097: .getProject().getDescription();
098: String[] natures = description.getNatureIds();
099: String[] newNatures = new String[natures.length + 1];
100: System.arraycopy(natures, 0, newNatures, 0,
101: natures.length);
102: // must prefix with plugin id
103: newNatures[natures.length] = CommonNames.DERBY_NATURE;
104: description.setNatureIds(newNatures);
105: currentJavaProject.getProject().setDescription(
106: description, null);
107: }
108:
109: IClasspathEntry[] rawClasspath = currentJavaProject
110: .getRawClasspath();
111:
112: currentJavaProject.setRawClasspath(DerbyUtils
113: .addDerbyJars(rawClasspath), null);
114:
115: // refresh project so user sees new files, libraries, etc
116: currentJavaProject.getProject().refreshLocal(
117: IResource.DEPTH_INFINITE, null);
118: ((ApplicationWindow) window)
119: .setStatus(Messages.DERBY_NATURE_ADDED);
120:
121: } catch (Exception e) {
122: Logger.log(Messages.ERROR_ADDING_NATURE + " '"
123: + currentJavaProject.getProject().getName()
124: + "' : " + e, IStatus.ERROR);
125: Shell shell = new Shell();
126: MessageDialog.openInformation(shell,
127: CommonNames.PLUGIN_NAME,
128: Messages.ERROR_ADDING_NATURE + ":\n"
129: + SelectionUtil.getStatusMessages(e));
130: } finally {
131: window.getShell().setCursor(null);
132: waitCursor.dispose();
133: }
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
140: * org.eclipse.jface.viewers.ISelection)
141: */
142: public void selectionChanged(IAction action, ISelection selection) {
143: currentJavaProject = SelectionUtil
144: .findSelectedJavaProject(selection);
145:
146: if (currentJavaProject == null) {
147: currentProject = org.apache.derby.ui.util.SelectionUtil
148: .findSelectedProject(selection);
149: }
150:
151: }
152:
153: }
|