KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > domain > AdapterFactoryEditingDomain


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: AdapterFactoryEditingDomain.java,v 1.13 2005/06/08 06:17:06 nickb Exp $
16  */

17 package org.eclipse.emf.edit.domain;
18
19
20 import java.io.File JavaDoc;
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.eclipse.emf.common.CommonPlugin;
33 import org.eclipse.emf.common.command.Command;
34 import org.eclipse.emf.common.command.CommandStack;
35 import org.eclipse.emf.common.command.CompoundCommand;
36 import org.eclipse.emf.common.command.UnexecutableCommand;
37 import org.eclipse.emf.common.notify.AdapterFactory;
38 import org.eclipse.emf.common.notify.impl.AdapterImpl;
39 import org.eclipse.emf.common.util.AbstractTreeIterator;
40 import org.eclipse.emf.common.util.TreeIterator;
41 import org.eclipse.emf.common.util.URI;
42 import org.eclipse.emf.ecore.EObject;
43 import org.eclipse.emf.ecore.resource.Resource;
44 import org.eclipse.emf.ecore.resource.ResourceSet;
45 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
46 import org.eclipse.emf.ecore.util.EcoreUtil;
47 import org.eclipse.emf.ecore.util.FeatureMap;
48 import org.eclipse.emf.edit.EMFEditPlugin;
49 import org.eclipse.emf.edit.command.CommandParameter;
50 import org.eclipse.emf.edit.command.CopyToClipboardCommand;
51 import org.eclipse.emf.edit.command.CreateChildCommand;
52 import org.eclipse.emf.edit.command.CutToClipboardCommand;
53 import org.eclipse.emf.edit.command.OverrideableCommand;
54 import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
55 import org.eclipse.emf.edit.command.RemoveCommand;
56 import org.eclipse.emf.edit.command.ReplaceCommand;
57 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
58 import org.eclipse.emf.edit.provider.IWrapperItemProvider;
59 import org.eclipse.emf.edit.provider.ItemProviderAdapter;
60
61
62
63 /**
64  * This class implements an editing domain by delegating to adapters that implement
65  * {@link org.eclipse.emf.edit.provider.IEditingDomainItemProvider}.
66  */

