package jetbrains.mps.vcs.annotate;
/*Generated by MPS */
import com.intellij.openapi.vcs.annotate.LineAnnotationAspect;
import com.intellij.vcsUtil.VcsUtil;
import java.awt.FontMetrics;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ILeftCombinator;
/*package*/ class AnnotationAspectSubcolumn {
private AnnotationColumn myAnnotationColumn;
private LineAnnotationAspect myAnnotationAspect;
private int myWidth;
private boolean myEnabled;
public AnnotationAspectSubcolumn(AnnotationColumn annotationColumn, LineAnnotationAspect annotationAspect) {
myAnnotationColumn = annotationColumn;
myAnnotationAspect = annotationAspect;
myEnabled = VcsUtil.isAspectAvailableByDefault(getId());
}
public String getTextForFileLine(int fileLine) {
String value = myAnnotationAspect.getValue(fileLine);
if (myAnnotationAspect.getId() == LineAnnotationAspect.AUTHOR && ViewAction.isSet(ViewAction.SHORTEN_NAMES)) {
return shortenName(value);
} else {
return value;
}
}
public int getWidth() {
return myWidth;
}
public void computeWidth(final FontMetrics fontMetrics, Iterable<Integer> fileLines) {
myWidth = Sequence.fromIterable(fileLines).foldLeft(0, new ILeftCombinator<Integer, Integer>() {
public Integer combine(Integer s, Integer fl) {
return Math.max(s, fontMetrics.stringWidth(getTextForFileLine(fl)));
}
});
}
public String getId() {
return (myAnnotationAspect == null ? null : myAnnotationAspect.getId());
}
public boolean isEnabled() {
return myEnabled;
}
public boolean isRightAligned() {
return false;
}
public void setEnabled(boolean enabled) {
VcsUtil.setAspectAvailability(getId(), enabled);
myEnabled = enabled;
myAnnotationColumn.invalidateLayout();
}
private static String shortenName(String longName) {
if (longName != null) {
String[] strings = longName.split(" ");
if (strings.length > 1) {
// Middle name check: Vasya S. Pupkin
return (strings[1].length() < 3 && strings.length > 2 && strings[2].length() > 1 ? strings[2] : strings[1]);
}
if (longName.contains(".")) {
strings = longName.split(".");
// vasya.pupkin case
if (strings.length > 1) {
// Middle name check: Vasya.S.Pupkin
return (strings[1].length() == 1 && strings.length > 2 && strings[2].length() > 1 ? strings[2] : strings[1]);
}
}
}
return longName;
}
}