1 11 package org.eclipse.jdt.internal.ui.jarpackager; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.MultiStatus; 21 import org.eclipse.core.runtime.Status; 22 23 import org.eclipse.core.resources.IFile; 24 25 import org.eclipse.swt.widgets.Shell; 26 27 import org.eclipse.jface.action.IAction; 28 import org.eclipse.jface.dialogs.ErrorDialog; 29 30 import org.eclipse.ui.PlatformUI; 31 32 import org.eclipse.jdt.internal.corext.util.Messages; 33 34 import org.eclipse.jdt.ui.jarpackager.IJarDescriptionReader; 35 import org.eclipse.jdt.ui.jarpackager.IJarExportRunnable; 36 import org.eclipse.jdt.ui.jarpackager.JarPackageData; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 40 41 public class CreateJarActionDelegate extends JarPackageActionDelegate { 42 43 46 public void run(IAction action) { 47 IFile[] descriptions= getDescriptionFiles(getSelection()); 48 MultiStatus mergedStatus; 49 int length= descriptions.length; 50 if (length < 1) 51 return; 52 53 String message; 55 if (length > 1) 56 message= JarPackagerMessages.JarFileExportOperation_creationOfSomeJARsFailed; 57 else 58 message= JarPackagerMessages.JarFileExportOperation_jarCreationFailed; 59 MultiStatus readStatus= new MultiStatus(JavaPlugin.getPluginId(), 0, message, null); 60 JarPackageData[] jarPackages= readJarPackages(descriptions, readStatus); 61 if (jarPackages.length > 0) { 62 IStatus status= export(jarPackages); 63 if (status == null) 64 return; if (readStatus.getSeverity() == IStatus.ERROR) 66 message= readStatus.getMessage(); 67 else 68 message= status.getMessage(); 69 mergedStatus= new MultiStatus(JavaPlugin.getPluginId(), status.getCode(), readStatus.getChildren(), message, null); 71 mergedStatus.merge(status); 72 } else 73 mergedStatus= readStatus; 74 75 if (!mergedStatus.isOK()) 76 ErrorDialog.openError(getShell(), JarPackagerMessages.CreateJarActionDelegate_jarExport_title, null, mergedStatus); 77 } 78 79 private JarPackageData[] readJarPackages(IFile[] descriptions, MultiStatus readStatus) { 80 List jarPackagesList= new ArrayList (descriptions.length); 81 for (int i= 0; i < descriptions.length; i++) { 82 JarPackageData jarPackage= readJarPackage(descriptions[i], readStatus); 83 if (jarPackage != null) 84 jarPackagesList.add(jarPackage); 85 } 86 return (JarPackageData[])jarPackagesList.toArray(new JarPackageData[jarPackagesList.size()]); 87 } 88 89 private IStatus export(JarPackageData[] jarPackages) { 90 Shell shell= getShell(); 91 IJarExportRunnable op= jarPackages[0].createJarExportRunnable(jarPackages, shell); 92 try { 93 PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(false, true, op); 94 } catch (InvocationTargetException ex) { 96 if (ex.getTargetException() != null) { 97 ExceptionHandler.handle(ex, shell, JarPackagerMessages.CreateJarActionDelegate_jarExportError_title, JarPackagerMessages.CreateJarActionDelegate_jarExportError_message); 98 return null; 99 } 100 } catch (InterruptedException e) { 101 return null; 103 } 104 return op.getStatus(); 105 } 106 107 110 protected JarPackageData readJarPackage(IFile description, MultiStatus readStatus) { 111 Assert.isLegal(description.isAccessible()); 112 Assert.isNotNull(description.getFileExtension()); 113 Assert.isLegal(description.getFileExtension().equals(JarPackagerUtil.DESCRIPTION_EXTENSION)); 114 JarPackageData jarPackage= new JarPackageData(); 115 IJarDescriptionReader reader= null; 116 try { 117 reader= jarPackage.createJarDescriptionReader(description.getContents()); 118 reader.read(jarPackage); 120 jarPackage.setSaveManifest(false); 121 jarPackage.setSaveDescription(false); 122 } catch (CoreException ex) { 123 String message= Messages.format(JarPackagerMessages.JarFileExportOperation_errorReadingFile, new Object [] {description.getFullPath(), ex.getStatus().getMessage()}); 124 addToStatus(readStatus, jarPackage, message, ex); 125 return null; 126 } finally { 127 if (reader != null) 128 readStatus.addAll(reader.getStatus()); 130 try { 131 if (reader != null) 132 reader.close(); 133 } 134 catch (CoreException ex) { 135 String message= Messages.format(JarPackagerMessages.JarFileExportOperation_errorClosingJarPackageDescriptionReader, description.getFullPath()); 136 addToStatus(readStatus, jarPackage, message, ex); 137 } 138 } 139 return jarPackage; 140 } 141 142 protected void addToStatus(MultiStatus multiStatus, JarPackageData jarPackage, String defaultMessage, CoreException ex) { 143 IStatus status= ex.getStatus(); 144 String message= ex.getLocalizedMessage(); 145 if (message == null || message.length() < 1) 146 status= new Status(status.getSeverity(), status.getPlugin(), status.getCode(), defaultMessage, ex); 147 multiStatus.add(status); 148 } 149 } 150 | Popular Tags |