Add the default context preference to user accounts

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2008-12-29 11:43:07 -08:00
parent b2e7300b7c
commit 51781f5828
11 changed files with 187 additions and 15 deletions

View File

@@ -22,6 +22,7 @@ public interface AccountConstants extends Constants {
String fullName(); String fullName();
String preferredEmail(); String preferredEmail();
String registeredOn(); String registeredOn();
String defaultContext();
String tabPreferences(); String tabPreferences();
String tabSshKeys(); String tabSshKeys();

View File

@@ -3,6 +3,7 @@ accountSettingsHeading = Account Settings
fullName = Full Name fullName = Full Name
preferredEmail = Email Address preferredEmail = Email Address
registeredOn = Registered registeredOn = Registered
defaultContext = Default Context
tabPreferences = Preferences tabPreferences = Preferences
tabSshKeys = SSH Keys tabSshKeys = SSH Keys

View File

@@ -0,0 +1,21 @@
// Copyright 2008 Google Inc.
//
// 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.client.account;
import com.google.gwt.i18n.client.Messages;
public interface AccountMessages extends Messages {
String lines(short cnt);
}

View File

@@ -0,0 +1 @@
lines = {0} lines

View File

@@ -15,16 +15,15 @@
package com.google.gerrit.client.account; package com.google.gerrit.client.account;
import com.google.gerrit.client.reviewdb.Account; import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.AccountSshKey;
import com.google.gerrit.client.rpc.SignInRequired; import com.google.gerrit.client.rpc.SignInRequired;
import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.RemoteJsonService; import com.google.gwtjsonrpc.client.RemoteJsonService;
import com.google.gwtjsonrpc.client.VoidResult; import com.google.gwtjsonrpc.client.VoidResult;
import java.util.List;
import java.util.Set;
public interface AccountService extends RemoteJsonService { public interface AccountService extends RemoteJsonService {
@SignInRequired @SignInRequired
void myAccount(AsyncCallback<Account> callback); void myAccount(AsyncCallback<Account> callback);
@SignInRequired
void changeDefaultContext(short newSetting, AsyncCallback<VoidResult> callback);
} }

View File

@@ -19,9 +19,12 @@ import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.BaseServiceImplementation; import com.google.gerrit.client.rpc.BaseServiceImplementation;
import com.google.gerrit.client.rpc.RpcUtil; import com.google.gerrit.client.rpc.RpcUtil;
import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.VoidResult;
import com.google.gwtorm.client.OrmException; import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory; import com.google.gwtorm.client.SchemaFactory;
import java.util.Collections;
public class AccountServiceImpl extends BaseServiceImplementation implements public class AccountServiceImpl extends BaseServiceImplementation implements
AccountService { AccountService {
public AccountServiceImpl(final SchemaFactory<ReviewDb> rdf) { public AccountServiceImpl(final SchemaFactory<ReviewDb> rdf) {
@@ -35,4 +38,16 @@ public class AccountServiceImpl extends BaseServiceImplementation implements
} }
}); });
} }
public void changeDefaultContext(final short newSetting,
final AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>() {
public VoidResult run(final ReviewDb db) throws OrmException {
final Account a = db.accounts().get(RpcUtil.getAccountId());
a.setDefaultContext(newSetting);
db.accounts().update(Collections.singleton(a));
return VoidResult.INSTANCE;
}
});
}
} }

View File

