Exemplo n.º 1
0
 private boolean setSelection(ISelection selection, boolean reveal) {
   if (selection instanceof IStructuredSelection) {
     IStructuredSelection structuredSelection = (IStructuredSelection) selection;
     Object object = structuredSelection.getFirstElement();
     if (object instanceof EObject) {
       EObject element = (EObject) object;
       Resource resource = element.eResource();
       if (resource == null) {
         return false;
       }
       if (!(resource
           instanceof
           com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource)) {
         return false;
       }
       com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource
           textResource =
               (com.github.funthomas424242.rezeptsammler.rezept.resource.rezept
                       .IRezeptTextResource)
                   resource;
       com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptLocationMap
           locationMap = textResource.getLocationMap();
       int destination = locationMap.getCharStart(element);
       if (destination < 0) {
         destination = 0;
       }
       selectAndReveal(destination, 0);
       int length = locationMap.getCharEnd(element) - destination + 1;
       getSourceViewer().setRangeIndication(destination, length, true);
       getSourceViewer().setSelectedRange(destination, length);
       return true;
     }
   }
   return false;
 }
Exemplo n.º 2
0
  /**
   * Sets the caret to the offset of the given element.
   *
   * @param element has to be contained in the resource of this editor.
   */
  public void setCaret(EObject element, String text) {
    try {
      if (element == null || text == null || text.equals("")) {
        return;
      }
      ISourceViewer viewer = getSourceViewer();
      com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource
          textResource =
              (com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource)
                  element.eResource();
      com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptLocationMap
          locationMap = textResource.getLocationMap();
      int destination = locationMap.getCharStart(element);
      int length = locationMap.getCharEnd(element) + 1 - destination;

      com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextScanner lexer =
          getResource().getMetaInformation().createLexer();
      try {
        lexer.setText(viewer.getDocument().get(destination, length));
        com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextToken token =
            lexer.getNextToken();
        String tokenText = token.getText();
        while (tokenText != null) {
          if (token.getText().equals(text)) {
            destination += token.getOffset();
            break;
          }
          token = lexer.getNextToken();
          if (token == null) {
            break;
          }
          tokenText = token.getText();
        }
      } catch (BadLocationException e) {
      }
      destination = ((ProjectionViewer) viewer).modelOffset2WidgetOffset(destination);
      if (destination < 0) {
        destination = 0;
      }
      viewer.getTextWidget().setSelection(destination);
    } catch (Exception e) {
      com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.ui.RezeptUIPlugin.logError(
          "Exception in setCaret()", e);
    }
  }