Improving the UI

To provide a better experience to the user of the SmallJava editor and tooling, we customize the appearance of SmallJava members (fields and methods) in several places of the UI.

First of all, we give a better string representation of members by also showing their type feature; thus, the string representation of a member is its name and its type's name separated by a colon :. Moreover, for methods, we also show the type of each parameter. We mimic the representation of Java members as implemented by Eclipse JDT. We then implement the methods for string representation in SmallJavaModelUtil:

def memberAsString(SJMember m) {
  m.name +
  if (m instanceof SJMethod)
    "(" + m.params.map[type.name].join(", ") + ")"
  else ""
}

def memberAsStringWithType(SJMember m) {
  m.memberAsString + " : " + m.type.name
}

We also borrow icons from Eclipse JDT for classes, fields, and methods. As we saw in Chapter 6, Customizing Xtext Components, we can specify the label representation for our DSL model elements by implementing text and image methods in SmallJavaLabelProvider. When we implement text, we can return a JFace StyledString object, which allows us to also specify the style (font, color, and so on) of the resulting label. For instance, we want to represent the part starting with : using a different style, again to mimic JDT:

class SmallJavaLabelProvider extends DefaultEObjectLabelProvider {
...

  @Inject extension SmallJavaModelUtil

  def text(SJMember m) {
    new StyledString(m.memberAsString).
      append(new StyledString(" : " + m.type.name,
        StyledString.DECORATIONS_STYLER))
  }
...

We also want a custom representation of members when they are proposed by the content assist; this will provide the user with additional information about the available members. In our SmallJavaProposalProvider class, we can simply override the getStyledDisplayString method that is automatically called by the default implementation of the proposal provider to represent the proposals. In this case, we return a custom StyledString object for representing an SJMember element, and we use a different style for representing the class containing the proposed member:

class SmallJavaProposalProvider extends
          AbstractSmallJavaProposalProvider {

  @Inject extension SmallJavaModelUtil

  override getStyledDisplayString(EObject element,
          String qualifiedNameAsString, String shortName) {
    if (element instanceof SJMember) {
      new StyledString(element.memberAsStringWithType).
      append(new StyledString(" - " +
        (element.eContainer as SJClass).name,
        StyledString.QUALIFIER_STYLER))
    } else
      super.getStyledDisplayString(element,
          qualifiedNameAsString, shortName)
  }...

The result can be seen in the following screenshot (we also customized the outline view, as we did in Chapter 6, Customizing Xtext Components). Note all the type related information that is now available in the UI:

Improving the UI
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.87.156