Skip to content

Commit

Permalink
Merge format utils
Browse files Browse the repository at this point in the history
[REFACTOR] Move methods *FormatUtils->CoreUtils
[REFACTOR] Update format method references
  • Loading branch information
m273d15 committed Sep 25, 2019
1 parent 0f3d31d commit 18d77e9
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 113 deletions.
Expand Up @@ -27,7 +27,7 @@
import saros.session.IActivityConsumer;
import saros.session.IActivityConsumer.Priority;
import saros.session.ISarosSession;
import saros.session.UserFormatUtils;
import saros.util.CoreUtils;

/**
* This class is responsible for two things:
Expand Down Expand Up @@ -203,7 +203,7 @@ public void runRecovery(IProgressMonitor monitor) {

remoteProgress.beginTask(
"Consistency recovery for user "
+ UserFormatUtils.getDisplayName(currentSession.getLocalUser()),
+ CoreUtils.determineUserDisplayName(currentSession.getLocalUser()),
filesRemaining.get());

fireActivity(
Expand Down
21 changes: 0 additions & 21 deletions core/src/saros/session/UserFormatUtils.java

This file was deleted.

39 changes: 39 additions & 0 deletions core/src/saros/util/CoreUtils.java
@@ -1,5 +1,12 @@
package saros.util;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import saros.net.util.XMPPUtils;
import saros.net.xmpp.JID;
import saros.session.User;

public class CoreUtils {

/**
Expand Down Expand Up @@ -58,4 +65,36 @@ public static String formatDuration(long seconds) {
format += seconds > 0 ? String.format(minutes > 0 ? "%02d" : "%d", secs) + "s" : "";
return format;
}

/**
* This method formats patterns for display in the UI by replacing occurrences of {@link User}
* objects by {@link ModelFormatUtils#determineUserDisplayName(User)}.
*
* @param pattern
* @param arguments occurrences of User objects are replaced by their display name
* @return the formatted string
*/
public static String format(String pattern, Object... arguments) {
List<Object> mappedValues = new ArrayList<Object>(arguments.length);
for (Object obj : arguments) {
if (obj instanceof User) {
User user = (User) obj;
mappedValues.add(determineUserDisplayName(user));
} else {
mappedValues.add(obj);
}
}
return MessageFormat.format(pattern, mappedValues.toArray());
}

/**
* Retrieves a user's nickname from the XMPP roster. If none is present it returns the base name.
*
* @param user
* @return the user's nickname, or if none is set JID's base.
*/
public static String determineUserDisplayName(User user) {
JID jid = user.getJID();
return XMPPUtils.getNickname(null, jid, jid.getBase());
}
}
Expand Up @@ -4,7 +4,7 @@
import saros.editor.internal.ContributionAnnotationManager;
import saros.session.User;
import saros.ui.Messages;
import saros.ui.util.ModelFormatUtils;
import saros.util.CoreUtils;