@@ -14,11 +14,13 @@
package com.google.gerrit.client.account; package com.google.gerrit.client.account;
import com.google.gerrit.client.FormatUtil; import static com.google.gerrit.client.FormatUtil.mediumFormat;
import com.google.gerrit.client.Gerrit; import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.reviewdb.Account; import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.rpc.GerritCallback; import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.AccountScreen; import com.google.gerrit.client.ui.AccountScreen;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.Label;
@@ -27,17 +29,26 @@ import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter; import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
public class AccountSettings extends AccountScreen { public class AccountSettings extends AccountScreen {
private final int labelIdx, fieldIdx;
private final Grid info; private final Grid info;
private TabPanel tabs; private TabPanel tabs;
private Panel prefsPanel; private PreferencePanel prefsPanel;
private Panel agreementsPanel; private Panel agreementsPanel;
private SshKeyPanel keysPanel; private SshKeyPanel keysPanel;
public AccountSettings() { public AccountSettings() {
super(Util.C.accountSettingsHeading()); super(Util.C.accountSettingsHeading());
if (LocaleInfo.getCurrentLocale().isRTL()) {
labelIdx = 1;
fieldIdx = 0;
} else {
labelIdx = 0;
fieldIdx = 1;
}
info = new Grid(3, 2); info = new Grid(3, 2);
info.setStyleName("gerrit-InfoBlock"); info.setStyleName("gerrit-InfoBlock");
info.addStyleName("gerrit-AccountInfoBlock"); info.addStyleName("gerrit-AccountInfoBlock");
@@ -52,9 +63,7 @@ public class AccountSettings extends AccountScreen {
fmt.addStyleName(0, 1, "topmost"); fmt.addStyleName(0, 1, "topmost");
fmt.addStyleName(2, 0, "bottomheader"); fmt.addStyleName(2, 0, "bottomheader");
prefsPanel = new FlowPanel(); prefsPanel = new PreferencePanel();
prefsPanel.add(new Label("Not Implemented"));
keysPanel = new SshKeyPanel(); keysPanel = new SshKeyPanel();
agreementsPanel = new FlowPanel(); agreementsPanel = new FlowPanel();
@@ -76,7 +85,7 @@ public class AccountSettings extends AccountScreen {
} }
private void infoRow(final int row, final String name) { private void infoRow(final int row, final String name) {
info.setText(row, 0, name); info.setText(row, labelIdx, name);
info.getCellFormatter().addStyleName(row, 0, "header"); info.getCellFormatter().addStyleName(row, 0, "header");
} }
@@ -98,8 +107,9 @@ public class AccountSettings extends AccountScreen {
} }
void display(final Account account) { void display(final Account account) {
info.setText(0, 1, account.getFullName()); info.setText(0, fieldIdx, account.getFullName());
info.setText(1, 1, account.getPreferredEmail()); info.setText(1, fieldIdx, account.getPreferredEmail());
info.setText(2, 1, FormatUtil.mediumFormat(account.getRegisteredOn())); info.setText(2, fieldIdx, mediumFormat(account.getRegisteredOn()));
prefsPanel.display(account);
} }
} }

View File

@@ -0,0 +1,100 @@
// Copyright 2008 Google Inc.
//
// 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.client.account;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwtjsonrpc.client.VoidResult;
public class PreferencePanel extends Composite {
private ListBox defaultContext;
private short oldDefaultContext;
public PreferencePanel() {
final FlowPanel body = new FlowPanel();
defaultContext = new ListBox();
for (final short v : Account.CONTEXT_CHOICES) {
defaultContext.addItem(Util.M.lines(v), String.valueOf(v));
}
defaultContext.addChangeListener(new ChangeListener() {
public void onChange(Widget sender) {
final int idx = defaultContext.getSelectedIndex();
if (idx < 0) {
return;
}
final short newLines = Short.parseShort(defaultContext.getValue(idx));
if (newLines == oldDefaultContext) {
return;
}
Util.ACCOUNT_SVC.changeDefaultContext(newLines,
new GerritCallback<VoidResult>() {
public void onSuccess(final VoidResult result) {
oldDefaultContext = newLines;
Gerrit.getUserAccount().setDefaultContext(newLines);
}
@Override
public void onFailure(final Throwable caught) {
setDefaultContext(oldDefaultContext);
super.onFailure(caught);
}
});
}
});
final int labelIdx, fieldIdx;
if (LocaleInfo.getCurrentLocale().isRTL()) {
labelIdx = 1;
fieldIdx = 0;
} else {
labelIdx = 0;
fieldIdx = 1;
}
final Grid formGrid = new Grid(1, 2);
formGrid.setText(0, labelIdx, Util.C.defaultContext());
formGrid.setWidget(0, fieldIdx, defaultContext);
body.add(formGrid);
initWidget(body);
}
void display(final Account account) {
setDefaultContext(account.getDefaultContext());
}
private void setDefaultContext(final short lines) {
for (int i = 0; i < Account.CONTEXT_CHOICES.length; i++) {
if (Account.CONTEXT_CHOICES[i] == lines) {
oldDefaultContext = lines;
defaultContext.setSelectedIndex(i);
return;
}
}
setDefaultContext(Account.DEFAULT_CONTEXT);
}
}

View File

@@ -19,6 +19,7 @@ import com.google.gwtjsonrpc.client.JsonUtil;
public class Util { public class Util {
public static final AccountConstants C = GWT.create(AccountConstants.class); public static final AccountConstants C = GWT.create(AccountConstants.class);
public static final AccountMessages M = GWT.create(AccountMessages.class);
public static final AccountService ACCOUNT_SVC; public static final AccountService ACCOUNT_SVC;
public static final AccountSecurity ACCOUNT_SEC; public static final AccountSecurity ACCOUNT_SEC;
public static final LoginService LOGIN_SVC; public static final LoginService LOGIN_SVC;

View File

@@ -21,6 +21,12 @@ import java.sql.Timestamp;
/** Preferences and information about a single user. */ /** Preferences and information about a single user. */
public final class Account { public final class Account {
/** Default number of lines of context. */
public static final short DEFAULT_CONTEXT = 10;
/** Typical valid choices for the default context setting. */
public static final short[] CONTEXT_CHOICES = {3, 10, 25, 50, 75, 100};
/** Key local to Gerrit to identify a user. */ /** Key local to Gerrit to identify a user. */
public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> { public static class Id extends IntKey<com.google.gwtorm.client.Key<?>> {
@Column @Column
@@ -66,6 +72,10 @@ public final class Account {
@Column(notNull = false) @Column(notNull = false)
protected String preferredEmail; protected String preferredEmail;
/** Default number of lines of context when viewing a patch. */
@Column
protected short defaultContext;
/** Non-Internet based contact details for the account's owner. */ /** Non-Internet based contact details for the account's owner. */
@Column(notNull = false) @Column(notNull = false)
protected ContactInformation contact; protected ContactInformation contact;
@@ -81,6 +91,7 @@ public final class Account {
public Account(final Account.Id newId) { public Account(final Account.Id newId) {
accountId = newId; accountId = newId;
registeredOn = new Timestamp(System.currentTimeMillis()); registeredOn = new Timestamp(System.currentTimeMillis());
defaultContext = DEFAULT_CONTEXT;
} }
/** Get local id of this account, to link with in other entities */ /** Get local id of this account, to link with in other entities */
@@ -113,6 +124,16 @@ public final class Account {
return registeredOn; return registeredOn;
} }
/** Get the default number of lines of context when viewing a patch. */
public short getDefaultContext() {
return defaultContext;
}
/** Set the number of lines of context when viewing a patch. */
public void setDefaultContext(final short s) {
defaultContext = s;
}
public ContactInformation getContactInformation() { public ContactInformation getContactInformation() {
return contact; return contact;
} }

View File

@@ -31,7 +31,8 @@ INSERT INTO accounts
contact_address, contact_address,
contact_country, contact_country,
contact_phone_nbr, contact_phone_nbr,
contact_fax_nbr contact_fax_nbr,
default_context
) SELECT ) SELECT
nextval('account_id'), nextval('account_id'),
a.created, a.created,
@@ -40,7 +41,8 @@ INSERT INTO accounts
a.mailing_address, a.mailing_address,
a.mailing_address_country, a.mailing_address_country,
a.phone_number, a.phone_number,
a.fax_number a.fax_number,
a.default_context
FROM gerrit1.accounts a; FROM gerrit1.accounts a;
DELETE FROM account_external_ids; DELETE FROM account_external_ids;