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.7 2004-11-13 12:36: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 // System.out.print("MultiViewPartitioner::computePartitioning - Offset: ");
441 // System.out.print(offset);
442 // System.out.print(", Length: ");
443 // System.out.print(length);
444 // System.out.println("");
446 int end = offset + length;
448 int index = computeFlatNodeIndex(offset);
450 FlatNode prev = (index > 0) ? (FlatNode) nodes.get(index - 1) : null;
453 if (prev.overlapsWith(offset, length)) {
454 addInnerPartitions(list, offset, length, prev);
457 if (end <= prev.offset + prev.length) {
462 FlatNode next = (index < nodes.size()) ? (FlatNode) nodes.get(index) : null;
464 if (next == null || offset < next.offset) {
465 addOuterPartitions(list, offset, length, prev, next);
478 return (TypedRegion[]) list.toArray(new TypedRegion[list.size()]);
481 private void showList(List list) {
483 throw new NullPointerException();
484 } catch (Exception e){
487 System.out.println(">>>>>List start");
489 for (int i = 0; i < list.size(); i++) {
490 temp = (TypedRegion)list.get(i);
491 System.out.print("Offset: ");
492 System.out.print(temp.getOffset());
493 System.out.print(", Length: ");
494 System.out.print(temp.getLength());
495 System.out.print(", Type: ");
496 System.out.print(temp.getType());
497 System.out.println("");
499 System.out.println("<<<<<List end");
501 private void addOuterPartitions(List list, int offset, int length, FlatNode prev, FlatNode next) {
504 int end = offset + length;
506 if (prev != null && start < prev.offset + prev.length) {
507 start = prev.offset + prev.length;
510 if (next != null && next.offset < end) {
518 if (outerDocument == null) {
520 // if (end - start<0) {
521 // throw new IndexOutOfBoundsException();
524 list.add(new TypedRegion(start, end - start, getContentType(null, IDocument.DEFAULT_CONTENT_TYPE)));
529 // convert to outer offsets
530 start = outerDocument.getLocalOffset(start);
531 end = outerDocument.getLocalOffset(end);
532 if (end - start >= 0) {//jsurfer insert line
533 ITypedRegion[] regions = outerDocument.computePartitioning(start, end - start);
535 for (int i = 0; i < regions.length; i++) {
536 ITypedRegion region = regions[i];
538 // convert back to parent offsets
539 start = outerDocument.getParentOffset(region.getOffset());
540 end = start + region.getLength();
543 offset = prev.offset + prev.length;
544 if (start < offset) {
550 offset = next.offset;
556 // if (end - start<0) {
558 // System.out.print("MultiViewPartitioner::addOuterPartitions - Offset: ");
559 // System.out.print(offset);
560 // System.out.print(", Start: ");
561 // System.out.print(start);
562 // System.out.print(", End: ");
563 // System.out.print(end);
564 // System.out.print(", Type: ");
565 // System.out.print(region.getType());
566 // System.out.println("");
567 // throw new IndexOutOfBoundsException();
570 list.add(new TypedRegion(start, end - start, getContentType(null, region.getType())));
573 } catch (BadLocationException x) {
577 private void addInnerPartitions(List list, int offset, int length, FlatNode position) {
578 InnerDocumentView innerDocument = null;
579 if (position instanceof ViewNode) {
580 // TODO: revisit condition
581 innerDocument = ((ViewNode) position).view;
584 if (innerDocument == null) {
587 // if (position.length<0) {
588 // throw new IndexOutOfBoundsException();
591 list.add(new TypedRegion(position.offset, position.length, getContentType(position.type, null)));
595 // multiplexing to inner view
598 int start = Math.max(offset, position.offset);
599 int end = Math.min(offset + length, position.offset + position.length);
601 // convert to document offsets
602 length = end - start;
603 offset = innerDocument.getLocalOffset(start);
605 ITypedRegion[] regions = innerDocument.computePartitioning(offset, length);
607 for (int i = 0; i < regions.length; i++) {
608 ITypedRegion region = regions[i];
610 // convert back to parent offsets
611 offset = innerDocument.getParentOffset(region.getOffset());
612 length = region.getLength();
615 // throw new IndexOutOfBoundsException();
618 list.add(new TypedRegion(offset, length, getContentType(position.type, region.getType())));
620 } catch (BadLocationException x) {
625 * @see org.eclipse.jface.text.IDocumentPartitioner#getPartition(int)
627 public ITypedRegion getPartition(int offset) {
628 if (nodes.size() == 0) {
629 return getOuterPartition(offset, null, null);
632 int index = computeFlatNodeIndex(offset);
633 if (index < nodes.size()) {
634 FlatNode next = (FlatNode) nodes.get(index);
636 if (offset == next.offset) {
637 return getInnerPartition(offset, next);
641 return getOuterPartition(offset, null, next);
644 FlatNode prev = (FlatNode) nodes.get(index - 1);
646 if (prev.includes(offset)) {
647 return getInnerPartition(offset, prev);
650 return getOuterPartition(offset, prev, next);
653 FlatNode prev = (FlatNode) nodes.get(nodes.size() - 1);
655 if (prev.includes(offset)) {
656 return getInnerPartition(offset, prev);
659 return getOuterPartition(offset, prev, null);
662 protected ITypedRegion getOuterPartition(int offset, FlatNode prev, FlatNode next) {
667 if (outerDocument == null) {
669 end = document.getLength();
670 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
672 int outerOffset = outerDocument.getLocalOffset(offset);
674 if (outerOffset < 0) {
676 end = document.getLength();
677 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
680 ITypedRegion region = outerDocument.getPartition(outerOffset);
682 start = region.getOffset();
683 end = start + region.getLength();
685 // convert to parent offset
686 start = outerDocument.getParentOffset(start);
687 end = outerDocument.getParentOffset(end);
689 type = getContentType(null, region.getType());
694 offset = prev.offset + prev.length;
695 if (start < offset) {
701 offset = next.offset;
707 return new TypedRegion(start, end - start, type);
708 } catch (BadLocationException x) {
710 throw new IllegalArgumentException();
714 protected ITypedRegion getInnerPartition(int offset, FlatNode position) {
715 if (position instanceof ViewNode) {
716 // TODO: revisit condition
717 InnerDocumentView innerDocument = ((ViewNode) position).view;
719 if (innerDocument != null) {
720 // multiplexing to inner view
722 // convert to inner offset
723 ITypedRegion region = innerDocument.getPartition(innerDocument.getLocalOffset(offset));
725 // convert to parent offset
726 offset = innerDocument.getParentOffset(region.getOffset());
728 return new TypedRegion(offset, region.getLength(), getContentType(position.type, region.getType()));
729 } catch (BadLocationException x) {
735 return new TypedRegion(position.offset, position.length, position.type);