View Javadoc

1   /*
2    * $Id: EditorPlugin.java,v 1.19 2005/06/01 20:45:36 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   package com.bbn.swede.editor;
9   
10  import java.util.MissingResourceException;
11  import java.util.ResourceBundle;
12  
13  import org.eclipse.core.resources.IFile;
14  import org.eclipse.jface.text.BadLocationException;
15  import org.eclipse.ui.PartInitException;
16  import org.eclipse.ui.PlatformUI;
17  import org.eclipse.ui.ide.IDE;
18  import org.eclipse.ui.plugin.AbstractUIPlugin;
19  
20  import com.bbn.swede.core.IOWLDocument;
21  import com.bbn.swede.core.OWLCore;
22  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
23  import com.bbn.swede.core.dom.OAST;
24  import com.bbn.swede.core.dom.OASTNode;
25  
26  /***
27   * Plugin class for the com.bbn.swede.editor plugin.  Contains convenience
28   * methods for working with editors.
29   * @author jlerner
30   */
31  public class EditorPlugin extends AbstractUIPlugin
32  {
33     /***
34      * Editor preference controlling the width of a tab character, in spaces.
35      * This affects both how tab characters are displayed and how many spaces
36      * are substituted if PREFERENCE_SUBSTITUTE_SPACES is set to 
37      * <code>true</code>.
38      */
39     public static final String PREFERENCE_TAB_WIDTH = 
40        "preference-tab-width";
41     /***
42      * Editor preference controlling whether spaces are substituted for tab
43      * characters.
44      */
45     public static final String PREFERENCE_SUBSTITUTE_SPACES = 
46        "preference-substitute-spaces";
47     /***
48      * Editor preference controlling the preferred line length.  Other options
49      * will allow text to automatically be wrapped if this length is exceeded.
50      */
51     public static final String PREFERENCE_MAXIMUM_LINE_LENGTH = 
52        "preference-maximum-line-length";
53     
54     /***
55      * Editor preference controlling the amount of indentation for nested tags
56      * and literals.
57      */
58     public static final String PREFERENCE_TAG_INDENT = 
59        "preference-tag-indent";
60  
61     /***
62      * Editor preference controlling when a tag's attributes are split across
63      * multiple lines.  The value of this preference should be one of the 
64      * SPLIT_* constants.
65      */
66     public static final String PREFERENCE_ATTRIBUTE_SPLIT = 
67        "preference-attribute-split";
68     /***
69      * Editor preference controlling how wrapped attributes are indented.
70      */
71     public static final String PREFERENCE_ATTRIBUTE_INDENT = 
72        "preference-attribute-indent";
73     /***
74      * Editor preference controlling use of single vs. double quotes for
75      * attribute values.
76      */
77     public static final String PREFERENCE_ATTRIBUTE_DELIMITER = 
78        "preference-attribute-delimiter";
79     /***
80      * Editor preference controlling whether attributes are aligned with the
81      * end of the tag's name.  If this preference is set to false, the value
82      * of PREFERENCE_ATTRIBUTE_INDENT will be used for indented attributes in
83      * all tags.
84      */
85     public static final String PREFERENCE_ATTRIBUTE_ALIGN = 
86        "preference-attribute-align";
87     /***
88      * Editor preference controlling whether the '=' is aligned for wrapped
89      * attributes.
90      */
91     public static final String PREFERENCE_ATTRIBUTE_ALIGN_VALUE =
92        "preference-attribute-align-value";
93  
94     /***
95      * Editor preference controlling when literals are wrapped across multiple
96      * lines.  The value of this preference should be one of the 
97      * SPLIT_* constants.
98      */
99     public static final String PREFERENCE_LITERAL_SPLIT = 
100       "preference-literal-split";
101 
102    /***
103     * Attribute split constant indicating that attributes should never be 
104     * split across multiple lines.
105     */
106    public static final int SPLIT_NEVER  = 1;
107    /***
108     * Attribute split constant indicating that attributes should always be
109     * split across multiple lines.
110     */
111    public static final int SPLIT_ALWAYS = 2;
112    /***
113     * Attribute split constant indicating that attributes should be split
114     * across multiple lines only if they exceed PREFERENCE_MAXIMUM_LINE_LENGTH.
115     */
116    public static final int SPLIT_LONG   = 4;
117    
118    private static EditorPlugin _plugin;
119    private ResourceBundle _resourceBundle;
120    /***
121     * Creates a new editor plugin and sets it as the singleton instance.
122     */
123    public EditorPlugin()
124    {
125       super();
126       _plugin = this;
127       try
128       {
129          _resourceBundle =
130             ResourceBundle.getBundle(
131                "com.bbn.swede.editor.EditorPluginResources");
132       }
133       catch (MissingResourceException x)
134       {
135          _resourceBundle = null;
136       }
137    }
138 
139    /***
140     * Returns the one and only instance of EditorPlugin.
141     * @return The singleton instance
142     */
143    public static EditorPlugin getPlugin()
144    {
145       return _plugin;
146    }
147 
148    /***
149     * Returns the string from the plugin's resource bundle,
150     * or 'key' if not found.
151     * @param key The key for the desired resource string
152     * @return The resource string for <code>key</code>.
153     */
154    public static String getResourceString(String key)
155    {
156       ResourceBundle bundle = _plugin.getResourceBundle();
157       try
158       {
159          return bundle.getString(key);
160       }
161       catch (MissingResourceException e)
162       {
163          return key;
164       }
165    }
166 
167    /***
168     * Retrieves the plugin's resource bundle.
169     * @return The resource bundle
170     */
171    public ResourceBundle getResourceBundle()
172    {
173 
174       return _resourceBundle;
175    }
176 
177    /***
178     * Inserts a new node into an OWL document, directly under the rdf:RDF tag
179     * The document will be opened in the editor automatically.
180     * @param doc The document to receive the new node
181     * @param node The node being inserted
182     * @param bBefore <code>true</code> to insert the node before any existing 
183     *                tags under rdf:RDF, <code>false</code> to insert after all
184     *                existing nodes under rdf:RDF. 
185     * @deprecated As of SWeDE 2.0.0, the OAST API should be used to insert nodes
186     *             into the document.
187     */
188    public static void insertNode(
189       IOWLDocument doc,
190       OASTNode node,
191       boolean bBefore)
192    {
193       IOWLAbstractSyntaxTree tree = doc.getDocumentInfo().getOAST();
194       if (!(tree instanceof OAST))
195       {
196          return;
197       }
198       OASTNode[] nodes = tree.getRoot().getNodesOfType(OASTNode.RDF_RDF, 1);
199       if (nodes.length > 0)
200       {
201          insertNode(doc, (OASTNode) nodes[0], node, bBefore);
202       }
203    }
204 
205    /***
206     * Inserts a new node into an OWL document, directly under a specified parent node.
207     * The document will be opened in the editor automatically.
208     * @param doc The document to receive the new node
209     * @param parent The parent node of the new node
210     * @param node The node being inserted
211     * @param bBefore <code>true</code> to insert the node before any existing 
212     *                tags under rdf:RDF, <code>false</code> to insert after all
213     *                existing nodes under rdf:RDF.
214     * @deprecated As of SWeDE 2.0.0, the OAST API should be used to insert nodes
215     *             into the document.
216     */
217    public static void insertNode(
218       IOWLDocument doc,
219       OASTNode parent,
220       OASTNode node,
221       boolean bBefore)
222    {
223       IFile file = doc.getFile();
224       try
225       {
226          OWLEditor ed = (OWLEditor)IDE.openEditor(
227             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(),
228             file,
229             true);
230          ed.getSourceEditor().insertNode(parent, node, bBefore);
231       }
232       catch (PartInitException e)
233       {
234          OWLCore.logError(
235             getID(),
236             "Error opening editor for " + doc.getElementName(),
237             e);
238       }
239       catch (BadLocationException e)
240       {
241          OWLCore.logError(
242             getID(),
243             "Error inserting text into " + doc.getElementName(),
244             e);
245       }
246    }
247 
248    /***
249     * Retrieves the plugin's ID from the resource bundle.
250     * @return The plugin ID
251     */
252    public static String getID()
253    {
254       return EditorPlugin.getPlugin().getBundle().getSymbolicName();
255    }
256 
257    /***
258     * Indicates whether spaces should be substituted for tab characters,
259     * based on editor preferences.
260     * @return <code>true</code> if spaces should be substituted, 
261     *         <code>false</code> to allow tab characters
262     */
263    public static boolean spacesForTabs()
264    {
265       return _plugin.getPluginPreferences().getBoolean(PREFERENCE_SUBSTITUTE_SPACES);
266    }
267    
268    /***
269     * Returns the width in spaces of the tab character, 
270     * based on editor preferences.
271     * @return The width of a tab character
272     */
273    public static int getTabWidth()
274    {
275       return _plugin.getPluginPreferences().getInt(PREFERENCE_TAB_WIDTH);
276    }
277    
278    /***
279     * Returns the indentation amount for nested tags and literals, based on
280     * editor preferences.
281     * @return The number of tabs to indent between open and close tags.
282     */
283    public static int getTagIndent()
284    {
285       return _plugin.getPluginPreferences().getInt(PREFERENCE_TAG_INDENT);
286    }
287    
288    /***
289     * Returns the indentation amount for wrapped attributes, based on editor
290     * preferences.
291     * @return The number of tabs to indent wrapped attributes inside an open or
292     *         singleton tag.
293     */
294    public static int getAttributeIndent()
295    {
296       return _plugin.getPluginPreferences().getInt(PREFERENCE_ATTRIBUTE_INDENT);
297    }
298    
299    /***
300     * Returns the maximum line length for the OWL source editor, based on
301     * editor preferences.
302     * @return The maximum number of characters allowed on a line before 
303     *         literals and attributes are wrapped
304     */
305    public static int getMaximumLineLength()
306    {
307       return _plugin.getPluginPreferences().getInt(PREFERENCE_MAXIMUM_LINE_LENGTH);
308    }
309    
310    private static boolean split(int iSplit, int iLineLength)
311    {
312       if (iSplit == SPLIT_ALWAYS)
313       {
314          return true;
315       }
316       else if (iSplit == SPLIT_NEVER)
317       {
318          return false;
319       }
320       
321       return (iLineLength > getMaximumLineLength());
322    }
323    
324    /***
325     * Indicates whether a literal should be wrapped across multiple lines,
326     * based on editor preferences and the length of the line containing it.
327     * @param iLineLength The length of the line containing the literal
328     * @return <code>true</code> if the literal should be wrapped,
329     *         <code>false</code> if it should remain on one line.
330     */
331    public static boolean splitLiteral(int iLineLength)
332    {
333       int iSplit = _plugin.getPluginPreferences().getInt(PREFERENCE_LITERAL_SPLIT);
334       return split(iSplit, iLineLength);
335    }
336 
337    /***
338     * Indicates whether a tag's attributes should be wrapped across multiple
339     * lines, based on editor preferences and the length of the line containing
340     * the tag.
341     * @param iLineLength The length of the line containing the open tag.
342     * @return <code>true</code> if the tag's attributes should be wrapped,
343     *         <code>false</code> if they should remain on one line.
344     */
345    public static boolean splitAttributes(int iLineLength)
346    {
347       int iSplit = _plugin.getPluginPreferences().getInt(PREFERENCE_ATTRIBUTE_SPLIT);
348       return split(iSplit, iLineLength);
349    }
350    
351    /***
352     * Returns the delimeter to be used for attribute values, based on editor
353     * preferences.
354     * @return A string containing the character used to delimit attributes, 
355     *         either ' or "
356     */
357    public static String getAttributeDelimeter()
358    {
359       return _plugin.getPluginPreferences().getString(PREFERENCE_ATTRIBUTE_DELIMITER);
360    }
361    
362    /***
363     * Indicates whether or not wrapped attributes should be aligned with the
364     * name of the containing tag, based on editor preferences. 
365     * @return <code>true</code> if attributes should be aligned with the tag
366     *         name, <code>false</code> to indent them all based on
367     *         getAttributeIndent().
368     */
369    public static boolean alignAttributes()
370    {
371       return _plugin.getPluginPreferences().getBoolean(PREFERENCE_ATTRIBUTE_ALIGN);
372    }
373    
374    /***
375     * Indicates whether or not the values of wrapped attributes should be
376     * aligned, based on editor preferences.
377     * @return <code>true</code> if values should be aligned, <code>false</code>
378     *         if not.
379     */
380    public static boolean alignAttributeValues()
381    {
382       return _plugin.getPluginPreferences().getBoolean(PREFERENCE_ATTRIBUTE_ALIGN_VALUE);
383    }
384 }