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.1 2004-09-02 18:26:29 jsurfer 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 DocumentEvent event = new DocumentEvent(outerDocument, outerDocument.getLocalOffset(position.offset), position.length, null);
121 outerDocument.fireDocumentAboutToBeChanged(event);
122 super.addInnerRegion(position);
123 outerDocument.fireDocumentChanged(event);
125 super.addInnerRegion(position);
128 if (position instanceof ViewNode) {
129 // TODO: revisit condition
130 IDocumentPartitioner partitioner = createPartitioner(position.type);
131 if (partitioner != null) {
132 InnerDocumentView innerDocument = new InnerDocumentView(document, (ViewNode) position);
134 ((ViewNode) position).view = innerDocument;
136 partitioner.connect(innerDocument);
137 innerDocument.setDocumentPartitioner(partitioner);
138 innerDocument.addDocumentPartitioningListener(viewListener);
143 protected void removeInnerRegion(FlatNode position) {
145 if (outerDocument != null) {
146 DocumentEvent event = null;
147 if (position.offset >= 0) {
148 event = new DocumentEvent(outerDocument, outerDocument.getLocalOffset(position.offset), 0, document.get(position.offset,
151 outerDocument.fireDocumentAboutToBeChanged(event);
153 super.removeInnerRegion(position);
154 if (position.offset >= 0) {
155 outerDocument.fireDocumentChanged(event);
158 super.removeInnerRegion(position);
161 if (position instanceof ViewNode) {
162 // TODO: revisit condition
163 InnerDocumentView innerDocument = ((ViewNode) position).view;
164 if (innerDocument != null) {
165 IDocumentPartitioner partitioner = innerDocument.getDocumentPartitioner();
167 innerDocument.removeDocumentPartitioningListener(viewListener);
168 innerDocument.setDocumentPartitioner(null);
169 partitioner.disconnect();
172 } catch (BadLocationException e) {
176 protected void deleteInnerRegion(FlatNode position) {
177 super.deleteInnerRegion(position);
179 if (position instanceof ViewNode) {
180 // TODO: revisit condition
181 InnerDocumentView innerDocument = ((ViewNode) position).view;
182 if (innerDocument != null) {
183 IDocumentPartitioner partitioner = innerDocument.getDocumentPartitioner();
185 innerDocument.removeDocumentPartitioningListener(viewListener);
186 innerDocument.setDocumentPartitioner(null);
187 partitioner.disconnect();
192 public void connect(IDocument document) {
193 // outerDocument = new OuterDocumentView(document, innerPositions);
195 super.connect(document);
197 setOuterPartitioner(createPartitioner(null));
198 // IDocumentPartitioner partitioner =
199 // partitioner.connect(outerDocument);
200 // outerDocument.setDocumentPartitioner(partitioner);
201 // outerDocument.addDocumentPartitioningListener(viewListener);
204 public void disconnect() {
206 if (outerDocument != null) {
207 outerDocument.removeDocumentPartitioningListener(viewListener);
209 IDocumentPartitioner partitioner = outerDocument.getDocumentPartitioner();
211 outerDocument.setDocumentPartitioner(null);
212 partitioner.disconnect();
215 // TODO: cleanup listeners
216 outerDocument = null;
221 * @see org.eclipse.jface.text.IDocumentPartitioner#documentAboutToBeChanged(DocumentEvent)
223 public void documentAboutToBeChanged(DocumentEvent event) {
224 super.documentAboutToBeChanged(event);
226 outerDocumentEvent = null;
228 int offset = event.getOffset();
229 int length = event.getLength();
230 int end = offset + length;
232 // find left partition
233 int first = computeFlatNodeIndex(offset);
235 FlatNode p = (FlatNode) nodes.get(first - 1);
237 int right = p.offset + p.length;
238 if (offset < right) {
239 // change overlaps with partition
240 InnerDocumentView innerDocument = null;
241 if (p instanceof ViewNode) {
242 // TODO: revisit condition
243 innerDocument = ((ViewNode) p).view;
247 if (innerDocument != null) {
248 // cahnge completely inside partition
249 int start = innerDocument.getLocalOffset(offset);
250 innerDocument.fireDocumentAboutToBeChanged(new DocumentEvent(innerDocument, start, length, event.getText()));
256 if (innerDocument != null) {
257 // cut partition at right
258 int start = innerDocument.getLocalOffset(offset);
259 innerDocument.fireDocumentAboutToBeChanged(new DocumentEvent(innerDocument, start, innerDocument.getLength() - start,
265 // find right partition
266 int last = computeFlatNodeIndex(end);
268 FlatNode p = (FlatNode) nodes.get(last - 1);
270 if (p instanceof ViewNode) {
271 // TODO: revisit condition
272 InnerDocumentView innerDocument = ((ViewNode) p).view;
273 if (innerDocument != null) {
274 int right = p.offset + p.length;
276 // cut partition at left
277 int cut = innerDocument.getLocalOffset(end);
278 innerDocument.fireDocumentAboutToBeChanged(new DocumentEvent(innerDocument, 0, cut, null));
284 if (outerDocument != null) {
285 int left = outerDocument.getLocalOffset(offset);
286 int right = outerDocument.getLocalOffset(end);
288 String text = event.getText();
290 if (left != right || text != null && text.length() > 0) {
291 outerDocumentEvent = new DocumentEvent(outerDocument, left, right - left, text);
293 outerDocument.fireDocumentAboutToBeChanged(outerDocumentEvent);
298 protected int fixupPartitions(DocumentEvent event) {
299 int offset = event.getOffset();
300 int length = event.getLength();
301 int end = offset + length;
303 // fixup/notify inner views laying on change boundaries
305 int first = computeFlatNodeIndex(offset);
307 FlatNode p = (FlatNode) nodes.get(first - 1);
309 int right = p.offset + p.length;
310 if (offset < right) {
311 // change overlaps with partition
313 // cahnge completely inside partition
314 String text = event.getText();
317 p.length += text.length();
320 if (p instanceof ViewNode) {
321 // TODO: revisit condition
322 InnerDocumentView innerDocument = ((ViewNode) p).view;
323 if (innerDocument != null) {
324 int start = innerDocument.getLocalOffset(offset);
325 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, length, text));
329 // cut partition at right
330 int cut = p.offset + p.length - offset;
333 if (p instanceof ViewNode) {
334 // TODO: revisit condition
335 InnerDocumentView innerDocument = ((ViewNode) p).view;
336 if (innerDocument != null) {
337 int start = innerDocument.getLocalOffset(offset);
338 // TODO: ???fireDocumentAboutToBeChanged???
339 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, cut, null));
346 int last = computeFlatNodeIndex(end);
347 if (last > 0 && first != last) {
348 FlatNode p = (FlatNode) nodes.get(last - 1);
350 int right = p.offset + p.length;
352 // cut partition at left
353 int cut = end - p.offset;
357 String text = event.getText();
359 p.offset += text.length();
362 if (p instanceof ViewNode) {
363 // TODO: revisit condition
364 InnerDocumentView innerDocument = ((ViewNode) p).view;
365 if (innerDocument != null) {
366 // TODO: ???fireDocumentAboutToBeChanged???
367 innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, 0, cut, null));
375 // fixup inner views laying afrer change
377 String text = event.getText();
379 length -= text.length();
382 for (int i = last, size = nodes.size(); i < size; i++) {
383 ((FlatNode) nodes.get(i)).offset -= length;
386 // delete inner views laying completely inside change boundaries
390 deleteInnerRegion((FlatNode) nodes.get(--last));
391 } while (first < last);
393 rememberRegion(offset, 0);
398 if (outerDocumentEvent != null) {
399 outerDocument.fireDocumentChanged(outerDocumentEvent);
406 * @see org.eclipse.jface.text.IDocumentPartitioner#computePartitioning(int, int)
408 protected String getContentType(String parent, String view) {
413 if (parent != null) {
417 return IDocument.DEFAULT_CONTENT_TYPE;
421 * @see org.eclipse.jface.text.IDocumentPartitioner#computePartitioning(int, int)
423 public ITypedRegion[] computePartitioning(int offset, int length) {
424 List list = new ArrayList();
426 int end = offset + length;
428 int index = computeFlatNodeIndex(offset);
430 FlatNode prev = (index > 0) ? (FlatNode) nodes.get(index - 1) : null;
433 if (prev.overlapsWith(offset, length)) {
434 addInnerPartitions(list, offset, length, prev);
437 if (end <= prev.offset + prev.length) {
442 FlatNode next = (index < nodes.size()) ? (FlatNode) nodes.get(index) : null;
444 if (next == null || offset < next.offset) {
445 addOuterPartitions(list, offset, length, prev, next);
455 return (TypedRegion[]) list.toArray(new TypedRegion[list.size()]);
458 private void addOuterPartitions(List list, int offset, int length, FlatNode prev, FlatNode next) {
461 int end = offset + length;
463 if (prev != null && start < prev.offset + prev.length) {
464 start = prev.offset + prev.length;
467 if (next != null && next.offset < end) {
475 if (outerDocument == null) {
476 list.add(new TypedRegion(start, end - start, getContentType(null, IDocument.DEFAULT_CONTENT_TYPE)));
481 // convert to outer offsets
482 start = outerDocument.getLocalOffset(start);
483 end = outerDocument.getLocalOffset(end);
484 if (end - start >= 0) {//jsurfer insert line
485 ITypedRegion[] regions = outerDocument.computePartitioning(start, end - start);
487 for (int i = 0; i < regions.length; i++) {
488 ITypedRegion region = regions[i];
490 // convert back to parent offsets
491 start = outerDocument.getParentOffset(region.getOffset());
492 end = start + region.getLength();
495 offset = prev.offset + prev.length;
496 if (start < offset) {
502 offset = next.offset;
508 list.add(new TypedRegion(start, end - start, getContentType(null, region.getType())));
511 } catch (BadLocationException x) {
515 private void addInnerPartitions(List list, int offset, int length, FlatNode position) {
516 InnerDocumentView innerDocument = null;
517 if (position instanceof ViewNode) {
518 // TODO: revisit condition
519 innerDocument = ((ViewNode) position).view;
522 if (innerDocument == null) {
524 list.add(new TypedRegion(position.offset, position.length, getContentType(position.type, null)));
528 // multiplexing to inner view
531 int start = Math.max(offset, position.offset);
532 int end = Math.min(offset + length, position.offset + position.length);
534 // convert to document offsets
535 length = end - start;
536 offset = innerDocument.getLocalOffset(start);
538 ITypedRegion[] regions = innerDocument.computePartitioning(offset, length);
540 for (int i = 0; i < regions.length; i++) {
541 ITypedRegion region = regions[i];
543 // convert back to parent offsets
544 offset = innerDocument.getParentOffset(region.getOffset());
545 length = region.getLength();
547 list.add(new TypedRegion(offset, length, getContentType(position.type, region.getType())));
549 } catch (BadLocationException x) {
554 * @see org.eclipse.jface.text.IDocumentPartitioner#getPartition(int)
556 public ITypedRegion getPartition(int offset) {
557 if (nodes.size() == 0) {
558 return getOuterPartition(offset, null, null);
561 int index = computeFlatNodeIndex(offset);
562 if (index < nodes.size()) {
563 FlatNode next = (FlatNode) nodes.get(index);
565 if (offset == next.offset) {
566 return getInnerPartition(offset, next);
570 return getOuterPartition(offset, null, next);
573 FlatNode prev = (FlatNode) nodes.get(index - 1);
575 if (prev.includes(offset)) {
576 return getInnerPartition(offset, prev);
579 return getOuterPartition(offset, prev, next);
582 FlatNode prev = (FlatNode) nodes.get(nodes.size() - 1);
584 if (prev.includes(offset)) {
585 return getInnerPartition(offset, prev);
588 return getOuterPartition(offset, prev, null);
591 protected ITypedRegion getOuterPartition(int offset, FlatNode prev, FlatNode next) {
596 if (outerDocument == null) {
598 end = document.getLength();
599 type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
601 ITypedRegion region = outerDocument.getPartition(outerDocument.getLocalOffset(offset));
603 start = region.getOffset();
604 end = start + region.getLength();
606 // convert to parent offset
607 start = outerDocument.getParentOffset(start);
608 end = outerDocument.getParentOffset(end);
610 type = getContentType(null, region.getType());
614 offset = prev.offset + prev.length;
615 if (start < offset) {
621 offset = next.offset;
627 return new TypedRegion(start, end - start, type);
628 } catch (BadLocationException x) {
629 throw new IllegalArgumentException();
633 protected ITypedRegion getInnerPartition(int offset, FlatNode position) {
634 if (position instanceof ViewNode) {
635 // TODO: revisit condition
636 InnerDocumentView innerDocument = ((ViewNode) position).view;
638 if (innerDocument != null) {
639 // multiplexing to inner view
641 // convert to inner offset
642 ITypedRegion region = innerDocument.getPartition(innerDocument.getLocalOffset(offset));
644 // convert to parent offset
645 offset = innerDocument.getParentOffset(region.getOffset());
647 return new TypedRegion(offset, region.getLength(), getContentType(position.type, region.getType()));
648 } catch (BadLocationException x) {
654 return new TypedRegion(position.offset, position.length, position.type);