Make account formatter available to plugins

This allows plugins to use the same code as Gerrit core to format
account names.

Change-Id: I5331a2f543c3d5a592420c671347b1f9214af486
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-27 15:55:30 +02:00
parent 2fc17f4e52
commit 010d30592f
3 changed files with 107 additions and 22 deletions

View File

@@ -14,11 +14,17 @@
package com.google.gerrit.plugin.client;
import com.google.gerrit.client.AccountFormatter;
import com.google.gerrit.client.DateFormatter;
import com.google.gerrit.client.info.AccountInfo;
import java.util.Date;
public class FormatUtil {
private final static AccountFormatter accountFormatter =
new AccountFormatter(Plugin.get().getServerInfo().user()
.anonymousCowardName());
/** Format a date using a really short format. */
public static String shortFormat(Date dt) {
return createDateFormatter().shortFormat(dt);
@@ -37,4 +43,29 @@ public class FormatUtil {
private static DateFormatter createDateFormatter() {
return new DateFormatter(Plugin.get().getUserPreferences());
}
/**
* Formats an account as a name and an email address.
* <p>
* Example output:
* <ul>
* <li>{@code A U. Thor &lt;author@example.com&gt;}: full populated</li>
* <li>{@code A U. Thor (12)}: missing email address</li>
* <li>{@code Anonymous Coward &lt;author@example.com&gt;}: missing name</li>
* <li>{@code Anonymous Coward (12)}: missing name and email address</li>
* </ul>
*/
public static String nameEmail(AccountInfo info) {
return accountFormatter.nameEmail(info);
}
/**
* Formats an account name.
* <p>
* If the account has a full name, it returns only the full name. Otherwise it
* returns a longer form that includes the email address.
*/
public static String name(AccountInfo info) {
return accountFormatter.name(info);
}
}