Create and use a StarCache in the ChangeScreen

A StarCache keeps track of the starred state of a
Change and can inform its listeners when it changes.
It is also able to create star Images and KeyCommands
which already know how to update the StarCache, the
Images will also update themselves to reflect the
proper starred state of the Change they are bound to.

Use the StarCache in the ChangeScreen instead of
creating the star Image and KeyCommand and getting
and updating the starred value, and updating the star
Image.

Change-Id: I3f47fb1153674c6a172bf3319ad2d39df3762138
This commit is contained in:
Martin Fick
2012-01-06 13:58:42 -07:00
parent ade9eb3c9f
commit e52462edaf
3 changed files with 140 additions and 57 deletions

View File

@@ -16,7 +16,6 @@ package com.google.gerrit.client.changes;
import com.google.gerrit.client.Dispatcher;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.CommentPanel;
import com.google.gerrit.client.ui.ComplexDisclosurePanel;
import com.google.gerrit.client.ui.ExpandAllCommand;
@@ -27,7 +26,6 @@ import com.google.gerrit.common.data.AccountInfo;
import com.google.gerrit.common.data.AccountInfoCache;
import com.google.gerrit.common.data.ChangeDetail;
import com.google.gerrit.common.data.ChangeInfo;
import com.google.gerrit.common.data.ToggleStarRequest;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
@@ -35,8 +33,6 @@ import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.Change.Status;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
@@ -54,7 +50,6 @@ import com.google.gwt.user.client.ui.Panel;
import com.google.gwtexpui.globalkey.client.GlobalKey;
import com.google.gwtexpui.globalkey.client.KeyCommand;
import com.google.gwtexpui.globalkey.client.KeyCommandSet;
import com.google.gwtjsonrpc.client.VoidResult;
import java.sql.Timestamp;
import java.util.List;
@@ -65,9 +60,9 @@ public class ChangeScreen extends Screen
private final Change.Id changeId;
private final PatchSet.Id openPatchSetId;
private ChangeDetailCache detailCache;
private StarCache starred;
private Image starChange;
private boolean starred;
private ChangeDescriptionBlock descriptionBlock;
private ApprovalTable approvals;
@@ -159,15 +154,6 @@ public class ChangeScreen extends Screen
}
}
private void setStarred(final boolean s) {
if (s) {
starChange.setResource(Gerrit.RESOURCES.starFilled());
} else {
starChange.setResource(Gerrit.RESOURCES.starOpen());
}
starred = s;
}
@Override
protected void onInitUI() {
super.onInitUI();
@@ -177,6 +163,8 @@ public class ChangeScreen extends Screen
detailCache = cache.getChangeDetailCache();
detailCache.addValueChangeHandler(this);
starred = cache.getStarCache();
addStyleName(Gerrit.RESOURCES.css().changeScreen());
keysNavigation = new KeyCommandSet(Gerrit.C.sectionNavigation());
@@ -185,19 +173,12 @@ public class ChangeScreen extends Screen
keysNavigation.add(new ExpandCollapseDependencySectionKeyCommand(0, 'd', Util.C.expandCollapseDependencies()));
if (Gerrit.isSignedIn()) {
keysAction.add(new StarKeyCommand(0, 's', Util.C.changeTableStar()));
keysAction.add(starred.new KeyCommand(0, 's', Util.C.changeTableStar()));
keysAction.add(new PublishCommentsKeyCommand(0, 'r', Util.C
.keyPublishComments()));
starChange = new Image(Gerrit.RESOURCES.starOpen());
starChange = starred.createStar();
starChange.setStyleName(Gerrit.RESOURCES.css().changeScreenStarIcon());
starChange.setVisible(Gerrit.isSignedIn());
starChange.addClickHandler(new ClickHandler() {
@Override
public void onClick(final ClickEvent event) {
toggleStar();
}
});
setTitleWest(starChange);
}
@@ -287,10 +268,6 @@ public class ChangeScreen extends Screen
private void display(final ChangeDetail detail) {
displayTitle(detail.getChange().getKey(), detail.getChange().getSubject());
if (starChange != null) {
setStarred(detail.isStarred());
}
if (Status.MERGED == detail.getChange().getStatus()) {
includedInPanel.setVisible(true);
includedInPanel.addOpenHandler(includedInTable);
@@ -422,24 +399,6 @@ public class ChangeScreen extends Screen
return menuBar;
}
private void toggleStar() {
final boolean prior = starred;
setStarred(!prior);
final ToggleStarRequest req = new ToggleStarRequest();
req.toggle(changeId, starred);
Util.LIST_SVC.toggleStars(req, new GerritCallback<VoidResult>() {
public void onSuccess(final VoidResult result) {
}
@Override
public void onFailure(final Throwable caught) {
super.onFailure(caught);
setStarred(prior);
}
});
}
public class UpToListKeyCommand extends KeyCommand {
public UpToListKeyCommand(int mask, char key, String help) {
super(mask, key, help);
@@ -462,17 +421,6 @@ public class ChangeScreen extends Screen
}
}
public class StarKeyCommand extends NeedsSignInKeyCommand {
public StarKeyCommand(int mask, char key, String help) {
super(mask, key, help);
}
@Override
public void onKeyPress(final KeyPressEvent event) {
toggleStar();
}
}
public class PublishCommentsKeyCommand extends NeedsSignInKeyCommand {
public PublishCommentsKeyCommand(int mask, char key, String help) {
super(mask, key, help);