]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.ArrayList; | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.swt.widgets.Shell; | |
18 | ||
19 | import org.eclipse.core.runtime.Assert; | |
20 | import org.eclipse.core.runtime.CoreException; | |
21 | import org.eclipse.core.runtime.IStatus; | |
22 | import org.eclipse.core.runtime.MultiStatus; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | ||
25 | import org.eclipse.core.resources.IFile; | |
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 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
41 | ||
42 | public class CreateJarActionDelegate extends JarPackageActionDelegate { | |
43 | ||
44 | /* | |
45 | * @see IActionDelegate | |
46 | */ | |
47 | public void run(IAction action) { | |
48 | IFile[] descriptions= getDescriptionFiles(getSelection()); | |
49 | MultiStatus mergedStatus; | |
50 | int length= descriptions.length; | |
51 | if (length < 1) | |
52 | return; | |
53 | ||
54 | // Create read multi status | |
55 | String message; | |
56 | if (length > 1) | |
57 | message= JarPackagerMessages.JarFileExportOperation_creationOfSomeJARsFailed; | |
58 | else | |
59 | message= JarPackagerMessages.JarFileExportOperation_jarCreationFailed; | |
60 | MultiStatus readStatus= new MultiStatus(JavaPlugin.getPluginId(), 0, message, null); | |
61 | JarPackageData[] jarPackages= readJarPackages(descriptions, readStatus); | |
62 | if (jarPackages.length > 0) { | |
63 | IStatus status= export(jarPackages); | |
64 | if (status == null) | |
65 | return; // cancelled | |
66 | if (readStatus.getSeverity() == IStatus.ERROR) | |
67 | message= readStatus.getMessage(); | |
68 | else | |
69 | message= status.getMessage(); | |
70 | // Create new status because we want another message - no API to set message | |
71 | mergedStatus= new MultiStatus(JavaPlugin.getPluginId(), status.getCode(), readStatus.getChildren(), message, null); | |
72 | mergedStatus.merge(status); | |
73 | } else | |
74 | mergedStatus= readStatus; | |
75 | ||
76 | if (!mergedStatus.isOK()) | |
77 | ErrorDialog.openError(getShell(), JarPackagerMessages.CreateJarActionDelegate_jarExport_title, null, mergedStatus); | |
78 | } | |
79 | ||
80 | private JarPackageData[] readJarPackages(IFile[] descriptions, MultiStatus readStatus) { | |
81 | List<JarPackageData> jarPackagesList= new ArrayList<JarPackageData>(descriptions.length); | |
82 | for (int i= 0; i < descriptions.length; i++) { | |
83 | JarPackageData jarPackage= readJarPackage(descriptions[i], readStatus); | |
84 | if (jarPackage != null) | |
85 | jarPackagesList.add(jarPackage); | |
86 | } | |
87 | return jarPackagesList.toArray(new JarPackageData[jarPackagesList.size()]); | |
88 | } | |
89 | ||
90 | private IStatus export(JarPackageData[] jarPackages) { | |
91 | Shell shell= getShell(); | |
92 | IJarExportRunnable op= jarPackages[0].createJarExportRunnable(jarPackages, shell); | |
93 | try { | |
94 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(false, true, op); | |
95 | //PlatformUI.getWorkbench().getProgressService().run(false, true, op); // see bug 118152 | |
96 | } catch (InvocationTargetException ex) { | |
97 | if (ex.getTargetException() != null) { | |
98 | ExceptionHandler.handle(ex, shell, JarPackagerMessages.CreateJarActionDelegate_jarExportError_title, JarPackagerMessages.CreateJarActionDelegate_jarExportError_message); | |
99 | return null; | |
100 | } | |
101 | } catch (InterruptedException e) { | |
102 | // do nothing on cancel | |
103 | return null; | |
104 | } | |
105 | return op.getStatus(); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Reads the JAR package spec from file. | |
110 | * @param description the description file | |
111 | * @param readStatus status | |
112 | * @return returns the read the JAR package spec | |
113 | */ | |
114 | protected JarPackageData readJarPackage(IFile description, MultiStatus readStatus) { | |
115 | Assert.isLegal(description.isAccessible()); | |
116 | Assert.isNotNull(description.getFileExtension()); | |
117 | Assert.isLegal(description.getFileExtension().equals(JarPackagerUtil.DESCRIPTION_EXTENSION)); | |
118 | JarPackageData jarPackage= new JarPackageData(); | |
119 | IJarDescriptionReader reader= null; | |
120 | try { | |
121 | reader= jarPackage.createJarDescriptionReader(description.getContents()); | |
122 | // Do not save - only generate JAR | |
123 | reader.read(jarPackage); | |
124 | jarPackage.setSaveManifest(false); | |
125 | jarPackage.setSaveDescription(false); | |
126 | } catch (CoreException ex) { | |
127 | String message= Messages.format(JarPackagerMessages.JarFileExportOperation_errorReadingFile, new Object[] {BasicElementLabels.getPathLabel(description.getFullPath(), false), ex.getStatus().getMessage()}); | |
128 | addToStatus(readStatus, message, ex); | |
129 | return null; | |
130 | } finally { | |
131 | if (reader != null) | |
132 | // AddWarnings | |
133 | readStatus.addAll(reader.getStatus()); | |
134 | try { | |
135 | if (reader != null) | |
136 | reader.close(); | |
137 | } | |
138 | catch (CoreException ex) { | |
139 | String message= Messages.format(JarPackagerMessages.JarFileExportOperation_errorClosingJarPackageDescriptionReader, BasicElementLabels.getPathLabel(description.getFullPath(), false)); | |
140 | addToStatus(readStatus, message, ex); | |
141 | } | |
142 | } | |
143 | return jarPackage; | |
144 | } | |
145 | ||
146 | private void addToStatus(MultiStatus multiStatus, String defaultMessage, CoreException ex) { | |
147 | IStatus status= ex.getStatus(); | |
148 | String message= ex.getLocalizedMessage(); | |
149 | if (message == null || message.length() < 1) | |
150 | status= new Status(status.getSeverity(), status.getPlugin(), status.getCode(), defaultMessage, ex); | |
151 | multiStatus.add(status); | |
152 | } | |
153 | } |