1 | package de.uka.ipd.sdq.featureinstance; |
2 | |
3 | import java.io.File; |
4 | import java.io.IOException; |
5 | import java.lang.reflect.InvocationTargetException; |
6 | import java.util.Collections; |
7 | import java.util.HashMap; |
8 | import java.util.Iterator; |
9 | import java.util.List; |
10 | import java.util.Map; |
11 | |
12 | import org.eclipse.core.resources.IFile; |
13 | import org.eclipse.core.resources.ResourcesPlugin; |
14 | import org.eclipse.core.runtime.IPath; |
15 | import org.eclipse.core.runtime.IProgressMonitor; |
16 | import org.eclipse.core.runtime.NullProgressMonitor; |
17 | import org.eclipse.core.runtime.Platform; |
18 | import org.eclipse.emf.common.command.BasicCommandStack; |
19 | import org.eclipse.emf.common.util.EList; |
20 | import org.eclipse.emf.common.util.URI; |
21 | import org.eclipse.emf.ecore.EObject; |
22 | import org.eclipse.emf.ecore.resource.Resource; |
23 | import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
24 | import org.eclipse.emf.edit.domain.EditingDomain; |
25 | import org.eclipse.emf.edit.domain.IEditingDomainProvider; |
26 | import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor; |
27 | import org.eclipse.emf.edit.ui.action.LoadResourceAction.LoadResourceDialog; |
28 | import org.eclipse.emf.edit.ui.util.EditUIUtil; |
29 | import org.eclipse.jface.action.IMenuListener; |
30 | import org.eclipse.jface.action.IMenuManager; |
31 | import org.eclipse.jface.action.MenuManager; |
32 | import org.eclipse.jface.action.Separator; |
33 | import org.eclipse.jface.dialogs.MessageDialog; |
34 | import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
35 | import org.eclipse.jface.resource.ImageDescriptor; |
36 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
37 | import org.eclipse.jface.viewers.CheckboxTreeViewer; |
38 | import org.eclipse.jface.viewers.ICheckStateListener; |
39 | import org.eclipse.jface.viewers.ILabelProvider; |
40 | import org.eclipse.jface.viewers.ILabelProviderListener; |
41 | import org.eclipse.jface.viewers.ISelection; |
42 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
43 | import org.eclipse.jface.viewers.ISelectionProvider; |
44 | import org.eclipse.jface.viewers.IStructuredSelection; |
45 | import org.eclipse.jface.viewers.ITreeContentProvider; |
46 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
47 | import org.eclipse.jface.viewers.StructuredSelection; |
48 | import org.eclipse.jface.viewers.StructuredViewer; |
49 | import org.eclipse.jface.viewers.TreeViewer; |
50 | import org.eclipse.jface.viewers.Viewer; |
51 | import org.eclipse.jface.wizard.WizardDialog; |
52 | import org.eclipse.swt.SWT; |
53 | import org.eclipse.swt.custom.CTabFolder; |
54 | import org.eclipse.swt.graphics.Image; |
55 | import org.eclipse.swt.graphics.Point; |
56 | import org.eclipse.swt.layout.FillLayout; |
57 | import org.eclipse.swt.widgets.Composite; |
58 | import org.eclipse.swt.widgets.Menu; |
59 | import org.eclipse.ui.IEditorInput; |
60 | import org.eclipse.ui.IEditorPart; |
61 | import org.eclipse.ui.IEditorSite; |
62 | import org.eclipse.ui.IFileEditorInput; |
63 | import org.eclipse.ui.PartInitException; |
64 | import org.eclipse.ui.actions.WorkspaceModifyOperation; |
65 | import org.eclipse.ui.dialogs.SaveAsDialog; |
66 | import org.eclipse.ui.part.FileEditorInput; |
67 | import org.eclipse.ui.part.MultiPageEditorPart; |
68 | import org.eclipse.ui.views.contentoutline.IContentOutlinePage; |
69 | import org.eclipse.ui.views.properties.PropertySheetPage; |
70 | |
71 | import de.uka.ipd.sdq.dialogs.error.ErrorDisplayDialog; |
72 | import de.uka.ipd.sdq.featureconfig.ConfigNode; |
73 | import de.uka.ipd.sdq.featureconfig.ConfigState; |
74 | import de.uka.ipd.sdq.featureconfig.Configuration; |
75 | import de.uka.ipd.sdq.featureconfig.FeatureConfig; |
76 | import de.uka.ipd.sdq.featureconfig.impl.featureconfigFactoryImpl; |
77 | import de.uka.ipd.sdq.featuremodel.ChildRelation; |
78 | import de.uka.ipd.sdq.featuremodel.Feature; |
79 | import de.uka.ipd.sdq.featuremodel.FeatureDiagram; |
80 | import de.uka.ipd.sdq.featuremodel.FeatureGroup; |
81 | import de.uka.ipd.sdq.featuremodel.Simple; |
82 | |
83 | /** |
84 | * @author Grischa Liebel |
85 | * |
86 | */ |
87 | public class FeatureModelInstanceEditor extends MultiPageEditorPart implements ISelectionProvider, IEditingDomainProvider { |
88 | |
89 | protected ISelectionChangedListener selectionChangedListener; |
90 | |
91 | protected ISelection editorSelection = StructuredSelection.EMPTY; |
92 | |
93 | protected AdapterFactoryEditingDomain editingDomain; |
94 | |
95 | protected CheckboxTreeViewer treeViewer; |
96 | protected PropertySheetPage propertySheetPage; |
97 | |
98 | protected ICheckStateListener listener; |
99 | |
100 | protected Resource resource; |
101 | |
102 | protected FeatureDiagram featureDiagram; |
103 | |
104 | protected Object root; |
105 | |
106 | protected TreeViewer contentOutlineViewer; |
107 | |
108 | protected IContentOutlinePage contentOutlinePage; |
109 | |
110 | protected boolean dirtyFlag = false; |
111 | |
112 | protected FeatureConfig defaultConfig; |
113 | protected FeatureConfig overridesConfig; |
114 | |
115 | protected FeatureConfigFunctionality functions; |
116 | |
117 | protected Composite comp; |
118 | |
119 | public void addSelectionChangedListener(ISelectionChangedListener listener) { |
120 | selectionChangedListener = listener; |
121 | } |
122 | |
123 | public ISelection getSelection() { |
124 | return editorSelection; |
125 | } |
126 | |
127 | public void removeSelectionChangedListener( |
128 | ISelectionChangedListener listener) { |
129 | selectionChangedListener = null; |
130 | |
131 | } |
132 | |
133 | public void setSelection(ISelection selection) { |
134 | editorSelection = selection; |
135 | selectionChangedListener.selectionChanged(new SelectionChangedEvent(this, selection)); |
136 | } |
137 | |
138 | @Override |
139 | protected void firePropertyChange(int action) { |
140 | super.firePropertyChange(action); |
141 | } |
142 | |
143 | public EditingDomain getEditingDomain() { |
144 | return editingDomain; |
145 | } |
146 | |
147 | protected void createContextMenuFor(StructuredViewer viewer) { |
148 | MenuManager contextMenu = new MenuManager(); |
149 | contextMenu.setRemoveAllWhenShown(true); |
150 | contextMenu.addMenuListener(new IMenuListener() { |
151 | public void menuAboutToShow(IMenuManager manager) { |
152 | FeatureModelInstanceEditor.this.fillContextMenu(manager); |
153 | } |
154 | }); |
155 | |
156 | Menu menu= contextMenu.createContextMenu(viewer.getControl()); |
157 | viewer.getControl().setMenu(menu); |
158 | getSite().registerContextMenu(contextMenu, treeViewer); |
159 | } |
160 | |
161 | protected void fillContextMenu(IMenuManager manager) { |
162 | manager.add(new Separator("Validate")); |
163 | |
164 | ((FeatureModelInstanceContributor)getEditorSite().getActionBarContributor()).fillContextMenu(manager); |
165 | } |
166 | |
167 | public FeatureModelInstanceEditor() { |
168 | super(); |
169 | |
170 | functions = new FeatureConfigFunctionality(); |
171 | editingDomain = functions.initializeEditingDomain(); |
172 | } |
173 | |
174 | /** |
175 | * <!-- begin-user-doc --> |
176 | * <!-- end-user-doc --> |
177 | * @generated |
178 | */ |
179 | public EditingDomainActionBarContributor getActionBarContributor() { |
180 | return (EditingDomainActionBarContributor)getEditorSite().getActionBarContributor(); |
181 | } |
182 | |
183 | @Override |
184 | public void init(IEditorSite site, IEditorInput input) throws PartInitException { |
185 | if (!(input instanceof IFileEditorInput)) { |
186 | throw new PartInitException("Invalid input"); |
187 | } |
188 | super.init(site, input); |
189 | } |
190 | |
191 | /** |
192 | * Loads the resource-object through the editingDomain |
193 | */ |
194 | protected void createResource(URI uri) { |
195 | |
196 | if (uri == null) { |
197 | |
198 | URI resourceURI = EditUIUtil.getURI(getEditorInput()); |
199 | |
200 | //Try to load the resource through the editingDomain. |
201 | resource = null; |
202 | try { |
203 | resource = editingDomain.getResourceSet().getResource(resourceURI, true); |
204 | } |
205 | catch (Exception e) { |
206 | resource = editingDomain.getResourceSet().getResource(resourceURI, false); |
207 | } |
208 | } |
209 | else { |
210 | URI resourceURI = uri; |
211 | |
212 | //Try to load the resource through the editingDomain. |
213 | resource = null; |
214 | try { |
215 | resource = editingDomain.getResourceSet().getResource(resourceURI, true); |
216 | } |
217 | catch (Exception e) { |
218 | resource = editingDomain.getResourceSet().getResource(resourceURI, false); |
219 | } |
220 | } |
221 | } |
222 | |
223 | /** |
224 | * @return Returns the resource-object |
225 | */ |
226 | protected Resource getResource() { |
227 | return resource; |
228 | } |
229 | |
230 | /** |
231 | * Starts a ResourceWizard to create a new File |
232 | * |
233 | * @param fileName The default filename |
234 | * @return Returns the URI of the new file or null, if dialog was cancelled |
235 | */ |
236 | protected URI startFileWizard (String fileName, String message) { |
237 | FeatureConfigCreationWizard myWiz = new FeatureConfigCreationWizard(fileName, message); |
238 | myWiz.init(getEditorSite().getWorkbenchWindow().getWorkbench(), (IStructuredSelection)getSelection()); |
239 | WizardDialog dialog = new WizardDialog(null, myWiz); |
240 | dialog.create(); |
241 | dialog.open(); |
242 | |
243 | //get the location for the featureconfig |
244 | return myWiz.getNewResource(); |
245 | } |
246 | |
247 | /** |
248 | * Starts a LoadResourceDialog |
249 | * |
250 | * @param fileName The default filename |
251 | * @return Returns the URI selected by the user in the dialog or null if no file was selected or the dialog cancelled |
252 | */ |
253 | protected URI startOpenDialog (String fileName) { |
254 | |
255 | LoadResourceDialog myDialog = new LoadResourceDialog(getContainer().getShell(), editingDomain); |
256 | myDialog.setBlockOnOpen(true); |
257 | myDialog.open(); |
258 | |
259 | if (myDialog.getReturnCode() == 1) { |
260 | return null; |
261 | } |
262 | else { |
263 | List<URI> uriList = myDialog.getURIs(); |
264 | if (uriList.isEmpty()) { |
265 | return null; |
266 | } |
267 | else { |
268 | return uriList.get(0); |
269 | } |
270 | } |
271 | } |
272 | |
273 | /** |
274 | * Creates a new Configuration-Resource with the given newResourceURI corresponding to the given featureDiagram |
275 | * Overrides the old (*.featuremodel) resource-object |
276 | * |
277 | * @param newResourceURI The URI for the new Resource |
278 | * @param featureDiagram A FeatureDiagram-object to which the new Configuration should reference |
279 | * @param defaultRef A reference to the defaultConfig object or null, if none exists |
280 | */ |
281 | protected void createNewConfigResource (URI newResourceURI, FeatureDiagram featureDiagram, FeatureConfig defaultRef) { |
282 | if (newResourceURI == null) { |
283 | throw new NullPointerException ("No Config file stored in resource!"); |
284 | } |
285 | else { |
286 | //Create new featureconfig-resource and change current resource |
287 | resource = resource.getResourceSet().createResource(newResourceURI); |
288 | |
289 | featureconfigFactoryImpl factory = new featureconfigFactoryImpl(); |
290 | Configuration newConfig = factory.createConfiguration(); |
291 | newConfig.setName(featureDiagram.getName() + "_config"); |
292 | |
293 | FeatureConfig newOverrides = factory.createFeatureConfig(); |
294 | newConfig.getConfigOverrides().add(newOverrides); |
295 | |
296 | //set reference to default |
297 | if (defaultRef == null) { |
298 | defaultRef = factory.createFeatureConfig(); |
299 | } |
300 | newConfig.setDefaultConfig(defaultRef); |
301 | |
302 | ConfigNode rootConfigNode = factory.createConfigNode(); |
303 | rootConfigNode.setConfigState(ConfigState.ELIMINATED); |
304 | rootConfigNode.setOrigin((Feature)(featureDiagram).getRootFeature()); |
305 | |
306 | newOverrides.getConfignode().add(rootConfigNode); |
307 | |
308 | resource.getContents().add(newConfig); |
309 | |
310 | defaultConfig = defaultRef; |
311 | overridesConfig = newOverrides; |
312 | |
313 | try { |
314 | resource.load(Collections.EMPTY_MAP); |
315 | resource.save(Collections.EMPTY_MAP); |
316 | } |
317 | catch (IOException e) { |
318 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Could not load or save the Resource!")); |
319 | errord.open(); |
320 | } |
321 | } |
322 | } |
323 | |
324 | /** |
325 | * Checks if the FeatureDiagram object can be accessed in the loaded resource and returns it if possible |
326 | * |
327 | * @param resource The resource from which the FeatureDiagram-object can be accessed (a *.featuremodel-file) |
328 | * @return Returns a FeatureDiagram-object or null, if it can be accessed (e.g. no *.featuremodel-file) |
329 | */ |
330 | protected FeatureDiagram getFeatureDiagram (Resource resource) { |
331 | EList<EObject> tempList = resource.getContents(); |
332 | Iterator<EObject> tempIterator = tempList.iterator(); |
333 | EObject newResource; |
334 | if (tempIterator.hasNext()) { |
335 | newResource = tempIterator.next(); |
336 | } |
337 | else { |
338 | return null; |
339 | } |
340 | |
341 | if (!(newResource instanceof FeatureDiagram)) { |
342 | return null; |
343 | } |
344 | return (FeatureDiagram)newResource; |
345 | } |
346 | |
347 | |
348 | /** |
349 | * Handles the different file types for the loaded resource. |
350 | * |
351 | * @param fileExtension The file extension of the loaded resource |
352 | * @param path The complete path to the file including the fileName |
353 | * @param fileName The filename |
354 | */ |
355 | protected void handleFileCases(String fileExtension, String path, String fileName) { |
356 | //featuremodel file present |
357 | if (fileExtension.equals("featuremodel")) { |
358 | //Check if featuremodel file is valid (FeatureDiagram object can be referenced) |
359 | featureDiagram = getFeatureDiagram(resource); |
360 | boolean valid = true; |
361 | if (featureDiagram == null) { |
362 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Loaded *.featuremodel file is not valid! The FeatureDiagram object cannot be accessed.")); |
363 | errord.open(); |
364 | valid = false; |
365 | } |
366 | |
367 | //Check if the equivalent featureconfig-file already exists |
368 | File myFile = new File(path + ".featureconfig"); |
369 | |
370 | if (valid && myFile.exists()) { |
371 | //ask if existing file should be used |
372 | boolean answer = MessageDialog.openQuestion(null, "Load FeatureConfig ?", "A file named \""+ fileName + ".featureconfig\" was found. Should it be loaded?"); |
373 | |
374 | //use existing file |
375 | if (answer) { |
376 | |
377 | //load existing featureconfig file |
378 | Resource existingResource = resource.getResourceSet().createResource(URI.createFileURI(myFile.getPath())); |
379 | |
380 | try { |
381 | existingResource.load(Collections.EMPTY_MAP); |
382 | } |
383 | catch (IOException e) { |
384 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Existing featureconfig file couldn't be loaded!")); |
385 | errord.open(); |
386 | valid = false; |
387 | } |
388 | |
389 | //Check if *.featureconfig references right FeatureDiagram |
390 | if (functions.isFeatureDiagramReferenceCorrect(existingResource)) { |
391 | existingResource = handleConfigCases(existingResource); |
392 | try { |
393 | resource = existingResource; |
394 | resource.load(Collections.EMPTY_MAP); |
395 | resource.save(Collections.EMPTY_MAP); |
396 | } |
397 | catch (IOException e) { |
398 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Resource couldn't be loaded or saved!")); |
399 | errord.open(); |
400 | valid = false; |
401 | } |
402 | } |
403 | else { |
404 | URI newResourceURI = startFileWizard(fileName, "Loaded configuration file references wrong diagram. Create a new config file."); |
405 | createNewConfigResource(newResourceURI, featureDiagram, null); |
406 | } |
407 | |
408 | } |
409 | else { |
410 | URI newResourceURI = startFileWizard(fileName, "Create a new config file for the loaded diagram."); |
411 | createNewConfigResource(newResourceURI, featureDiagram, null); |
412 | } |
413 | } |
414 | else if (valid) { |
415 | URI configPath = startOpenDialog(fileName); |
416 | |
417 | if (configPath == null) { |
418 | //File selection Dialog has been canceled, call new file wizard |
419 | URI newResourceURI = startFileWizard(fileName, "File selection dialog has been cancelled. Create a new config resource."); |
420 | createNewConfigResource(newResourceURI, featureDiagram, null); |
421 | } |
422 | else { |
423 | Resource existingResource = resource.getResourceSet().createResource(configPath); |
424 | |
425 | try { |
426 | existingResource.load(Collections.EMPTY_MAP); |
427 | } |
428 | catch (IOException e) { |
429 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Resource couldn't be loaded or saved!")); |
430 | errord.open(); |
431 | valid = false; |
432 | } |
433 | |
434 | //Check if *.featureconfig references right FeatureDiagram |
435 | if (functions.isFeatureDiagramReferenceCorrect(existingResource)) { |
436 | existingResource = handleConfigCases(existingResource); |
437 | try { |
438 | resource = existingResource; |
439 | resource.load(Collections.EMPTY_MAP); |
440 | resource.save(Collections.EMPTY_MAP); |
441 | } |
442 | catch (IOException e) { |
443 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Resource couldn't be loaded or saved!")); |
444 | errord.open(); |
445 | valid = false; |
446 | } |
447 | } |
448 | else { |
449 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("The selected *.featureconfig file references the wrong FeatureDiagram! A new FileWizard will be started.")); |
450 | errord.open(); |
451 | URI newResourceURI = startFileWizard(fileName, "Loaded configuration file references wrong diagram. Create a new config file."); |
452 | createNewConfigResource(newResourceURI, featureDiagram, null); |
453 | } |
454 | } |
455 | } |
456 | } |
457 | //featureconfig file present |
458 | else if (fileExtension.equals("featureconfig")){ |
459 | handleConfigCases(resource); |
460 | } |
461 | //no featureconfig or featuremodel file present |
462 | else { |
463 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Editor supports only *.featuremodel and *.featureconfig files!")); |
464 | errord.open(); |
465 | URI newResourceURI = startOpenDialog(""); |
466 | createResource(newResourceURI); |
467 | } |
468 | } |
469 | |
470 | /** |
471 | * Handles the different cases for a loaded *.featuremodel-resource |
472 | * http://sdqweb.ipd.uka.de/mediawiki/images/6/61/Check_Cases.png |
473 | * @param resource The resource in which the configuration object should be stored |
474 | * @return The resource object which stores the (prop. new) overrides config object |
475 | */ |
476 | protected Resource handleConfigCases(Resource resource) { |
477 | |
478 | //Check if featureconfig file is valid (Configuration object can be referenced) |
479 | Configuration configuration = functions.getConfiguration(resource); |
480 | |
481 | EList<FeatureConfig> tempOverrides = configuration.getConfigOverrides(); |
482 | FeatureConfig tempDefault = configuration.getDefaultConfig(); |
483 | |
484 | //Both FeatureConfigs are null |
485 | if ( (tempOverrides == null || tempOverrides.isEmpty()) && tempDefault == null) { |
486 | //TODO: check whether to throw an exception |
487 | } |
488 | else if (tempOverrides == null && tempDefault != null) { //default exists |
489 | EList<ConfigNode> configList = tempDefault.getConfignode(); |
490 | |
491 | if (!configList.isEmpty()) { |
492 | //Try to reference the Feature Diagram object and create a new overrides from it |
493 | featureDiagram = functions.navigateToFeatureDiagram((Feature)configList.iterator().next().getOrigin(), editingDomain); |
494 | String fileName = resource.getURI().trimFileExtension().lastSegment(); |
495 | |
496 | //start wizard an ask for a new file location |
497 | //*.featureconfig with only a default config shall only be used as template |
498 | URI newResourceURI = startFileWizard(fileName, "Only a default configuration was found. Create a new file with a overrides configuration!"); |
499 | createNewConfigResource(newResourceURI, featureDiagram, tempDefault); |
500 | } |
501 | else { |
502 | //TODO: check whether to throw an exception |
503 | } |
504 | } |
505 | else if (tempOverrides != null && !tempOverrides.isEmpty() && tempDefault == null) { |
506 | FeatureConfig featureConfig = tempOverrides.get(0); //assumption: only one feature diagram present |
507 | EList<ConfigNode> configList = featureConfig.getConfignode(); |
508 | |
509 | //Try to reference the Feature Diagram object |
510 | if (!configList.isEmpty()) { |
511 | featureDiagram = functions.navigateToFeatureDiagram((Feature)configList.iterator().next().getOrigin(), editingDomain); |
512 | overridesConfig = featureConfig; |
513 | } |
514 | else { |
515 | //TODO: check whether to throw an exception |
516 | } |
517 | } |
518 | else { |
519 | //both configs are present |
520 | boolean configPresent = false; |
521 | EList<ConfigNode> configList; |
522 | |
523 | //Check for OverridesConfig |
524 | if (!tempOverrides.isEmpty()) { |
525 | FeatureConfig featureConfig = tempOverrides.get(0); //assumption: only one feature diagram present |
526 | configList = featureConfig.getConfignode(); |
527 | |
528 | //Try to reference the Feature Diagram object |
529 | if (!(configList.isEmpty())) { |
530 | featureDiagram = functions.navigateToFeatureDiagram((Feature)configList.iterator().next().getOrigin(), editingDomain); |
531 | overridesConfig = featureConfig; |
532 | configPresent = true; |
533 | } |
534 | } |
535 | |
536 | //Check for DefaultConfig |
537 | configList = tempDefault.getConfignode(); |
538 | |
539 | if (!(configList.isEmpty())) { |
540 | //if the feature diagram is already referenced, just save the default config |
541 | if (configPresent) { |
542 | defaultConfig = tempDefault; |
543 | } |
544 | //else, try to reference the feature config object by using the default config |
545 | //and ask for new save location (see case that only a default config is present) |
546 | else { |
547 | featureDiagram = functions.navigateToFeatureDiagram((Feature)configList.iterator().next().getOrigin(), editingDomain); |
548 | String fileName = resource.getURI().trimFileExtension().lastSegment(); |
549 | URI newResourceURI = startFileWizard(fileName, "Only a default configuration was found. Create a new file with a overrides configuration!"); |
550 | createNewConfigResource(newResourceURI, featureDiagram, tempDefault); |
551 | } |
552 | } |
553 | } |
554 | return resource; |
555 | } |
556 | |
557 | protected void createPages() { |
558 | createResource(null); |
559 | createEditor(); |
560 | } |
561 | |
562 | |
563 | /** |
564 | * Creates the editor layout and content |
565 | */ |
566 | protected void createEditor() { |
567 | //get the file extension, file name and the full path to the resource |
568 | String fileExtension = resource.getURI().fileExtension(); |
569 | String fileName = resource.getURI().trimFileExtension().lastSegment(); |
570 | String path = Platform.getLocation() + resource.getURI().trimFileExtension().toPlatformString(true); |
571 | |
572 | //handles the different cases of opened files and model/configuration cases |
573 | handleFileCases(fileExtension, path, fileName); |
574 | |
575 | if (featureDiagram != null && overridesConfig != null) { |
576 | |
577 | //Create the Viewer |
578 | comp = new Composite(getContainer(), SWT.NONE); |
579 | comp.setLayout(new FillLayout()); |
580 | |
581 | createViewer(featureDiagram); |
582 | |
583 | int index = addPage(comp); |
584 | setPageText(index, ""); |
585 | |
586 | //set the needed attributes for the validation in the actionBarContributor |
587 | EditingDomainActionBarContributor contrib = getActionBarContributor(); |
588 | if (contrib instanceof FeatureModelInstanceContributor) { |
589 | ((FeatureModelInstanceContributor)contrib).setConfiguration(functions.getConfiguration(resource)); |
590 | ((FeatureModelInstanceContributor)contrib).setShell(getContainer().getShell()); |
591 | } |
592 | |
593 | //Hide editor tabs |
594 | if (getContainer() instanceof CTabFolder) { |
595 | ((CTabFolder)getContainer()).setTabHeight(1); |
596 | Point point = getContainer().getSize(); |
597 | getContainer().setSize(point.x, point.y + 6); |
598 | } |
599 | } |
600 | } |
601 | |
602 | /** |
603 | * Creates a treeViewer of the given FeatureDiagram |
604 | * |
605 | * @param root The FeatureDiagram-object which shall be displayed |
606 | */ |
607 | public void createViewer (FeatureDiagram root) { |
608 | treeViewer = new CheckboxTreeViewer(comp); |
609 | |
610 | treeViewer.setContentProvider(new TreeContentProvider()); |
611 | treeViewer.setLabelProvider(new TreeLabelProvider()); |
612 | treeViewer.setInput(root); |
613 | treeViewer.expandAll(); |
614 | getSite().setSelectionProvider(treeViewer); |
615 | |
616 | if (root != null) { |
617 | treeViewer.setGrayed(root.getRootFeature(), true); |
618 | |
619 | //Gray FeatureGroups |
620 | Feature curRoot = root.getRootFeature(); |
621 | functions.grayFeatureGroups(curRoot.getChildrelation(), treeViewer); |
622 | } |
623 | |
624 | if (defaultConfig != null) { |
625 | functions.markDefaultConfig(defaultConfig,treeViewer); |
626 | } |
627 | if (overridesConfig != null) { |
628 | functions.markOverridesConfig(overridesConfig,treeViewer); |
629 | } |
630 | |
631 | listener = new ICheckStateListener() { |
632 | public void checkStateChanged(CheckStateChangedEvent event) { |
633 | |
634 | //make FeatureGroups readonly |
635 | if (event.getElement() instanceof FeatureGroup) { |
636 | treeViewer.setChecked(event.getElement(), !(event.getChecked())); |
637 | } |
638 | else { |
639 | Object parent = editingDomain.getParent(event.getElement()); |
640 | |
641 | //automatically unchecks Feature again, if its a mandatory Feature and the parent node is selected |
642 | if ((event.getElement() instanceof Feature) && (functions.checkMandatory((Feature)event.getElement(), editingDomain))) { |
643 | if (parent instanceof ChildRelation) { |
644 | parent = editingDomain.getParent(parent); |
645 | } |
646 | |
647 | if (treeViewer.getChecked(parent)) { |
648 | treeViewer.setChecked(event.getElement(), true); |
649 | } |
650 | } |
651 | //check if node is NOT the root node |
652 | if (parent != null && !(parent instanceof FeatureDiagram)) { |
653 | if (treeViewer.getGrayed(event.getElement())) { |
654 | treeViewer.setGrayed(event.getElement(), false); |
655 | } |
656 | |
657 | //check/uncheck recursively |
658 | if (event.getChecked()) { |
659 | dirtyFlag = true; |
660 | functions.uncheckInModel((Feature)event.getElement(),true, overridesConfig); |
661 | functions.checkParents(event.getElement(),treeViewer,overridesConfig,editingDomain); |
662 | } |
663 | else { |
664 | functions.uncheckInModel((Feature)event.getElement(),false, overridesConfig); |
665 | functions.uncheckParents(event.getElement(),treeViewer,overridesConfig,editingDomain); |
666 | } |
667 | } |
668 | //make root node readonly |
669 | else { |
670 | treeViewer.setChecked(event.getElement(), !(event.getChecked())); |
671 | } |
672 | } |
673 | } |
674 | }; |
675 | |
676 | treeViewer.addCheckStateListener(listener); |
677 | createContextMenuFor(treeViewer); |
678 | |
679 | //Selects all mandatory Features |
680 | if (root != null) { |
681 | Feature curRoot = root.getRootFeature(); |
682 | functions.selectMandatoryFeatures(curRoot.getChildrelation(), treeViewer, overridesConfig); |
683 | } |
684 | |
685 | |
686 | } |
687 | |
688 | @Override |
689 | public boolean isDirty() { |
690 | return dirtyFlag; |
691 | } |
692 | |
693 | public void doSave(IProgressMonitor progressMonitor) { |
694 | // Save only resources that have actually changed. |
695 | // |
696 | final Map<Object, Object> saveOptions = new HashMap<Object, Object>(); |
697 | saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER); |
698 | |
699 | // Do the work within an operation because this is a long running activity that modifies the workbench. |
700 | // |
701 | WorkspaceModifyOperation operation = |
702 | new WorkspaceModifyOperation() { |
703 | // This is the method that gets invoked when the operation runs. |
704 | // |
705 | public void execute(IProgressMonitor monitor) { |
706 | // Save the resources to the file system. |
707 | // |
708 | boolean first = true; |
709 | for (Resource resource : editingDomain.getResourceSet().getResources()) { |
710 | if ((first || !resource.getContents().isEmpty() || functions.isPersisted(resource, editingDomain)) && !editingDomain.isReadOnly(resource)) { |
711 | try { |
712 | resource.save(saveOptions); |
713 | } |
714 | catch (IOException exception) { |
715 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Resource couldn't be loaded or saved!")); |
716 | errord.open(); |
717 | } |
718 | first = false; |
719 | } |
720 | } |
721 | } |
722 | }; |
723 | |
724 | try { |
725 | // This runs the options, and shows progress. |
726 | // |
727 | new ProgressMonitorDialog(getSite().getShell()).run(true, false, operation); |
728 | |
729 | // Refresh the necessary state. |
730 | // |
731 | ((BasicCommandStack)editingDomain.getCommandStack()).saveIsDone(); |
732 | dirtyFlag = false; |
733 | firePropertyChange(IEditorPart.PROP_DIRTY); |
734 | } |
735 | catch (InvocationTargetException e) { |
736 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Resource couldn't be saved!")); |
737 | errord.open(); |
738 | } |
739 | catch (InterruptedException e) { |
740 | ErrorDisplayDialog errord = new ErrorDisplayDialog(getContainer().getShell(),new Throwable("Resource couldn't be saved!")); |
741 | errord.open(); |
742 | } |
743 | } |
744 | |
745 | public void doSaveAs() { |
746 | SaveAsDialog saveAsDialog = new SaveAsDialog(getSite().getShell()); |
747 | saveAsDialog.open(); |
748 | IPath path = saveAsDialog.getResult(); |
749 | if (path != null) { |
750 | IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); |
751 | if (file != null) { |
752 | doSaveAs(URI.createPlatformResourceURI(file.getFullPath().toString(), true), new FileEditorInput(file)); |
753 | } |
754 | } |
755 | } |
756 | |
757 | /** |
758 | * <!-- begin-user-doc --> |
759 | * <!-- end-user-doc --> |
760 | * @generated |
761 | */ |
762 | protected void doSaveAs(URI uri, IEditorInput editorInput) { |
763 | (editingDomain.getResourceSet().getResources().get(0)).setURI(uri); |
764 | setInputWithNotify(editorInput); |
765 | setPartName(editorInput.getName()); |
766 | IProgressMonitor progressMonitor = new NullProgressMonitor(); |
767 | doSave(progressMonitor); |
768 | } |
769 | |
770 | public boolean isSaveAsAllowed() { |
771 | return true; |
772 | } |
773 | } |
774 | |
775 | class TreeLabelProvider implements ILabelProvider { |
776 | |
777 | public Image getImage(Object element){ |
778 | ImageDescriptor descriptor = null; |
779 | if (element instanceof Feature) { |
780 | descriptor = FeatureModelInstancePlugin.getImageDescriptor("Feature.png"); |
781 | } |
782 | else if (element instanceof FeatureGroup) { |
783 | descriptor = FeatureModelInstancePlugin.getImageDescriptor("FeatureGroup.png"); |
784 | } |
785 | Image image = descriptor.createImage(); |
786 | return image; |
787 | } |
788 | |
789 | public String getText(Object element) { |
790 | if (element instanceof Feature) { |
791 | return ((Feature)element).getName(); |
792 | } |
793 | else if (element instanceof FeatureGroup) { |
794 | return "FeatureGroup; Min: " + ((FeatureGroup)element).getMin() + ", Max: " + ((FeatureGroup)element).getMax(); |
795 | } |
796 | return null; |
797 | } |
798 | |
799 | public void addListener(ILabelProviderListener listener) { |
800 | } |
801 | |
802 | public void dispose() { |
803 | |
804 | } |
805 | public boolean isLabelProperty(Object element, String property) { |
806 | return false; |
807 | } |
808 | |
809 | public void removeListener(ILabelProviderListener listener) { |
810 | |
811 | } |
812 | |
813 | } |
814 | |
815 | class TreeContentProvider implements ITreeContentProvider { |
816 | |
817 | public Object[] getChildren(Object parentElement) { |
818 | if (parentElement instanceof Feature) { |
819 | Object [] children; |
820 | ChildRelation childRel = ((Feature)parentElement).getChildrelation(); |
821 | if (childRel instanceof Simple) { |
822 | Object [] mandatory = ((Simple)childRel).getMandatoryChildren().toArray(); |
823 | Object [] optional = ((Simple)childRel).getOptionalChildren().toArray(); |
824 | children = new Object [mandatory.length + optional.length]; |
825 | |
826 | System.arraycopy(mandatory, 0, children, 0, mandatory.length); |
827 | System.arraycopy(optional, 0, children, mandatory.length, optional.length); |
828 | } |
829 | //childRel is FeatureGroup |
830 | else { |
831 | children = new Object[1]; |
832 | children[0] = ((FeatureGroup)childRel); |
833 | } |
834 | return children; |
835 | } |
836 | else if (parentElement instanceof FeatureGroup) { |
837 | return ((FeatureGroup)parentElement).getChildren().toArray(); |
838 | } |
839 | else if (parentElement instanceof FeatureDiagram) { |
840 | Object [] newArray = new Object[1]; |
841 | newArray[0] = ((FeatureDiagram)parentElement).getRootFeature(); |
842 | return newArray; |
843 | } |
844 | return null; |
845 | } |
846 | |
847 | public Object getParent(Object element) { |
848 | if (element instanceof Feature) { |
849 | } |
850 | return null; |
851 | } |
852 | |
853 | public boolean hasChildren(Object element) { |
854 | boolean children = true; |
855 | if (element instanceof Feature) { |
856 | ChildRelation childRel = ((Feature)element).getChildrelation(); |
857 | if (childRel == null) { |
858 | children = false; |
859 | } |
860 | else if (childRel instanceof Simple) { |
861 | if ((((Simple)childRel).getMandatoryChildren().size() == 0) && (((Simple)childRel).getOptionalChildren().size() == 0)) { |
862 | children = false; |
863 | } |
864 | } |
865 | else { |
866 | if (((FeatureGroup)childRel).getChildren().size() == 0) { |
867 | children = false; |
868 | } |
869 | } |
870 | } |
871 | else if (element instanceof FeatureGroup) { |
872 | if (((FeatureGroup) element).getChildren().size() == 0) { |
873 | children = false; |
874 | } |
875 | } |
876 | return children; |
877 | } |
878 | |
879 | public Object[] getElements(Object inputElement) { |
880 | return getChildren(inputElement); |
881 | } |
882 | |
883 | public void dispose() { |
884 | } |
885 | |
886 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
887 | |
888 | } |
889 | |
890 | } |