/**
* Marks textual additions and changes of other users. They are meant to inform the user of recent
Expand All @@ -24,7 +24,7 @@ public ContributionAnnotation(User source, IAnnotationModel model) {
super(
ContributionAnnotation.TYPE,
true,
ModelFormatUtils.format(Messages.ContributionAnnotation_text_contributed_by, source),
CoreUtils.format(Messages.ContributionAnnotation_text_contributed_by, source),
source);
this.model = model;
}
Expand Down
4 changes: 2 additions & 2 deletions eclipse/src/saros/editor/annotations/SelectionAnnotation.java
Expand Up @@ -3,7 +3,7 @@
import saros.editor.internal.LocationAnnotationManager;
import saros.session.User;
import saros.ui.Messages;
import saros.ui.util.ModelFormatUtils;
import saros.util.CoreUtils;

/**
* Mimics the text cursor and selection of other session participants.
Expand All @@ -19,7 +19,7 @@ public SelectionAnnotation(User source, boolean isCursor) {
super(
SelectionAnnotation.TYPE,
true,
ModelFormatUtils.format(
CoreUtils.format(
isCursor
? Messages.SelectionAnnotation_cursor_of
: Messages.SelectionAnnotation_selection_of,
Expand Down
4 changes: 2 additions & 2 deletions eclipse/src/saros/editor/annotations/ViewportAnnotation.java
Expand Up @@ -12,7 +12,7 @@
import saros.session.User.Permission;
import saros.ui.Messages;
import saros.ui.util.ColorUtils;
import saros.ui.util.ModelFormatUtils;
import saros.util.CoreUtils;

/**
* The annotation that shows the viewports of users with {@link Permission#WRITE_ACCESS}.
Expand Down Expand Up @@ -41,7 +41,7 @@ public ViewportAnnotation(User source) {
super(
ViewportAnnotation.TYPE,
true,
ModelFormatUtils.format(Messages.ViewportAnnotation_visible_scope_of, source),
CoreUtils.format(Messages.ViewportAnnotation_visible_scope_of, source),
source);

Display display = Display.getDefault();
Expand Down
Expand Up @@ -12,7 +12,7 @@
import saros.activities.ProgressActivity.ProgressAction;
import saros.session.User;
import saros.ui.Messages;
import saros.ui.util.ModelFormatUtils;
import saros.util.CoreUtils;

/** Eclipse-specific implementation of the {@link IRemoteProgressIndicator} interface. */
final class EclipseRemoteProgressIndicatorImpl implements IRemoteProgressIndicator {
Expand Down Expand Up @@ -64,8 +64,7 @@ public synchronized void start() {
started = true;

final Job job =
new Job(
ModelFormatUtils.format(Messages.RemoteProgress_observing_progress_for, remoteUser)) {
new Job(CoreUtils.format(Messages.RemoteProgress_observing_progress_for, remoteUser)) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
Expand Down
4 changes: 2 additions & 2 deletions eclipse/src/saros/ui/actions/FollowModeAction.java
Expand Up @@ -30,9 +30,9 @@
import saros.session.User;
import saros.ui.ImageManager;
import saros.ui.Messages;
import saros.ui.util.ModelFormatUtils;
import saros.ui.util.SWTUtils;
import saros.ui.util.selection.SelectionUtils;
import saros.util.CoreUtils;

/** Action to enter into FollowMode via toolbar. */
public class FollowModeAction extends Action implements IMenuCreator, Disposable {
Expand Down Expand Up @@ -330,6 +330,6 @@ private void updateEnablement() {
}

private String getFollowUserMessage(User user) {
return ModelFormatUtils.format(Messages.FollowModeAction_follow_user, user);
return CoreUtils.format(Messages.FollowModeAction_follow_user, user);
}
}
4 changes: 2 additions & 2 deletions eclipse/src/saros/ui/actions/RemoveUserAction.java
Expand Up @@ -19,10 +19,10 @@
import saros.session.ISessionLifecycleListener;
import saros.session.User;
import saros.ui.ImageManager;
import saros.ui.util.ModelFormatUtils;
import saros.ui.util.SWTUtils;
import saros.ui.util.selection.SelectionUtils;
import saros.ui.util.selection.retriever.SelectionRetrieverFactory;
import saros.util.CoreUtils;

public class RemoveUserAction extends Action {

Expand Down Expand Up @@ -115,7 +115,7 @@ public void run(IProgressMonitor monitor) throws InterruptedException {

List<String> userNames = new ArrayList<String>();

for (User user : users) userNames.add(ModelFormatUtils.getDisplayName(user));
for (User user : users) userNames.add(CoreUtils.determineUserDisplayName(user));

monitor.beginTask(
"Removing user(s): " + StringUtils.join(userNames, ", "),
Expand Down
10 changes: 5 additions & 5 deletions eclipse/src/saros/ui/eventhandler/UserStatusChangeHandler.java
Expand Up @@ -7,8 +7,8 @@
import saros.session.SessionEndReason;
import saros.session.User;
import saros.ui.Messages;
import saros.ui.util.ModelFormatUtils;
import saros.ui.views.SarosView;
import saros.util.CoreUtils;

/**
* Simple handler that informs the local user of the status changes for users in the current
Expand Down Expand Up @@ -45,7 +45,7 @@ public void permissionChanged(User user) {
if (user.isLocal()) {
SarosView.showNotification(
Messages.UserStatusChangeHandler_permission_changed,
ModelFormatUtils.format(
CoreUtils.format(
Messages.UserStatusChangeHandler_you_have_now_access,
user,
user.hasWriteAccess()
Expand All @@ -54,7 +54,7 @@ public void permissionChanged(User user) {
} else {
SarosView.showNotification(
Messages.UserStatusChangeHandler_permission_changed,
ModelFormatUtils.format(
CoreUtils.format(
Messages.UserStatusChangeHandler_he_has_now_access,
user,
user.hasWriteAccess()
Expand All @@ -68,14 +68,14 @@ public void userJoined(User user) {

SarosView.showNotification(
Messages.UserStatusChangeHandler_user_joined,
ModelFormatUtils.format(Messages.UserStatusChangeHandler_user_joined_text, user));
CoreUtils.format(Messages.UserStatusChangeHandler_user_joined_text, user));
}

@Override
public void userLeft(User user) {
SarosView.showNotification(
Messages.UserStatusChangeHandler_user_left,
ModelFormatUtils.format(Messages.UserStatusChangeHandler_user_left_text, user));
CoreUtils.format(Messages.UserStatusChangeHandler_user_left_text, user));
}
};

Expand Down
Expand Up @@ -8,7 +8,7 @@
import saros.session.User;
import saros.ui.ImageManager;
import saros.ui.Messages;
import saros.ui.util.ModelFormatUtils;
import saros.util.CoreUtils;

/**
* This is a tree element that can be displayed as a child element of the user entry in the Saros
Expand Down Expand Up @@ -41,7 +41,7 @@ public StyledString getStyledText() {
User followee = collector.getFollowedUser(user);
if (followee != null) {
if (collector.isActiveEditorShared(followee)) {
styledString.append("following " + ModelFormatUtils.getDisplayName(followee));
styledString.append("following " + CoreUtils.determineUserDisplayName(followee));
} else {
styledString.append(following_paused);
}
Expand Down
8 changes: 4 additions & 4 deletions eclipse/src/saros/ui/model/session/UserElement.java
Expand Up @@ -11,8 +11,8 @@
import saros.ui.Messages;
import saros.ui.model.TreeContentProvider;
import saros.ui.model.TreeElement;
import saros.ui.util.ModelFormatUtils;
import saros.ui.util.SWTBoldStyler;
import saros.util.CoreUtils;

/**
* Model element for a session {@link User user}. This element can only be used in conjunction with
Expand Down Expand Up @@ -81,7 +81,7 @@ public StyledString getStyledText() {
text.append(Messages.UserElement_host, StyledString.COUNTER_STYLER);
}

text.append(ModelFormatUtils.getDisplayName(user));
text.append(CoreUtils.determineUserDisplayName(user));

/*
* Right level
Expand Down Expand Up @@ -158,8 +158,8 @@ public int compareTo(final UserElement other) {

if (user2.isHost()) return +1;

String nickname1 = ModelFormatUtils.getDisplayName(user1);
String nickname2 = ModelFormatUtils.getDisplayName(user2);
String nickname1 = CoreUtils.determineUserDisplayName(user1);
String nickname2 = CoreUtils.determineUserDisplayName(user2);

return nickname1.compareToIgnoreCase(nickname2);
}
Expand Down
6 changes: 3 additions & 3 deletions eclipse/src/saros/ui/views/SarosView.java
Expand Up @@ -82,13 +82,13 @@
import saros.ui.sounds.SoundPlayer;
import saros.ui.sounds.Sounds;
import saros.ui.util.LayoutUtils;
import saros.ui.util.ModelFormatUtils;
import saros.ui.util.SWTUtils;
import saros.ui.util.selection.retriever.SelectionRetrieverFactory;
import saros.ui.widgets.ConnectionStateComposite;
import saros.ui.widgets.chat.ChatRoomsComposite;
import saros.ui.widgets.viewer.ViewerComposite;
import saros.ui.widgets.viewer.session.XMPPSessionDisplayComposite;
import saros.util.CoreUtils;

/**
* @JTourBusStop 1, The Interface Tour:
Expand Down Expand Up @@ -667,12 +667,12 @@ public static void showStopNotification(User user, SessionEndReason reason) {
switch (reason) {
case KICKED:
title = Messages.SessionStop_host_removed_you_title;
text = ModelFormatUtils.format(Messages.SessionStop_host_removed_you_message, user);
text = CoreUtils.format(Messages.SessionStop_host_removed_you_message, user);
break;

case HOST_LEFT:
title = Messages.SessionStop_host_closed_session_title;
text = ModelFormatUtils.format(Messages.SessionStop_host_closed_session_message, user);
text = CoreUtils.format(Messages.SessionStop_host_closed_session_message, user);
break;
case CONNECTION_LOST:
// TODO display the error
Expand Down
4 changes: 2 additions & 2 deletions eclipse/src/saros/ui/widgets/chat/ChatControl.java
Expand Up @@ -48,7 +48,6 @@
import saros.ui.Messages;
import saros.ui.sounds.SoundPlayer;
import saros.ui.sounds.Sounds;
import saros.ui.util.ModelFormatUtils;
import saros.ui.util.SWTUtils;
import saros.ui.widgets.ExplanationComposite;
import saros.ui.widgets.chat.events.CharacterEnteredEvent;
Expand All @@ -60,6 +59,7 @@
import saros.ui.widgets.chat.parts.IChatDisplay;
import saros.ui.widgets.chat.parts.IRCStyleChatDisplay;
import saros.ui.widgets.chat.parts.SkypeStyleChatDisplay;
import saros.util.CoreUtils;

/**
* This composite displays a chat conversation and the possibility to enter text.
Expand Down Expand Up @@ -634,7 +634,7 @@ private String getNickname(JID jid) {
if (rqJID != null) {
final User user = session.getUser(rqJID);

if (user != null) return ModelFormatUtils.getDisplayName(user);
if (user != null) return CoreUtils.determineUserDisplayName(user);
}
}
}
Expand Down
Expand Up @@ -13,7 +13,7 @@
import saros.monitoring.remote.IRemoteProgressIndicator;
import saros.monitoring.remote.RemoteProgressManager;
import saros.session.User;
import saros.ui.util.ModelFormatUtils;
import saros.util.CoreUtils;

/** IntelliJ implementation of the {@link IRemoteProgressIndicator} interface. */
final class IntelliJRemoteProgressIndicatorImpl implements IRemoteProgressIndicator {
Expand Down Expand Up @@ -65,8 +65,7 @@ public synchronized void start() {
started = true;

final UIMonitoredJob job =
new UIMonitoredJob(
ModelFormatUtils.format("Observing remote progress for {0}", remoteUser)) {
new UIMonitoredJob(CoreUtils.format("Observing remote progress for {0}", remoteUser)) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
Expand Down

0 comments on commit 18d77e9

Please sign in to comment.