View | Details | Raw Unified | Return to bug 131365 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/gmf/internal/graphdef/codegen/ui/ConverterSection.java (-64 / +129 lines)
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
}
(-)src/org/eclipse/gmf/internal/graphdef/codegen/ui/GeneratePluginAction.java (-28 / +46 lines)
Lines 21-26 Link Here
21
import org.eclipse.core.runtime.Status;
21
import org.eclipse.core.runtime.Status;
22
import org.eclipse.core.runtime.jobs.Job;
22
import org.eclipse.core.runtime.jobs.Job;
23
import org.eclipse.emf.common.util.URI;
23
import org.eclipse.emf.common.util.URI;
24
import org.eclipse.emf.ecore.EObject;
24
import org.eclipse.emf.ecore.resource.Resource;
25
import org.eclipse.emf.ecore.resource.Resource;
25
import org.eclipse.emf.ecore.resource.ResourceSet;
26
import org.eclipse.emf.ecore.resource.ResourceSet;
26
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
27
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
Lines 67-72 Link Here
67
		generator.setSkipPluginStructure(false);
68
		generator.setSkipPluginStructure(false);
68
69
69
		new Job(action.getText()) {
70
		new Job(action.getText()) {
71
			private IContainer myResourcesContainer;
70
			{
72
			{
71
				// setUser(true); FIXME fixed after M5, uncoment when switching to M6 
73
				// setUser(true); FIXME fixed after M5, uncoment when switching to M6 
72
			}
74
			}
Lines 77-85 Link Here
77
					if (!generator.getRunStatus().isOK()) {
79
					if (!generator.getRunStatus().isOK()) {
78
						return generator.getRunStatus();
80
						return generator.getRunStatus();
79
					}
81
					}
80
					IFile galleryFile = decideOnDestinationFile();
82
					StandaloneGalleryConverter converter = new StandaloneGalleryConverter(generator.getGenerationInfo());					
81
					Resource r = rs.createResource(URI.createPlatformResourceURI(galleryFile.getFullPath().toString()));
83
82
					return saveNewFigureGallery(r, generator.getGenerationInfo());
84
					IStatus result = saveToFile(decideOnDestinationFile("bundled"), converter.convertFigureGallery());
85
					if (result.isOK()){
86
						result = saveToFile(decideOnDestinationFile("mirrored"), converter.mirrorDiagramElements(rs.getResources()));
87
					}
88
					return result;
83
				} catch (InterruptedException e) {
89
				} catch (InterruptedException e) {
84
					return Status.CANCEL_STATUS;
90
					return Status.CANCEL_STATUS;
85
				} finally {
91
				} finally {
Lines 89-123 Link Here
89
				}
95
				}
90
			}
96
			}
91
97
92
			private IFile decideOnDestinationFile() {
98
			private IFile decideOnDestinationFile(String baseName) {
93
				StandaloneGenerator.Config config = generator.getGenerationInfo().getConfig();
99
				final IPath basePath = new Path(baseName);
94
				IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(config.getPluginID());
95
				assert p.exists(); // generator.runStatus.ok makes us believe
96
				IContainer fileContainer = p;
97
				IFolder modelsFolder = p.getFolder("models");
98
				if (!modelsFolder.exists()) {
99
					try {
100
						modelsFolder.create(true, true, new NullProgressMonitor());
101
						fileContainer = modelsFolder;
102
					} catch (CoreException ex) {
103
						// ignore or log?
104
					}
105
				}
106
				final IPath baseName = new Path("bundled");
107
				final String ext = "gmfgraph";
100
				final String ext = "gmfgraph";
108
				IFile galleryFile = fileContainer.getFile(baseName.addFileExtension(ext));
101
				IFile resultFile = getResourcesContainer().getFile(basePath.addFileExtension(ext));
109
				for (int i = 1; galleryFile.exists(); i++) {
102
				for (int i = 1; resultFile.exists(); i++) {
110
					galleryFile = fileContainer.getFile(new Path(baseName.lastSegment() + String.valueOf(i)).addFileExtension(ext));
103
					resultFile = getResourcesContainer().getFile(new Path(basePath.lastSegment() + String.valueOf(i)).addFileExtension(ext));
104
				}
105
				return resultFile;
106
			}
107
			
108
			private IContainer getResourcesContainer(){
109
				if (myResourcesContainer == null){
110
					IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(getConfig().getPluginID());
111
					assert p.exists(); // generator.runStatus.ok makes us believe
112
					myResourcesContainer = p;
113
					IFolder modelsFolder = p.getFolder("models");
114
					if (!modelsFolder.exists()) {
115
						try {
116
							modelsFolder.create(true, true, new NullProgressMonitor());
117
							myResourcesContainer = modelsFolder;
118
						} catch (CoreException ex) {
119
							// ignore or log?
120
						}
121
					}
111
				}
122
				}
112
				return galleryFile;
123
				return myResourcesContainer;
124
			}
125
			
126
			private StandaloneGenerator.Config getConfig(){
127
				return generator.getGenerationInfo().getConfig();
113
			}
128
			}
