View Javadoc

1   /*
2    * $Id: OWLContentFormatter.java,v 1.4 2005/06/01 17:38:33 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.editor.formatting;
10  
11  import org.eclipse.jface.text.IDocument;
12  import org.eclipse.jface.text.formatter.IFormattingContext;
13  import org.eclipse.jface.text.formatter.MultiPassContentFormatter;
14  import org.eclipse.jface.text.source.ISourceViewer;
15  
16  import com.bbn.swede.core.OWLCore;
17  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
18  import com.bbn.swede.editor.EditorPlugin;
19  import com.bbn.swede.editor.OWLDocumentPartitioner;
20  import com.bbn.swede.editor.OWLPartitionScanner;
21  import com.bbn.swede.editor.OWLSourceViewerConfiguration;
22  
23  /***
24   * <p>Content formatter for OWL documents.  On the first pass, the
25   * <code>OWLFormattingStrategy</code> adjusts the whitespace between tags
26   * to ensure proper linebreaks and indentation.  On the second pass,
27   * each individual BEGIN_TAG partition within the formatting region is further
28   * modified by <code>AttributeFormattingStrategy</code> to adjust whitespace
29   * between attributes to ensure proper line wrapping and indentation.</p>
30   * 
31   * <p>All modifications made by the content formatter respect the user's formatting
32   * preferences, as specified on the OWL editor prefererence page.</p>
33   * @author jlerner
34   */
35  public class OWLContentFormatter extends MultiPassContentFormatter
36  {
37     private OWLDocumentPartitioner _partitioner; 
38     /***
39      * Creates a content formatter for an OWL document.
40      * @param config The source viewer configuration of the document's editor
41      * @param viewer The source viewer of the document's editor
42      * @param oast The document's OWL abstract syntax tree
43      */
44     public OWLContentFormatter(OWLSourceViewerConfiguration config, 
45        ISourceViewer viewer, IOWLAbstractSyntaxTree oast)
46     {
47        super(config.getConfiguredDocumentPartitioning(viewer), 
48           OWLPartitionScanner.END_TAG);
49        setMasterStrategy(new OWLFormattingStrategy());
50        setSlaveStrategy(new AttributeFormattingStrategy(oast),
51                         OWLPartitionScanner.BEGIN_TAG);
52     }
53     /*
54      *  (non-Javadoc)
55      * @see org.eclipse.jface.text.formatter.MultiPassContentFormatter#formatMaster(
56      *    org.eclipse.jface.text.formatter.IFormattingContext, org.eclipse.jface.text.IDocument, int, int)
57      */
58     protected void formatMaster(IFormattingContext context, IDocument document,
59        int offset, int length)
60     {
61        try
62        {
63           super.formatMaster(context, document, offset, length);
64        }
65        catch (Exception e)
66        {
67           OWLCore.logWarning(EditorPlugin.getID(), "Error during master format", e);
68        }
69        
70        //Store a reference to the document partitioner...
71        _partitioner = (OWLDocumentPartitioner)document.getDocumentPartitioner();
72     }
73     /*
74      *  (non-Javadoc)
75      * @see org.eclipse.jface.text.formatter.MultiPassContentFormatter#formatSlave(
76      *    org.eclipse.jface.text.formatter.IFormattingContext, 
77      *    org.eclipse.jface.text.IDocument, int, int, java.lang.String)
78      */
79     protected void formatSlave(IFormattingContext context, IDocument document,
80        int offset, int length, String type)
81     {
82        //...so we can use it to force an OAST update before formatting attributes
83        _partitioner.updateOAST();
84  
85        super.formatSlave(context, document, offset, length, type);
86        
87     }
88  }