KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > ecore > genmodel > impl > GenEnumImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: GenEnumImpl.java,v 1.5 2005/06/08 06:18:44 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.ecore.genmodel.impl;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
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 /**
51  * <!-- begin-user-doc -->
52  * An implementation of the model object '<em><b>Gen Enum</b></em>'.
53  * <!-- end-user-doc -->
54  * <p>
55  * The following features are implemented:
56  * <ul>
57  * <li>{@link org.eclipse.emf.codegen.ecore.genmodel.impl.GenEnumImpl#getEcoreEnum <em>Ecore Enum</em>}</li>
58  * <li>{@link org.eclipse.emf.codegen.ecore.genmodel.impl.GenEnumImpl#getGenEnumLiterals <em>Gen Enum Literals</em>}</li>
59  * </ul>
60  * </p>
61  *
62  * @generated
63  */

64 public class GenEnumImpl extends GenDataTypeImpl implements GenEnum
65 {
66   /**
67    * The cached value of the '{@link #getEcoreEnum() <em>Ecore Enum</em>}' reference.
68    * <!-- begin-user-doc -->
69    * <!-- end-user-doc -->
70    * @see #getEcoreEnum()
71    * @generated
72    * @ordered
73    */

74   protected EEnum ecoreEnum = null;
75
76   /**
77    * The cached value of the '{@link #getGenEnumLiterals() <em>Gen Enum Literals</em>}' containment reference list.
78    * <!-- begin-user-doc -->
79    * <!-- end-user-doc -->
80    * @see #getGenEnumLiterals()
81    * @generated
82    * @ordered
83    */

84   protected EList genEnumLiterals = null;
85
86   /**
87    * <!-- begin-user-doc -->
88    * <!-- end-user-doc -->
89    * @generated modifiable
90    */

91   protected GenEnumImpl()
92   {
93     super();
94   }
95
96   /**
97    * <!-- begin-user-doc -->
98    * <!-- end-user-doc -->
99    * @generated
100    */

101   protected EClass eStaticClass()
102   {
103     return GenModelPackage.eINSTANCE.getGenEnum();
104   }
105
106   /**
107    * <!-- begin-user-doc -->
108    * <!-- end-user-doc -->
109    * @generated
110    */

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   /**
127    * <!-- begin-user-doc -->
128    * <!-- end-user-doc -->
129    * @generated
130    */

131   public EEnum basicGetEcoreEnum()
132   {
133     return ecoreEnum;
134   }
135
136   /**
137    * <!-- begin-user-doc -->
138    * <!-- end-user-doc -->
139    * @generated
140    */

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   /**
150    * <!-- begin-user-doc -->
151    * <!-- end-user-doc -->
152    * @generated
153    */

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   /**
164    * <!-- begin-user-doc -->
165    * <!-- end-user-doc -->
166    * @generated
167    */

168   public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class JavaDoc 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   /**
186    * <!-- begin-user-doc -->
187    * <!-- end-user-doc -->
188    * @generated
189    */

190   public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class JavaDoc 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   /**
206    * <!-- begin-user-doc -->
207    * <!-- end-user-doc -->
208    * @generated
209    */

210   public Object JavaDoc 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   /**
229    * <!-- begin-user-doc -->
230    * <!-- end-user-doc -->
231    * @generated
232    */

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   /**
250    * <!-- begin-user-doc -->
251    * <!-- end-user-doc -->
252    * @generated
253    */

254   public void eSet(EStructuralFeature eFeature, Object JavaDoc 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 JavaDoc)newValue);
267         return;
268     }
269     eDynamicSet(eFeature, newValue);
270   }
271
272   /**
273    * <!-- begin-user-doc -->
274    * <!-- end-user-doc -->
275    * @generated
276    */

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 JavaDoc getImportedMetaType()
305   {
306     return getGenModel().getImportedName("org.eclipse.emf.ecore.EEnum");
307   }
308
309   public String JavaDoc getQualifiedName()
310   {
311     return getGenPackage().getInterfacePackageName() + "." + getName();
312   }
313
314   public String JavaDoc getImportedName()
315   {
316     return getGenModel().getImportedName(getQualifiedName());
317   }
318
319 /*
320   public String getImportedInstanceClassName()
321   {
322     return getGenModel().getImportedName("org.eclipse.emf.ecore.EEnumLiteral");
323   }
324 */

325
326   public String JavaDoc 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 JavaDoc literalName)
337   {
338     for (Iterator JavaDoc 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     //FB TBD if (literalName != null || getGenEnumLiterals().isEmpty()) report error?
347
return getGenEnumLiterals().isEmpty() ? null : (GenEnumLiteral)getGenEnumLiterals().get(0);
348   }
349
350   public List JavaDoc getUniqueValuedGenEnumLiterals()
351   {
352     List JavaDoc result = new ArrayList JavaDoc();
353     Set JavaDoc resultSet = new HashSet JavaDoc();
354
355     for (Iterator JavaDoc iter = getGenEnumLiterals().iterator(); iter.hasNext(); )
356     {
357       GenEnumLiteral genEnumLiteral = (GenEnumLiteral)iter.next();
358       if (resultSet.add(new Integer JavaDoc(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 JavaDoc iter = eEnum.getELiterals().iterator(); iter.hasNext(); )
377     {
378       EEnumLiteral eEnumLiteral = (EEnumLiteral)iter.next();
379
380       for (Iterator JavaDoc 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 JavaDoc [] { getFormattedName() }));
411       progressMonitor.subTask
412         (CodeGenEcorePlugin.INSTANCE.getString
413            ("_UI_GeneratingJavaClass_message", new Object JavaDoc [] { 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 JavaDoc i = getGenEnumLiterals().iterator(); i.hasNext(); )
444       {
445         GenEnumLiteral genEnumLiteral = (GenEnumLiteral)i.next();
446         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc getStaticValue(String JavaDoc literal)
491   {
492     GenEnumLiteral genEnumLiteral = getGenEnumLiteral(literal);
493     return genEnumLiteral == null ? "null" : getImportedName() + "." + genEnumLiteral.getEnumLiteralID() + "_LITERAL";
494   }
495
496 } //GenEnumImpl
497
Popular Tags