edu.rice.cs.drjava.model
Class ConcreteRegionManager<R extends OrderedDocumentRegion>

java.lang.Object
  extended by edu.rice.cs.drjava.model.EventNotifier<RegionManagerListener<R>>
      extended by edu.rice.cs.drjava.model.ConcreteRegionManager<R>
All Implemented Interfaces:
RegionManager<R>

public class ConcreteRegionManager<R extends OrderedDocumentRegion>
extends EventNotifier<RegionManagerListener<R>>
implements RegionManager<R>

Simple region manager for the entire model. Follows readers/writers locking protocol of EventNotifier. TODO: fix the typing of regions. In many (all?) places, R should be OrderedDocumentRegion.


Field Summary
 
Fields inherited from class edu.rice.cs.drjava.model.EventNotifier
_listeners, _lock
 
Constructor Summary
ConcreteRegionManager()
           
 
Method Summary
 void addRegion(R region)
          Add the supplied DocumentRegion to the manager.
 void changeRegion(R region, Lambda<R,Object> cmd)
          Apply the given command to the specified region to change it.
 void clearRegions()
          Tells the manager to remove all regions.
 boolean contains(R region)
          Tests if specified region r is contained in this manager.
 Set<OpenDefinitionsDocument> getDocuments()
           
 ArrayList<FileRegion> getFileRegions()
           
 SortedSet<R> getHeadSet(R r)
          Gets the sorted set of regions less than r.
 R getRegionAt(OpenDefinitionsDocument odd, int offset)
          Returns the region [start, end) containing offset.
 Pair<R,R> getRegionInterval(OpenDefinitionsDocument odd, int offset)
          Finds the interval of regions in odd such that the line label (excerpt) for the region contains offset.
 ArrayList<R> getRegions()
           
 SortedSet<R> getRegions(OpenDefinitionsDocument odd)
           
 Collection<R> getRegionsOverlapping(OpenDefinitionsDocument odd, int startOffset, int endOffset)
          Returns the set of regions in the given document that overlap the specified interval [startOffset, endOffset), including degenerate regions [offset, offset) where [offset, offset] is a subset of (startOffset, endOffset).
 SortedSet<R> getTailSet(R r)
          Gets the sorted set of regions greater than or equal to r.
 void removeRegion(R region)
          Remove the given IDocumentRegion from the manager.
 void removeRegions(Iterable<? extends R> regions)
          Invoke removeRegion(R) on all of the given regions.
 void removeRegions(OpenDefinitionsDocument doc)
          Remove the specified document from _documents and _regions (removing all of its contained regions).
 void updateLines(R firstRegion, R lastRegion)
          Updates _lineStartPos, _lineEndPos of regions in the interval [firstRegion, lastRegion] using total ordering on regions.
 
Methods inherited from class edu.rice.cs.drjava.model.EventNotifier
addListener, removeAllListeners, removeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface edu.rice.cs.drjava.model.RegionManager
addListener, removeAllListeners, removeListener
 

Constructor Detail

ConcreteRegionManager

public ConcreteRegionManager()
Method Detail

getDocuments

public Set<OpenDefinitionsDocument> getDocuments()
Specified by:
getDocuments in interface RegionManager<R extends OrderedDocumentRegion>
Returns:
the set of documents containing regions.

getHeadSet

public SortedSet<R> getHeadSet(R r)
Gets the sorted set of regions less than r.


getTailSet

public SortedSet<R> getTailSet(R r)
Gets the sorted set of regions greater than or equal to r.

Specified by:
getTailSet in interface RegionManager<R extends OrderedDocumentRegion>

getRegionAt

public R getRegionAt(OpenDefinitionsDocument odd,
                     int offset)
Returns the region [start, end) containing offset. Since regions can never overlap, there is at most one such region in the given document. (Degenerate regions can coalesce but they are empty implying that they are never returned by this method.) Only runs in the event thread.

Specified by:
getRegionAt in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
odd - the document
offset - the offset in the document
Returns:
the DocumentRegion at the given offset, or null if it does not exist.

getRegionInterval

public Pair<R,R> getRegionInterval(OpenDefinitionsDocument odd,
                                   int offset)
Finds the interval of regions in odd such that the line label (excerpt) for the region contains offset.

Specified by:
getRegionInterval in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
odd - the document
offset - the offset in the document
Returns:
the unique DocumentRegion containing the given offset, or null if it does not exist.

getRegionsOverlapping

public Collection<R> getRegionsOverlapping(OpenDefinitionsDocument odd,
                                           int startOffset,
                                           int endOffset)
Returns the set of regions in the given document that overlap the specified interval [startOffset, endOffset), including degenerate regions [offset, offset) where [offset, offset] is a subset of (startOffset, endOffset). Assumes that all regions in the document are disjoint. Note: degenerate empty regions with form [offset, offset) vacuously satisfy this property. Only executes in the event thread. Note: this method could be implemented more cleanly using a revserseIterator on the headSet containing all regions preceding or equal to the selection. but this functionality was not added to TreeSet until Java 6.0.

Specified by:
getRegionsOverlapping in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
odd - the document
startOffset - the left end of the specified interval
endOffset - the right end of the specified interval
Returns:
the Collection of regions overlapping the interval.

addRegion

public void addRegion(R region)
Add the supplied DocumentRegion to the manager. Only runs in event thread after initialization?

Specified by:
addRegion in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
region - the DocumentRegion to be inserted into the manager

removeRegion

public void removeRegion(R region)
Remove the given IDocumentRegion from the manager. If any document's regions are emptied, remove the document from the keys in _regions. Notification removes the panel node for the region.

Specified by:
removeRegion in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
region - the IDocumentRegion to be removed.

removeRegions

public void removeRegions(Iterable<? extends R> regions)
Invoke removeRegion(R) on all of the given regions.

Specified by:
removeRegions in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
regions - the DocumentRegions to be removed.

removeRegions

public void removeRegions(OpenDefinitionsDocument doc)
Remove the specified document from _documents and _regions (removing all of its contained regions).

Specified by:
removeRegions in interface RegionManager<R extends OrderedDocumentRegion>

getRegions

public SortedSet<R> getRegions(OpenDefinitionsDocument odd)
Specified by:
getRegions in interface RegionManager<R extends OrderedDocumentRegion>
Returns:
a Vector containing the DocumentRegion objects for document odd in this mangager.

getRegions

public ArrayList<R> getRegions()
Specified by:
getRegions in interface RegionManager<R extends OrderedDocumentRegion>
Returns:
a Vector containing all the DocumentRegion objects in this mangager.

getFileRegions

public ArrayList<FileRegion> getFileRegions()

contains

public boolean contains(R region)
Description copied from interface: RegionManager
Tests if specified region r is contained in this manager.

Specified by:
contains in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
region - The region
Returns:
whether the manager contains region r

clearRegions

public void clearRegions()
Tells the manager to remove all regions.

Specified by:
clearRegions in interface RegionManager<R extends OrderedDocumentRegion>

changeRegion

public void changeRegion(R region,
                         Lambda<R,Object> cmd)
Apply the given command to the specified region to change it.

Specified by:
changeRegion in interface RegionManager<R extends OrderedDocumentRegion>
Parameters:
region - the region to find and change
cmd - command that mutates the region.

updateLines

public void updateLines(R firstRegion,
                        R lastRegion)
Updates _lineStartPos, _lineEndPos of regions in the interval [firstRegion, lastRegion] using total ordering on regions. Removes empty regions. firstRegion and lastRegion are not necessarily regions in this manager.

Specified by:
updateLines in interface RegionManager<R extends OrderedDocumentRegion>