Refactor SettingsPanel to use a Listenable object

Create a ListenableAccountDiffPreference and use it from
the PatchScriptSettingsPanel so that the PatschScreen can
listen directly to the data model for changes instead of the
PatchScriptSettingsPanel.

Change-Id: I52fb415fcbc1ed15a453840ebde0de8b8d8f6710
This commit is contained in:
Martin Fick
2010-08-30 14:08:03 -06:00
committed by Shawn O. Pearce
parent ed86f099a3
commit 8100029b44
4 changed files with 105 additions and 22 deletions

View File

@@ -22,6 +22,7 @@ import com.google.gerrit.client.changes.PatchTable;
import com.google.gerrit.client.changes.Util;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.ScreenLoadCallback;
import com.google.gerrit.client.ui.ListenableAccountDiffPreference;
import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.common.data.PatchScript;
import com.google.gerrit.common.data.PatchSetDetail;
@@ -166,14 +167,16 @@ public abstract class PatchScreen extends Screen implements
idSideB = diffSideB != null ? diffSideB : id.getParentKey();
this.patchIndex = patchIndex;
settingsPanel = new PatchScriptSettingsPanel();
settingsPanel
.addValueChangeHandler(new ValueChangeHandler<AccountDiffPreference>() {
ListenableAccountDiffPreference prefs =
new ListenableAccountDiffPreference();
prefs.addValueChangeHandler(new ValueChangeHandler<AccountDiffPreference>() {
@Override
public void onValueChange(ValueChangeEvent<AccountDiffPreference> event) {
update(event.getValue());
}
});
settingsPanel = new PatchScriptSettingsPanel(prefs);
settingsPanel.getReviewedCheckBox().addValueChangeHandler(
new ValueChangeHandler<Boolean>() {
@Override

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.client.patches;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.account.Util;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.ListenableAccountDiffPreference;
import com.google.gerrit.client.ui.NpIntTextBox;
import com.google.gerrit.reviewdb.AccountDiffPreference;
import com.google.gerrit.reviewdb.AccountDiffPreference.Whitespace;
@@ -48,7 +49,7 @@ public class PatchScriptSettingsPanel extends Composite implements
interface MyUiBinder extends UiBinder<Widget, PatchScriptSettingsPanel> {
}
private AccountDiffPreference value;
private ListenableAccountDiffPreference listenablePrefs;
private boolean enableIntralineDifference = true;
private boolean enableSmallFileFeatures = true;
@@ -96,7 +97,8 @@ public class PatchScriptSettingsPanel extends Composite implements
*/
private int setEnabledCounter;
public PatchScriptSettingsPanel() {
public PatchScriptSettingsPanel(ListenableAccountDiffPreference prefs) {
listenablePrefs = prefs;
initWidget(uiBinder.createAndBindUi(this));
initIgnoreWhitespace(ignoreWhitespace);
initContext(context);
@@ -115,11 +117,7 @@ public class PatchScriptSettingsPanel extends Composite implements
tabWidth.addKeyPressHandler(onEnter);
colWidth.addKeyPressHandler(onEnter);
if (Gerrit.isSignedIn() && Gerrit.getAccountDiffPreference() != null) {
setValue(Gerrit.getAccountDiffPreference());
} else {
setValue(AccountDiffPreference.createDefault(null));
}
display();
}
@Override
@@ -147,7 +145,7 @@ public class PatchScriptSettingsPanel extends Composite implements
public void setEnableSmallFileFeatures(final boolean on) {
enableSmallFileFeatures = on;
if (enableSmallFileFeatures) {
syntaxHighlighting.setValue(value.isSyntaxHighlighting());
syntaxHighlighting.setValue(getValue().isSyntaxHighlighting());
} else {
syntaxHighlighting.setValue(false);
}
@@ -157,7 +155,7 @@ public class PatchScriptSettingsPanel extends Composite implements
public void setEnableIntralineDifference(final boolean on) {
enableIntralineDifference = on;
if (enableIntralineDifference) {
intralineDifference.setValue(value.isIntralineDifference());
intralineDifference.setValue(getValue().isIntralineDifference());
} else {
intralineDifference.setValue(false);
}
@@ -178,10 +176,16 @@ public class PatchScriptSettingsPanel extends Composite implements
}
public AccountDiffPreference getValue() {
return value;
return listenablePrefs.get();
}
public void setValue(final AccountDiffPreference dp) {
listenablePrefs.set(dp);
display();
}
protected void display() {
final AccountDiffPreference dp = getValue();
setIgnoreWhitespace(dp.getIgnoreWhitespace());
if (enableSmallFileFeatures) {
syntaxHighlighting.setValue(dp.isSyntaxHighlighting());
@@ -195,8 +199,6 @@ public class PatchScriptSettingsPanel extends Composite implements
intralineDifference.setValue(dp.isIntralineDifference());
whitespaceErrors.setValue(dp.isShowWhitespaceErrors());
showTabs.setValue(dp.isShowTabs());
value = dp;
}
@UiHandler("update")
@@ -205,7 +207,7 @@ public class PatchScriptSettingsPanel extends Composite implements
}
private void update() {
AccountDiffPreference dp = new AccountDiffPreference(value);
AccountDiffPreference dp = new AccountDiffPreference(getValue());
dp.setIgnoreWhitespace(getIgnoreWhitespace());
dp.setContext(getContext());
dp.setTabSize(tabWidth.getIntValue());
@@ -215,8 +217,7 @@ public class PatchScriptSettingsPanel extends Composite implements
dp.setShowWhitespaceErrors(whitespaceErrors.getValue());
dp.setShowTabs(showTabs.getValue());
value = dp;
fireEvent(new ValueChangeEvent<AccountDiffPreference>(dp) {});
listenablePrefs.set(dp);
if (Gerrit.isSignedIn()) {
persistDiffPreferences();
@@ -225,10 +226,11 @@ public class PatchScriptSettingsPanel extends Composite implements
private void persistDiffPreferences() {
setEnabled(false);
Util.ACCOUNT_SVC.changeDiffPreferences(value, new GerritCallback<VoidResult>() {
Util.ACCOUNT_SVC.changeDiffPreferences(getValue(),
new GerritCallback<VoidResult>() {
@Override
public void onSuccess(VoidResult result) {
Gerrit.setAccountDiffPreference(value);
Gerrit.setAccountDiffPreference(getValue());
setEnabled(true);
}
@@ -267,7 +269,7 @@ public class PatchScriptSettingsPanel extends Composite implements
if (0 <= sel) {
return Whitespace.valueOf(ignoreWhitespace.getValue(sel));
}
return value.getIgnoreWhitespace();
return getValue().getIgnoreWhitespace();
}
private void setIgnoreWhitespace(Whitespace s) {
@@ -285,7 +287,7 @@ public class PatchScriptSettingsPanel extends Composite implements
if (0 <= sel) {
return Short.parseShort(context.getValue(sel));
}
return (short) value.getContext();
return (short) getValue().getContext();
}
private void setContext(int ctx) {

View File

@@ -0,0 +1,30 @@
// Copyright (C) 2010 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.client.ui;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.reviewdb.AccountDiffPreference;
public class ListenableAccountDiffPreference
extends ListenableValue<AccountDiffPreference> {
public ListenableAccountDiffPreference() {
if (Gerrit.isSignedIn() && Gerrit.getAccountDiffPreference() != null) {
set(Gerrit.getAccountDiffPreference());
} else {
set(AccountDiffPreference.createDefault(null));
}
}
}

View File

@@ -0,0 +1,48 @@
// Copyright (C) 2010 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.client.ui;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
public class ListenableValue<T> implements HasValueChangeHandlers<T> {
private HandlerManager manager = new HandlerManager(this);
private T value;
public T get() {
return value;
}
public void set(final T value) {
this.value = value;
fireEvent(new ValueChangeEvent<T>(value) {});
}
public void fireEvent(GwtEvent<?> event) {
manager.fireEvent(event);
}
public HandlerRegistration addValueChangeHandler(
ValueChangeHandler<T> handler) {
return manager.addHandler(ValueChangeEvent.getType(), handler);
}
}