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.8 2005-05-05 19:08:50 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 >= 0 && (right - left >= 0)) {
305 if (left != right || text != null && text.length() > 0) {
306 outerDocumentEvent = new DocumentEvent(outerDocument, left, right - left, text);
308 outerDocument.fireDocumentAboutToBeChanged(outerDocumentEvent);
314 protected int fixupPartitions(DocumentEvent event) {
315 int offset = event.getOffset();
316 int length = event.getLength();
317 int end = offset + length;
319 // fixup/notify inner views laying on change boundaries
321 int first = computeFlatNodeIndex(offset);
323 FlatNode p = (FlatNode) nodes.get(first - 1);
325 int right = p.offset + p.length;
326 if (offset < right) {
327 // change overlaps with partition
329 // cahnge completely inside partition
330 String text = event.getText();
333 p.length += text.length();
336 if (p instanceof ViewNode) {
337 // TODO: revisit condition
338 InnerDocumentView innerDocument = ((ViewNode) p).view;
339 if (innerDocument != null) {
340 int start = innerDocument.getLocalOffset(offset);
341 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, length, text));
345 // cut partition at right
346 int cut = p.offset + p.length - offset;
349 if (p instanceof ViewNode) {
350 // TODO: revisit condition
351 InnerDocumentView innerDocument = ((ViewNode) p).view;
352 if (innerDocument != null) {
353 int start = innerDocument.getLocalOffset(offset);
354 // TODO: ???fireDocumentAboutToBeChanged???
355 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, cut, null));
362 int last = computeFlatNodeIndex(end);
363 if (last > 0 && first != last) {
364 FlatNode p = (FlatNode) nodes.get(last - 1);
366 int right = p.offset + p.length;
368 // cut partition at left
369 int cut = end - p.offset;
373 String text = event.getText();
375 p.offset += text.length();
378 if (p instanceof ViewNode) {
379 // TODO: revisit condition
380 InnerDocumentView innerDocument = ((ViewNode) p).view;
381 if (innerDocument != null) {
382 // TODO: ???fireDocumentAboutToBeChanged???
383 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, 0, cut, null));
391 // fixup inner views laying afrer change
393 String text = event.getText();
395 length -= text.length();
398 for (int i = last, size = nodes.size(); i < size; i++) {
399 ((FlatNode) nodes.get(i)).offset -= length;
402 // delete inner views laying completely inside change boundaries
406 deleteInnerRegion((FlatNode) nodes.get(--last));
407 } while (first < last);
409 rememberRegion(offset, 0);
414 if (outerDocumentEvent != null) {
415 outerDocument.fireDocumentChanged(outerDocumentEvent);
422 * @see org.eclipse.jface.text.IDocumentPartitioner#computePartitioning(int, int)
424 protected String getContentType(String parent, String view) {
429 if (parent != null) {
433 return IDocument.DEFAULT_CONTENT_TYPE;
437 * @see org.eclipse.jface.text.IDocumentPartitioner#computePartitioning(int, int)
439 public ITypedRegion[] computePartitioning(int offset, int length) {
440 List list = new ArrayList();
442 // System.out.print("MultiViewPartitioner::computePartitioning - Offset: ");
443 // System.out.print(offset);
444 // System.out.print(", Length: ");
445 // System.out.print(length);
446 // System.out.println("");
448 int end = offset + length;
450 int index = computeFlatNodeIndex(offset);
452 FlatNode prev = (index > 0) ? (FlatNode) nodes.get(index - 1) : null;
455 if (prev.overlapsWith(offset, length)) {
456 addInnerPartitions(list, offset, length, prev);
459 if (end <= prev.offset + prev.length) {
464 FlatNode next = (index < nodes.size()) ? (FlatNode) nodes.get(index) : null;
466 if (next == null || offset < next.offset) {
467 addOuterPartitions(list, offset, length, prev, next);
480 return (TypedRegion[]) list.toArray(new TypedRegion[list.size()]);
483 private void showList(List list) {
485 throw new NullPointerException();
486 } catch (Exception e) {
489 System.out.println(">>>>>List start");
491 for (int i = 0; i < list.size(); i++) {
492 temp = (TypedRegion) list.get(i);
493 System.out.print("Offset: ");
494 System.out.print(temp.getOffset());
495 System.out.print(", Length: ");
496 System.out.print(temp.getLength());
497 System.out.print(", Type: ");
498 System.out.print(temp.getType());
499 System.out.println("");
501 System.out.println("<<<<<List end");
504 private void addOuterPartitions(List list, int offset, int length, FlatNode prev, FlatNode next) {
507 int end = offset + length;
509 if (prev != null && start < prev.offset + prev.length) {
510 start = prev.offset + prev.length;
513 if (next != null && next.offset < end) {
521 if (outerDocument == null) {
523 // if (end - start<0) {
524 // throw new IndexOutOfBoundsException();
527 list.add(new TypedRegion(start, end - start, getContentType(null, IDocument.DEFAULT_CONTENT_TYPE)));
532 // convert to outer offsets
533 start = outerDocument.getLocalOffset(start);
534 end = outerDocument.getLocalOffset(end);
535 if (end - start >= 0) {//jsurfer insert line
536 ITypedRegion[] regions = outerDocument.computePartitioning(start, end - start);
538 for (int i = 0; i < regions.length; i++) {
539 ITypedRegion region = regions[i];
541 // convert back to parent offsets
542 start = outerDocument.getParentOffset(region.getOffset());
543 end = start + region.getLength();
546 offset = prev.offset + prev.length;
547 if (start < offset) {
553 offset = next.offset;
559 // if (end - start<0) {
561 // System.out.print("MultiViewPartitioner::addOuterPartitions - Offset: ");
562 // System.out.print(offset);
563 // System.out.print(", Start: ");
564 // System.out.print(start);
565 // System.out.print(", End: ");
566 // System.out.print(end);
567 // System.out.print(", Type: ");
568 // System.out.print(region.getType());
569 // System.out.println("");
570 // throw new IndexOutOfBoundsException();
573 list.add(new TypedRegion(start, end - start, getContentType(null, region.getType())));
576 } catch (BadLocationException x) {
580 private void addInnerPartitions(List list, int offset, int length, FlatNode position) {
581 InnerDocumentView innerDocument = null;
582 if (position instanceof ViewNode) {
583 // TODO: revisit condition
584 innerDocument = ((ViewNode) position).view;
587 if (innerDocument == null) {
590 // if (position.length<0) {
591 // throw new IndexOutOfBoundsException();
594 list.add(new TypedRegion(position.offset, position.length, getContentType(position.type, null)));
598 // multiplexing to inner view
601 int start = Math.max(offset, position.offset);
602 int end = Math.min(offset + length, position.offset + position.length);
604 // convert to document offsets
605 length = end - start;
606 offset = innerDocument.getLocalOffset(start);
608 ITypedRegion[] regions = innerDocument.computePartitioning(offset, length);
610 for (int i = 0; i < regions.length; i++) {
611 ITypedRegion region = regions[i];
613 // convert back to parent offsets
614 offset = innerDocument.getParentOffset(region.getOffset());
615 length = region.getLength();
618 // throw new IndexOutOfBoundsException();
621 list.add(new TypedRegion(offset, length, getContentType(position.type, region.getType())));
623 } catch (BadLocationException x) {
628 * @see org.eclipse.jface.text.IDocumentPartitioner#getPartition(int)
630 public ITypedRegion getPartition(int offset) {
631 if (nodes.size() == 0) {
632 return getOuterPartition(offset, null, null);
635 int index = computeFlatNodeIndex(offset);
636 if (index < nodes.size()) {
637 FlatNode next = (FlatNode) nodes.get(index);
639 if (offset == next.offset) {
640 return getInnerPartition(offset, next);
644 return getOuterPartition(offset, null, next);
647 FlatNode prev = (FlatNode) nodes.get(index - 1);
649 if (prev.includes(offset)) {
650 return getInnerPartition(offset, prev);
653 return getOuterPartition(offset, prev, next);
656 FlatNode prev = (FlatNode) nodes.get(nodes.size() - 1);
658 if (prev.includes(offset)) {
659 return getInnerPartition(offset, prev);
662 return getOuterPartition(offset, prev, null);
665 protected ITypedRegion getOuterPartition(int offset, FlatNode prev, FlatNode next) {
670 if (outerDocument == null) {
672 end = document.getLength();
673 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
675 int outerOffset = outerDocument.getLocalOffset(offset);
677 if (outerOffset < 0) {
679 end = document.getLength();
680 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
683 ITypedRegion region = outerDocument.getPartition(outerOffset);
685 start = region.getOffset();
686 end = start + region.getLength();
688 // convert to parent offset
689 start = outerDocument.getParentOffset(start);
690 end = outerDocument.getParentOffset(end);
692 type = getContentType(null, region.getType());
697 offset = prev.offset + prev.length;
698 if (start < offset) {
704 offset = next.offset;
710 return new TypedRegion(start, end - start, type);
711 } catch (BadLocationException x) {
713 throw new IllegalArgumentException();
717 protected ITypedRegion getInnerPartition(int offset, FlatNode position) {
718 if (position instanceof ViewNode) {
719 // TODO: revisit condition
720 InnerDocumentView innerDocument = ((ViewNode) position).view;
722 if (innerDocument != null) {
723 // multiplexing to inner view
725 // convert to inner offset
726 ITypedRegion region = innerDocument.getPartition(innerDocument.getLocalOffset(offset));
728 // convert to parent offset
729 offset = innerDocument.getParentOffset(region.getOffset());
731 return new TypedRegion(offset, region.getLength(), getContentType(position.type, region.getType()));
732 } catch (BadLocationException x) {
738 return new TypedRegion(position.offset, position.length, position.type);