Lines 33-40
Link Here
|
33 |
import org.eclipse.emf.common.util.URI; |
33 |
import org.eclipse.emf.common.util.URI; |
34 |
import org.eclipse.emf.ecore.EObject; |
34 |
import org.eclipse.emf.ecore.EObject; |
35 |
import org.eclipse.emf.ecore.resource.Resource; |
35 |
import org.eclipse.emf.ecore.resource.Resource; |
|
|
36 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
36 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
37 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
37 |
import org.eclipse.gmf.gmfgraph.Figure; |
38 |
import org.eclipse.gmf.gmfgraph.Canvas; |
|
|
39 |
import org.eclipse.gmf.gmfgraph.DiagramElement; |
38 |
import org.eclipse.gmf.gmfgraph.FigureGallery; |
40 |
import org.eclipse.gmf.gmfgraph.FigureGallery; |
39 |
import org.eclipse.gmf.gmfgraph.util.RuntimeFQNSwitch; |
41 |
import org.eclipse.gmf.gmfgraph.util.RuntimeFQNSwitch; |
40 |
import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator; |
42 |
import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator; |
Lines 59-80
Link Here
|
59 |
public static final String OPTION_MAIN_PACKAGE_NAME = SECTION_ID + ".mainPackageName"; |
61 |
public static final String OPTION_MAIN_PACKAGE_NAME = SECTION_ID + ".mainPackageName"; |
60 |
public static final String OPTION_NEEDS_MAP_MODE = SECTION_ID + ".needsMapMode"; |
62 |
public static final String OPTION_NEEDS_MAP_MODE = SECTION_ID + ".needsMapMode"; |
61 |
public static final String OPTION_INPUT_RESOURCE_FULL_PATH = SECTION_ID + ".inputResource"; |
63 |
public static final String OPTION_INPUT_RESOURCE_FULL_PATH = SECTION_ID + ".inputResource"; |
62 |
public static final String OPTION_OUTPUT_RESOURCE_FULL_PATH = SECTION_ID + ".outputResource"; |
64 |
public static final String OPTION_OUTPUT_GALLERY_FULL_PATH = SECTION_ID + ".outputGallery"; |
|
|
65 |
public static final String OPTION_OUTPUT_DIAGRAM_ELEMENTS_FULL_PATH = SECTION_ID + ".outputDiagramElements"; |
63 |
|
66 |
|
64 |
private TemplateOption myPackageNameOption; |
67 |
private TemplateOption myPackageNameOption; |
65 |
private FileNameOption myInputPathOption; |
68 |
private FileNameOption myInputPathOption; |
66 |
private FileNameOption myOutputGalleryPathOption; |
69 |
private FileNameOption myOutputGalleryPathOption; |
67 |
private final CachedInputValidationState myCachedInputValidationState; |
70 |
private FileNameOption myOutputDiagramElementsPathOption; |
|
|
71 |
private final InputValidationState myCachedInputValidationState; |
68 |
private ManifestElement[] myRequiredBundles; |
72 |
private ManifestElement[] myRequiredBundles; |
69 |
|
73 |
|
70 |
public ConverterSection(){ |
74 |
public ConverterSection(){ |
71 |
setPageCount(THE_ONLY_PAGE_INDEX + 1); |
75 |
setPageCount(THE_ONLY_PAGE_INDEX + 1); |
72 |
myPackageNameOption = addOption(OPTION_MAIN_PACKAGE_NAME, "Generate figures package", null, THE_ONLY_PAGE_INDEX); |
76 |
myPackageNameOption = addOption(OPTION_MAIN_PACKAGE_NAME, "Generate figures package", null, THE_ONLY_PAGE_INDEX); |
73 |
myInputPathOption = addFileNameOption(false, OPTION_INPUT_RESOURCE_FULL_PATH, "Input GMFGraph instance", "", THE_ONLY_PAGE_INDEX); |
77 |
myInputPathOption = addFileNameOption(false, OPTION_INPUT_RESOURCE_FULL_PATH, "Input GMFGraph instance", "", THE_ONLY_PAGE_INDEX); |
74 |
myOutputGalleryPathOption = addFileNameOption(true, OPTION_OUTPUT_RESOURCE_FULL_PATH, "Create Figure Gallery", "", THE_ONLY_PAGE_INDEX); |
78 |
myOutputGalleryPathOption = addFileNameOption(true, OPTION_OUTPUT_GALLERY_FULL_PATH, "Create Figure Gallery", "", THE_ONLY_PAGE_INDEX); |
75 |
myOutputGalleryPathOption.setRequired(false); |
79 |
myOutputGalleryPathOption.setRequired(false); |
|
|
80 |
myOutputDiagramElementsPathOption = addFileNameOption(true, OPTION_OUTPUT_DIAGRAM_ELEMENTS_FULL_PATH, "Mirror diagram elements", "", THE_ONLY_PAGE_INDEX); |
81 |
myOutputDiagramElementsPathOption.setRequired(false); |
76 |
addOption(OPTION_NEEDS_MAP_MODE, "Use IMapMode", false, THE_ONLY_PAGE_INDEX); |
82 |
addOption(OPTION_NEEDS_MAP_MODE, "Use IMapMode", false, THE_ONLY_PAGE_INDEX); |
77 |
myCachedInputValidationState = new CachedInputValidationState(); |
83 |
myCachedInputValidationState = new InputValidationState(myOutputGalleryPathOption, myOutputDiagramElementsPathOption); |
78 |
} |
84 |
} |
79 |
|
85 |
|
80 |
public void addPages(Wizard wizard) { |
86 |
public void addPages(Wizard wizard) { |
Lines 85-92
Link Here
|
85 |
wizard.addPage(page); |
91 |
wizard.addPage(page); |
86 |
markPagesAdded(); |
92 |
markPagesAdded(); |
87 |
validateOptions(myPackageNameOption); |
93 |
validateOptions(myPackageNameOption); |
88 |
validateOptions(myInputPathOption); |
|
|
89 |
validateOptions(myOutputGalleryPathOption); |
90 |
} |
94 |
} |
91 |
|
95 |
|
92 |
public IPluginReference[] getDependencies(String schemaVersion) { |
96 |
public IPluginReference[] getDependencies(String schemaVersion) { |
Lines 95-102
Link Here
|
95 |
} |
99 |
} |
96 |
|
100 |
|
97 |
protected void generateFiles(IProgressMonitor monitor) throws CoreException { |
101 |
protected void generateFiles(IProgressMonitor monitor) throws CoreException { |
98 |
Resource resource = loadResource(myInputPathOption.getText()); |
102 |
Resource input = loadResource(myInputPathOption.getText()); |
99 |
FigureGallery[] figures = findFigures(resource); |
103 |
FigureGallery[] figures = findFigures(input); |
|
|
104 |
assert(figures.length > 0); |
100 |
StandaloneGenerator.Config config = new StandaloneGeneratorConfigAdapter(this); |
105 |
StandaloneGenerator.Config config = new StandaloneGeneratorConfigAdapter(this); |
101 |
StandaloneGenerator generator = new StandaloneGenerator(figures, config, new RuntimeFQNSwitch()); |
106 |
StandaloneGenerator generator = new StandaloneGenerator(figures, config, new RuntimeFQNSwitch()); |
102 |
generator.setSkipPluginStructure(false); |
107 |
generator.setSkipPluginStructure(false); |
Lines 105-110
Link Here
|
105 |
readRequiredBundles(); |
110 |
readRequiredBundles(); |
106 |
// XXX readBuildProperties() and use getNewFiles to propagate |
111 |
// XXX readBuildProperties() and use getNewFiles to propagate |
107 |
// XXX readPluginProperties(), use ??? |
112 |
// XXX readPluginProperties(), use ??? |
|
|
113 |
if (!generator.getRunStatus().isOK()){ |
114 |
throw new CoreException(generator.getRunStatus()); |
115 |
} |
116 |
createSeparateResources(generator.getGenerationInfo(), input); |
108 |
} catch (InterruptedException e) { |
117 |
} catch (InterruptedException e) { |
109 |
String message = e.getMessage(); |
118 |
String message = e.getMessage(); |
110 |
if (message == null){ |
119 |
if (message == null){ |
Lines 115-143
Link Here
|
115 |
// perhaps, don't need to treat this as error? |
124 |
// perhaps, don't need to treat this as error? |
116 |
throw new CoreException(new Status(IStatus.ERROR, MY_PLUGIN_ID, 0, "Failed to read generated manifest.mf", ex)); |
125 |
throw new CoreException(new Status(IStatus.ERROR, MY_PLUGIN_ID, 0, "Failed to read generated manifest.mf", ex)); |
117 |
} finally { |
126 |
} finally { |
118 |
resource.unload(); |
127 |
input.unload(); |
119 |
} |
|
|
120 |
if (!generator.getRunStatus().isOK()){ |
121 |
throw new CoreException(generator.getRunStatus()); |
122 |
} |
128 |
} |
123 |
createFigureGallery(generator.getGenerationInfo()); |
|
|
124 |
} |
129 |
} |
125 |
|
130 |
|
126 |
private void createFigureGallery(StandaloneGenerator.GenerationInfo info) throws CoreException { |
131 |
private void createSeparateResources(StandaloneGenerator.GenerationInfo info, Resource input) throws CoreException { |
127 |
if (!myOutputGalleryPathOption.isEmpty()){ |
132 |
if (shouldGenerate(myOutputGalleryPathOption)){ |
128 |
String path = myOutputGalleryPathOption.getText(); |
133 |
String figureGalleryPath = myOutputGalleryPathOption.getText(); |
129 |
Resource galleryResource = new ResourceSetImpl().createResource(URI.createFileURI(path)); |
134 |
ResourceSet separateResourceSet = new ResourceSetImpl(); |
130 |
galleryResource.getContents().add(new StandaloneGalleryConverter().convertFigureGallery(info)); |
135 |
StandaloneGalleryConverter converter = new StandaloneGalleryConverter(info); |
|
|
136 |
|
137 |
Resource galleryResource = separateResourceSet.createResource(URI.createFileURI(figureGalleryPath)); |
138 |
galleryResource.getContents().add(converter.convertFigureGallery()); |
139 |
|
140 |
Resource diagramElementsResource = null; |
141 |
if (shouldGenerate(myOutputDiagramElementsPathOption)){ |
142 |
Canvas mirror = converter.mirrorDiagramElements(Collections.singleton(input)); |
143 |
if (mirror != null){ |
144 |
diagramElementsResource = separateResourceSet.createResource(URI.createFileURI(myOutputDiagramElementsPathOption.getText())); |
145 |
diagramElementsResource.getContents().add(mirror); |
146 |
} |
147 |
} |
148 |
|
131 |
try { |
149 |
try { |
132 |
galleryResource.save(null); |
150 |
galleryResource.save(null); |
|
|
151 |
if (diagramElementsResource != null){ |
152 |
diagramElementsResource.save(null); |
153 |
} |
133 |
} catch (IOException e) { |
154 |
} catch (IOException e) { |
134 |
throw new CoreException(new Status(// |
155 |
throw new CoreException(new Status(IStatus.ERROR, MY_PLUGIN_ID, 0, e.getMessage(), e)); |
135 |
IStatus.ERROR, MY_PLUGIN_ID, 0, e.getMessage(), e |
|
|
136 |
)); |
137 |
} |
156 |
} |
138 |
} |
157 |
} |
139 |
} |
158 |
} |
140 |
|
159 |
|
|
|
160 |
private boolean shouldGenerate(FileNameOption option){ |
161 |
return option.isEnabled() && !option.isEmpty(); |
162 |
} |
163 |
|
141 |
private void readRequiredBundles() throws CoreException, IOException { |
164 |
private void readRequiredBundles() throws CoreException, IOException { |
142 |
try { |
165 |
try { |
143 |
IFile f = findGeneratedManifest(); |
166 |
IFile f = findGeneratedManifest(); |
Lines 161-167
Link Here
|
161 |
private FigureGallery[] findFigures(Resource resource) { |
184 |
private FigureGallery[] findFigures(Resource resource) { |
162 |
return new FigureFinder().findFigures(resource); |
185 |
return new FigureFinder().findFigures(resource); |
163 |
} |
186 |
} |
164 |
|
187 |
|
165 |
public String getPluginActivatorClassFQN(){ |
188 |
public String getPluginActivatorClassFQN(){ |
166 |
return model instanceof IPluginModel ? ((IPluginModel)model).getPlugin().getClassName() : null; |
189 |
return model instanceof IPluginModel ? ((IPluginModel)model).getPlugin().getClassName() : null; |
167 |
} |
190 |
} |
Lines 191-206
Link Here
|
191 |
//does not affect state |
214 |
//does not affect state |
192 |
return; |
215 |
return; |
193 |
} |
216 |
} |
194 |
if (!validatePackageName()){ |
217 |
if (validateInputPath() && validatePackageName() && |
195 |
return; |
218 |
validateOutputOption(myOutputDiagramElementsPathOption) && |
196 |
} |
219 |
validateOutputOption(myOutputGalleryPathOption)){ |
197 |
if (!validateInputPath()){ |
220 |
resetPageState(); |
198 |
return; |
|
|
199 |
} |
221 |
} |
200 |
if (!validateOutputGalleryPath()){ |
|
|
201 |
return; |
202 |
} |
203 |
resetPageState(); |
204 |
} |
222 |
} |
205 |
|
223 |
|
206 |
public boolean isDependentOnParentWizard() { |
224 |
public boolean isDependentOnParentWizard() { |
Lines 281-319
Link Here
|
281 |
private boolean validateInputPath() { |
299 |
private boolean validateInputPath() { |
282 |
if (myInputPathOption.isEmpty()){ |
300 |
if (myInputPathOption.isEmpty()){ |
283 |
flagMissingRequiredOption(myInputPathOption); |
301 |
flagMissingRequiredOption(myInputPathOption); |
|
|
302 |
myOutputDiagramElementsPathOption.setEnabled(false); |
303 |
myOutputGalleryPathOption.setEnabled(false); |
284 |
return false; |
304 |
return false; |
285 |
} |
305 |
} |
286 |
String path = myInputPathOption.getText(); |
306 |
String path = myInputPathOption.getText(); |
287 |
myCachedInputValidationState.updateInput(path); |
307 |
myCachedInputValidationState.updateInput(path); |
288 |
if (!myCachedInputValidationState.isValid()){ |
308 |
if (!myCachedInputValidationState.isValid()){ |
289 |
getTheOnlyPage().setPageComplete(false); |
309 |
flagError(myCachedInputValidationState.getErrorMessage()); |
290 |
getTheOnlyPage().setErrorMessage(myCachedInputValidationState.getErrorMessage()); |
|
|
291 |
return false; |
310 |
return false; |
292 |
} |
311 |
} |
293 |
return true; |
312 |
return true; |
294 |
} |
313 |
} |
295 |
|
314 |
|
296 |
private boolean validateOutputGalleryPath() { |
315 |
private boolean validateOutputOption(FileNameOption option) { |
297 |
if (myOutputGalleryPathOption.isEmpty()){ |
316 |
if (!option.isEnabled()){ |
|
|
317 |
return false; |
318 |
} |
319 |
if (!validateMirrorDiagramWithoutFigureGallery()){ |
320 |
return false; |
321 |
} |
322 |
if (option.isEmpty()){ |
298 |
//optional -- ok |
323 |
//optional -- ok |
299 |
return true; |
324 |
return true; |
300 |
} |
325 |
} |
301 |
String path = myOutputGalleryPathOption.getText(); |
326 |
String path = option.getText(); |
|
|
327 |
return validatePath(path); |
328 |
} |
329 |
|
330 |
private boolean validateMirrorDiagramWithoutFigureGallery(){ |
331 |
if (!myOutputDiagramElementsPathOption.isEmpty()){ |
332 |
if (myOutputGalleryPathOption.isEmpty() || myOutputDiagramElementsPathOption.getText().equals(myOutputGalleryPathOption.getText())){ |
333 |
flagError("In order to mirror diagram elements you have to generate separate figure gallery"); |
334 |
return false; |
335 |
} |
336 |
} |
337 |
return true; |
338 |
} |
339 |
|
340 |
private boolean validatePath(String path){ |
302 |
try { |
341 |
try { |
303 |
URI.createFileURI(path); |
342 |
return URI.createFileURI(path) != null; |
304 |
} catch (IllegalArgumentException e){ |
343 |
} catch (IllegalArgumentException e){ |
305 |
String message = MessageFormat.format("Path {0} is invalid", new Object[] {path}); |
344 |
flagError(MessageFormat.format("Path {0} is invalid", new Object[] {path})); |
306 |
getTheOnlyPage().setPageComplete(false); |
|
|
307 |
getTheOnlyPage().setErrorMessage(message); |
308 |
return false; |
345 |
return false; |
309 |
} |
346 |
} |
310 |
return true; |
|
|
311 |
} |
347 |
} |
312 |
|
348 |
|
313 |
private WizardPage getTheOnlyPage() { |
349 |
private WizardPage getTheOnlyPage() { |
314 |
return getPage(THE_ONLY_PAGE_INDEX); |
350 |
return getPage(THE_ONLY_PAGE_INDEX); |
315 |
} |
351 |
} |
316 |
|
352 |
|
|
|
353 |
private void flagError(String message){ |
354 |
getTheOnlyPage().setPageComplete(false); |
355 |
getTheOnlyPage().setErrorMessage(message); |
356 |
} |
357 |
|
317 |
private static Resource loadResource(String path){ |
358 |
private static Resource loadResource(String path){ |
318 |
Resource resource = new ResourceSetImpl().createResource(URI.createFileURI(path)); |
359 |
Resource resource = new ResourceSetImpl().createResource(URI.createFileURI(path)); |
319 |
try { |
360 |
try { |
Lines 324-376
Link Here
|
324 |
} |
365 |
} |
325 |
} |
366 |
} |
326 |
|
367 |
|
327 |
private static class CachedInputValidationState { |
368 |
private static class InputValidationState { |
328 |
private String myCachedPath; |
369 |
private String myCachedPath; |
329 |
private boolean myCachedIsValid; |
|
|
330 |
private String myCachedErrorMessage; |
370 |
private String myCachedErrorMessage; |
|
|
371 |
private boolean myHasDiagramElement; |
372 |
private boolean myHasFigure; |
373 |
private final FileNameOption myDiagramElementsOption; |
374 |
private final FileNameOption myGalleryOption; |
375 |
|
376 |
public InputValidationState(FileNameOption galleryOption, FileNameOption diagramElementsOption){ |
377 |
myGalleryOption = galleryOption; |
378 |
myDiagramElementsOption = diagramElementsOption; |
379 |
} |
331 |
|
380 |
|
332 |
public void updateInput(String path){ |
381 |
public void updateInput(String path){ |
333 |
if (myCachedPath == null || !myCachedPath.equals(path)){ |
382 |
if (myCachedPath == null || !myCachedPath.equals(path)){ |
334 |
myCachedIsValid = validateInputPath(path); |
383 |
myCachedPath = path; |
|
|
384 |
validateInputPath(path); |
385 |
myGalleryOption.setEnabled(myHasFigure); |
386 |
myDiagramElementsOption.setEnabled(myHasDiagramElement); |
335 |
} |
387 |
} |
336 |
} |
388 |
} |
337 |
|
389 |
|
338 |
public boolean isValid(){ |
390 |
public boolean isValid(){ |
339 |
return myCachedIsValid; |
391 |
return myHasFigure; |
340 |
} |
392 |
} |
341 |
|
393 |
|
342 |
public String getErrorMessage(){ |
394 |
public String getErrorMessage(){ |
343 |
return myCachedErrorMessage; |
395 |
return myCachedErrorMessage; |
344 |
} |
396 |
} |
345 |
|
397 |
|
346 |
private boolean hasAtLeastOneFigure(Resource resource){ |
398 |
private void validateInputPath(String path) { |
347 |
for (TreeIterator contents = resource.getAllContents(); contents.hasNext();){ |
399 |
myHasDiagramElement = false; |
348 |
EObject next = (EObject) contents.next(); |
400 |
myHasFigure = false; |
349 |
if (next instanceof Figure){ |
|
|
350 |
return true; |
351 |
} |
352 |
} |
353 |
return false; |
354 |
} |
355 |
|
356 |
private boolean validateInputPath(String path) { |
357 |
myCachedErrorMessage = null; |
401 |
myCachedErrorMessage = null; |
|
|
402 |
|
358 |
if (path == null || !new File(path).exists()){ |
403 |
if (path == null || !new File(path).exists()){ |
359 |
myCachedErrorMessage = MessageFormat.format("Can not find file {0}", new Object[] {path}); |
404 |
myCachedErrorMessage = MessageFormat.format("Can not find file {0}", new Object[] {path}); |
360 |
return false; |
405 |
return; |
361 |
} |
406 |
} |
362 |
|
407 |
|
363 |
Resource resource = loadResource(path); |
408 |
Resource resource = loadResource(path); |
364 |
boolean isValid = resource != null && hasAtLeastOneFigure(resource); |
|
|
365 |
if (resource != null){ |
409 |
if (resource != null){ |
366 |
resource.unload(); |
410 |
classifyContents(resource); |
367 |
} |
411 |
} |
368 |
if (!isValid){ |
412 |
|
|
|
413 |
if (!myHasFigure){ |
369 |
myCachedErrorMessage = MessageFormat.format("File {0} does not contain any figure definitions", new Object[] {path}); |
414 |
myCachedErrorMessage = MessageFormat.format("File {0} does not contain any figure definitions", new Object[] {path}); |
370 |
return false; |
|
|
371 |
} |
415 |
} |
372 |
return true; |
|
|
373 |
} |
416 |
} |
374 |
|
417 |
|
|
|
418 |
private void classifyContents(Resource resource){ |
419 |
myHasDiagramElement = false; |
420 |
myHasFigure = false; |
421 |
for (TreeIterator contents = resource.getAllContents(); contents.hasNext();){ |
422 |
EObject next = (EObject) contents.next(); |
423 |
if (next instanceof FigureGallery){ |
424 |
if (!myHasFigure){ |
425 |
FigureGallery nextGallery = (FigureGallery) next; |
426 |
myHasFigure = !nextGallery.getFigures().isEmpty(); |
427 |
} |
428 |
contents.prune(); |
429 |
} |
430 |
if (next instanceof DiagramElement){ |
431 |
myHasDiagramElement = true; |
432 |
contents.prune(); |
433 |
} |
434 |
if (myHasDiagramElement && myHasFigure){ |
435 |
break; |
436 |
} |
437 |
} |
438 |
} |
439 |
|
375 |
} |
440 |
} |
376 |
} |
441 |
} |