View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors;
23  
24  import org.apache.log4j.Logger;
25  import org.eclipse.swt.SWT;
26  import org.eclipse.jface.text.IDocument;
27  import org.eclipse.jface.text.ITextDoubleClickStrategy;
28  import org.eclipse.jface.text.TextAttribute;
29  import org.eclipse.jface.text.contentassist.IContentAssistant;
30  import org.eclipse.jface.text.presentation.IPresentationReconciler;
31  import org.eclipse.jface.text.presentation.PresentationReconciler;
32  import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
33  import org.eclipse.jface.text.rules.Token;
34  import org.eclipse.jface.text.source.ISourceViewer;
35  import org.eclipse.jface.text.source.SourceViewerConfiguration;
36  import org.eclipse.jface.text.contentassist.ContentAssistant;
37  
38  /**
39  * ...
40  * @author zhanghao
41  *
42  */
43  public class XMLConfiguration extends SourceViewerConfiguration {
44  	
45  	private XMLDoubleClickStrategy doubleClickStrategy;
46  	private XMLTagScanner tagScanner;
47  	private XMLScanner scanner;
48  	private ColorManager colorManager;
49  
50  	public XMLConfiguration(ColorManager colorManager) {
51  		this.colorManager = colorManager;
52  	}
53  	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
54  		return new String[] {
55  			IDocument.DEFAULT_CONTENT_TYPE,
56  			XMLPartitionScanner.XML_COMMENT,
57  			XMLPartitionScanner.XML_TAG };
58  	}
59  	public ITextDoubleClickStrategy getDoubleClickStrategy(
60  		ISourceViewer sourceViewer,
61  		String contentType) {
62  		if (doubleClickStrategy == null)
63  			doubleClickStrategy = new XMLDoubleClickStrategy();
64  		return doubleClickStrategy;
65  	}
66  
67  	protected XMLScanner getXMLScanner() {
68  		if (scanner == null) {
69  			scanner = new XMLScanner(colorManager);
70  			scanner.setDefaultReturnToken(
71  				new Token(
72  					new TextAttribute(
73  						colorManager.getColor(IXMLColorConstants.DEFAULT)
74  						, colorManager.getColor(IXMLColorConstants.BACKGROUND)
75  						, SWT.BOLD)));
76  		}
77  		return scanner;
78  	}
79  	protected XMLTagScanner getXMLTagScanner() {
80  		if (tagScanner == null) {
81  			tagScanner = new XMLTagScanner(colorManager);
82  			tagScanner.setDefaultReturnToken(
83  				new Token(
84  					new TextAttribute(
85  						colorManager.getColor(IXMLColorConstants.TAG)
86  						, colorManager.getColor(IXMLColorConstants.BACKGROUND)
87  						, SWT.NONE)));
88  		}
89  		return tagScanner;
90  	}
91  
92  	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
93  		PresentationReconciler reconciler = new PresentationReconciler();
94  
95  		DefaultDamagerRepairer dr =
96  			new DefaultDamagerRepairer(getXMLTagScanner());
97  		reconciler.setDamager(dr, XMLPartitionScanner.XML_TAG);
98  		reconciler.setRepairer(dr, XMLPartitionScanner.XML_TAG);
99  
100 		dr = new DefaultDamagerRepairer(getXMLScanner());
101 		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
102 		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
103 
104 		NonRuleBasedDamagerRepairer ndr =
105 			new NonRuleBasedDamagerRepairer(
106 				new TextAttribute(
107 					colorManager.getColor(IXMLColorConstants.XML_COMMENT)));
108 		reconciler.setDamager(ndr, XMLPartitionScanner.XML_COMMENT);
109 		reconciler.setRepairer(ndr, XMLPartitionScanner.XML_COMMENT);
110 
111 		return reconciler;
112 	}
113 	
114 	@Override
115 	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
116 		
117 		ContentAssistant assistant = new ContentAssistant();
118 		assistant.setContentAssistProcessor(new LLRPContentAssistant(), IDocument.DEFAULT_CONTENT_TYPE);
119 		assistant.enableAutoActivation(true);
120 		assistant.enableAutoInsert(true);
121 		
122 		return assistant;
123 	}
124 
125 }