private void addToExpression(String value){
  if (value == null) {
    return;
  }
  String selectedText=currentExpression.getSelectedText();
  if (selectedText != null && selectedText.length() > 0) {
    if (value.endsWith("()")) {
      int selectionStart=currentExpression.getSelectionStart();
      int selectionEnd=currentExpression.getSelectionEnd();
      String text=currentExpression.getText();
      String firstPart=text.substring(0,selectionStart);
      String lastPart=text.substring(selectionEnd);
      currentExpression.setText(firstPart + value + lastPart);
      int lengthForCaretPosition=value.length();
      if (value.endsWith("()")) {
        lengthForCaretPosition--;
      }
      currentExpression.setCaretPosition(selectionStart + lengthForCaretPosition);
      addToExpression(selectedText);
      currentExpression.setCaretPosition(currentExpression.getCaretPosition() + 1);
      validateExpression();
      requestExpressionFocus();
    }
 else {
      int selectionStart=currentExpression.getSelectionStart();
      int selectionEnd=currentExpression.getSelectionEnd();
      String text=currentExpression.getText();
      String firstPart=text.substring(0,selectionStart);
      String lastPart=text.substring(selectionEnd);
      currentExpression.setText(firstPart + value + lastPart);
      int lengthForCaretPosition=value.length();
      if (value.endsWith("()")) {
        lengthForCaretPosition--;
      }
      currentExpression.setCaretPosition(selectionStart + lengthForCaretPosition);
      validateExpression();
      requestExpressionFocus();
    }
  }
 else {
    int caretPosition=currentExpression.getCaretPosition();
    String text=currentExpression.getText();
    if (text != null && text.length() > 0) {
      String firstPart=text.substring(0,caretPosition);
      String lastPart=text.substring(caretPosition);
      currentExpression.setText(firstPart + value + lastPart);
      int lengthForCaretPosition=value.length();
      if (value.endsWith("()")) {
        lengthForCaretPosition--;
      }
      currentExpression.setCaretPosition(caretPosition + lengthForCaretPosition);
    }
 else {
      currentExpression.setText(value);
      int lengthForCaretPosition=value.length();
      if (value.endsWith("()")) {
        lengthForCaretPosition--;
      }
      currentExpression.setCaretPosition(caretPosition + lengthForCaretPosition);
      currentExpression.setCaretPosition(lengthForCaretPosition);
    }
    validateExpression();
    requestExpressionFocus();
  }
}

private void addToExpression ( String value ) { if ( value == null ) { return ; } String selectedText = currentExpression . getSelectedText ( ) ; if ( selectedText != null && selectedText . length ( ) > 0 ) { if ( value . endsWith ( "()" ) ) { int selectionStart = currentExpression . getSelectionStart ( ) ; int selectionEnd = currentExpression . getSelectionEnd ( ) ; String text = currentExpression . getText ( ) ; String firstPart = text . substring ( 0 , selectionStart ) ; String lastPart = text . substring ( selectionEnd ) ; currentExpression . setText ( firstPart + value + lastPart ) ; int lengthForCaretPosition = value . length ( ) ; if ( value . endsWith ( "()" ) ) { lengthForCaretPosition -- ; } currentExpression . setCaretPosition ( selectionStart + lengthForCaretPosition ) ; addToExpression ( selectedText ) ; currentExpression . setCaretPosition ( currentExpression . getCaretPosition ( ) + 1 ) ; validateExpression ( ) ; requestExpressionFocus ( ) ; } else { int selectionStart = currentExpression . getSelectionStart ( ) ; int selectionEnd = currentExpression . getSelectionEnd ( ) ; String text = currentExpression . getText ( ) ; String firstPart = text . substring ( 0 , selectionStart ) ; String lastPart = text . substring ( selectionEnd ) ; currentExpression . setText ( firstPart + value + lastPart ) ; int lengthForCaretPosition = value . length ( ) ; if ( value . endsWith ( "()" ) ) { lengthForCaretPosition -- ; } currentExpression . setCaretPosition ( selectionStart + lengthForCaretPosition ) ; validateExpression ( ) ; requestExpressionFocus ( ) ; } } else { int caretPosition = currentExpression . getCaretPosition ( ) ; String text = currentExpression . getText ( ) ; if ( text != null && text . length ( ) > 0 ) { String firstPart = text . substring ( 0 , caretPosition ) ; String lastPart = text . substring ( caretPosition ) ; currentExpression . setText ( firstPart + value + lastPart ) ; int lengthForCaretPosition = value . length ( ) ; if ( value . endsWith ( "()" ) ) { lengthForCaretPosition -- ; } currentExpression . setCaretPosition ( caretPosition + lengthForCaretPosition ) ; } else { currentExpression . setText ( value ) ; int lengthForCaretPosition = value . length ( ) ; if ( value . endsWith ( "()" ) ) { lengthForCaretPosition -- ; } currentExpression . setCaretPosition ( caretPosition + lengthForCaretPosition ) ; currentExpression . setCaretPosition ( lengthForCaretPosition ) ; } validateExpression ( ) ; requestExpressionFocus ( ) ; } }
adds the given value to the expression text
