KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > CreateJarActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.jarpackager;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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     /*
44      * @see IActionDelegate
45      */

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         // Create read multi status
54
String JavaDoc 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; // cancelled
65
if (readStatus.getSeverity() == IStatus.ERROR)
66                 message= readStatus.getMessage();
67             else
68                 message= status.getMessage();
69             // Create new status because we want another message - no API to set message
70
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 JavaDoc jarPackagesList= new ArrayList JavaDoc(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             //PlatformUI.getWorkbench().getProgressService().run(false, true, op); // see bug 118152
95
} catch (InvocationTargetException JavaDoc 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 JavaDoc e) {
101             // do nothing on cancel
102
return null;
103         }
104         return op.getStatus();
105     }
106     
107     /**
108      * Reads the JAR package spec from file.
109      */

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             // Do not save - only generate JAR
119
reader.read(jarPackage);
120             jarPackage.setSaveManifest(false);
121             jarPackage.setSaveDescription(false);
122         } catch (CoreException ex) {
123                 String JavaDoc message= Messages.format(JarPackagerMessages.JarFileExportOperation_errorReadingFile, new Object JavaDoc[] {description.getFullPath(), ex.getStatus().getMessage()});
124                 addToStatus(readStatus, jarPackage, message, ex);
125                 return null;
126         } finally {
127             if (reader != null)
128                 // AddWarnings
129
readStatus.addAll(reader.getStatus());
130             try {
131                 if (reader != null)
132                     reader.close();
133             }
134             catch (CoreException ex) {
135                 String JavaDoc 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 JavaDoc defaultMessage, CoreException ex) {
143         IStatus status= ex.getStatus();
144         String JavaDoc 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