67 public class AdapterFactoryEditingDomain implements EditingDomain
68 {
69   /**
70    * This class is an adapter than implements {@link IEditingDomainProvider}.
71    */

72   public static class EditingDomainProvider extends AdapterImpl implements IEditingDomainProvider
73   {
74     /**
75      * Keeps track of the editing domain.
76      */

77     protected EditingDomain editingDomain;
78
79     public EditingDomainProvider(EditingDomain editingDomain)
80     {
81       this.editingDomain = editingDomain;
82     }
83
84     public EditingDomain getEditingDomain()
85     {
86       return editingDomain;
87     }
88
89     public boolean isAdapterForType(Object JavaDoc type)
90     {
91       return type == IEditingDomainProvider.class;
92     }
93   }
94
95   /**
96    * This returns the editing domain of the given EMF object, or null, if it can't be determined.
97    * This is implemented by checking whether the {@link org.eclipse.emf.ecore.resource.ResourceSet} of the object
98    * implements {@link IEditingDomainProvider}
99    * and returns that result or null.
100    *
101    * <p>
102    * Just as for {@link #getEditingDomainFor(java.lang.Object) getEditingDomainFor(Object)},
103    * it is recommended that you always keep an editing domain instance available through some other means.
104    */

105   static public EditingDomain getEditingDomainFor(EObject object)
106   {
107     Resource resource = object.eResource();
108     if (resource != null)
109     {
110       IEditingDomainProvider editingDomainProvider =
111         (IEditingDomainProvider)EcoreUtil.getExistingAdapter(resource, IEditingDomainProvider.class);
112       if (editingDomainProvider != null)
113       {
114         return editingDomainProvider.getEditingDomain();
115       }
116       else
117       {
118         ResourceSet resourceSet = resource.getResourceSet();
119         if (resourceSet instanceof IEditingDomainProvider)
120         {
121           EditingDomain editingDomain = ((IEditingDomainProvider)resourceSet).getEditingDomain();
122           return editingDomain;
123         }
124         else if (resourceSet != null)
125         {
126           editingDomainProvider = (IEditingDomainProvider)EcoreUtil.getExistingAdapter(resourceSet, IEditingDomainProvider.class);
127           if (editingDomainProvider != null)
128           {
129             return editingDomainProvider.getEditingDomain();
130           }
131         }
132       }
133     }
134
135     return null;
136   }
137
138   /**
139    * This returns the editing domain for the given aribtrary object, or null, if it can't be determined.
140    * It is recommended that you always work directly with an EditingDomain instance whenever possible.
141    * This is implemented to checks if the object itself implements {@link org.eclipse.emf.edit.domain.IEditingDomainProvider}
142    * and returns that result.
143    * Otherwise it checks if it is valid to call
144    * {@link #getEditingDomainFor(org.eclipse.emf.ecore.EObject) getEditingDomainFor(EObject)}
145    * and returns that result or null.
146    *
147    * <p>
148    * It is recommended that you always keep an editing domain instance available through some other means;
149    * this should only be used to implement things such as a global popup action for some object;
150    * in such a cases such as that the editing domain returned here
151    * may well be one that belongs to some editor you know nothing about,
152    * which is what you want.
153    */

154   static public EditingDomain getEditingDomainFor(Object JavaDoc object)
155   {
156     if (object instanceof IEditingDomainProvider)
157     {
158       EditingDomain editingDomain = ((IEditingDomainProvider)object).getEditingDomain();
159       return editingDomain;
160     }
161     else if (object instanceof EObject)
162     {
163       EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor((EObject)object);
164       return editingDomain;
165     }
166     else if (object instanceof FeatureMap.Entry)
167     {
168       return getEditingDomainFor(((FeatureMap.Entry)object).getValue());
169     }
170     else if (object instanceof IWrapperItemProvider)
171     {
172       return getEditingDomainFor(((IWrapperItemProvider)object).getValue());
173     }
174     else
175     {
176       return null;
177     }
178   }
179
180   /**
181    * This is a convenient way to determine the adapter to which editing domain methods are delegated for a given object,
182    * by providing nothing more than the object itself.
183    * It is recommended that you always work directly with an EditingDomain instance whenever possible.
184    * If the object is an EMF object,
185    * this is implemented to call {@link #getEditingDomainFor(java.lang.Object) getEditingDomainFor(Object)}
186    * and to return the adapter returned from the domain's adapter factory.
187    * If the object itself is an IEditingDomainItemProvider, it returns the object.
188    * Otherwise, it returns null.
189    */

190   static public IEditingDomainItemProvider getEditingDomainItemProviderFor(Object JavaDoc object)
191   {
192     if (object instanceof EObject)
193     {
194       EObject eObject = (EObject)object;
195       EditingDomain editingDomain = getEditingDomainFor(eObject);
196       if (editingDomain instanceof AdapterFactoryEditingDomain)
197       {
198         object = ((AdapterFactoryEditingDomain)editingDomain).getAdapterFactory().adapt(eObject, IEditingDomainItemProvider.class);
199       }
200     }
201
202     return
203       object instanceof IEditingDomainItemProvider ?
204         (IEditingDomainItemProvider)object :
205         object instanceof IWrapperItemProvider ?
206           getEditingDomainItemProviderFor(((IWrapperItemProvider)object).getValue()) :
207           object instanceof FeatureMap.Entry ?
208             getEditingDomainItemProviderFor(((FeatureMap.Entry)object).getValue()) :
209             null;
210   }
211
212   /**
213    * This is an implementation of a context that knows about this editing domain.
214    * It is used to help implement
215    * {@link #getEditingDomainFor(java.lang.Object) getEditingDomainFor(Object)}
216    * and {@link #getEditingDomainFor(org.eclipse.emf.ecore.EObject) getEditingDomainFor(EObject)}
217    * An instance of this is created if needed in the constructor.
218    *
219    */

220   protected class AdapterFactoryEditingDomainResourceSet extends ResourceSetImpl implements IEditingDomainProvider
221   {
222     public AdapterFactoryEditingDomainResourceSet()
223     {
224       super();
225       // setResourceFactoryRegister(new ExtensibleContextResourceFactoryRegister());
226
//EATM setResourceFactoryRegister(new ContextResourceFactoryRegister());
227
}
228
229     public EditingDomain getEditingDomain()
230     {
231       return AdapterFactoryEditingDomain.this;
232     }
233   }
234
235   /**
236    * This is the adapter factory used to create the adapter to which calls are delegated.
237    */

238   protected AdapterFactory adapterFactory;
239
240   /**
241    * This is the command stack that was passed into the constructor.
242    */

243   protected CommandStack commandStack;
244
245   /**
246    * This is the resource set used to contain all created and loaded resources.
247    */

248   protected ResourceSet resourceSet;
249
250   /**
251    * This is the current clipboard.
252    */

253   protected Collection JavaDoc clipboard;
254
255   /**
256    * This controls whether or not copy command optimizations are safe in this domain.
257    */

258   protected boolean optimizeCopy = true;
259
260   /**
261    * This controls whether the domain is read only.
262    */

263   protected Map JavaDoc resourceToReadOnlyMap;
264
265   /**
266    * Create an instance from the adapter factory, and the specialized command stack.
267    */

268   public AdapterFactoryEditingDomain(AdapterFactory adapterFactory, CommandStack commandStack)
269   {
270     this.adapterFactory = adapterFactory;
271     this.commandStack = commandStack;
272     this.resourceSet = new AdapterFactoryEditingDomainResourceSet();
273   }
274
275   /**
276    * Create an instance from the adapter factory, the specialized command stack, and the map used to maintain read only state.
277    */

278   public AdapterFactoryEditingDomain(AdapterFactory adapterFactory, CommandStack commandStack, Map JavaDoc resourceToReadOnlyMap)
279   {
280     this.adapterFactory = adapterFactory;
281     this.commandStack = commandStack;
282     this.resourceSet = new AdapterFactoryEditingDomainResourceSet();
283     this.resourceToReadOnlyMap = resourceToReadOnlyMap;
284   }
285
286   /**
287    * Create an instance from the adapter factory, the specialized command stack, and the specialized resource set.
288    * If the resource set's context is null, one will be created here;
289    * otherwize, the existing context should implement {@link org.eclipse.emf.edit.domain.IEditingDomainProvider}.
290    */

291   public AdapterFactoryEditingDomain(AdapterFactory adapterFactory, CommandStack commandStack, ResourceSet resourceSet)
292   {
293     this.adapterFactory = adapterFactory;
294     this.commandStack = commandStack;
295     this.resourceSet = resourceSet;
296
297 /*
298     if (resourceSet.getContext() == null)
299     {
300       Context context = new AdapterFactoryEditingDomainContext();
301       resourceSet.setContext(context);
302     }
303 */

304   }
305
306   /**
307    * This returns the adapter factory used by this domain.
308    */

309   public AdapterFactory getAdapterFactory()
310   {
311     return adapterFactory;
312   }
313
314   /**
315    * This sets the adapter factory after the domain is already created.
316    */

317   public void setAdapterFactory(AdapterFactory adapterFactory)
318   {
319     this.adapterFactory = adapterFactory;
320   }
321
322   /**
323    * This is a convenience method to create a resource, you could use the resource set returned by {@link #getResourceSet} directly.
324    */

325   public Resource createResource(String JavaDoc fileNameURI)
326   {
327     URI uri = URI.createURI(fileNameURI);
328     Resource resource = resourceSet.createResource(uri);
329     return resource;
330   }
331
332   /**
333    * This is a convenience method to load a resource, you could use the resource set returned by {@link #getResourceSet} directly.
334    */

335   public Resource loadResource(String JavaDoc fileNameURI)
336   {
337     try
338     {
339       Resource resource = resourceSet.getResource(URI.createURI(fileNameURI), true);
340       return resource;
341     }
342     catch (Exception JavaDoc exception)
343     {
344       EMFEditPlugin.INSTANCE.log(exception);
345     }
346
347     return null;
348   }
349
350   /**
351    * This returns the resource set used to contain all created and loaded resources.
352    */

353   public ResourceSet getResourceSet()
354   {
355     return resourceSet;
356   }
357
358   /**
359    * This delegates to
360    * {@link org.eclipse.emf.edit.provider.IEditingDomainItemProvider#createCommand IEditingDomainItemProvider.createCommand}.
361    */

362   public Command createCommand(Class JavaDoc commandClass, CommandParameter commandParameter)
363   {
364     // If the owner parameter is set, we delegate to the owner's adapter
365
//
366
Object JavaDoc owner = commandParameter.getOwner();
367     if (commandClass == CopyToClipboardCommand.class)
368     {
369       return new CopyToClipboardCommand(this, commandParameter.getCollection());
370     }
371     else if (commandClass == PasteFromClipboardCommand.class)
372     {
373       return new PasteFromClipboardCommand
374         (this, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getIndex(), getOptimizeCopy());
375     }
376     else if (commandClass == CutToClipboardCommand.class)
377     {
378       return new CutToClipboardCommand
379         (this, RemoveCommand.create(this, commandParameter.getOwner(), commandParameter.getFeature(), commandParameter.getCollection()));
380     }
381     else if (owner != null)
382     {
383       // If there is an adapter of the correct type...
384
//
385
IEditingDomainItemProvider editingDomainItemProvider =
386         (IEditingDomainItemProvider)
387           adapterFactory.adapt(owner, IEditingDomainItemProvider.class);
388
389       return
390         editingDomainItemProvider != null ?
391           editingDomainItemProvider.createCommand(owner, this, commandClass, commandParameter) :
392         new ItemProviderAdapter(null).createCommand(owner, this, commandClass, commandParameter);
393     }
394     else
395     {
396       // If command has no owner specified
397
//
398
if (commandClass == RemoveCommand.class)
399       {
400         // For RemoveCommand, we will find the owner by calling EditingDomain.getParent() on the object(s) being removed.
401
//
402
CompoundCommand removeCommand = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL);
403
404         List JavaDoc objects = new ArrayList JavaDoc(commandParameter.getCollection());
405         while (!objects.isEmpty())
406         {
407           // We will iterate over the whole collection, removing some as we go.
408
//
409
ListIterator JavaDoc remainingObjects = objects.listIterator();
410
411           // Take the first object, and remove it.
412
//
413
Object JavaDoc object = remainingObjects.next();
414           remainingObjects.remove();
415
416           // Determine the object's parent.
417
//
418
Object JavaDoc parent = getParent(object);
419
420           if (parent != null)
421           {
422             // Now we want to find all the other objects with this same parent.
423
// So we can collection siblings together and give the parent control over their removal.
424
//
425
List JavaDoc siblings = new ArrayList JavaDoc();
426             siblings.add(object);
427
428             while (remainingObjects.hasNext())
429             {
430               // Get the next object and check if it has the same parent.
431
//
432
Object JavaDoc otherObject = remainingObjects.next();
433               Object JavaDoc otherParent = getParent(otherObject);
434               if (otherParent == parent)
435               {
436                 // Remove the object and add it as a sibling.
437
//
438
remainingObjects.remove();
439                 siblings.add(otherObject);
440               }
441             }
442
443             // We will now create a command with this implied parent
444
//
445
removeCommand.append(createCommand(RemoveCommand.class, new CommandParameter(parent, null, siblings)));
446           }
447           else
448           {
449             // The parent is null, which implies a top-leve removal, so create a self-removing command.
450
//
451
removeCommand.append(createCommand(RemoveCommand.class, new CommandParameter(object, null, Collections.singleton(object))));
452           }
453         }
454
455         return removeCommand.unwrap();
456       }
457       else if (commandClass == ReplaceCommand.class)
458       {
459         Object JavaDoc obj = commandParameter.getValue();
460         Object JavaDoc parent = (obj == null) ? null : getParent(obj);
461         if (parent == null) parent = obj;
462         return createCommand(ReplaceCommand.class, new CommandParameter(parent, null, commandParameter.getCollection()));
463       }
464       else if (commandClass == CreateChildCommand.class)
465       {
466         // For CreateChildCommand, we will find the owner by calling EditingDomain.getParent() on the first selected object
467
Collection JavaDoc sel = commandParameter.getCollection();
468         Object JavaDoc parent = sel == null ? null : getParent(sel.iterator().next());
469         if (parent == null)
470         {
471           return UnexecutableCommand.INSTANCE;
472         }
473         return createCommand(CreateChildCommand.class, new CommandParameter(parent, commandParameter.getFeature(), commandParameter.getValue(), commandParameter.getCollection(), commandParameter.getIndex()));
474       }
475     }
476
477     try
478     {
479       Constructor JavaDoc constructor = commandClass.getConstructor(new Class JavaDoc [] { EditingDomain.class, CommandParameter.class });
480       Object JavaDoc command = constructor.newInstance(new Object JavaDoc [] { this, commandParameter });
481       return (Command)command;
482     }
483     catch (IllegalAccessException JavaDoc exception)
484     {
485     }
486     catch (InstantiationException JavaDoc exception)
487     {
488     }
489     catch (NoSuchMethodException JavaDoc exception)
490     {
491     }
492     catch (InvocationTargetException JavaDoc exception)
493     {
494     }
495
496     return UnexecutableCommand.INSTANCE;
497   }
498
499   /**
500    * This just returns null, since this is an optional feature that we don't support here.
501    */

