View Javadoc

1   /*
2    * $Id: OWLColorProvider.java,v 1.15 2004/09/20 03:51:39 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;
10  
11  import org.eclipse.jface.text.TextAttribute;
12  import org.eclipse.jface.viewers.IColorProvider;
13  import org.eclipse.swt.SWT;
14  import org.eclipse.swt.graphics.Color;
15  import org.eclipse.swt.widgets.Display;
16  import org.eclipse.ui.PlatformUI;
17  
18  /***
19   * Maps content types to text colors for syntax highlighting in the OWL source 
20   * editor.
21   * @author jlerner
22   */
23  public class OWLColorProvider implements IColorProvider
24  {
25     private static Display _display =
26        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay();
27     /***
28      * The singleton instance.
29      */
30     protected static OWLColorProvider _provider = new OWLColorProvider();
31  
32     /***
33      * Creates an OWL color provider.
34      */
35     protected OWLColorProvider()
36     {
37     }
38  
39     /***
40      * Returns the one and only instance of OWLColorProvider.
41      * @return The singleton instance
42      */
43     public static OWLColorProvider getColorProvider()
44     {
45        return _provider;
46     }
47     
48     /***
49      * Content type string for attribute values.  The attribute value is
50      * everything between the single or double quotes, inclusive.
51      */
52     public static final String ATTRIBUTE_VALUE = "ATTRIBUTE_VALUE";
53     /***
54      * Content type string for language tags and attributes.  A language
55      * element is anything from the owl, rdf, or rdfs namespace.
56      */
57     public static final String LANG = "LANG";
58     /***
59      * Content type string for generic attributes.  A generic attribute is
60      * anything used as an attribute from a namespace besides owl, rdf, or rdfs.
61      */
62     public static final String GENERIC_ATTRIBUTE = "GENERIC_ATTRIBUTE";
63     /***
64      * Content type string for generic tags.  A generic tag is anything used as
65      * a tag from a namespace besides owl, rdf, or rdfs.
66      */
67     public static final String GENERIC_TAG = "GENERIC_TAG";
68     /***
69      * Content type strying for XML comments.
70      */
71     public static final String COMMENT = "COMMENT";
72     /***
73      * Content type string for XML processing instructions.  For syntax
74      * highlihgting purposes, the XML version line is treated as a PI.
75      */
76     public static final String PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION";
77     /***
78      * Content type string for XML DOCTYPE.
79      */
80     public static final String DOCTYPE = "DOCTYPE";
81     /***
82      * Content type string for XML attributes.  This includes xml:base and
83      * namespace declarations.
84      */
85     public static final String XML_ATTRIBUTE = "XML_ATTRIBUTE";
86     /***
87      * Content type string for literal content.
88      */
89     public static final String CDATA = "CDATA";
90  
91     /* (non-Javadoc)
92      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(Object)
93      */
94     public Color getForeground(Object element)
95     {
96        if (!(element instanceof String))
97        {
98           return _display.getSystemColor(SWT.COLOR_BLACK);
99        }
100 
101       String s = (String) element;
102       if (s.equals(CDATA))
103       {
104          return _display.getSystemColor(SWT.COLOR_DARK_BLUE);
105       }
106       if (s.equals(COMMENT))
107       {
108          return _display.getSystemColor(SWT.COLOR_DARK_GRAY);
109       }
110       if (s.equals(LANG))
111       {
112          return _display.getSystemColor(SWT.COLOR_DARK_GREEN);
113       }
114       if (s.equals(ATTRIBUTE_VALUE))
115       {
116          return _display.getSystemColor(SWT.COLOR_BLUE);
117       }
118       if (s.equals(XML_ATTRIBUTE))
119       {
120          return _display.getSystemColor(SWT.COLOR_MAGENTA);
121       }
122       if (s.equals(GENERIC_TAG) || s.equals(GENERIC_ATTRIBUTE))
123       {
124          return _display.getSystemColor(SWT.COLOR_DARK_RED);
125       }
126 
127       return _display.getSystemColor(SWT.COLOR_BLACK);
128    }
129 
130    /* (non-Javadoc)
131     * @see org.eclipse.jface.viewers.IColorProvider#getForeground(Object)
132     */
133    public Color getBackground(Object element)
134    {
135 //      String s = (String)element;
136 //      if (s.equals(OWLPartitionScanner.BEGIN_TAG))
137 //      {
138 //          return _display.getSystemColor(SWT.COLOR_CYAN);
139 //      }
140 //      if (s.equals(OWLPartitionScanner.END_TAG))
141 //      {
142 //          return _display.getSystemColor(SWT.COLOR_GREEN);
143 //      }
144 //      if (s.equals(PROCESSING_INSTRUCTION))
145 //      {
146 //         return _display.getSystemColor(SWT.COLOR_MAGENTA);
147 //      }
148 //      if (s.equals(DOCTYPE))
149 //      {
150 //         return _display.getSystemColor(SWT.COLOR_GRAY);
151 //      }
152 //      if (OASTDocumentPartitioner.isLiteral(s))
153 //      {
154 //          return _display.getSystemColor(SWT.COLOR_YELLOW);
155 //      }
156       return null;
157    }
158    
159    /***
160     * Creates a org.eclipse.jface.text.TextAttribute to define the text style 
161     * for a content type.  The TextAttribute returned by this method will use
162     * a regular font weight.  This is a convenience method, fully equivalent to 
163     * createTextAttribute(sType,STW.NONE)
164     * @param sType The content type of the text that needs to be styled
165     * @return A org.eclipse.jface.text.TextAttribute with the proper text color
166     *         and a regular font weight
167     */
168    public TextAttribute createTextAttribute(String sType)
169    {
170       return createTextAttribute(sType, SWT.NONE);
171    }
172    
173    /***
174     * Creates a org.eclipse.jface.text.TextAttribute to define the text style 
175     * for a content type.
176     * @param sType The content type of the text that needs to be styled
177     * @param style An SWT style constant indicating the desired font weight
178     * @return A org.eclipse.jface.text.TextAttribute with the proper text color
179     *         and the requested font weight
180     */
181    public TextAttribute createTextAttribute(String sType, int style)
182    {
183       TextAttribute ta = new TextAttribute(
184          getForeground(sType),
185          getBackground(sType),
186          style);
187       return ta;
188    }
189 
190 }