001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.text;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.Assert;
017:
018: /**
019: * Event describing the change of document partitionings.
020: *
021: * @see org.eclipse.jface.text.IDocumentExtension3
022: * @since 3.0
023: */
024: public class DocumentPartitioningChangedEvent {
025:
026: /** The document whose partitionings changed */
027: private final IDocument fDocument;
028: /** The map of partitionings to changed regions. */
029: private final Map fMap = new HashMap();
030:
031: /**
032: * Creates a new document partitioning changed event for the given document.
033: * Initially this event is empty, i.e. does not describe any change.
034: *
035: * @param document the changed document
036: */
037: public DocumentPartitioningChangedEvent(IDocument document) {
038: fDocument = document;
039: }
040:
041: /**
042: * Returns the changed document.
043: *
044: * @return the changed document
045: */
046: public IDocument getDocument() {
047: return fDocument;
048: }
049:
050: /**
051: * Returns the changed region of the given partitioning or <code>null</code>
052: * if the given partitioning did not change.
053: *
054: * @param partitioning the partitioning
055: * @return the changed region of the given partitioning or <code>null</code>
056: */
057: public IRegion getChangedRegion(String partitioning) {
058: return (IRegion) fMap.get(partitioning);
059: }
060:
061: /**
062: * Returns the set of changed partitionings.
063: *
064: * @return the set of changed partitionings
065: */
066: public String[] getChangedPartitionings() {
067: String[] partitionings = new String[fMap.size()];
068: fMap.keySet().toArray(partitionings);
069: return partitionings;
070: }
071:
072: /**
073: * Sets the specified range as changed region for the given partitioning.
074: *
075: * @param partitioning the partitioning
076: * @param offset the region offset
077: * @param length the region length
078: */
079: public void setPartitionChange(String partitioning, int offset,
080: int length) {
081: Assert.isNotNull(partitioning);
082: fMap.put(partitioning, new Region(offset, length));
083: }
084:
085: /**
086: * Returns <code>true</code> if the set of changed partitionings is empty,
087: * <code>false</code> otherwise.
088: *
089: * @return <code>true</code> if the set of changed partitionings is empty
090: */
091: public boolean isEmpty() {
092: return fMap.isEmpty();
093: }
094:
095: /**
096: * Returns the coverage of this event. This is the minimal region that
097: * contains all changed regions of all changed partitionings.
098: *
099: * @return the coverage of this event
100: */
101: public IRegion getCoverage() {
102: if (fMap.isEmpty())
103: return new Region(0, 0);
104:
105: int offset = -1;
106: int endOffset = -1;
107: Iterator e = fMap.values().iterator();
108: while (e.hasNext()) {
109: IRegion r = (IRegion) e.next();
110:
111: if (offset < 0 || r.getOffset() < offset)
112: offset = r.getOffset();
113:
114: int end = r.getOffset() + r.getLength();
115: if (end > endOffset)
116: endOffset = end;
117: }
118:
119: return new Region(offset, endOffset - offset);
120: }
121: }
|