View Javadoc

1   /*
2    * $Id: OASTNodeLabelProvider.java,v 1.10 2005/05/19 21:01:23 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.ui;
9   
10  import java.util.Iterator;
11  
12  import org.eclipse.jface.viewers.LabelProvider;
13  import org.eclipse.swt.graphics.Image;
14  
15  import com.bbn.swede.core.dom.AttributeNode;
16  import com.bbn.swede.core.dom.ClassNode;
17  import com.bbn.swede.core.dom.Literal;
18  import com.bbn.swede.core.dom.OASTNode;
19  import com.bbn.swede.core.dom.PropertyNode;
20  import com.bbn.swede.core.dom.UnparseableNode;
21  
22  
23  /***
24   * Provides text and images for OASTNodes that appear in JFace viewers.  
25   * Node text simply relies on the value returned by a node's
26   * overloaded toString() method, while icons from the OWLImages class are used
27   * to differentiate between things, predicates, attributes, literals, and
28   * unparseables.
29   * @see OASTContentProvider
30   * @author jlerner
31   */
32  public class OASTNodeLabelProvider extends LabelProvider
33  {
34     /*
35      *  (non-Javadoc)
36      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
37      */
38     public Image getImage(Object element)
39     {
40        if (!(element instanceof OASTNode))
41        {
42           return super.getImage(element);
43        }
44  
45        OASTNode node = (OASTNode) element;
46        if (node instanceof ClassNode)
47        {
48           return OWLImages.getSharedImage(OWLImages.IMG_THING);
49        }
50        if (node instanceof PropertyNode)
51        {
52           return OWLImages.getSharedImage(OWLImages.IMG_PREDICATE);
53        }
54        if (node instanceof AttributeNode)
55        {
56           return OWLImages.getSharedImage(OWLImages.IMG_ATTRIB_OBJ);
57        }
58        if (node instanceof Literal)
59        {
60           return OWLImages.getSharedImage(OWLImages.IMG_LIT_OBJ);
61        }
62        if (node instanceof UnparseableNode)
63        {
64           return OWLImages.getSharedImage(OWLImages.IMG_UNPARSEABLE);
65        }
66        return OWLImages.getSharedImage(OWLImages.IMG_OWL_OBJ);
67     }
68  
69     /*
70      *  (non-Javadoc)
71      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
72      */
73     public String getText(Object element)
74     {
75        String idText = null;
76        Iterator iterator = null;
77        if (!(element instanceof OASTNode))
78        {
79           return super.getText(element);
80        }
81  
82        OASTNode node = (OASTNode) element;
83        OASTNode child = null;
84        OASTNode[] children = node.getChildren();
85        for (int i = 0; i < children.length; i++)
86        {
87           child = children[i];
88           if(child instanceof AttributeNode)
89           {
90              if(((AttributeNode) child).toString().startsWith("rdf:ID=\""))
91              {
92                 idText = ((AttributeNode) child).getValue();
93                 break;
94              }
95           }
96        }
97        
98        if(idText != null)
99        {
100          idText = element.toString() + " (" + idText + ")";
101       }
102       
103       return (idText == null) ? element.toString() : idText;
104    }
105 
106 }