We’d like to know a little bit about you for our files

When we trig the content assist (with CTRL+Space) at a cross-reference position, we will see all the possible candidates. Imagine now that we want to add a little piece of information to each element’s label, for instance the name of the file that contains that element, how can we do that?
The ProposalProvider (MyDslProposalProvider) comes to our rescue, again. If we look in one of its super class, the AbstractJavaBasedContentProposalProvider class, we will see that it has the factory that creates the proposals, called DefaultProposalCreator. In this class the method getStyledDisplayString is called to get the styled string to display. So we have to override this method accordingly. Remember that we don’t want to replace the label, we want to append something to it. Here is the code:

protected StyledString getStyledDisplayString(EObject element,
                                                 String qualifiedName,
                                                 String shortName)
      //Get the display string of the element. We want to append something to it.
      String display = getDisplayString(element, qualifiedName, shortName);
      //Get the uri of the element. We will use it to compute the file name.
      org.eclipse.emf.common.util.URI uri;
      if(element.eIsProxy())//If it is an eIsProxy, get the proxy uri. In this case .eResource() returns null.
         uri = ((InternalEObject)element).eProxyURI();
         uri = element.eResource().getURI();
      //Get the URI as a String.
      String toAppend = uri.toString();
      //Now trim & cut the string, so that only the desired information remain.
      int start = (toAppend.indexOf("resource/"));
      start+=9;//"resource/" has 9 characters.
      int end = toAppend.length();
         end = toAppend.indexOf('#');
      toAppend = toAppend.substring(start, end);
      //Now build the styled string with the computed string.
      StyledString styled = new StyledString(display);
      //We use a pre-built style to make it quick.
      styled.append(new StyledString(" <-> "+toAppend, StyledString.QUALIFIER_STYLER));
      return styled;

Here is the result:


About Luong

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s