114
129
115
			private IStatus saveNewFigureGallery(Resource galleryResource, StandaloneGenerator.GenerationInfo info) {
130
			private IStatus saveToFile(IFile outputFile, EObject root) {
116
				galleryResource.getContents().add(new StandaloneGalleryConverter().convertFigureGallery(info));
131
				if (root != null){
117
				try {
132
					Resource outputResource = rs.createResource(URI.createPlatformResourceURI(outputFile.getFullPath().toString()));
118
					galleryResource.save(null);
133
					outputResource.getContents().add(root);
119
				} catch (IOException e) {
134
					try {
120
					return new Status(IStatus.ERROR, "org.eclipse.gmf.graphdef.codegen.ui", 0, e.getMessage(), e);
135
						outputResource.save(null);
136
					} catch (IOException e) {
137
						return new Status(IStatus.ERROR, "org.eclipse.gmf.graphdef.codegen.ui", 0, e.getMessage(), e);
138
					}
121
				}
139
				}
122
				return Status.OK_STATUS;
140
				return Status.OK_STATUS;
123
			}
141
			}
(-)src/org/eclipse/gmf/internal/graphdef/codegen/ui/StandaloneGalleryConverter.java (-4 / +69 lines)
Lines 12-19 Link Here
12
12
13
package org.eclipse.gmf.internal.graphdef.codegen.ui;
13
package org.eclipse.gmf.internal.graphdef.codegen.ui;
14
14
15
import java.util.Collection;
15
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.HashSet;
18
import java.util.Iterator;
16
19
20
import org.eclipse.emf.ecore.EObject;
21
import org.eclipse.emf.ecore.EReference;
22
import org.eclipse.emf.ecore.resource.Resource;
23
import org.eclipse.emf.ecore.util.EcoreUtil;
24
import org.eclipse.gmf.gmfgraph.Canvas;
17
import org.eclipse.gmf.gmfgraph.ConnectionFigure;
25
import org.eclipse.gmf.gmfgraph.ConnectionFigure;
18
import org.eclipse.gmf.gmfgraph.CustomFigure;
26
import org.eclipse.gmf.gmfgraph.CustomFigure;
19
import org.eclipse.gmf.gmfgraph.DecorationFigure;
27
import org.eclipse.gmf.gmfgraph.DecorationFigure;
Lines 23-42 Link Here
23
import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator;
31
import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator;
24
32
25
public class StandaloneGalleryConverter {
33
public class StandaloneGalleryConverter {
26
	public FigureGallery convertFigureGallery(StandaloneGenerator.GenerationInfo generationInfo){
34
	private final StandaloneGenerator.GenerationInfo myGenerationInfo;
35
	private final DiagramElementsCopier myDiagramElementsCopier;
36
	
37
	public StandaloneGalleryConverter(StandaloneGenerator.GenerationInfo generationInfo){
38
		myGenerationInfo = generationInfo;
39
		myDiagramElementsCopier = new DiagramElementsCopier();
40
	}
41
	
42
	public FigureGallery convertFigureGallery(){
27
		FigureGallery result = GMFGraphFactory.eINSTANCE.createFigureGallery();
43
		FigureGallery result = GMFGraphFactory.eINSTANCE.createFigureGallery();
28
		String generatedBundle = generationInfo.getConfig().getPluginID();
44
		String generatedBundle = myGenerationInfo.getConfig().getPluginID();
29
		result.setImplementationBundle(generatedBundle);
45
		result.setImplementationBundle(generatedBundle);
30
		
46
		
31
		for (Enumeration originalFigures = generationInfo.getProcessedFigures(); originalFigures.hasMoreElements();){
47
		for (Enumeration originalFigures = myGenerationInfo.getProcessedFigures(); originalFigures.hasMoreElements();){
32
			Figure nextOriginal = (Figure) originalFigures.nextElement();
48
			Figure nextOriginal = (Figure) originalFigures.nextElement();
33
			String nextConvertedFqn = generationInfo.getGeneratedClassFQN(nextOriginal);
49
			String nextConvertedFqn = myGenerationInfo.getGeneratedClassFQN(nextOriginal);
34
			CustomFigure custom = createCustomFigure(nextOriginal);
50
			CustomFigure custom = createCustomFigure(nextOriginal);
35
			custom.setName(nextOriginal.getName());
51
			custom.setName(nextOriginal.getName());
36
			custom.setBundleName(generatedBundle);
52
			custom.setBundleName(generatedBundle);
37
			custom.setQualifiedClassName(nextConvertedFqn);
53
			custom.setQualifiedClassName(nextConvertedFqn);
38
			
54
			
39
			result.getFigures().add(custom);
55
			result.getFigures().add(custom);
56
			myDiagramElementsCopier.registerSubstitution(nextOriginal, custom);
57
		}
58
		return result;
59
	}
60
	
61
	public Canvas mirrorDiagramElements(Collection resources){
62
		Canvas result = null;
63
		for (Iterator it = resources.iterator(); it.hasNext();){
64
			Resource next = (Resource)it.next();
65
			if (!next.getContents().isEmpty()){
66
				EObject root = (EObject) next.getContents().get(0);
67
				if (root instanceof Canvas){
68
					Canvas original = (Canvas)root;
69
					if (result == null){
70
						result = GMFGraphFactory.eINSTANCE.createCanvas();
71
						result.setName(original.getName());
72
					}
73
					Collection children = myDiagramElementsCopier.copyAll(original.getChildren());
74
					Collection compartments = myDiagramElementsCopier.copyAll(original.getCompartments());
75
					Collection labels = myDiagramElementsCopier.copyAll(original.getLabels());
76
					Collection nodes = myDiagramElementsCopier.copyAll(original.getNodes());
77
78
					result.getChildren().addAll(children);
79
					result.getCompartments().addAll(compartments);
80
					result.getLabels().addAll(labels);
81
					result.getNodes().addAll(nodes);
82
				}
83
			}
84
		}
85
		if (result != null && !result.eContents().isEmpty()){
86
			myDiagramElementsCopier.copyReferences();	
40
		}
87
		}
41
		return result;
88
		return result;
42
	}
89
	}
Lines 51-55 Link Here
51
		}
98
		}
52
		return factory.createCustomFigure();
99
		return factory.createCustomFigure();
53
	}
100
	}
101
	
102
	private static class DiagramElementsCopier extends EcoreUtil.Copier {
103
		private final HashSet myOriginalFigures = new HashSet();
104
		
105
		public void registerSubstitution(Figure original, CustomFigure substituted){
106
			put(original, substituted);
107
			myOriginalFigures.add(original);
108
		}
109
		
110
		protected void copyReference(EReference eReference, EObject eObject, EObject copyEObject) {
111
			if (EcoreUtil.isAncestor(myOriginalFigures, eObject)){
112
				//no such features in the CustomFigure's
113
				return;
114
			}
115
			super.copyReference(eReference, eObject, copyEObject);
116
		}
117
		
118
	}
54
119
55
}
120
}

Return to bug 131365