| 1 | package de.uka.ipd.sdq.featureinstance; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.lang.reflect.InvocationTargetException; |
| 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.LinkedList; |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | |
| 11 | import org.eclipse.core.runtime.IProgressMonitor; |
| 12 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 13 | import org.eclipse.emf.common.command.BasicCommandStack; |
| 14 | import org.eclipse.emf.common.util.EList; |
| 15 | import org.eclipse.emf.common.util.URI; |
| 16 | import org.eclipse.emf.ecore.resource.Resource; |
| 17 | import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
| 18 | import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 19 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 20 | import org.eclipse.jface.viewers.CheckboxTreeViewer; |
| 21 | import org.eclipse.jface.viewers.ICheckStateListener; |
| 22 | import org.eclipse.jface.viewers.TreeViewer; |
| 23 | import org.eclipse.swt.widgets.Composite; |
| 24 | import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| 25 | import org.eclipse.ui.views.contentoutline.IContentOutlinePage; |
| 26 | import org.eclipse.ui.views.properties.PropertySheetPage; |
| 27 | |
| 28 | import de.uka.ipd.sdq.dialogs.error.ErrorDisplayDialog; |
| 29 | import de.uka.ipd.sdq.featureconfig.ConfigNode; |
| 30 | import de.uka.ipd.sdq.featureconfig.ConfigState; |
| 31 | import de.uka.ipd.sdq.featureconfig.Configuration; |
| 32 | import de.uka.ipd.sdq.featureconfig.FeatureConfig; |
| 33 | import de.uka.ipd.sdq.featureconfig.impl.featureconfigFactoryImpl; |
| 34 | import de.uka.ipd.sdq.featuremodel.Feature; |
| 35 | import de.uka.ipd.sdq.featuremodel.FeatureDiagram; |
| 36 | import de.uka.ipd.sdq.featuremodel.FeatureGroup; |
| 37 | |
| 38 | public class FeatureConfigWidget { |
| 39 | |
| 40 | private Composite parent; |
| 41 | private String sourceInput; |
| 42 | private String targetInput; |
| 43 | protected AdapterFactoryEditingDomain editingDomain; |
| 44 | protected CheckboxTreeViewer treeViewer; |
| 45 | protected PropertySheetPage propertySheetPage; |
| 46 | protected ICheckStateListener listener; |
| 47 | protected Resource resource; |
| 48 | protected FeatureDiagram featureDiagram; |
| 49 | protected Object root; |
| 50 | protected TreeViewer contentOutlineViewer; |
| 51 | protected IContentOutlinePage contentOutlinePage; |
| 52 | protected boolean dirtyFlag = false; |
| 53 | protected FeatureConfig defaultConfig; |
| 54 | protected FeatureConfig overridesConfig; |
| 55 | private List<ICheckStateListener> checkStateListeners; |
| 56 | private String errorMessage; |
| 57 | private InstanceValidateAction validateAction; |
| 58 | private FeatureConfigFunctionality functions; |
| 59 | |
| 60 | public FeatureConfigWidget(Composite parent) { |
| 61 | this.parent = parent; |
| 62 | checkStateListeners = new LinkedList<ICheckStateListener>(); |
| 63 | validateAction = new InstanceValidateAction(); |
| 64 | |
| 65 | functions = new FeatureConfigFunctionality(); |
| 66 | editingDomain = functions.initializeEditingDomain(); |
| 67 | } |
| 68 | |
| 69 | public void setSourceInput(String sourceInput) { |
| 70 | this.sourceInput = sourceInput; |
| 71 | } |
| 72 | |
| 73 | public void setTargetInput(String targetInput) { |
| 74 | this.targetInput = targetInput; |
| 75 | } |
| 76 | |
| 77 | public void validate () { |
| 78 | validateAction.setConfiguration(functions.getConfiguration(resource)); |
| 79 | validateAction.setShell(parent.getShell()); |
| 80 | validateAction.run(); |
| 81 | } |
| 82 | |
| 83 | public boolean createPages() { |
| 84 | boolean valid = checkValid(); |
| 85 | if (valid) { |
| 86 | createResource(); |
| 87 | |
| 88 | if (resource == null) { |
| 89 | errorMessage = "Resource couldn't be loaded"; |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return createEditor(); |
| 94 | } else { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates the editor layout and content |
| 101 | */ |
| 102 | protected boolean createEditor() { |
| 103 | // handles the different cases of opened files and model/configuration |
| 104 | // cases |
| 105 | boolean valid = handleConfigCases(); |
| 106 | |
| 107 | if (featureDiagram != null && overridesConfig != null && valid) { |
| 108 | |
| 109 | createViewer(featureDiagram); |
| 110 | // //set the needed attributes for the validation in the |
| 111 | // actionBarContributor |
| 112 | // EditingDomainActionBarContributor contrib = |
| 113 | // getActionBarContributor(); |
| 114 | // if (contrib instanceof FeatureModelInstanceContributor) { |
| 115 | // ((FeatureModelInstanceContributor)contrib).setConfiguration(getConfiguration(resource)); |
| 116 | // ((FeatureModelInstanceContributor)contrib).setShell(getContainer().getShell()); |
| 117 | // } |
| 118 | return valid; |
| 119 | // |
| 120 | } else { |
| 121 | errorMessage = "FeatureDiagram couldn't be referenced"; |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Handles the different cases for a loaded *.featureconfig-resource |
| 128 | * http://sdqweb.ipd.uka.de/mediawiki/images/6/61/Check_Cases.png |
| 129 | * |
| 130 | * @param resource |
| 131 | * The resource in which the configuration object should be |
| 132 | * stored |
| 133 | * @return The resource object which stores the (prop. new) overrides config |
| 134 | * object |
| 135 | */ |
| 136 | private boolean handleConfigCases() { |
| 137 | |
| 138 | // Check if featureconfig file is valid (Configuration object can be |
| 139 | // referenced) |
| 140 | Configuration configuration = functions.getConfiguration(resource); |
| 141 | |
| 142 | EList<FeatureConfig> tempOverrides = configuration.getConfigOverrides(); |
| 143 | FeatureConfig tempDefault = configuration.getDefaultConfig(); |
| 144 | |
| 145 | // Both FeatureConfigs are null |
| 146 | if ((tempOverrides == null || tempOverrides.isEmpty()) |
| 147 | && tempDefault == null) { |
| 148 | errorMessage = "Model contains no FeatureConfig object"; |
| 149 | return false; |
| 150 | } else if (tempOverrides == null && tempDefault != null) { // default |
| 151 | // exists |
| 152 | EList<ConfigNode> configList = tempDefault.getConfignode(); |
| 153 | |
| 154 | if (!configList.isEmpty()) { |
| 155 | // Try to reference the Feature Diagram object and create a new |
| 156 | // overrides from it |
| 157 | featureDiagram = functions.navigateToFeatureDiagram((Feature) configList |
| 158 | .iterator().next().getOrigin(), editingDomain); |
| 159 | |
| 160 | // start wizard an ask for a new file location |
| 161 | // *.featureconfig with only a default config shall only be used |
| 162 | // as template |
| 163 | URI newResourceURI = URI.createPlatformResourceURI(targetInput, |
| 164 | true); |
| 165 | createNewConfigResource(newResourceURI, featureDiagram, |
| 166 | tempDefault); |
| 167 | } else { |
| 168 | errorMessage = "FeatureConfig objects are empty"; |
| 169 | return false; |
| 170 | } |
| 171 | } else if (tempOverrides != null && !tempOverrides.isEmpty() |
| 172 | && tempDefault == null) { |
| 173 | FeatureConfig featureConfig = tempOverrides.get(0); // assumption: |
| 174 | // only one |
| 175 | // feature |
| 176 | // diagram |
| 177 | // present |
| 178 | EList<ConfigNode> configList = featureConfig.getConfignode(); |
| 179 | |
| 180 | // Try to reference the Feature Diagram object |
| 181 | if (!configList.isEmpty()) { |
| 182 | featureDiagram = functions.navigateToFeatureDiagram((Feature) configList |
| 183 | .iterator().next().getOrigin(), editingDomain); |
| 184 | overridesConfig = featureConfig; |
| 185 | URI newResourceURI = URI.createPlatformResourceURI(targetInput, |
| 186 | true); |
| 187 | doSaveAs(newResourceURI); |
| 188 | } else { |
| 189 | errorMessage = "FeatureConfig objects are empty"; |
| 190 | return false; |
| 191 | } |
| 192 | } else { |
| 193 | // both configs are present |
| 194 | boolean configPresent = false; |
| 195 | EList<ConfigNode> configList; |
| 196 | |
| 197 | // Check for OverridesConfig |
| 198 | if (!tempOverrides.isEmpty()) { |
| 199 | FeatureConfig featureConfig = tempOverrides.get(0); // assumption: |
| 200 | // only one |
| 201 | // feature |
| 202 | // diagram |
| 203 | // present |
| 204 | configList = featureConfig.getConfignode(); |
| 205 | |
| 206 | // Try to reference the Feature Diagram object |
| 207 | if (!(configList.isEmpty())) { |
| 208 | featureDiagram = functions.navigateToFeatureDiagram((Feature) configList |
| 209 | .iterator().next().getOrigin(), editingDomain); |
| 210 | overridesConfig = featureConfig; |
| 211 | configPresent = true; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Check for DefaultConfig |
| 216 | configList = tempDefault.getConfignode(); |
| 217 | |
| 218 | if (!(configList.isEmpty())) { |
| 219 | // if the feature diagram is already referenced, just save the |
| 220 | // default config |
| 221 | if (configPresent) { |
| 222 | defaultConfig = tempDefault; |
| 223 | URI newResourceURI = URI.createPlatformResourceURI( |
| 224 | targetInput, true); |
| 225 | doSaveAs(newResourceURI); |
| 226 | } |
| 227 | // else, try to reference the feature config object by using the |
| 228 | // default config |
| 229 | // and ask for new save location (see case that only a default |
| 230 | // config is present) |
| 231 | else { |
| 232 | featureDiagram = functions.navigateToFeatureDiagram((Feature) configList |
| 233 | .iterator().next().getOrigin(), editingDomain); |
| 234 | URI newResourceURI = URI.createPlatformResourceURI( |
| 235 | targetInput, true); |
| 236 | createNewConfigResource(newResourceURI, featureDiagram, |
| 237 | tempDefault); |
| 238 | } |
| 239 | } else { |
| 240 | if (configPresent) { |
| 241 | URI newResourceURI = URI.createPlatformResourceURI( |
| 242 | targetInput, true); |
| 243 | doSaveAs(newResourceURI); |
| 244 | } else { |
| 245 | errorMessage = "FeatureConfig objects are empty"; |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Creates a treeViewer of the given FeatureDiagram |
| 255 | * |
| 256 | * @param root |
| 257 | * The FeatureDiagram-object which shall be displayed |
| 258 | */ |
| 259 | public void createViewer(FeatureDiagram root) { |
| 260 | if (treeViewer == null) { |
| 261 | treeViewer = new CheckboxTreeViewer(parent); |
| 262 | treeViewer.setContentProvider(new TreeContentProvider()); |
| 263 | treeViewer.setLabelProvider(new TreeLabelProvider()); |
| 264 | |
| 265 | for (ICheckStateListener currentListener : checkStateListeners) { |
| 266 | treeViewer.addCheckStateListener(currentListener); |
| 267 | } |
| 268 | } |
| 269 | treeViewer.setInput(root); |
| 270 | treeViewer.expandAll(); |
| 271 | |
| 272 | if (root != null) { |
| 273 | treeViewer.setGrayed(root.getRootFeature(), true); |
| 274 | |
| 275 | // Gray FeatureGroups |
| 276 | Feature curRoot = root.getRootFeature(); |
| 277 | functions.grayFeatureGroups(curRoot.getChildrelation(), treeViewer); |
| 278 | } |
| 279 | |
| 280 | if (defaultConfig != null) { |
| 281 | functions.markDefaultConfig(defaultConfig,treeViewer); |
| 282 | } |
| 283 | if (overridesConfig != null) { |
| 284 | functions.markOverridesConfig(overridesConfig,treeViewer); |
| 285 | } |
| 286 | |
| 287 | listener = new ICheckStateListener() { |
| 288 | public void checkStateChanged(CheckStateChangedEvent event) { |
| 289 | |
| 290 | // make FeatureGroups readonly |
| 291 | if (event.getElement() instanceof FeatureGroup) { |
| 292 | treeViewer.setChecked(event.getElement(), !(event |
| 293 | .getChecked())); |
| 294 | } else { |
| 295 | Object parent = editingDomain.getParent(event.getElement()); |
| 296 | |
| 297 | // automatically unchecks Feature again, if its a mandatory |
| 298 | // Feature and the parent node is selected |
| 299 | if ((event.getElement() instanceof Feature)) { |
| 300 | |
| 301 | if ((functions.checkMandatory((Feature) event.getElement(), editingDomain))) { |
| 302 | treeViewer.setChecked(event.getElement(), true); |
| 303 | functions.uncheckInModel((Feature)event.getElement(), true, overridesConfig); |
| 304 | } |
| 305 | // check if node is NOT the root node |
| 306 | else if (parent != null && !(parent instanceof FeatureDiagram)) { |
| 307 | if (treeViewer.getGrayed(event.getElement())) { |
| 308 | treeViewer.setGrayed(event.getElement(), false); |
| 309 | } |
| 310 | |
| 311 | // check/uncheck recursively |
| 312 | if (event.getChecked()) { |
| 313 | functions.uncheckInModel((Feature) event.getElement(), true, overridesConfig); |
| 314 | functions.checkParents(event.getElement(),treeViewer,overridesConfig,editingDomain); |
| 315 | } else { |
| 316 | functions.uncheckInModel((Feature) event.getElement(), false, overridesConfig); |
| 317 | functions.uncheckParents(event.getElement(),treeViewer,overridesConfig,editingDomain); |
| 318 | } |
| 319 | } |
| 320 | // make root node readonly |
| 321 | else { |
| 322 | treeViewer.setChecked(event.getElement(), !(event |
| 323 | .getChecked())); |
| 324 | } |
| 325 | dirtyFlag = true; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | }; |
| 330 | |
| 331 | treeViewer.addCheckStateListener(listener); |
| 332 | // createContextMenuFor(treeViewer); |
| 333 | |
| 334 | // Selects all mandatory Features |
| 335 | if (root != null) { |
| 336 | Feature curRoot = root.getRootFeature(); |
| 337 | functions.selectMandatoryFeatures(curRoot.getChildrelation(), treeViewer, overridesConfig); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Loads the resource-object through the editingDomain |
| 343 | */ |
| 344 | protected void createResource() { |
| 345 | URI resourceURI = URI.createPlatformResourceURI(sourceInput, true); |
| 346 | |
| 347 | // Try to load the resource through the editingDomain. |
| 348 | resource = null; |
| 349 | try { |
| 350 | resource = editingDomain.getResourceSet().getResource(resourceURI, |
| 351 | true); |
| 352 | } catch (Exception e) { |
| 353 | resource = editingDomain.getResourceSet().getResource(resourceURI, |
| 354 | false); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // /** |
| 359 | // * <!-- begin-user-doc --> |
| 360 | // * <!-- end-user-doc --> |
| 361 | // * @generated |
| 362 | // */ |
| 363 | // public EditingDomainActionBarContributor getActionBarContributor() { |
| 364 | // return |
| 365 | // (EditingDomainActionBarContributor)getEditorSite().getActionBarContributor(); |
| 366 | // } |
| 367 | |
| 368 | /** |
| 369 | * Creates a new Configuration-Resource with the given newResourceURI |
| 370 | * corresponding to the given featureDiagram Overrides the old |
| 371 | * (*.featuremodel) resource-object |
| 372 | * |
| 373 | * @param newResourceURI |
| 374 | * The URI for the new Resource |
| 375 | * @param featureDiagram |
| 376 | * A FeatureDiagram-object to which the new Configuration should |
| 377 | * reference |
| 378 | * @param defaultRef |
| 379 | * A reference to the defaultConfig object or null, if none |
| 380 | * exists |
| 381 | */ |
| 382 | protected void createNewConfigResource(URI newResourceURI, |
| 383 | FeatureDiagram featureDiagram, FeatureConfig defaultRef) { |
| 384 | if (newResourceURI == null) { |
| 385 | throw new NullPointerException("No Config file stored in resource!"); |
| 386 | } else { |
| 387 | // Create new featureconfig-resource and change current resource |
| 388 | resource = resource.getResourceSet().createResource(newResourceURI); |
| 389 | |
| 390 | featureconfigFactoryImpl factory = new featureconfigFactoryImpl(); |
| 391 | Configuration newConfig = factory.createConfiguration(); |
| 392 | newConfig.setName(featureDiagram.getName() + "_config"); |
| 393 | |
| 394 | FeatureConfig newOverrides = factory.createFeatureConfig(); |
| 395 | newConfig.getConfigOverrides().add(newOverrides); |
| 396 | |
| 397 | // set reference to default |
| 398 | if (defaultRef == null) { |
| 399 | defaultRef = factory.createFeatureConfig(); |
| 400 | } |
| 401 | newConfig.setDefaultConfig(defaultRef); |
| 402 | |
| 403 | ConfigNode rootConfigNode = factory.createConfigNode(); |
| 404 | rootConfigNode.setConfigState(ConfigState.ELIMINATED); |
| 405 | rootConfigNode.setOrigin((Feature) (featureDiagram) |
| 406 | .getRootFeature()); |
| 407 | |
| 408 | newOverrides.getConfignode().add(rootConfigNode); |
| 409 | |
| 410 | resource.getContents().add(newConfig); |
| 411 | |
| 412 | defaultConfig = defaultRef; |
| 413 | overridesConfig = newOverrides; |
| 414 | |
| 415 | try { |
| 416 | resource.load(Collections.EMPTY_MAP); |
| 417 | resource.save(Collections.EMPTY_MAP); |
| 418 | } catch (IOException e) { |
| 419 | ErrorDisplayDialog errord = new ErrorDisplayDialog(parent |
| 420 | .getShell(), new Throwable( |
| 421 | "Could not load or save the Resource!")); |
| 422 | errord.open(); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Handles the different file types for the loaded resource. |
| 429 | * |
| 430 | * @param fileExtension |
| 431 | * The file extension of the loaded resource |
| 432 | * @param path |
| 433 | * The complete path to the file including the fileName |
| 434 | * @param fileName |
| 435 | * The filename |
| 436 | */ |
| 437 | private boolean checkValid() { |
| 438 | if (sourceInput == null || targetInput == null |
| 439 | || !sourceInput.endsWith(".featureconfig") |
| 440 | || !targetInput.endsWith(".featureconfig")) { |
| 441 | errorMessage = "Empty Source/Target file or wrong file ending"; |
| 442 | return false; |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | public boolean isDirty() { |
| 448 | return dirtyFlag; |
| 449 | } |
| 450 | |
| 451 | public void doSave(IProgressMonitor progressMonitor) { |
| 452 | // Save only resources that have actually changed. |
| 453 | // |
| 454 | final Map<Object, Object> saveOptions = new HashMap<Object, Object>(); |
| 455 | saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, |
| 456 | Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER); |
| 457 | |
| 458 | // Do the work within an operation because this is a long running |
| 459 | // activity that modifies the workbench. |
| 460 | // |
| 461 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { |
| 462 | // This is the method that gets invoked when the operation runs. |
| 463 | // |
| 464 | @Override |
| 465 | public void execute(IProgressMonitor monitor) { |
| 466 | // Save the resources to the file system. |
| 467 | // |
| 468 | boolean first = true; |
| 469 | for (Resource resource : editingDomain.getResourceSet() |
| 470 | .getResources()) { |
| 471 | if ((first || !resource.getContents().isEmpty() || functions.isPersisted(resource, editingDomain)) |
| 472 | && !editingDomain.isReadOnly(resource)) { |
| 473 | try { |
| 474 | resource.save(saveOptions); |
| 475 | } catch (IOException exception) { |
| 476 | ErrorDisplayDialog errord = new ErrorDisplayDialog( |
| 477 | parent.getShell(), |
| 478 | new Throwable( |
| 479 | "Resource couldn't be loaded or saved!")); |
| 480 | errord.open(); |
| 481 | } |
| 482 | first = false; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | }; |
| 487 | |
| 488 | try { |
| 489 | // This runs the options, and shows progress. |
| 490 | // |
| 491 | new ProgressMonitorDialog(parent.getShell()).run(true, false, |
| 492 | operation); |
| 493 | |
| 494 | // Refresh the necessary state. |
| 495 | // |
| 496 | ((BasicCommandStack) editingDomain.getCommandStack()).saveIsDone(); |
| 497 | dirtyFlag = false; |
| 498 | // firePropertyChange(IEditorPart.PROP_DIRTY); |
| 499 | } catch (InvocationTargetException e) { |
| 500 | ErrorDisplayDialog errord = new ErrorDisplayDialog(parent |
| 501 | .getShell(), new Throwable("Resource couldn't be saved!")); |
| 502 | errord.open(); |
| 503 | } catch (InterruptedException e) { |
| 504 | ErrorDisplayDialog errord = new ErrorDisplayDialog(parent |
| 505 | .getShell(), new Throwable("Resource couldn't be saved!")); |
| 506 | errord.open(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | protected void doSaveAs(URI uri) { |
| 511 | (editingDomain.getResourceSet().getResources().get(0)).setURI(uri); |
| 512 | IProgressMonitor progressMonitor = new NullProgressMonitor(); |
| 513 | doSave(progressMonitor); |
| 514 | } |
| 515 | |
| 516 | public boolean isSaveAsAllowed() { |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | public void addCheckStateListener(ICheckStateListener iCheckStateListener) { |
| 521 | checkStateListeners.add(iCheckStateListener); |
| 522 | } |
| 523 | |
| 524 | public String getErrorMessage() { |
| 525 | return errorMessage; |
| 526 | } |
| 527 | |
| 528 | } |