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:
committed by
Shawn O. Pearce
parent
ed86f099a3
commit
8100029b44
@@ -22,6 +22,7 @@ import com.google.gerrit.client.changes.PatchTable;
|
|||||||
import com.google.gerrit.client.changes.Util;
|
import com.google.gerrit.client.changes.Util;
|
||||||
import com.google.gerrit.client.rpc.GerritCallback;
|
import com.google.gerrit.client.rpc.GerritCallback;
|
||||||
import com.google.gerrit.client.rpc.ScreenLoadCallback;
|
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.client.ui.Screen;
|
||||||
import com.google.gerrit.common.data.PatchScript;
|
import com.google.gerrit.common.data.PatchScript;
|
||||||
import com.google.gerrit.common.data.PatchSetDetail;
|
import com.google.gerrit.common.data.PatchSetDetail;
|
||||||
@@ -166,14 +167,16 @@ public abstract class PatchScreen extends Screen implements
|
|||||||
idSideB = diffSideB != null ? diffSideB : id.getParentKey();
|
idSideB = diffSideB != null ? diffSideB : id.getParentKey();
|
||||||
this.patchIndex = patchIndex;
|
this.patchIndex = patchIndex;
|
||||||
|
|
||||||
settingsPanel = new PatchScriptSettingsPanel();
|
ListenableAccountDiffPreference prefs =
|
||||||
settingsPanel
|
new ListenableAccountDiffPreference();
|
||||||
.addValueChangeHandler(new ValueChangeHandler<AccountDiffPreference>() {
|
prefs.addValueChangeHandler(new ValueChangeHandler<AccountDiffPreference>() {
|
||||||
@Override
|
@Override
|
||||||
public void onValueChange(ValueChangeEvent<AccountDiffPreference> event) {
|
public void onValueChange(ValueChangeEvent<AccountDiffPreference> event) {
|
||||||
update(event.getValue());
|
update(event.getValue());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
settingsPanel = new PatchScriptSettingsPanel(prefs);
|
||||||
settingsPanel.getReviewedCheckBox().addValueChangeHandler(
|
settingsPanel.getReviewedCheckBox().addValueChangeHandler(
|
||||||
new ValueChangeHandler<Boolean>() {
|
new ValueChangeHandler<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package com.google.gerrit.client.patches;
|
|||||||
import com.google.gerrit.client.Gerrit;
|
import com.google.gerrit.client.Gerrit;
|
||||||
import com.google.gerrit.client.account.Util;
|
import com.google.gerrit.client.account.Util;
|
||||||
import com.google.gerrit.client.rpc.GerritCallback;
|
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.client.ui.NpIntTextBox;
|
||||||
import com.google.gerrit.reviewdb.AccountDiffPreference;
|
import com.google.gerrit.reviewdb.AccountDiffPreference;
|
||||||
import com.google.gerrit.reviewdb.AccountDiffPreference.Whitespace;
|
import com.google.gerrit.reviewdb.AccountDiffPreference.Whitespace;
|
||||||
@@ -48,7 +49,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
interface MyUiBinder extends UiBinder<Widget, PatchScriptSettingsPanel> {
|
interface MyUiBinder extends UiBinder<Widget, PatchScriptSettingsPanel> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private AccountDiffPreference value;
|
private ListenableAccountDiffPreference listenablePrefs;
|
||||||
private boolean enableIntralineDifference = true;
|
private boolean enableIntralineDifference = true;
|
||||||
private boolean enableSmallFileFeatures = true;
|
private boolean enableSmallFileFeatures = true;
|
||||||
|
|
||||||
@@ -96,7 +97,8 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
*/
|
*/
|
||||||
private int setEnabledCounter;
|
private int setEnabledCounter;
|
||||||
|
|
||||||
public PatchScriptSettingsPanel() {
|
public PatchScriptSettingsPanel(ListenableAccountDiffPreference prefs) {
|
||||||
|
listenablePrefs = prefs;
|
||||||
initWidget(uiBinder.createAndBindUi(this));
|
initWidget(uiBinder.createAndBindUi(this));
|
||||||
initIgnoreWhitespace(ignoreWhitespace);
|
initIgnoreWhitespace(ignoreWhitespace);
|
||||||
initContext(context);
|
initContext(context);
|
||||||
@@ -115,11 +117,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
tabWidth.addKeyPressHandler(onEnter);
|
tabWidth.addKeyPressHandler(onEnter);
|
||||||
colWidth.addKeyPressHandler(onEnter);
|
colWidth.addKeyPressHandler(onEnter);
|
||||||
|
|
||||||
if (Gerrit.isSignedIn() && Gerrit.getAccountDiffPreference() != null) {
|
display();
|
||||||
setValue(Gerrit.getAccountDiffPreference());
|
|
||||||
} else {
|
|
||||||
setValue(AccountDiffPreference.createDefault(null));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -147,7 +145,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
public void setEnableSmallFileFeatures(final boolean on) {
|
public void setEnableSmallFileFeatures(final boolean on) {
|
||||||
enableSmallFileFeatures = on;
|
enableSmallFileFeatures = on;
|
||||||
if (enableSmallFileFeatures) {
|
if (enableSmallFileFeatures) {
|
||||||
syntaxHighlighting.setValue(value.isSyntaxHighlighting());
|
syntaxHighlighting.setValue(getValue().isSyntaxHighlighting());
|
||||||
} else {
|
} else {
|
||||||
syntaxHighlighting.setValue(false);
|
syntaxHighlighting.setValue(false);
|
||||||
}
|
}
|
||||||
@@ -157,7 +155,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
public void setEnableIntralineDifference(final boolean on) {
|
public void setEnableIntralineDifference(final boolean on) {
|
||||||
enableIntralineDifference = on;
|
enableIntralineDifference = on;
|
||||||
if (enableIntralineDifference) {
|
if (enableIntralineDifference) {
|
||||||
intralineDifference.setValue(value.isIntralineDifference());
|
intralineDifference.setValue(getValue().isIntralineDifference());
|
||||||
} else {
|
} else {
|
||||||
intralineDifference.setValue(false);
|
intralineDifference.setValue(false);
|
||||||
}
|
}
|
||||||
@@ -178,10 +176,16 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
public AccountDiffPreference getValue() {
|
public AccountDiffPreference getValue() {
|
||||||
return value;
|
return listenablePrefs.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValue(final AccountDiffPreference dp) {
|
public void setValue(final AccountDiffPreference dp) {
|
||||||
|
listenablePrefs.set(dp);
|
||||||
|
display();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void display() {
|
||||||
|
final AccountDiffPreference dp = getValue();
|
||||||
setIgnoreWhitespace(dp.getIgnoreWhitespace());
|
setIgnoreWhitespace(dp.getIgnoreWhitespace());
|
||||||
if (enableSmallFileFeatures) {
|
if (enableSmallFileFeatures) {
|
||||||
syntaxHighlighting.setValue(dp.isSyntaxHighlighting());
|
syntaxHighlighting.setValue(dp.isSyntaxHighlighting());
|
||||||
@@ -195,8 +199,6 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
intralineDifference.setValue(dp.isIntralineDifference());
|
intralineDifference.setValue(dp.isIntralineDifference());
|
||||||
whitespaceErrors.setValue(dp.isShowWhitespaceErrors());
|
whitespaceErrors.setValue(dp.isShowWhitespaceErrors());
|
||||||
showTabs.setValue(dp.isShowTabs());
|
showTabs.setValue(dp.isShowTabs());
|
||||||
|
|
||||||
value = dp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiHandler("update")
|
@UiHandler("update")
|
||||||
@@ -205,7 +207,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void update() {
|
private void update() {
|
||||||
AccountDiffPreference dp = new AccountDiffPreference(value);
|
AccountDiffPreference dp = new AccountDiffPreference(getValue());
|
||||||
dp.setIgnoreWhitespace(getIgnoreWhitespace());
|
dp.setIgnoreWhitespace(getIgnoreWhitespace());
|
||||||
dp.setContext(getContext());
|
dp.setContext(getContext());
|
||||||
dp.setTabSize(tabWidth.getIntValue());
|
dp.setTabSize(tabWidth.getIntValue());
|
||||||
@@ -215,8 +217,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
dp.setShowWhitespaceErrors(whitespaceErrors.getValue());
|
dp.setShowWhitespaceErrors(whitespaceErrors.getValue());
|
||||||
dp.setShowTabs(showTabs.getValue());
|
dp.setShowTabs(showTabs.getValue());
|
||||||
|
|
||||||
value = dp;
|
listenablePrefs.set(dp);
|
||||||
fireEvent(new ValueChangeEvent<AccountDiffPreference>(dp) {});
|
|
||||||
|
|
||||||
if (Gerrit.isSignedIn()) {
|
if (Gerrit.isSignedIn()) {
|
||||||
persistDiffPreferences();
|
persistDiffPreferences();
|
||||||
@@ -225,10 +226,11 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
|
|
||||||
private void persistDiffPreferences() {
|
private void persistDiffPreferences() {
|
||||||
setEnabled(false);
|
setEnabled(false);
|
||||||
Util.ACCOUNT_SVC.changeDiffPreferences(value, new GerritCallback<VoidResult>() {
|
Util.ACCOUNT_SVC.changeDiffPreferences(getValue(),
|
||||||
|
new GerritCallback<VoidResult>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(VoidResult result) {
|
public void onSuccess(VoidResult result) {
|
||||||
Gerrit.setAccountDiffPreference(value);
|
Gerrit.setAccountDiffPreference(getValue());
|
||||||
setEnabled(true);
|
setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,7 +269,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
if (0 <= sel) {
|
if (0 <= sel) {
|
||||||
return Whitespace.valueOf(ignoreWhitespace.getValue(sel));
|
return Whitespace.valueOf(ignoreWhitespace.getValue(sel));
|
||||||
}
|
}
|
||||||
return value.getIgnoreWhitespace();
|
return getValue().getIgnoreWhitespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setIgnoreWhitespace(Whitespace s) {
|
private void setIgnoreWhitespace(Whitespace s) {
|
||||||
@@ -285,7 +287,7 @@ public class PatchScriptSettingsPanel extends Composite implements
|
|||||||
if (0 <= sel) {
|
if (0 <= sel) {
|
||||||
return Short.parseShort(context.getValue(sel));
|
return Short.parseShort(context.getValue(sel));
|
||||||
}
|
}
|
||||||
return (short) value.getContext();
|
return (short) getValue().getContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setContext(int ctx) {
|
private void setContext(int ctx) {
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user