Merge topic 'my-menu'

* changes:
  Allow users to reset the 'My' menu entries to the server defaults
  Add REST endpoints to get/set default user preferences
This commit is contained in:
Shawn Pearce
2014-04-11 01:38:43 +00:00
committed by Gerrit Code Review
12 changed files with 207 additions and 49 deletions

View File

@@ -232,6 +232,7 @@ public interface GerritCss extends CssResource {
String sshHostKeyPanelKnownHostEntry();
String sshKeyPanelEncodedKey();
String sshKeyPanelInvalid();
String stringListPanelButtons();
String topMostCell();
String topmenu();
String topmenuMenuLeft();

View File

@@ -40,10 +40,11 @@ import java.util.List;
public class StringListPanel extends FlowPanel {
private final StringListTable t;
private final Button deleteButton;
private final HorizontalPanel titlePanel;
protected final HorizontalPanel buttonPanel;
private final Button deleteButton;
private Image info;
private FocusWidget widget;
protected FocusWidget widget;
public StringListPanel(String title, List<String> fieldNames, FocusWidget w,
boolean autoSort) {
@@ -56,9 +57,10 @@ public class StringListPanel extends FlowPanel {
t = new StringListTable(fieldNames, autoSort);
add(t);
buttonPanel = new HorizontalPanel();
buttonPanel.setStyleName(Gerrit.RESOURCES.css().stringListPanelButtons());
deleteButton = new Button(Gerrit.C.stringListPanelDelete());
deleteButton.setEnabled(false);
add(deleteButton);
deleteButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
@@ -66,6 +68,8 @@ public class StringListPanel extends FlowPanel {
t.deleteChecked();
}
});
buttonPanel.add(deleteButton);
add(buttonPanel);
}
public void display(List<List<String>> values) {

View File

@@ -43,6 +43,7 @@ public interface AccountConstants extends Constants {
String myMenuInfo();
String myMenuName();
String myMenuUrl();
String myMenuReset();
String changeScreenOldUi();
String changeScreenNewUi();

View File

@@ -23,6 +23,7 @@ myMenu = My Menu
myMenuInfo = Menu Items for the 'My' top level menu.
myMenuName = Name
myMenuUrl = URL
myMenuReset = Reset
changeScreenOldUi = Old Screen
changeScreenNewUi = New Screen

View File

@@ -20,6 +20,7 @@ import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.PAGESI
import com.google.gerrit.client.Dispatcher;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.StringListPanel;
import com.google.gerrit.client.config.ConfigServerApi;
import com.google.gerrit.client.extensions.TopMenuItem;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.Natives;
@@ -27,6 +28,7 @@ import com.google.gerrit.client.rpc.ScreenLoadCallback;
import com.google.gerrit.client.ui.OnEditEnabler;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.CommentVisibilityStrategy;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
@@ -208,10 +210,7 @@ public class MyPreferencesScreen extends SettingsScreen {
}
});
myMenus = new StringListPanel(Util.C.myMenu(),
Arrays.asList(Util.C.myMenuName(), Util.C.myMenuUrl()),
save, false);
myMenus.setInfo(Util.C.myMenuInfo());
myMenus = new MyMenuPanel(save);
add(myMenus);
add(save);
@@ -282,9 +281,12 @@ public class MyPreferencesScreen extends SettingsScreen {
setListBox(diffView,
AccountGeneralPreferences.DiffView.SIDE_BY_SIDE,
p.diffView());
display(p.my());
}
private void display(JsArray<TopMenuItem> items) {
List<List<String>> values = new ArrayList<>();
for (TopMenuItem item : Natives.asList(p.my())) {
for (TopMenuItem item : Natives.asList(items)) {
values.add(Arrays.asList(item.getName(), item.getUrl()));
}
myMenus.display(values);
@@ -409,4 +411,28 @@ public class MyPreferencesScreen extends SettingsScreen {
return ui.name();
}
}
private class MyMenuPanel extends StringListPanel {
MyMenuPanel(Button save) {
super(Util.C.myMenu(), Arrays.asList(Util.C.myMenuName(),
Util.C.myMenuUrl()), save, false);
setInfo(Util.C.myMenuInfo());
Button resetButton = new Button(Util.C.myMenuReset());
resetButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
ConfigServerApi.defaultPreferences(new GerritCallback<Preferences>() {
@Override
public void onSuccess(Preferences p) {
MyPreferencesScreen.this.display(p.my());
widget.setEnabled(true);
}
});
}
});
buttonPanel.add(resetButton);
}
}
}

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.client.config;
import com.google.gerrit.client.account.Preferences;
import com.google.gerrit.client.extensions.TopMenuList;
import com.google.gerrit.client.rpc.NativeMap;
import com.google.gerrit.client.rpc.RestApi;
@@ -32,4 +33,8 @@ public class ConfigServerApi {
public static void topMenus(AsyncCallback<TopMenuList> cb) {
new RestApi("/config/server/top-menus").get(cb);
}
public static void defaultPreferences(AsyncCallback<Preferences> cb) {
new RestApi("/config/server/preferences").get(cb);
}
}

