2 * Copyright (c) 2002-2004 Widespace, OU and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://solareclipse.sourceforge.net/legal/cpl-v10.html
9 * Igor Malinin - initial contribution
11 * $Id: MultiViewPartitioner.java,v 1.5 2004-11-10 20:39:32 axelcl Exp $
14 package net.sourceforge.phpeclipse.ui.text.rules;
16 import java.util.ArrayList;
17 import java.util.List;
19 import org.eclipse.jface.text.Assert;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.DocumentEvent;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IDocumentPartitioner;
24 import org.eclipse.jface.text.IDocumentPartitioningListener;
25 import org.eclipse.jface.text.IDocumentPartitioningListenerExtension;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITypedRegion;
28 import org.eclipse.jface.text.TypedRegion;
29 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
32 * Advanced partitioner which maintains partitions as views to connected document. Views have own partitioners themselves. This
33 * class is designed as a base for complex partitioners such as for JSP, PHP, ASP, etc. languages.
35 * @author Igor Malinin
37 public abstract class MultiViewPartitioner extends AbstractPartitioner {
39 class ViewListener implements IDocumentPartitioningListener, IDocumentPartitioningListenerExtension {
42 * @see org.eclipse.jface.text.IDocumentPartitioningListener#documentPartitioningChanged(IDocument)
44 public void documentPartitioningChanged(IDocument document) {
45 IDocumentView view = (IDocumentView) document;
47 int start = view.getParentOffset(0);
48 int end = view.getParentOffset(view.getLength());
50 rememberRegion(start, end - start);
54 * @see org.eclipse.jface.text.IDocumentPartitioningListenerExtension#documentPartitioningChanged(IDocument, IRegion)
56 public void documentPartitioningChanged(IDocument document, IRegion region) {
57 IDocumentView view = (IDocumentView) document;
59 int offset = region.getOffset();
61 int start = view.getParentOffset(offset);
62 int end = view.getParentOffset(offset + region.getLength());
64 rememberRegion(start, end - start);
68 private ViewListener viewListener = new ViewListener();
70 private OuterDocumentView outerDocument;
72 private DocumentEvent outerDocumentEvent;
74 public MultiViewPartitioner(IPartitionTokenScanner scanner) {
78 public void setOuterPartitioner(IDocumentPartitioner partitioner) {
79 if (outerDocument == null) {
80 if (partitioner == null) {
84 outerDocument = new OuterDocumentView(document, nodes);
85 outerDocument.addDocumentPartitioningListener(viewListener);
88 IDocumentPartitioner old = outerDocument.getDocumentPartitioner();
90 outerDocument.setDocumentPartitioner(null);
94 if (partitioner != null) {
95 partitioner.connect(outerDocument);
98 outerDocument.setDocumentPartitioner(partitioner);
100 if (partitioner == null) {
101 outerDocument.removeDocumentPartitioningListener(viewListener);
102 outerDocument = null;
107 * Create subpartitioner.
110 * name of inner partition or <code>null</code> for outer partition
112 protected abstract IDocumentPartitioner createPartitioner(String contentType);
114 protected void addInnerRegion(FlatNode position) {
115 if (outerDocument != null) {
117 Assert.isTrue(position.offset >= 0, Integer.toString(position.offset));
119 int outerOffset = outerDocument.getLocalOffset(position.offset);
121 DocumentEvent event = null;
122 if (outerOffset>=0) {
124 event = new DocumentEvent(outerDocument, outerOffset, position.length, null);
126 outerDocument.fireDocumentAboutToBeChanged(event);
128 super.addInnerRegion(position);
132 outerDocument.fireDocumentChanged(event);
135 super.addInnerRegion(position);
138 if (position instanceof ViewNode) {
139 // TODO: revisit condition
140 IDocumentPartitioner partitioner = createPartitioner(position.type);
141 if (partitioner != null) {
142 InnerDocumentView innerDocument = new InnerDocumentView(document, (ViewNode) position);
144 ((ViewNode) position).view = innerDocument;
146 partitioner.connect(innerDocument);
147 innerDocument.setDocumentPartitioner(partitioner);
148 innerDocument.addDocumentPartitioningListener(viewListener);
153 protected void removeInnerRegion(FlatNode position) {
155 if (outerDocument != null) {
156 DocumentEvent event = null;
157 if (position.offset >= 0 && position.length >= 0) {
158 int outerOffset = outerDocument.getLocalOffset(position.offset);
159 if (outerOffset > 0) {
160 event = new DocumentEvent(outerDocument, outerOffset, 0, document.get(position.offset, position.length));
162 outerDocument.fireDocumentAboutToBeChanged(event);
165 super.removeInnerRegion(position);
166 if (position.offset >= 0) {
168 outerDocument.fireDocumentChanged(event);
172 super.removeInnerRegion(position);
175 if (position instanceof ViewNode) {
176 // TODO: revisit condition
177 InnerDocumentView innerDocument = ((ViewNode) position).view;
178 if (innerDocument != null) {
179 IDocumentPartitioner partitioner = innerDocument.getDocumentPartitioner();
181 innerDocument.removeDocumentPartitioningListener(viewListener);
182 innerDocument.setDocumentPartitioner(null);
183 partitioner.disconnect();
186 } catch (BadLocationException e) {
190 protected void deleteInnerRegion(FlatNode position) {
191 super.deleteInnerRegion(position);
193 if (position instanceof ViewNode) {
194 // TODO: revisit condition
195 InnerDocumentView innerDocument = ((ViewNode) position).view;
196 if (innerDocument != null) {
197 IDocumentPartitioner partitioner = innerDocument.getDocumentPartitioner();
199 innerDocument.removeDocumentPartitioningListener(viewListener);
200 innerDocument.setDocumentPartitioner(null);
201 partitioner.disconnect();
206 public void connect(IDocument document) {
207 // outerDocument = new OuterDocumentView(document, innerPositions);
209 super.connect(document);
211 setOuterPartitioner(createPartitioner(null));
212 // IDocumentPartitioner partitioner =
213 // partitioner.connect(outerDocument);
214 // outerDocument.setDocumentPartitioner(partitioner);
215 // outerDocument.addDocumentPartitioningListener(viewListener);
218 public void disconnect() {
220 if (outerDocument != null) {
221 outerDocument.removeDocumentPartitioningListener(viewListener);
223 IDocumentPartitioner partitioner = outerDocument.getDocumentPartitioner();
225 outerDocument.setDocumentPartitioner(null);
226 partitioner.disconnect();
229 // TODO: cleanup listeners
230 outerDocument = null;
235 * @see org.eclipse.jface.text.IDocumentPartitioner#documentAboutToBeChanged(DocumentEvent)
237 public void documentAboutToBeChanged(DocumentEvent event) {
238 super.documentAboutToBeChanged(event);
240 outerDocumentEvent = null;
242 int offset = event.getOffset();
243 int length = event.getLength();
244 int end = offset + length;
246 // find left partition
247 int first = computeFlatNodeIndex(offset);
249 FlatNode p = (FlatNode) nodes.get(first - 1);
251 int right = p.offset + p.length;
252 if (offset < right) {
253 // change overlaps with partition
254 InnerDocumentView innerDocument = null;
255 if (p instanceof ViewNode) {
256 // TODO: revisit condition
257 innerDocument = ((ViewNode) p).view;
261 if (innerDocument != null) {
262 // cahnge completely inside partition
263 int start = innerDocument.getLocalOffset(offset);
264 innerDocument.fireDocumentAboutToBeChanged(new DocumentEvent(innerDocument, start, length, event.getText()));
270 if (innerDocument != null) {
271 // cut partition at right
272 int start = innerDocument.getLocalOffset(offset);
273 innerDocument.fireDocumentAboutToBeChanged(new DocumentEvent(innerDocument, start, innerDocument.getLength() - start,
279 // find right partition
280 int last = computeFlatNodeIndex(end);
282 FlatNode p = (FlatNode) nodes.get(last - 1);
284 if (p instanceof ViewNode) {
285 // TODO: revisit condition
286 InnerDocumentView innerDocument = ((ViewNode) p).view;
287 if (innerDocument != null) {
288 int right = p.offset + p.length;
290 // cut partition at left
291 int cut = innerDocument.getLocalOffset(end);
292 innerDocument.fireDocumentAboutToBeChanged(new DocumentEvent(innerDocument, 0, cut, null));
298 if (outerDocument != null) {
299 int left = outerDocument.getLocalOffset(offset);
300 int right = outerDocument.getLocalOffset(end);
302 String text = event.getText();
304 if (left != right || text != null && text.length() > 0) {
305 outerDocumentEvent = new DocumentEvent(outerDocument, left, right - left, text);
307 outerDocument.fireDocumentAboutToBeChanged(outerDocumentEvent);
312 protected int fixupPartitions(DocumentEvent event) {
313 int offset = event.getOffset();
314 int length = event.getLength();
315 int end = offset + length;
317 // fixup/notify inner views laying on change boundaries
319 int first = computeFlatNodeIndex(offset);
321 FlatNode p = (FlatNode) nodes.get(first - 1);
323 int right = p.offset + p.length;
324 if (offset < right) {
325 // change overlaps with partition
327 // cahnge completely inside partition
328 String text = event.getText();
331 p.length += text.length();
334 if (p instanceof ViewNode) {
335 // TODO: revisit condition
336 InnerDocumentView innerDocument = ((ViewNode) p).view;
337 if (innerDocument != null) {
338 int start = innerDocument.getLocalOffset(offset);
339 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, length, text));
343 // cut partition at right
344 int cut = p.offset + p.length - offset;
347 if (p instanceof ViewNode) {
348 // TODO: revisit condition
349 InnerDocumentView innerDocument = ((ViewNode) p).view;
350 if (innerDocument != null) {
351 int start = innerDocument.getLocalOffset(offset);
352 // TODO: ???fireDocumentAboutToBeChanged???
353 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, cut, null));
360 int last = computeFlatNodeIndex(end);
361 if (last > 0 && first != last) {
362 FlatNode p = (FlatNode) nodes.get(last - 1);
364 int right = p.offset + p.length;
366 // cut partition at left
367 int cut = end - p.offset;
371 String text = event.getText();
373 p.offset += text.length();
376 if (p instanceof ViewNode) {
377 // TODO: revisit condition
378 InnerDocumentView innerDocument = ((ViewNode) p).view;
379 if (innerDocument != null) {
380 // TODO: ???fireDocumentAboutToBeChanged???
381 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, 0, cut, null));
389 // fixup inner views laying afrer change
391 String text = event.getText();
393 length -= text.length();
396 for (int i = last, size = nodes.size(); i < size; i++) {
397 ((FlatNode) nodes.get(i)).offset -= length;
400 // delete inner views laying completely inside change boundaries
404 deleteInnerRegion((FlatNode) nodes.get(--last));
405 } while (first < last);
407 rememberRegion(offset, 0);
412 if (outerDocumentEvent != null) {
413 outerDocument.fireDocumentChanged(outerDocumentEvent);
420 * @see org.eclipse.jface.text.IDocumentPartitioner#computePartitioning(int, int)
422 protected String getContentType(String parent, String view) {
427 if (parent != null) {
431 return IDocument.DEFAULT_CONTENT_TYPE;
435 * @see org.eclipse.jface.text.IDocumentPartitioner#computePartitioning(int, int)
437 public ITypedRegion[] computePartitioning(int offset, int length) {
438 List list = new ArrayList();
440 int end = offset + length;
442 int index = computeFlatNodeIndex(offset);
444 FlatNode prev = (index > 0) ? (FlatNode) nodes.get(index - 1) : null;
447 if (prev.overlapsWith(offset, length)) {
448 addInnerPartitions(list, offset, length, prev);
451 if (end <= prev.offset + prev.length) {
456 FlatNode next = (index < nodes.size()) ? (FlatNode) nodes.get(index) : null;
458 if (next == null || offset < next.offset) {
459 addOuterPartitions(list, offset, length, prev, next);
469 return (TypedRegion[]) list.toArray(new TypedRegion[list.size()]);
472 private void addOuterPartitions(List list, int offset, int length, FlatNode prev, FlatNode next) {
475 int end = offset + length;
477 if (prev != null && start < prev.offset + prev.length) {
478 start = prev.offset + prev.length;
481 if (next != null && next.offset < end) {
489 if (outerDocument == null) {
490 list.add(new TypedRegion(start, end - start, getContentType(null, IDocument.DEFAULT_CONTENT_TYPE)));
495 // convert to outer offsets
496 start = outerDocument.getLocalOffset(start);
497 end = outerDocument.getLocalOffset(end);
498 if (end - start >= 0) {//jsurfer insert line
499 ITypedRegion[] regions = outerDocument.computePartitioning(start, end - start);
501 for (int i = 0; i < regions.length; i++) {
502 ITypedRegion region = regions[i];
504 // convert back to parent offsets
505 start = outerDocument.getParentOffset(region.getOffset());
506 end = start + region.getLength();
509 offset = prev.offset + prev.length;
510 if (start < offset) {
516 offset = next.offset;
522 list.add(new TypedRegion(start, end - start, getContentType(null, region.getType())));
525 } catch (BadLocationException x) {
529 private void addInnerPartitions(List list, int offset, int length, FlatNode position) {
530 InnerDocumentView innerDocument = null;
531 if (position instanceof ViewNode) {
532 // TODO: revisit condition
533 innerDocument = ((ViewNode) position).view;
536 if (innerDocument == null) {
538 list.add(new TypedRegion(position.offset, position.length, getContentType(position.type, null)));
542 // multiplexing to inner view
545 int start = Math.max(offset, position.offset);
546 int end = Math.min(offset + length, position.offset + position.length);
548 // convert to document offsets
549 length = end - start;
550 offset = innerDocument.getLocalOffset(start);
552 ITypedRegion[] regions = innerDocument.computePartitioning(offset, length);
554 for (int i = 0; i < regions.length; i++) {
555 ITypedRegion region = regions[i];
557 // convert back to parent offsets
558 offset = innerDocument.getParentOffset(region.getOffset());
559 length = region.getLength();
561 list.add(new TypedRegion(offset, length, getContentType(position.type, region.getType())));
563 } catch (BadLocationException x) {
568 * @see org.eclipse.jface.text.IDocumentPartitioner#getPartition(int)
570 public ITypedRegion getPartition(int offset) {
571 if (nodes.size() == 0) {
572 return getOuterPartition(offset, null, null);
575 int index = computeFlatNodeIndex(offset);
576 if (index < nodes.size()) {
577 FlatNode next = (FlatNode) nodes.get(index);
579 if (offset == next.offset) {
580 return getInnerPartition(offset, next);
584 return getOuterPartition(offset, null, next);
587 FlatNode prev = (FlatNode) nodes.get(index - 1);
589 if (prev.includes(offset)) {
590 return getInnerPartition(offset, prev);
593 return getOuterPartition(offset, prev, next);
596 FlatNode prev = (FlatNode) nodes.get(nodes.size() - 1);
598 if (prev.includes(offset)) {
599 return getInnerPartition(offset, prev);
602 return getOuterPartition(offset, prev, null);
605 protected ITypedRegion getOuterPartition(int offset, FlatNode prev, FlatNode next) {
610 if (outerDocument == null) {
612 end = document.getLength();
613 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
615 int outerOffset = outerDocument.getLocalOffset(offset);
617 if (outerOffset < 0) {
619 end = document.getLength();
620 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
623 ITypedRegion region = outerDocument.getPartition(outerOffset);
625 start = region.getOffset();
626 end = start + region.getLength();
628 // convert to parent offset
629 start = outerDocument.getParentOffset(start);
630 end = outerDocument.getParentOffset(end);
632 type = getContentType(null, region.getType());
637 offset = prev.offset + prev.length;
638 if (start < offset) {
644 offset = next.offset;
650 return new TypedRegion(start, end - start, type);
651 } catch (BadLocationException x) {
652 throw new IllegalArgumentException();
656 protected ITypedRegion getInnerPartition(int offset, FlatNode position) {
657 if (position instanceof ViewNode) {
658 // TODO: revisit condition
659 InnerDocumentView innerDocument = ((ViewNode) position).view;
661 if (innerDocument != null) {
662 // multiplexing to inner view
664 // convert to inner offset
665 ITypedRegion region = innerDocument.getPartition(innerDocument.getLocalOffset(offset));
667 // convert to parent offset
668 offset = innerDocument.getParentOffset(region.getOffset());
670 return new TypedRegion(offset, region.getLength(), getContentType(position.type, region.getType()));
671 } catch (BadLocationException x) {
677 return new TypedRegion(position.offset, position.length, position.type);