EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.featureinstance]

COVERAGE SUMMARY FOR SOURCE FILE [FeatureModelInstanceEditor.java]

nameclass, %method, %block, %line, %
FeatureModelInstanceEditor.java0%   (0/6)0%   (0/48)0%   (0/1606)0%   (0/347)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FeatureModelInstanceEditor0%   (0/1)0%   (0/28)0%   (0/1163)0%   (0/262)
FeatureModelInstanceEditor (): void 0%   (0/1)0%   (0/19)0%   (0/6)
access$0 (FeatureModelInstanceEditor): Composite 0%   (0/1)0%   (0/3)0%   (0/1)
addSelectionChangedListener (ISelectionChangedListener): void 0%   (0/1)0%   (0/4)0%   (0/2)
createContextMenuFor (StructuredViewer): void 0%   (0/1)0%   (0/29)0%   (0/7)
createEditor (): void 0%   (0/1)0%   (0/106)0%   (0/19)
createNewConfigResource (URI, FeatureDiagram, FeatureConfig): void 0%   (0/1)0%   (0/100)0%   (0/24)
createPages (): void 0%   (0/1)0%   (0/6)0%   (0/3)
createResource (URI): void 0%   (0/1)0%   (0/52)0%   (0/12)
createViewer (FeatureDiagram): void 0%   (0/1)0%   (0/100)0%   (0/21)
doSave (IProgressMonitor): void 0%   (0/1)0%   (0/69)0%   (0/15)
doSaveAs (): void 0%   (0/1)0%   (0/34)0%   (0/8)
doSaveAs (URI, IEditorInput): void 0%   (0/1)0%   (0/24)0%   (0/6)
fillContextMenu (IMenuManager): void 0%   (0/1)0%   (0/13)0%   (0/3)
firePropertyChange (int): void 0%   (0/1)0%   (0/4)0%   (0/2)
getActionBarContributor (): EditingDomainActionBarContributor 0%   (0/1)0%   (0/5)0%   (0/1)
getEditingDomain (): EditingDomain 0%   (0/1)0%   (0/3)0%   (0/1)
getFeatureDiagram (Resource): FeatureDiagram 0%   (0/1)0%   (0/24)0%   (0/8)
getResource (): Resource 0%   (0/1)0%   (0/3)0%   (0/1)
getSelection (): ISelection 0%   (0/1)0%   (0/3)0%   (0/1)
handleConfigCases (Resource): Resource 0%   (0/1)0%   (0/166)0%   (0/34)
handleFileCases (String, String, String): void 0%   (0/1)0%   (0/299)0%   (0/61)
init (IEditorSite, IEditorInput): void 0%   (0/1)0%   (0/13)0%   (0/4)
isDirty (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isSaveAsAllowed (): boolean 0%   (0/1)0%   (0/2)0%   (0/1)
removeSelectionChangedListener (ISelectionChangedListener): void 0%   (0/1)0%   (0/4)0%   (0/2)
setSelection (ISelection): void 0%   (0/1)0%   (0/12)0%   (0/3)
startFileWizard (String, String): URI 0%   (0/1)0%   (0/29)0%   (0/6)
startOpenDialog (String): URI 0%   (0/1)0%   (0/34)0%   (0/9)
     
class FeatureModelInstanceEditor$10%   (0/1)0%   (0/2)0%   (0/11)0%   (0/4)
FeatureModelInstanceEditor$1 (FeatureModelInstanceEditor): void 0%   (0/1)0%   (0/6)0%   (0/2)
menuAboutToShow (IMenuManager): void 0%   (0/1)0%   (0/5)0%   (0/2)
     
class FeatureModelInstanceEditor$20%   (0/1)0%   (0/2)0%   (0/164)0%   (0/21)
FeatureModelInstanceEditor$2 (FeatureModelInstanceEditor): void 0%   (0/1)0%   (0/6)0%   (0/2)
checkStateChanged (CheckStateChangedEvent): void 0%   (0/1)0%   (0/158)0%   (0/19)
     
class FeatureModelInstanceEditor$30%   (0/1)0%   (0/2)0%   (0/71)0%   (0/11)
FeatureModelInstanceEditor$3 (FeatureModelInstanceEditor, Map): void 0%   (0/1)0%   (0/9)0%   (0/2)
execute (IProgressMonitor): void 0%   (0/1)0%   (0/62)0%   (0/9)
     
class TreeContentProvider0%   (0/1)0%   (0/7)0%   (0/141)0%   (0/38)
TreeContentProvider (): void 0%   (0/1)0%   (0/3)0%   (0/1)
dispose (): void 0%   (0/1)0%   (0/1)0%   (0/1)
getChildren (Object): Object [] 0%   (0/1)0%   (0/77)0%   (0/18)
getElements (Object): Object [] 0%   (0/1)0%   (0/4)0%   (0/1)
getParent (Object): Object 0%   (0/1)0%   (0/5)0%   (0/2)
hasChildren (Object): boolean 0%   (0/1)0%   (0/50)0%   (0/14)
inputChanged (Viewer, Object, Object): void 0%   (0/1)0%   (0/1)0%   (0/1)
     
class TreeLabelProvider0%   (0/1)0%   (0/7)0%   (0/56)0%   (0/17)
TreeLabelProvider (): void 0%   (0/1)0%   (0/3)0%   (0/1)
addListener (ILabelProviderListener): void 0%   (0/1)0%   (0/1)0%   (0/1)
dispose (): void 0%   (0/1)0%   (0/1)0%   (0/1)
getImage (Object): Image 0%   (0/1)0%   (0/20)0%   (0/7)
getText (Object): String 0%   (0/1)0%   (0/28)0%   (0/5)
isLabelProperty (Object, String): boolean 0%   (0/1)0%   (0/2)0%   (0/1)
removeListener (ILabelProviderListener): void 0%   (0/1)0%   (0/1)0%   (0/1)

1package de.uka.ipd.sdq.featureinstance;
2 
3import java.io.File;
4import java.io.IOException;
5import java.lang.reflect.InvocationTargetException;
6import java.util.Collections;
7import java.util.HashMap;
8import java.util.Iterator;
9import java.util.List;
10import java.util.Map;
11 
12import org.eclipse.core.resources.IFile;
13import org.eclipse.core.resources.ResourcesPlugin;
14import org.eclipse.core.runtime.IPath;
15import org.eclipse.core.runtime.IProgressMonitor;
16import org.eclipse.core.runtime.NullProgressMonitor;
17import org.eclipse.core.runtime.Platform;
18import org.eclipse.emf.common.command.BasicCommandStack;
19import org.eclipse.emf.common.util.EList;
20import org.eclipse.emf.common.util.URI;
21import org.eclipse.emf.ecore.EObject;
22import org.eclipse.emf.ecore.resource.Resource;
23import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
24import org.eclipse.emf.edit.domain.EditingDomain;
25import org.eclipse.emf.edit.domain.IEditingDomainProvider;
26import org.eclipse.emf.edit.ui.action.EditingDomainActionBarContributor;
27import org.eclipse.emf.edit.ui.action.LoadResourceAction.LoadResourceDialog;
28import org.eclipse.emf.edit.ui.util.EditUIUtil;
29import org.eclipse.jface.action.IMenuListener;
30import org.eclipse.jface.action.IMenuManager;
31import org.eclipse.jface.action.MenuManager;
32import org.eclipse.jface.action.Separator;
33import org.eclipse.jface.dialogs.MessageDialog;
34import org.eclipse.jface.dialogs.ProgressMonitorDialog;
35import org.eclipse.jface.resource.ImageDescriptor;
36import org.eclipse.jface.viewers.CheckStateChangedEvent;
37import org.eclipse.jface.viewers.CheckboxTreeViewer;
38import org.eclipse.jface.viewers.ICheckStateListener;
39import org.eclipse.jface.viewers.ILabelProvider;
40import org.eclipse.jface.viewers.ILabelProviderListener;
41import org.eclipse.jface.viewers.ISelection;
42import org.eclipse.jface.viewers.ISelectionChangedListener;
43import org.eclipse.jface.viewers.ISelectionProvider;
44import org.eclipse.jface.viewers.IStructuredSelection;
45import org.eclipse.jface.viewers.ITreeContentProvider;
46import org.eclipse.jface.viewers.SelectionChangedEvent;
47import org.eclipse.jface.viewers.StructuredSelection;
48import org.eclipse.jface.viewers.StructuredViewer;
49import org.eclipse.jface.viewers.TreeViewer;
50import org.eclipse.jface.viewers.Viewer;
51import org.eclipse.jface.wizard.WizardDialog;
52import org.eclipse.swt.SWT;
53import org.eclipse.swt.custom.CTabFolder;
54import org.eclipse.swt.graphics.Image;
55import org.eclipse.swt.graphics.Point;
56import org.eclipse.swt.layout.FillLayout;
57import org.eclipse.swt.widgets.Composite;
58import org.eclipse.swt.widgets.Menu;
59import org.eclipse.ui.IEditorInput;
60import org.eclipse.ui.IEditorPart;
61import org.eclipse.ui.IEditorSite;
62import org.eclipse.ui.IFileEditorInput;
63import org.eclipse.ui.PartInitException;
64import org.eclipse.ui.actions.WorkspaceModifyOperation;
65import org.eclipse.ui.dialogs.SaveAsDialog;
66import org.eclipse.ui.part.FileEditorInput;
67import org.eclipse.ui.part.MultiPageEditorPart;
68import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
69import org.eclipse.ui.views.properties.PropertySheetPage;
70 
71import de.uka.ipd.sdq.dialogs.error.ErrorDisplayDialog;
72import de.uka.ipd.sdq.featureconfig.ConfigNode;
73import de.uka.ipd.sdq.featureconfig.ConfigState;
74import de.uka.ipd.sdq.featureconfig.Configuration;
75import de.uka.ipd.sdq.featureconfig.FeatureConfig;
76import de.uka.ipd.sdq.featureconfig.impl.featureconfigFactoryImpl;
77import de.uka.ipd.sdq.featuremodel.ChildRelation;
78import de.uka.ipd.sdq.featuremodel.Feature;
79import de.uka.ipd.sdq.featuremodel.FeatureDiagram;
80import de.uka.ipd.sdq.featuremodel.FeatureGroup;
81import de.uka.ipd.sdq.featuremodel.Simple;
82 
83/**
84 * @author Grischa Liebel
85 *
86 */
87public 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 
775class 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 
815class 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}

[all classes][de.uka.ipd.sdq.featureinstance]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov