initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiReconcilingStrategy.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation 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://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.wiki.editor;
12
13 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaOutlineParser;
14 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.reconciler.DirtyRegion;
20 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
21 import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
22 import org.eclipse.swt.widgets.Shell;
23
24
25
26 public class WikiReconcilingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension {
27
28         private WikiEditor fEditor;
29         private IDocument fDocument;
30         private IProgressMonitor fProgressMonitor;
31         
32         private WikipediaOutlineParser fParser;
33         private WikiFoldingStructureProvider fFoldingStructureProvider;
34
35         public WikiReconcilingStrategy(WikiEditor editor) {
36                 fEditor= editor;
37                 fParser= new WikipediaOutlineParser();
38                 fFoldingStructureProvider= new WikiFoldingStructureProvider(editor);
39         }
40
41         public void setDocument(IDocument document) {
42                 fDocument= document;
43                 fFoldingStructureProvider.setDocument(fDocument);
44         }
45
46         public void setProgressMonitor(IProgressMonitor monitor) {
47                 fProgressMonitor= monitor;
48                 fFoldingStructureProvider.setProgressMonitor(fProgressMonitor);
49         }
50
51         public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
52                 reconcile();
53         }
54
55         public void reconcile(IRegion partition) {
56                 reconcile();
57         }
58
59         public void initialReconcile() {
60                 reconcile();
61         }
62
63         private void reconcile() {
64                 final WikipediaSection section= fParser.parse(fDocument);
65                 if (section == null)
66                         return;
67                 
68                 Shell shell= fEditor.getSite().getShell();
69                 if (shell == null || shell.isDisposed())
70                         return;
71                 
72                 shell.getDisplay().asyncExec(new Runnable() {
73                         public void run() {
74                                 fEditor.setSection(section);
75                         }
76                 });
77                 fFoldingStructureProvider.updateFoldingRegions(section);
78         }
79 }