public static String clipString(JComponent c, FontMetrics fm,
			String string, int availTextWidth) {
		//c may be null here.
		String clipString = "...";
		int stringLength = string.length();
		availTextWidth -= fm.stringWidth(clipString);
		boolean needsTextLayout = false;

		synchronized (charsBufferLock) {
			if (charsBuffer == null || charsBuffer.length < stringLength) {
				charsBuffer  = string.toCharArray();
			} else {
				string.getChars(0, stringLength, charsBuffer, 0);
			}
			needsTextLayout = 
				isComplexLayout(charsBuffer, 0, stringLength);
			if (!needsTextLayout) {
				int width = 0;
				for (int nChars = 0; nChars < stringLength; nChars++) {
					width += fm.charWidth(charsBuffer[nChars]);
					if (width > availTextWidth) {
						string = string.substring(0, nChars);
						break;
					}
				}
			}
		}
		
		if (needsTextLayout) {
			throw new RuntimeException("does not support complex text layout");
		}
		
		if (string.isEmpty()) {
			return "";
		} else {
			return string + clipString;
		}
	}