public String shortenName(String name,int desiredLength){
  int originalLength=name.length();
  if ((desiredLength <= 0) || (originalLength <= desiredLength)) {
    return name;
  }
  int delta=originalLength - desiredLength;
  int startCut=desiredLength / 2;
  StringBuffer result=new StringBuffer();
  result.append(name.substring(0,startCut));
  if (((startCut == 0) || (name.charAt(startCut - 1) != '_')) && ((startCut + delta + 1 == originalLength) || (name.charAt(startCut + delta + 1) != '_'))) {
    result.append("_");
  }
  result.append(name.substring(startCut + delta + 1,originalLength));
  return result.toString();
}

public String shortenName ( String name , int desiredLength ) { int originalLength = name . length ( ) ; if ( ( desiredLength <= 0 ) || ( originalLength <= desiredLength ) ) { return name ; } int delta = originalLength - desiredLength ; int startCut = desiredLength / 2 ; StringBuffer result = new StringBuffer ( ) ; result . append ( name . substring ( 0 , startCut ) ) ; if ( ( ( startCut == 0 ) || ( name . charAt ( startCut - 1 ) != '_' ) ) && ( ( startCut + delta + 1 == originalLength ) || ( name . charAt ( startCut + delta + 1 ) != '_' ) ) ) { result . append ( "_" ) ; } result . append ( name . substring ( startCut + delta + 1 , originalLength ) ) ; return result . toString ( ) ; }
generates a version of the name that has at most the specified length .
