1 17 package org.eclipse.emf.codegen.ecore.genmodel.impl; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 30 import org.eclipse.emf.codegen.ecore.CodeGenEcorePlugin; 31 import org.eclipse.emf.codegen.ecore.Generator; 32 import org.eclipse.emf.codegen.ecore.genmodel.GenEnum; 33 import org.eclipse.emf.codegen.ecore.genmodel.GenEnumLiteral; 34 import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage; 35 import org.eclipse.emf.common.notify.Notification; 36 import org.eclipse.emf.common.notify.NotificationChain; 37 import org.eclipse.emf.common.util.EList; 38 import org.eclipse.emf.ecore.EClass; 39 import org.eclipse.emf.ecore.EClassifier; 40 import org.eclipse.emf.ecore.EDataType; 41 import org.eclipse.emf.ecore.EEnum; 42 import org.eclipse.emf.ecore.EEnumLiteral; 43 import org.eclipse.emf.ecore.EStructuralFeature; 44 import org.eclipse.emf.ecore.InternalEObject; 45 import org.eclipse.emf.ecore.impl.ENotificationImpl; 46 import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList; 47 import org.eclipse.emf.ecore.util.InternalEList; 48 49 50 64 public class GenEnumImpl extends GenDataTypeImpl implements GenEnum 65 { 66 74 protected EEnum ecoreEnum = null; 75 76 84 protected EList genEnumLiterals = null; 85 86 91 protected GenEnumImpl() 92 { 93 super(); 94 } 95 96 101 protected EClass eStaticClass() 102 { 103 return GenModelPackage.eINSTANCE.getGenEnum(); 104 } 105 106 111 public EEnum getEcoreEnum() 112 { 113 if (ecoreEnum != null && ecoreEnum.eIsProxy()) 114 { 115 EEnum oldEcoreEnum = ecoreEnum; 116 ecoreEnum = (EEnum)eResolveProxy((InternalEObject)ecoreEnum); 117 if (ecoreEnum != oldEcoreEnum) 118 { 119 if (eNotificationRequired()) 120 eNotify(new ENotificationImpl(this, Notification.RESOLVE, GenModelPackage.GEN_ENUM__ECORE_ENUM, oldEcoreEnum, ecoreEnum)); 121 } 122 } 123 return ecoreEnum; 124 } 125 126 131 public EEnum basicGetEcoreEnum() 132 { 133 return ecoreEnum; 134 } 135 136 141 public void setEcoreEnum(EEnum newEcoreEnum) 142 { 143 EEnum oldEcoreEnum = ecoreEnum; 144 ecoreEnum = newEcoreEnum; 145 if (eNotificationRequired()) 146 eNotify(new ENotificationImpl(this, Notification.SET, GenModelPackage.GEN_ENUM__ECORE_ENUM, oldEcoreEnum, ecoreEnum)); 147 } 148 149 154 public EList getGenEnumLiterals() 155 { 156 if (genEnumLiterals == null) 157 { 158 genEnumLiterals = new EObjectContainmentWithInverseEList(GenEnumLiteral.class, this, GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS, GenModelPackage.GEN_ENUM_LITERAL__GEN_ENUM); 159 } 160 return genEnumLiterals; 161 } 162 163 168 public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 169 { 170 if (featureID >= 0) 171 { 172 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 173 { 174 case GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS: 175 return ((InternalEList)getGenEnumLiterals()).basicAdd(otherEnd, msgs); 176 default: 177 return eDynamicInverseAdd(otherEnd, featureID, baseClass, msgs); 178 } 179 } 180 if (eContainer != null) 181 msgs = eBasicRemoveFromContainer(msgs); 182 return eBasicSetContainer(otherEnd, featureID, msgs); 183 } 184 185 190 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 191 { 192 if (featureID >= 0) 193 { 194 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 195 { 196 case GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS: 197 return ((InternalEList)getGenEnumLiterals()).basicRemove(otherEnd, msgs); 198 default: 199 return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs); 200 } 201 } 202 return eBasicSetContainer(null, featureID, msgs); 203 } 204 205 210 public Object eGet(EStructuralFeature eFeature, boolean resolve) 211 { 212 switch (eDerivedStructuralFeatureID(eFeature)) 213 { 214 case GenModelPackage.GEN_ENUM__GEN_PACKAGE: 215 return getGenPackage(); 216 case GenModelPackage.GEN_ENUM__ECORE_DATA_TYPE: 217 if (resolve) return getEcoreDataType(); 218 return basicGetEcoreDataType(); 219 case GenModelPackage.GEN_ENUM__ECORE_ENUM: 220 if (resolve) return getEcoreEnum(); 221 return basicGetEcoreEnum(); 222 case GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS: 223 return getGenEnumLiterals(); 224 } 225 return eDynamicGet(eFeature, resolve); 226 } 227 228 233 public boolean eIsSet(EStructuralFeature eFeature) 234 { 235 switch (eDerivedStructuralFeatureID(eFeature)) 236 { 237 case GenModelPackage.GEN_ENUM__GEN_PACKAGE: 238 return getGenPackage() != null; 239 case GenModelPackage.GEN_ENUM__ECORE_DATA_TYPE: 240 return ecoreDataType != null; 241 case GenModelPackage.GEN_ENUM__ECORE_ENUM: 242 return ecoreEnum != null; 243 case GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS: 244 return genEnumLiterals != null && !genEnumLiterals.isEmpty(); 245 } 246 return eDynamicIsSet(eFeature); 247 } 248 249 254 public void eSet(EStructuralFeature eFeature, Object newValue) 255 { 256 switch (eDerivedStructuralFeatureID(eFeature)) 257 { 258 case GenModelPackage.GEN_ENUM__ECORE_DATA_TYPE: 259 setEcoreDataType((EDataType)newValue); 260 return; 261 case GenModelPackage.GEN_ENUM__ECORE_ENUM: 262 setEcoreEnum((EEnum)newValue); 263 return; 264 case GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS: 265 getGenEnumLiterals().clear(); 266 getGenEnumLiterals().addAll((Collection )newValue); 267 return; 268 } 269 eDynamicSet(eFeature, newValue); 270 } 271 272 277 public void eUnset(EStructuralFeature eFeature) 278 { 279 switch (eDerivedStructuralFeatureID(eFeature)) 280 { 281 case GenModelPackage.GEN_ENUM__ECORE_DATA_TYPE: 282 setEcoreDataType((EDataType)null); 283 return; 284 case GenModelPackage.GEN_ENUM__ECORE_ENUM: 285 setEcoreEnum((EEnum)null); 286 return; 287 case GenModelPackage.GEN_ENUM__GEN_ENUM_LITERALS: 288 getGenEnumLiterals().clear(); 289 return; 290 } 291 eDynamicUnset(eFeature); 292 } 293 294 public EClassifier getEcoreClassifier() 295 { 296 return getEcoreEnum(); 297 } 298 299 public EDataType getEcoreDataType() 300 { 301 return getEcoreEnum(); 302 } 303 304 public String getImportedMetaType() 305 { 306 return getGenModel().getImportedName("org.eclipse.emf.ecore.EEnum"); 307 } 308 309 public String getQualifiedName() 310 { 311 return getGenPackage().getInterfacePackageName() + "." + getName(); 312 } 313 314 public String getImportedName() 315 { 316 return getGenModel().getImportedName(getQualifiedName()); 317 } 318 319 325 326 public String getEnumLiteralID(GenEnumLiteral genEnumLiteral) 327 { 328 return getClassifierID() + "__" + genEnumLiteral.getEnumLiteralID(); 329 } 330 331 public int getEnumLiteralValue(GenEnumLiteral genEnumLiteral) 332 { 333 return getGenEnumLiterals().indexOf(genEnumLiteral); 334 } 335 336 public GenEnumLiteral getGenEnumLiteral(String literalName) 337 { 338 for (Iterator iter = getGenEnumLiterals().iterator(); iter.hasNext(); ) 339 { 340 GenEnumLiteral genEnumLiteral = (GenEnumLiteral)iter.next(); 341 if (genEnumLiteral.getName().equals(literalName)) 342 { 343 return genEnumLiteral; 344 } 345 } 346 return getGenEnumLiterals().isEmpty() ? null : (GenEnumLiteral)getGenEnumLiterals().get(0); 348 } 349 350 public List getUniqueValuedGenEnumLiterals() 351 { 352 List result = new ArrayList (); 353 Set resultSet = new HashSet (); 354 355 for (Iterator iter = getGenEnumLiterals().iterator(); iter.hasNext(); ) 356 { 357 GenEnumLiteral genEnumLiteral = (GenEnumLiteral)iter.next(); 358 if (resultSet.add(new Integer (genEnumLiteral.getValue()))) 359 { 360 result.add(genEnumLiteral); 361 } 362 } 363 return result; 364 } 365 366 public boolean isSerializable() 367 { 368 return true; 369 } 370 371 public void initialize(EEnum eEnum) 372 { 373 setEcoreEnum(eEnum); 374 375 LOOP: 376 for (Iterator iter = eEnum.getELiterals().iterator(); iter.hasNext(); ) 377 { 378 EEnumLiteral eEnumLiteral = (EEnumLiteral)iter.next(); 379 380 for (Iterator j = getGenEnumLiterals().iterator(); j.hasNext(); ) 381 { 382 GenEnumLiteral genEnumLiteral = (GenEnumLiteral)j.next(); 383 if (genEnumLiteral.getEcoreEnumLiteral() == eEnumLiteral) 384 { 385 genEnumLiteral.initialize(eEnumLiteral); 386 continue LOOP; 387 } 388 } 389 390 GenEnumLiteral genEnumLiteral = getGenModel().createGenEnumLiteral(); 391 getGenEnumLiterals().add(genEnumLiteral); 392 genEnumLiteral.initialize(eEnumLiteral); 393 } 394 } 395 396 protected boolean hasModelContribution() 397 { 398 return true; 399 } 400 401 public void generate(IProgressMonitor progressMonitor) 402 { 403 try 404 { 405 if (!canGenerate()) return; 406 407 progressMonitor.beginTask("", 1); 408 progressMonitor.subTask 409 (CodeGenEcorePlugin.INSTANCE.getString 410 ("_UI_Generating_message", new Object [] { getFormattedName() })); 411 progressMonitor.subTask 412 (CodeGenEcorePlugin.INSTANCE.getString 413 ("_UI_GeneratingJavaClass_message", new Object [] { getGenPackage().getInterfacePackageName() + "." + getName() })); 414 generate 415 (new SubProgressMonitor(progressMonitor, 1), 416 Generator.EMF_MODEL_PROJECT_STYLE, 417 getGenModel().getEffectiveModelPluginVariables(), 418 getGenModel().getModelDirectory(), 419 getGenPackage().getInterfacePackageName(), 420 getName(), 421 getGenModel().getEnumClassEmitter()); 422 } 423 finally 424 { 425 progressMonitor.done(); 426 } 427 } 428 429 public boolean canGenerateEdit() 430 { 431 return false; 432 } 433 434 public boolean canGenerateEditor() 435 { 436 return false; 437 } 438 439 public boolean reconcile(GenEnum oldGenEnumVersion) 440 { 441 if (getEcoreEnum().getName().equals(oldGenEnumVersion.getEcoreEnum().getName())) 442 { 443 for (Iterator i = getGenEnumLiterals().iterator(); i.hasNext(); ) 444 { 445 GenEnumLiteral genEnumLiteral = (GenEnumLiteral)i.next(); 446 for (Iterator j = oldGenEnumVersion.getGenEnumLiterals().iterator(); j.hasNext(); ) 447 { 448 GenEnumLiteral oldGenEnumLiteralVersion = (GenEnumLiteral)j.next(); 449 if (genEnumLiteral.reconcile(oldGenEnumLiteralVersion)) 450 { 451 break; 452 } 453 } 454 } 455 reconcileSettings(oldGenEnumVersion); 456 return true; 457 } 458 else 459 { 460 return false; 461 } 462 } 463 464 protected void reconcileSettings(GenEnum oldGenEnumVersion) 465 { 466 } 467 468 public boolean reconcile() 469 { 470 EEnum eEnum = getEcoreEnum(); 471 if (eEnum == null || eEnum.eIsProxy() || eEnum.eResource() == null) 472 { 473 return false; 474 } 475 else 476 { 477 for (Iterator i = getGenEnumLiterals().iterator(); i.hasNext(); ) 478 { 479 GenEnumLiteral genEnumLiteral = (GenEnumLiteral)i.next(); 480 if (!genEnumLiteral.reconcile()) 481 { 482 i.remove(); 483 } 484 } 485 486 return true; 487 } 488 } 489 490 public String getStaticValue(String literal) 491 { 492 GenEnumLiteral genEnumLiteral = getGenEnumLiteral(literal); 493 return genEnumLiteral == null ? "null" : getImportedName() + "." + genEnumLiteral.getEnumLiteralID() + "_LITERAL"; 494 } 495 496 } | Popular Tags |