View File

@@ -1628,3 +1628,13 @@ a:hover.downloadLink {
padding-top: 5px;
padding-left: 5px;
}
/* StringListPanel */
.stringListPanelButtons {
margin-left: 0.5em;
}
.stringListPanelButtons .gwt-Button {
margin-right: 2em;
font-size: 7pt;
padding: 1px;
}

View File

@@ -76,10 +76,10 @@ public class GetPreferences implements RestReadView<AccountResource> {
rsrc.getUser().getAccountId(), allUsers);
}
static class PreferenceInfo {
public static class PreferenceInfo {
final String kind = "gerritcodereview#preferences";
short changesPerPage;
Short changesPerPage;
Boolean showSiteHeader;
Boolean useFlashClipboard;
DownloadScheme downloadScheme;
@@ -96,29 +96,36 @@ public class GetPreferences implements RestReadView<AccountResource> {
ChangeScreen changeScreen;
List<TopMenu.MenuItem> my;
PreferenceInfo(AccountGeneralPreferences p, Account.Id accountId,
public PreferenceInfo(AccountGeneralPreferences p, Account.Id accountId,
ProjectState allUsers) {
changesPerPage = p.getMaximumPageSize();
showSiteHeader = p.isShowSiteHeader() ? true : null;
useFlashClipboard = p.isUseFlashClipboard() ? true : null;
downloadScheme = p.getDownloadUrl();
downloadCommand = p.getDownloadCommand();
copySelfOnEmail = p.isCopySelfOnEmails() ? true : null;
dateFormat = p.getDateFormat();
timeFormat = p.getTimeFormat();
reversePatchSetOrder = p.isReversePatchSetOrder() ? true : null;
showUsernameInReviewCategory = p.isShowUsernameInReviewCategory() ? true : null;
relativeDateInChangeTable = p.isRelativeDateInChangeTable() ? true : null;
sizeBarInChangeTable = p.isSizeBarInChangeTable() ? true : null;
commentVisibilityStrategy = p.getCommentVisibilityStrategy();
diffView = p.getDiffView();
changeScreen = p.getChangeScreen();
my = my(accountId, allUsers);
this(p, RefNames.refsUsers(accountId), allUsers);
}
private List<TopMenu.MenuItem> my(Account.Id accountId, ProjectState allUsers) {
List<TopMenu.MenuItem> my = my(allUsers, RefNames.refsUsers(accountId));
if (my.isEmpty()) {
public PreferenceInfo(AccountGeneralPreferences p, String ref,
ProjectState allUsers) {
if (p != null) {
changesPerPage = p.getMaximumPageSize();
showSiteHeader = p.isShowSiteHeader() ? true : null;
useFlashClipboard = p.isUseFlashClipboard() ? true : null;
downloadScheme = p.getDownloadUrl();
downloadCommand = p.getDownloadCommand();
copySelfOnEmail = p.isCopySelfOnEmails() ? true : null;
dateFormat = p.getDateFormat();
timeFormat = p.getTimeFormat();
reversePatchSetOrder = p.isReversePatchSetOrder() ? true : null;
showUsernameInReviewCategory = p.isShowUsernameInReviewCategory() ? true : null;
relativeDateInChangeTable = p.isRelativeDateInChangeTable() ? true : null;
sizeBarInChangeTable = p.isSizeBarInChangeTable() ? true : null;
commentVisibilityStrategy = p.getCommentVisibilityStrategy();
diffView = p.getDiffView();
changeScreen = p.getChangeScreen();
}
my = my(ref, allUsers);
}
private List<TopMenu.MenuItem> my(String ref, ProjectState allUsers) {
List<TopMenu.MenuItem> my = my(allUsers, ref);
if (my.isEmpty() && !ref.equals(RefNames.REFS_USER + "default")) {
my = my(allUsers, RefNames.REFS_USER + "default");
}
if (my.isEmpty()) {

View File

@@ -53,23 +53,23 @@ import java.util.Collections;
import java.util.List;
public class SetPreferences implements RestModifyView<AccountResource, Input> {
static class Input {
Short changesPerPage;
Boolean showSiteHeader;
Boolean useFlashClipboard;
DownloadScheme downloadScheme;
DownloadCommand downloadCommand;
Boolean copySelfOnEmail;
DateFormat dateFormat;
TimeFormat timeFormat;
Boolean reversePatchSetOrder;
Boolean showUsernameInReviewCategory;
Boolean relativeDateInChangeTable;
Boolean sizeBarInChangeTable;
CommentVisibilityStrategy commentVisibilityStrategy;
DiffView diffView;
ChangeScreen changeScreen;
List<TopMenu.MenuItem> my;
public static class Input {
public Short changesPerPage;
public Boolean showSiteHeader;
public Boolean useFlashClipboard;
public DownloadScheme downloadScheme;
public DownloadCommand downloadCommand;
public Boolean copySelfOnEmail;
public DateFormat dateFormat;
public TimeFormat timeFormat;
public Boolean reversePatchSetOrder;
public Boolean showUsernameInReviewCategory;
public Boolean relativeDateInChangeTable;
public Boolean sizeBarInChangeTable;
public CommentVisibilityStrategy commentVisibilityStrategy;
public DiffView diffView;
public ChangeScreen changeScreen;
public List<TopMenu.MenuItem> my;
}
private final Provider<CurrentUser> self;
@@ -173,9 +173,13 @@ public class SetPreferences implements RestModifyView<AccountResource, Input> {
private void storeMyMenus(Account.Id accountId, List<TopMenu.MenuItem> my)
throws IOException {
storeMyMenus(RefNames.refsUsers(accountId), my);
}
public void storeMyMenus(String ref, List<TopMenu.MenuItem> my)
throws IOException {
ProjectLevelConfig prefsCfg =
allUsers.getConfig(PREFERENCES,
RefNames.refsUsers(accountId));
allUsers.getConfig(PREFERENCES, ref);
Config cfg = prefsCfg.get();
if (my != null) {
unsetSection(cfg, MY);

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.account.GetPreferences.PreferenceInfo;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject;
public class GetPreferences implements RestReadView<ConfigResource> {
private final ProjectState allUsers;
@Inject
public GetPreferences(ProjectCache projectCache) {
this.allUsers = projectCache.getAllUsers();
}
@Override
public PreferenceInfo apply(ConfigResource rsrc) {
return new PreferenceInfo(null, RefNames.REFS_USER + "default", allUsers);
}
}

View File

@@ -30,5 +30,7 @@ public class Module extends RestApiModule {
child(CONFIG_KIND, "capabilities").to(CapabilitiesCollection.class);
child(CONFIG_KIND, "top-menus").to(TopMenuCollection.class);
get(CONFIG_KIND, "version").to(GetVersion.class);
get(CONFIG_KIND, "preferences").to(GetPreferences.class);
put(CONFIG_KIND, "preferences").to(SetPreferences.class);
}
}

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.account.SetPreferences.Input;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.io.IOException;
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
public class SetPreferences implements RestModifyView<ConfigResource, Input> {
private final Provider<com.google.gerrit.server.account.SetPreferences> setPreferences;
private final Provider<GetPreferences> getDefaultPreferences;
@Inject
SetPreferences(
Provider<com.google.gerrit.server.account.SetPreferences> setPreferences,
Provider<GetPreferences> getDefaultPreferences) {
this.setPreferences = setPreferences;
this.getDefaultPreferences = getDefaultPreferences;
}
@Override
public Object apply(ConfigResource rsrc, Input i) throws BadRequestException,
IOException {
if (i.changesPerPage != null || i.showSiteHeader != null
|| i.useFlashClipboard != null || i.downloadScheme != null
|| i.downloadCommand != null || i.copySelfOnEmail != null
|| i.dateFormat != null || i.timeFormat != null
|| i.reversePatchSetOrder != null
|| i.showUsernameInReviewCategory != null
|| i.relativeDateInChangeTable != null
|| i.sizeBarInChangeTable != null
|| i.commentVisibilityStrategy != null || i.diffView != null
|| i.changeScreen != null) {
throw new BadRequestException("unsupported option");
}
if (i.my != null) {
setPreferences.get().storeMyMenus(RefNames.REFS_USER + "default", i.my);
}
return getDefaultPreferences.get().apply(rsrc);
}
}