001: /*
002:
003: Derby - Class org.apache.derby.ui.popup.actions.RemoveDerbyNature
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: package org.apache.derby.ui.popup.actions;
022:
023: import java.util.ArrayList;
024:
025: import org.apache.derby.ui.common.CommonNames;
026: import org.apache.derby.ui.common.Messages;
027: import org.apache.derby.ui.util.DerbyServerUtils;
028: import org.apache.derby.ui.util.DerbyUtils;
029: import org.apache.derby.ui.util.Logger;
030: import org.apache.derby.ui.util.SelectionUtil;
031: import org.eclipse.core.resources.IProject;
032: import org.eclipse.core.resources.IProjectDescription;
033: import org.eclipse.core.resources.IResource;
034: import org.eclipse.core.runtime.IStatus;
035: import org.eclipse.jdt.core.IClasspathEntry;
036: import org.eclipse.jdt.core.IJavaProject;
037: import org.eclipse.jdt.core.JavaCore;
038: import org.eclipse.jface.action.IAction;
039: import org.eclipse.jface.dialogs.MessageDialog;
040: import org.eclipse.jface.viewers.ISelection;
041: import org.eclipse.jface.window.ApplicationWindow;
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 RemoveDerbyNature implements IObjectActionDelegate {
049:
050: private IJavaProject currentJavaProject;
051: private IProject currentProject;
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
055: */
056: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
057: }
058:
059: private static String[] removeDerbyNature(String[] natures) {
060: ArrayList arrL = new ArrayList();
061:
062: for (int i = 0; i < natures.length; i++) {
063: if (!(natures[i].equalsIgnoreCase(CommonNames.DERBY_NATURE))) {
064: arrL.add(natures[i]);
065: }
066: }
067: String[] newNatures = new String[arrL.size()];
068: for (int i = 0; i < arrL.size(); i++) {
069: newNatures[i] = (String) arrL.get(i);
070: }
071: return newNatures;
072: }
073:
074: /* (non-Javadoc)
075: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
076: */
077: public void run(IAction action) {
078: IWorkbenchWindow window = PlatformUI.getWorkbench()
079: .getActiveWorkbenchWindow();
080: try {
081: ((ApplicationWindow) window)
082: .setStatus(Messages.REMOVING_NATURE);
083:
084: if (currentJavaProject == null) {
085: currentJavaProject = JavaCore.create(currentProject);
086: }
087: //Shutdown server if running for the current project
088: if (DerbyServerUtils.getDefault().getRunning(
089: currentJavaProject.getProject())) {
090: DerbyServerUtils.getDefault().stopDerbyServer(
091: currentJavaProject.getProject());
092: }
093: IClasspathEntry[] rawClasspath = currentJavaProject
094: .getRawClasspath();
095: currentJavaProject.setRawClasspath(DerbyUtils
096: .removeDerbyJars(rawClasspath), null);
097:
098: IProjectDescription description = currentJavaProject
099: .getProject().getDescription();
100: String[] natures = description.getNatureIds();
101:
102: description.setNatureIds(removeDerbyNature(natures));
103: currentJavaProject.getProject().setDescription(description,
104: null);
105: // refresh project so user sees changes
106: currentJavaProject.getProject().refreshLocal(
107: IResource.DEPTH_INFINITE, null);
108: ((ApplicationWindow) window)
109: .setStatus(Messages.DERBY_NATURE_REMOVED);
110: } catch (Exception e) {
111: Logger.log(Messages.ERROR_REMOVING_NATURE + " '"
112: + currentJavaProject.getProject().getName() + "': "
113: + e, IStatus.ERROR);
114:
115: Shell shell = new Shell();
116: MessageDialog.openInformation(shell,
117: CommonNames.PLUGIN_NAME,
118: Messages.ERROR_REMOVING_NATURE + ":\n"
119: + SelectionUtil.getStatusMessages(e));
120: }
121:
122: }
123:
124: /* (non-Javadoc)
125: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
126: */
127: public void selectionChanged(IAction action, ISelection selection) {
128: currentJavaProject = SelectionUtil
129: .findSelectedJavaProject(selection);
130: if (currentJavaProject == null) {
131: currentProject = org.apache.derby.ui.util.SelectionUtil
132: .findSelectedProject(selection);
133: }
134: }
135:
136: }
|