502   public Command createOverrideCommand(OverrideableCommand command)
503   {
504     return null;
505   }
506
507   /**
508    * This returns the command stack provided in the constructor.
509    */

510   public CommandStack getCommandStack()
511   {
512     return commandStack;
513   }
514
515   /**
516    * This delegates to
517    * {@link org.eclipse.emf.edit.provider.IEditingDomainItemProvider#getChildren IEditingDomainItemProvider.getChildren}.
518    */

519   public Collection JavaDoc getChildren(Object JavaDoc object)
520   {
521     // If there is an adapter of the correct type...
522
//
523
IEditingDomainItemProvider editingDomainItemProvider =
524       (IEditingDomainItemProvider)
525         adapterFactory.adapt(object, IEditingDomainItemProvider.class);
526
527     return
528       editingDomainItemProvider != null ?
529         editingDomainItemProvider.getChildren(object) :
530         Collections.EMPTY_LIST;
531   }
532
533   /**
534    * This delegates to
535    * {@link org.eclipse.emf.edit.provider.IEditingDomainItemProvider#getParent IEditingDomainItemProvider.getParent}.
536    */

537   public Object JavaDoc getParent(Object JavaDoc object)
538   {
539     // If there is an adapter of the correct type...
540
//
541
IEditingDomainItemProvider editingDomainItemProvider =
542       (IEditingDomainItemProvider)
543         adapterFactory.adapt(object, IEditingDomainItemProvider.class);
544
545     return
546       editingDomainItemProvider != null ?
547         editingDomainItemProvider.getParent(object) :
548         null;
549   }
550
551   public Object JavaDoc getRoot(Object JavaDoc object)
552   {
553     Object JavaDoc result = object;
554     for (Object JavaDoc parent = getParent(object); parent != null; parent = getParent(parent))
555     {
556       result = parent;
557     }
558     return result;
559   }
560
561   public Object JavaDoc getWrapper(Object JavaDoc object)
562   {
563     if (object != null)
564     {
565       for (Iterator JavaDoc i = treeIterator(getRoot(object)); i.hasNext(); )
566       {
567         Object JavaDoc element = i.next();
568         Object JavaDoc elementValue = element;
569         while (elementValue instanceof IWrapperItemProvider)
570         {
571           elementValue = ((IWrapperItemProvider)elementValue).getValue();
572         }
573         if (elementValue == object)
574         {
575           return element;
576         }
577         else if (elementValue instanceof FeatureMap.Entry)
578         {
579           Object JavaDoc entryValue = ((FeatureMap.Entry)elementValue).getValue();
580           if (entryValue == object)
581           {
582             return element;
583           }
584         }
585       }
586     }
587     return object;
588   }
589
590   public static Object JavaDoc unwrap(Object JavaDoc object)
591   {
592     while (object instanceof IWrapperItemProvider)
593     {
594       object = ((IWrapperItemProvider)object).getValue();
595     }
596     if (object instanceof FeatureMap.Entry)
597     {
598       object = ((FeatureMap.Entry)object).getValue();
599     }
600     return object;
601   }
602
603   /**
604    * This delegates to
605    * {@link org.eclipse.emf.edit.provider.IEditingDomainItemProvider#getNewChildDescriptors IEditingDomainItemProvider.getNewChildDescriptors}.
606    */

