Lines 10-21
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.wst.common.snippets.internal.palette; |
11 |
package org.eclipse.wst.common.snippets.internal.palette; |
12 |
|
12 |
|
|
|
13 |
import java.io.BufferedInputStream; |
14 |
import java.io.BufferedOutputStream; |
15 |
import java.io.File; |
16 |
import java.io.FileInputStream; |
13 |
import java.io.FileNotFoundException; |
17 |
import java.io.FileNotFoundException; |
14 |
import java.io.FileOutputStream; |
18 |
import java.io.FileOutputStream; |
15 |
import java.io.IOException; |
19 |
import java.io.IOException; |
|
|
20 |
import java.io.InputStream; |
16 |
import java.io.OutputStream; |
21 |
import java.io.OutputStream; |
|
|
22 |
import java.util.Enumeration; |
17 |
import java.util.List; |
23 |
import java.util.List; |
|
|
24 |
import java.util.zip.ZipEntry; |
25 |
import java.util.zip.ZipException; |
26 |
import java.util.zip.ZipFile; |
27 |
import java.util.zip.ZipOutputStream; |
18 |
|
28 |
|
|
|
29 |
import org.eclipse.core.runtime.Platform; |
19 |
import org.eclipse.gef.palette.PaletteDrawer; |
30 |
import org.eclipse.gef.palette.PaletteDrawer; |
20 |
import org.eclipse.gef.palette.PaletteEntry; |
31 |
import org.eclipse.gef.palette.PaletteEntry; |
21 |
import org.eclipse.gef.palette.PaletteRoot; |
32 |
import org.eclipse.gef.palette.PaletteRoot; |
Lines 32-47
Link Here
|
32 |
import org.eclipse.swt.widgets.Shell; |
43 |
import org.eclipse.swt.widgets.Shell; |
33 |
import org.eclipse.ui.PlatformUI; |
44 |
import org.eclipse.ui.PlatformUI; |
34 |
import org.eclipse.wst.common.snippets.core.ISnippetCategory; |
45 |
import org.eclipse.wst.common.snippets.core.ISnippetCategory; |
|
|
46 |
import org.eclipse.wst.common.snippets.core.ISnippetItem; |
35 |
import org.eclipse.wst.common.snippets.internal.IHelpContextIds; |
47 |
import org.eclipse.wst.common.snippets.internal.IHelpContextIds; |
36 |
import org.eclipse.wst.common.snippets.internal.SnippetDefinitions; |
48 |
import org.eclipse.wst.common.snippets.internal.SnippetDefinitions; |
37 |
import org.eclipse.wst.common.snippets.internal.SnippetsMessages; |
49 |
import org.eclipse.wst.common.snippets.internal.SnippetsMessages; |
|
|
50 |
import org.eclipse.wst.common.snippets.internal.SnippetsPlugin; |
38 |
import org.eclipse.wst.common.snippets.internal.SnippetsPluginImageHelper; |
51 |
import org.eclipse.wst.common.snippets.internal.SnippetsPluginImageHelper; |
39 |
import org.eclipse.wst.common.snippets.internal.SnippetsPluginImages; |
52 |
import org.eclipse.wst.common.snippets.internal.SnippetsPluginImages; |
40 |
import org.eclipse.wst.common.snippets.internal.model.SnippetManager; |
53 |
import org.eclipse.wst.common.snippets.internal.model.SnippetManager; |
41 |
import org.eclipse.wst.common.snippets.internal.ui.SnippetsCustomizer; |
54 |
import org.eclipse.wst.common.snippets.internal.ui.SnippetsCustomizer; |
|
|
55 |
import org.osgi.framework.Bundle; |
42 |
|
56 |
|
43 |
public class SnippetCustomizerDialog extends PaletteCustomizerDialog { |
57 |
public class SnippetCustomizerDialog extends PaletteCustomizerDialog { |
44 |
|
58 |
|
|
|
59 |
private enum EXPORT_IMPORT_STRATEGY { |
60 |
ARCHIVE, XML; |
61 |
} |
62 |
|
45 |
private class ExportAction extends PaletteCustomizationAction { |
63 |
private class ExportAction extends PaletteCustomizationAction { |
46 |
public ExportAction() { |
64 |
public ExportAction() { |
47 |
setEnabled(false); |
65 |
setEnabled(false); |
Lines 54-86
Link Here
|
54 |
|
72 |
|
55 |
protected void handleExport() { |
73 |
protected void handleExport() { |
56 |
PaletteDrawer exportCategory = (PaletteDrawer) getSelectedPaletteEntry(); |
74 |
PaletteDrawer exportCategory = (PaletteDrawer) getSelectedPaletteEntry(); |
|
|
75 |
EXPORT_IMPORT_STRATEGY strategy = exportStrategy(exportCategory); |
57 |
|
76 |
|
58 |
final FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE); |
77 |
if (EXPORT_IMPORT_STRATEGY.ARCHIVE == strategy){ |
59 |
fileDialog.setText(SnippetsMessages.Export_Snippets); |
78 |
exportArchive(exportCategory); |
60 |
fileDialog.setFileName("snippets.xml"); //$NON-NLS-1$ |
79 |
} else { |
61 |
String[] filterExtensions = new String[2]; |
80 |
exportXML(exportCategory); |
62 |
filterExtensions[0] = "*.xml"; //$NON-NLS-1$ |
81 |
} |
63 |
filterExtensions[1] = "*.*"; //$NON-NLS-1$ |
82 |
updateActions(); |
64 |
fileDialog.setFilterExtensions(filterExtensions); |
83 |
} |
65 |
String filename = fileDialog.open(); |
84 |
|
|
|
85 |
private EXPORT_IMPORT_STRATEGY exportStrategy(PaletteDrawer exportCategory) { |
86 |
List<ISnippetItem> children = exportCategory.getChildren(); |
87 |
for (ISnippetItem snippetItem : children) { |
88 |
File folder = new File(SnippetManager.getInstance().getStorage(snippetItem.getId()).toOSString()); |
89 |
if (folder.exists()){ |
90 |
return EXPORT_IMPORT_STRATEGY.ARCHIVE; |
91 |
} |
92 |
} |
93 |
return EXPORT_IMPORT_STRATEGY.XML; |
94 |
} |
95 |
|
96 |
private void exportArchive(PaletteDrawer exportCategory) { |
97 |
String filename = openFileDialog("*.zip");//$NON-NLS-1$ |
66 |
if (filename != null) { |
98 |
if (filename != null) { |
67 |
SnippetDefinitions definitions = ModelFactoryForUser.getInstance().load(filename); |
99 |
ZipOutputStream outputStream = null; |
68 |
ISnippetCategory existingCategory = definitions.getCategory(exportCategory.getId()); |
100 |
try { |
|
|
101 |
SnippetDefinitions definitions = getCategory(exportCategory, filename); |
102 |
outputStream = new ZipOutputStream(new FileOutputStream(filename)); |
103 |
ZipEntry descriptorFile = new ZipEntry("snippets.xml"); |
104 |
outputStream.putNextEntry(descriptorFile); |
105 |
new UserModelDumper().write(definitions, outputStream); |
106 |
ISnippetCategory existingCategory = definitions.getCategory(exportCategory.getId()); |
107 |
ISnippetItem[] items = existingCategory.getItems(); |
108 |
for (ISnippetItem item : items) { |
109 |
File folder = new File(SnippetManager.getInstance().getStorage(item.getId()).toOSString()); |
110 |
if (folder.exists()){ |
111 |
addToZip(folder.getParentFile(), folder, outputStream); |
112 |
} |
113 |
} |
114 |
} |
115 |
catch (FileNotFoundException e) { |
116 |
// should not have problem finding the output file |
117 |
e.printStackTrace(); |
118 |
} catch (IOException e) { |
119 |
e.printStackTrace(); |
120 |
} |
121 |
finally { |
122 |
if (outputStream != null) |
123 |
try { |
124 |
outputStream.close(); |
125 |
} |
126 |
catch (IOException e) { |
127 |
// should not have problem closing the output file |
128 |
e.printStackTrace(); |
129 |
} |
130 |
} |
131 |
} |
132 |
} |
69 |
|
133 |
|
70 |
if (existingCategory == null) |
134 |
private void addToZip(File root, File folder, ZipOutputStream outputStream) throws IOException { |
71 |
definitions.getCategories().add(exportCategory); |
135 |
File[] listFiles = folder.listFiles(); |
72 |
else { |
136 |
for (File file : listFiles) { |
73 |
String title = SnippetsMessages.SnippetCustomizerDialog_2; //$NON-NLS-1$ |
137 |
if (file.isDirectory()){ |
74 |
String message = NLS.bind(SnippetsMessages.SnippetCustomizerDialog_4, new String[]{existingCategory.getLabel()}); |
138 |
addToZip(root, file, outputStream); |
75 |
boolean answer = MessageDialog.openConfirm(getShell(), title, message); |
139 |
} else { |
76 |
if (answer) { |
140 |
ZipEntry ze = new ZipEntry(file.getAbsolutePath().substring(root.getAbsolutePath().length()+1)); |
77 |
definitions.getCategories().remove(existingCategory); |
141 |
outputStream.putNextEntry(ze); |
78 |
definitions.getCategories().add(exportCategory); |
142 |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), 1024); |
|
|
143 |
byte[] data = new byte[1024]; |
144 |
int count; |
145 |
while((count = bis.read(data,0,1024)) != -1) { |
146 |
outputStream.write(data, 0, count); |
79 |
} |
147 |
} |
|
|
148 |
bis.close(); |
80 |
} |
149 |
} |
|
|
150 |
} |
151 |
} |
81 |
|
152 |
|
82 |
OutputStream outputStream = null; |
153 |
private void exportXML(PaletteDrawer exportCategory) { |
83 |
try { |
154 |
String filename = openFileDialog("*.xml");//$NON-NLS-1$ |
|
|
155 |
|
156 |
OutputStream outputStream = null; |
157 |
if (filename != null) { |
158 |
try{ |
159 |
SnippetDefinitions definitions = getCategory(exportCategory, filename); |
84 |
outputStream = new FileOutputStream(filename); |
160 |
outputStream = new FileOutputStream(filename); |
85 |
new UserModelDumper().write(definitions, outputStream); |
161 |
new UserModelDumper().write(definitions, outputStream); |
86 |
} |
162 |
} |
Lines 93-106
Link Here
|
93 |
try { |
169 |
try { |
94 |
outputStream.close(); |
170 |
outputStream.close(); |
95 |
} |
171 |
} |
96 |
catch (IOException e) { |
172 |
catch (IOException e) { |
97 |
// should not have problem closing the output file |
173 |
// should not have problem closing the output file |
98 |
e.printStackTrace(); |
174 |
e.printStackTrace(); |
99 |
} |
175 |
} |
100 |
} |
176 |
} |
101 |
|
177 |
|
102 |
updateActions(); |
|
|
103 |
} |
178 |
} |
|
|
179 |
|
180 |
|
181 |
} |
182 |
|
183 |
private SnippetDefinitions getCategory(PaletteDrawer exportCategory, |
184 |
String fileName) { |
185 |
SnippetDefinitions definitions = ModelFactoryForUser.getInstance().load(fileName); |
186 |
ISnippetCategory existingCategory = definitions.getCategory(exportCategory.getId()); |
187 |
|
188 |
if (existingCategory == null) |
189 |
definitions.getCategories().add(exportCategory); |
190 |
else { |
191 |
String title = SnippetsMessages.SnippetCustomizerDialog_2; //$NON-NLS-1$ |
192 |
String message = NLS.bind(SnippetsMessages.SnippetCustomizerDialog_4, new String[]{existingCategory.getLabel()}); |
193 |
boolean answer = MessageDialog.openConfirm(getShell(), title, message); |
194 |
if (answer) { |
195 |
definitions.getCategories().remove(existingCategory); |
196 |
definitions.getCategories().add(exportCategory); |
197 |
} |
198 |
} |
199 |
return definitions; |
200 |
} |
201 |
|
202 |
private String openFileDialog(String extension) { |
203 |
final FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE); |
204 |
fileDialog.setText(SnippetsMessages.Export_Snippets); |
205 |
fileDialog.setFileName("snippets"+ extension.substring(1)); //$NON-NLS-1$ |
206 |
String[] filterExtensions = new String[2]; |
207 |
filterExtensions[0] = extension; |
208 |
filterExtensions[1] = "*.*"; //$NON-NLS-1$ |
209 |
fileDialog.setFilterExtensions(filterExtensions); |
210 |
String filename = fileDialog.open(); |
211 |
return filename; |
104 |
} |
212 |
} |
105 |
|
213 |
|
106 |
public void run() { |
214 |
public void run() { |
Lines 132-145
Link Here
|
132 |
protected void handleImport() { |
240 |
protected void handleImport() { |
133 |
final FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN); |
241 |
final FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN); |
134 |
fileDialog.setText(SnippetsMessages.Import_Snippets); |
242 |
fileDialog.setText(SnippetsMessages.Import_Snippets); |
135 |
fileDialog.setFileName("snippets.xml"); //$NON-NLS-1$ |
243 |
// fileDialog.setFileName("snippets.xml"); //$NON-NLS-1$ |
136 |
String[] filterExtensions = new String[2]; |
244 |
String[] filterExtensions = new String[2]; |
137 |
filterExtensions[0] = "*.xml"; //$NON-NLS-1$ |
245 |
filterExtensions[0] = "*.xml; *.zip"; //$NON-NLS-1$ |
138 |
filterExtensions[1] = "*.*"; //$NON-NLS-1$ |
246 |
filterExtensions[1] = "*.*"; //$NON-NLS-1$ |
139 |
fileDialog.setFilterExtensions(filterExtensions); |
247 |
fileDialog.setFilterExtensions(filterExtensions); |
140 |
String filename = fileDialog.open(); |
248 |
String filename = fileDialog.open(); |
141 |
if (filename != null) { |
249 |
try { |
142 |
SnippetDefinitions definitions = ModelFactoryForUser.getInstance().load(filename); |
250 |
if (filename.toLowerCase().endsWith(".zip")){ |
|
|
251 |
ZipFile zip = new ZipFile(new File(filename)); |
252 |
ZipEntry entry = zip.getEntry("snippets.xml"); |
253 |
loadMetadata(zip.getInputStream(entry)); |
254 |
Bundle bundle = Platform.getBundle(SnippetsPlugin.BUNDLE_ID); |
255 |
unzip(zip, Platform.getStateLocation(bundle).toOSString()); |
256 |
} else { |
257 |
loadMetadata(new FileInputStream(filename)); |
258 |
} |
259 |
|
260 |
|
261 |
} catch (FileNotFoundException e) { |
262 |
e.printStackTrace(); |
263 |
} catch (ZipException e) { |
264 |
// TODO Auto-generated catch block |
265 |
e.printStackTrace(); |
266 |
} catch (IOException e) { |
267 |
// TODO Auto-generated catch block |
268 |
e.printStackTrace(); |
269 |
} |
270 |
} |
271 |
|
272 |
private void loadMetadata(InputStream fileInputStream) { |
273 |
if (fileInputStream != null) { |
274 |
SnippetDefinitions definitions = ModelFactoryForUser.getInstance().load(fileInputStream); |
143 |
List importCategories = definitions.getCategories(); |
275 |
List importCategories = definitions.getCategories(); |
144 |
List currentCategories = SnippetManager.getInstance().getDefinitions().getCategories(); |
276 |
List currentCategories = SnippetManager.getInstance().getDefinitions().getCategories(); |
145 |
PaletteEntry lastImportEntry = null; |
277 |
PaletteEntry lastImportEntry = null; |
Lines 215-218
Link Here
|
215 |
|
347 |
|
216 |
return super.open(); |
348 |
return super.open(); |
217 |
} |
349 |
} |
|
|
350 |
|
351 |
public static final void copyInputStream(InputStream in, OutputStream out) |
352 |
throws IOException |
353 |
{ |
354 |
byte[] buffer = new byte[1024]; |
355 |
int len; |
356 |
|
357 |
while((len = in.read(buffer)) >= 0) |
358 |
out.write(buffer, 0, len); |
359 |
|
360 |
in.close(); |
361 |
out.close(); |
362 |
} |
363 |
|
364 |
public void unzip(ZipFile zipFile, String path) throws FileNotFoundException, IOException { |
365 |
Enumeration entries; |
366 |
entries = zipFile.entries(); |
367 |
|
368 |
while(entries.hasMoreElements()) { |
369 |
ZipEntry entry = (ZipEntry)entries.nextElement(); |
370 |
File file = new File(path + File.separator+ entry.getName()); |
371 |
if(entry.isDirectory()) { |
372 |
file.mkdir(); |
373 |
continue; |
374 |
} |
375 |
if (!file.getParentFile().exists()){ |
376 |
file.getParentFile().mkdirs(); |
377 |
} |
378 |
if (entry.getName().toLowerCase().equals("snippets.xml")){ |
379 |
continue; |
380 |
} |
381 |
|
382 |
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file))); |
383 |
} |
384 |
|
385 |
zipFile.close(); |
386 |
} |
387 |
|
218 |
} |
388 |
} |