| /****************************************************************************** |
| * Copyright (c) 2009 Red Hat, IBM |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Rob Stryker - initial implementation and ongoing maintenance |
| * Chuck Bridgham - additional support |
| ******************************************************************************/ |
| package org.eclipse.jst.j2ee.internal.ui; |
| |
| import java.lang.reflect.InvocationTargetException; |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.jst.j2ee.application.internal.operations.UpdateManifestDataModelProperties; |
| import org.eclipse.jst.j2ee.application.internal.operations.UpdateManifestDataModelProvider; |
| import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest; |
| import org.eclipse.jst.j2ee.internal.UpdateManifestOperation; |
| import org.eclipse.jst.j2ee.internal.common.classpath.J2EEComponentClasspathUpdater; |
| import org.eclipse.jst.j2ee.internal.componentcore.JavaEEModuleHandler; |
| import org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin; |
| import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities; |
| import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities; |
| import org.eclipse.wst.common.componentcore.internal.IModuleHandler; |
| import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; |
| import org.eclipse.wst.common.componentcore.ui.propertypage.AddModuleDependenciesPropertiesPage; |
| import org.eclipse.wst.common.componentcore.ui.propertypage.ModuleAssemblyRootPage; |
| import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory; |
| import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
| |
| public class J2EEModuleDependenciesPropertyPage extends |
| AddModuleDependenciesPropertiesPage { |
| |
| public J2EEModuleDependenciesPropertyPage(IProject project, |
| ModuleAssemblyRootPage page) { |
| super(project, page); |
| } |
| |
| @Override |
| public boolean postHandleChanges(IProgressMonitor monitor) { |
| return true; |
| } |
| |
| @Override |
| protected void handleRemoved(ArrayList<IVirtualComponent> removed) { |
| super.handleRemoved(removed); |
| |
| J2EEComponentClasspathUpdater.getInstance().queueUpdateEAR(rootComponent.getProject()); |
| } |
| |
| |
| @Override |
| protected String getAddFolderLabel() { |
| |
| if (JavaEEProjectUtilities.isEJBProject(project)) |
| return Messages.J2EEModuleDependenciesPropertyPage_0; |
| if (JavaEEProjectUtilities.isApplicationClientProject(project)) |
| return Messages.J2EEModuleDependenciesPropertyPage_1; |
| if (JavaEEProjectUtilities.isJCAProject(project)) |
| return Messages.J2EEModuleDependenciesPropertyPage_2; |
| return super.getAddFolderLabel(); |
| } |
| |
| @Override |
| protected String getModuleAssemblyRootPageDescription() { |
| |
| if (JavaEEProjectUtilities.isEJBProject(project)) |
| return Messages.J2EEModuleDependenciesPropertyPage_3; |
| if (JavaEEProjectUtilities.isApplicationClientProject(project)) |
| return Messages.J2EEModuleDependenciesPropertyPage_4; |
| if (JavaEEProjectUtilities.isJCAProject(project)) |
| return Messages.J2EEModuleDependenciesPropertyPage_5; |
| return super.getModuleAssemblyRootPageDescription(); |
| } |
| |
| |
| @Override |
| protected void addComponents(ArrayList<IVirtualComponent> components) throws CoreException { |
| // First add the components |
| super.addComponents(components); |
| // Now add to MANIFEST |
| addToManifest(components); |
| |
| } |
| |
| protected void addToManifest(ArrayList<IVirtualComponent> components) { |
| |
| StringBuffer newComps = getCompsForManifest(components); |
| if(newComps.toString().length() > 0) { |
| UpdateManifestOperation op = createManifestOperation(newComps.toString()); |
| try { |
| op.run(new NullProgressMonitor()); |
| } catch (InvocationTargetException e) { |
| J2EEUIPlugin.logError(e); |
| } catch (InterruptedException e) { |
| J2EEUIPlugin.logError(e); |
| } |
| } |
| } |
| |
| protected StringBuffer getCompsForManifest(ArrayList<IVirtualComponent> components) { |
| StringBuffer newComps = new StringBuffer(); |
| for (Iterator iterator = components.iterator(); iterator.hasNext();) { |
| IVirtualComponent comp = (IVirtualComponent) iterator.next(); |
| String archiveName = new Path(objectToRuntimePath.get(comp)).lastSegment(); |
| newComps.append(archiveName); |
| newComps.append(' '); |
| } |
| return newComps; |
| } |
| |
| protected UpdateManifestOperation createManifestOperation(String newComps) { |
| return new UpdateManifestOperation(project.getName(), newComps, false); |
| } |
| |
| @Override |
| protected void removeComponents(ArrayList<IVirtualComponent> removed) { |
| // First remove the components |
| super.removeComponents(removed); |
| // Now remove from MANIFEST |
| removeFromManifest(removed); |
| |
| } |
| |
| private void removeFromManifest(ArrayList<IVirtualComponent> removed) { |
| String sourceProjName = project.getName(); |
| IProgressMonitor monitor = new NullProgressMonitor(); |
| IFile manifestmf = J2EEProjectUtilities.getManifestFile(project); |
| ArchiveManifest mf = J2EEProjectUtilities.readManifest(project); |
| if (mf == null) |
| return; |
| IDataModel updateManifestDataModel = DataModelFactory.createDataModel(new UpdateManifestDataModelProvider()); |
| updateManifestDataModel.setProperty(UpdateManifestDataModelProperties.PROJECT_NAME, sourceProjName); |
| updateManifestDataModel.setBooleanProperty(UpdateManifestDataModelProperties.MERGE, false); |
| updateManifestDataModel.setProperty(UpdateManifestDataModelProperties.MANIFEST_FILE, manifestmf); |
| String[] cp = mf.getClassPathTokenized(); |
| List cpList = new ArrayList(); |
| |
| for (int i = 0; i < cp.length; i++) { |
| boolean foundMatch = false; |
| for (Iterator iterator = removed.iterator(); iterator.hasNext();) { |
| IVirtualComponent comp = (IVirtualComponent) iterator.next(); |
| String cpToRemove = new Path(oldComponentToRuntimePath.get(comp)).lastSegment(); |
| if (cp[i].equals(cpToRemove)) |
| foundMatch = true; |
| } |
| if (!foundMatch) |
| cpList.add(cp[i]); |
| } |
| updateManifestDataModel.setProperty(UpdateManifestDataModelProperties.JAR_LIST, cpList); |
| try { |
| updateManifestDataModel.getDefaultOperation().execute(monitor, null ); |
| } catch (ExecutionException e) { |
| J2EEUIPlugin.logError(e); |
| } |
| |
| } |
| |
| @Override |
| protected IModuleHandler getModuleHandler() { |
| if(moduleHandler == null) |
| moduleHandler = new JavaEEModuleHandler(); |
| return moduleHandler; |
| } |
| |
| |
| } |