607   public Collection JavaDoc getNewChildDescriptors(Object JavaDoc object, Object JavaDoc sibling)
608   {
609     // If no object is specified, but an existing sibling is, the object is
610
// its parent.
611
//
612
if (object == null)
613     {
614       object = getParent(sibling);
615     }
616
617     // If there is an adapter of the correct type...
618
//
619
IEditingDomainItemProvider editingDomainItemProvider =
620       (IEditingDomainItemProvider)
621         adapterFactory.adapt(object, IEditingDomainItemProvider.class);
622
623     return
624       editingDomainItemProvider != null ?
625         editingDomainItemProvider.getNewChildDescriptors(object, this, sibling) :
626         Collections.EMPTY_LIST;
627   }
628
629   /**
630    * This returns the clipboard of the editing domain.
631    */

632   public Collection JavaDoc getClipboard()
633   {
634     return clipboard;
635   }
636
637   /**
638    * This sets the clipboard of the editing domain.
639    */

640   public void setClipboard(Collection JavaDoc clipboard)
641   {
642     this.clipboard = clipboard;
643   }
644
645   /**
646    * This returns whether or not copy command optimizations are safe in this domain.
647    */

648   public boolean getOptimizeCopy()
649   {
650     return optimizeCopy;
651   }
652
653   /**
654    * This sets whether or not copy command optimizations are safe in this domain.
655    */

656   public void setOptimizeCopy(boolean optimizeCopy)
657   {
658     this.optimizeCopy = optimizeCopy;
659   }
660
661   /**
662    * Returns the map of resource to a Boolean value indicating whether the resource is read only.
663    */

664   public Map JavaDoc getResourceToReadOnlyMap()
665   {
666     return resourceToReadOnlyMap;
667   }
668
669   /**
670    * Set the map of resource to a Boolean value indicating whether the resource is read only.
671    */

672   public void setResourceToReadOnlyMap(Map JavaDoc resourceToReadOnlyMap)
673   {
674     this.resourceToReadOnlyMap = resourceToReadOnlyMap;
675   }
676
677   /**
678    * This returns whether the resource is read only.
679    */

680   public boolean isReadOnly(Resource resource)
681   {
682     if (resourceToReadOnlyMap == null)
683     {
684       return false;
685     }
686     else
687     {
688       Object JavaDoc result = resourceToReadOnlyMap.get(resource);
689       if (result == null && resource != null)
690       {
691         URI uri =
692           CommonPlugin.asLocalURI
693             ((resource.getResourceSet() == null ? resourceSet : resource.getResourceSet()).getURIConverter().normalize(resource.getURI()));
694         if (uri.isFile() && !uri.isRelative())
695         {
696           File JavaDoc file = new File JavaDoc(uri.toFileString());
697           resourceToReadOnlyMap.put(resource, result = !file.exists() || file.canWrite() ? Boolean.FALSE : Boolean.TRUE);
698         }
699       }
700       return Boolean.TRUE.equals(result);
701     }
702   }
703
704   /**
705    * This implements an tree iterator that iterates over an object, it's domain children, their domain children, and so on.
706    */

707   public static class DomainTreeIterator extends AbstractTreeIterator
708   {
709     /**
710      * This is the domain that defines the tree structured.
711      */

712     protected EditingDomain domain;
713
714     /**
715      * This constructs tree iterator that iterates over an object, it's domain children, their domain children, and so on.
716      */

717     public DomainTreeIterator(EditingDomain domain, Object JavaDoc object)
718     {
719       super(object);
720       this.domain = domain;
721     }
722
723     /**
724      * This constructs tree iterator that iterates over an object (but only if includeRoot is true),
725      * it's domain children, their domain children, and so on.
726      */

727     public DomainTreeIterator(EditingDomain domain, Object JavaDoc object, boolean includeRoot)
728     {
729       super(object, includeRoot);
730       this.domain = domain;
731     }
732
733     protected Iterator JavaDoc getChildren(Object JavaDoc o)
734     {
735       return domain.getChildren(o).iterator();
736     }
737   }
738
739   /**
740    * This returns a tree iterator that will yield the object, the children of the object, their children, and so on.
741    */

742   public TreeIterator treeIterator(Object JavaDoc object)
743   {
744     return new DomainTreeIterator(this, object);
745   }
746
747   /**
748    * This returns a path list from the root object to the given object in the tree.
749    */

750   public List JavaDoc getTreePath(Object JavaDoc object)
751   {
752     LinkedList JavaDoc result = new LinkedList JavaDoc();
753     result.addFirst(object);
754     while ((object = getParent(object)) != null)
755     {
756       result.addFirst(object);
757     }
758
759     return result;
760   }
761 }
762
763 /**
764  * This implementation of {@link org.eclipse.emf.ecore.resource.ContextResourceFactoryRegister}
765  * behaves so that it extends,
766  * and potentially overrides,
767  * the behaviour you would get without setting this into a {@link org.eclipse.emf.ecore.resource.Context},
768  * In other words,
769  * this provides for registrations in addition to the factories registered with {@link org.eclipse.emf.ecore.resource.ResourceFactoryRegister}.
770  * Since the are considered first, they can override default registrations.
771  * EATM We should move this.
772  */

773 /*
774 class ExtensibleContextResourceFactoryRegister extends ContextResourceFactoryRegister
775 {
776   public ExtensibleContextResourceFactoryRegister()
777   {
778   }
779
780   public ResourceFactory getFactory(String uri)
781   {
782     ResourceFactory resourceFactory = super.getFactory(uri);
783     if (resourceFactory == null)
784     {
785       resourceFactory = ResourceFactoryRegister.getFactory(uri);
786     }
787
788     return resourceFactory;
789   }
790
791   public ResourceFactory getFileFactory(String fileName)
792   {
793     ResourceFactory resourceFactory = super.getFileFactory(fileName);
794     if (resourceFactory == null)
795     {
796       resourceFactory = ResourceFactoryRegister.getFileFactory(fileName);
797     }
798
799     return resourceFactory;
800   }
801 }
802 */

803
Popular Tags